From dd30253cdcf9b43e401cb3ba6b973f8890551a81 Mon Sep 17 00:00:00 2001 From: Nathan Franke Date: Fri, 26 Nov 2021 19:18:26 -0600 Subject: [PATCH] PackedByteArray, Array slice end exclusive, rename subarray to slice --- core/multiplayer/multiplayer_replicator.cpp | 2 +- core/templates/vector.h | 33 +++++------ core/variant/array.cpp | 57 ++++++------------- core/variant/array.h | 2 - core/variant/variant_call.cpp | 18 +++--- doc/classes/Array.xml | 5 +- doc/classes/PackedByteArray.xml | 17 +++--- doc/classes/PackedColorArray.xml | 14 ++--- doc/classes/PackedFloat32Array.xml | 14 ++--- doc/classes/PackedFloat64Array.xml | 14 ++--- doc/classes/PackedInt32Array.xml | 14 ++--- doc/classes/PackedInt64Array.xml | 14 ++--- doc/classes/PackedStringArray.xml | 14 ++--- doc/classes/PackedVector2Array.xml | 14 ++--- doc/classes/PackedVector3Array.xml | 14 ++--- .../renderer_rd/renderer_scene_gi_rd.cpp | 4 +- .../renderer_rd/renderer_storage_rd.cpp | 2 +- servers/rendering_server.cpp | 2 +- tests/core/templates/test_vector.h | 44 +++++++------- tests/core/variant/test_array.h | 31 ++++++++++ 20 files changed, 169 insertions(+), 160 deletions(-) diff --git a/core/multiplayer/multiplayer_replicator.cpp b/core/multiplayer/multiplayer_replicator.cpp index 660451039458..c57562552a85 100644 --- a/core/multiplayer/multiplayer_replicator.cpp +++ b/core/multiplayer/multiplayer_replicator.cpp @@ -207,7 +207,7 @@ Error MultiplayerReplicator::_send_default_spawn_despawn(int p_peer_id, const Re const Vector names = rel_path.get_names(); ERR_FAIL_COND_V(names.size() < 2, ERR_INVALID_PARAMETER); - NodePath parent = NodePath(names.subarray(0, names.size() - 2), false); + NodePath parent = NodePath(names.slice(0, names.size() - 1), false); ERR_FAIL_COND_V_MSG(!root_node->has_node(parent), ERR_INVALID_PARAMETER, "Path not found: " + parent); int path_id = 0; diff --git a/core/templates/vector.h b/core/templates/vector.h index a955d49101e3..858896018a59 100644 --- a/core/templates/vector.h +++ b/core/templates/vector.h @@ -144,27 +144,28 @@ class Vector { return ret; } - Vector subarray(int p_from, int p_to) const { - if (p_from < 0) { - p_from = size() + p_from; - } - if (p_to < 0) { - p_to = size() + p_to; + Vector slice(int p_begin, int p_end) const { + Vector result; + + if (p_end < 0) { + p_end += size() + 1; } - ERR_FAIL_INDEX_V(p_from, size(), Vector()); - ERR_FAIL_INDEX_V(p_to, size(), Vector()); + ERR_FAIL_INDEX_V(p_begin, size(), result); + ERR_FAIL_INDEX_V(p_end, size() + 1, result); + + ERR_FAIL_COND_V(p_begin > p_end, result); + + int result_size = p_end - p_begin; + result.resize(result_size); - Vector slice; - int span = 1 + p_to - p_from; - slice.resize(span); - const T *r = ptr(); - T *w = slice.ptrw(); - for (int i = 0; i < span; ++i) { - w[i] = r[p_from + i]; + const T *const r = ptr(); + T *const w = result.ptrw(); + for (int i = 0; i < result_size; ++i) { + w[i] = r[p_begin + i]; } - return slice; + return result; } bool operator==(const Vector &p_arr) const { diff --git a/core/variant/array.cpp b/core/variant/array.cpp index b049c296884d..45f2e0c5ac2e 100644 --- a/core/variant/array.cpp +++ b/core/variant/array.cpp @@ -365,55 +365,30 @@ Array Array::recursive_duplicate(bool p_deep, int recursion_count) const { return new_arr; } -int Array::_clamp_slice_index(int p_index) const { - int arr_size = size(); - int fixed_index = CLAMP(p_index, -arr_size, arr_size - 1); - if (fixed_index < 0) { - fixed_index = arr_size + fixed_index; - } - return fixed_index; -} +Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { + Array result; -Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // like python, but inclusive on upper bound - - Array new_arr; + ERR_FAIL_COND_V_MSG(p_step == 0, result, "Slice step cannot be zero."); - ERR_FAIL_COND_V_MSG(p_step == 0, new_arr, "Array slice step size cannot be zero."); - - if (is_empty()) { // Don't try to slice empty arrays. - return new_arr; - } - if (p_step > 0) { - if (p_begin >= size() || p_end < -size()) { - return new_arr; - } - } else { // p_step < 0 - if (p_begin < -size() || p_end >= size()) { - return new_arr; - } + if (p_end < 0) { + p_end += size() + 1; } - int begin = _clamp_slice_index(p_begin); - int end = _clamp_slice_index(p_end); + ERR_FAIL_INDEX_V(p_begin, size(), result); + ERR_FAIL_INDEX_V(p_end, size() + 1, result); - int new_arr_size = MAX(((end - begin + p_step) / p_step), 0); - new_arr.resize(new_arr_size); + ERR_FAIL_COND_V_MSG(p_step > 0 && p_begin > p_end, result, "Slice is positive, but bounds is decreasing"); + ERR_FAIL_COND_V_MSG(p_step < 0 && p_begin < p_end, result, "Slice is negative, but bounds is increasing"); - if (p_step > 0) { - int dest_idx = 0; - for (int idx = begin; idx <= end; idx += p_step) { - ERR_FAIL_COND_V_MSG(dest_idx < 0 || dest_idx >= new_arr_size, Array(), "Bug in Array slice()"); - new_arr[dest_idx++] = p_deep ? get(idx).duplicate(p_deep) : get(idx); - } - } else { // p_step < 0 - int dest_idx = 0; - for (int idx = begin; idx >= end; idx += p_step) { - ERR_FAIL_COND_V_MSG(dest_idx < 0 || dest_idx >= new_arr_size, Array(), "Bug in Array slice()"); - new_arr[dest_idx++] = p_deep ? get(idx).duplicate(p_deep) : get(idx); - } + int result_size = (p_end - p_begin) / p_step; + result.resize(result_size); + + for (int src_idx = p_begin, dest_idx = 0; dest_idx < result_size; ++dest_idx) { + result[dest_idx] = p_deep ? get(src_idx).duplicate(true) : get(src_idx); + src_idx += p_step; } - return new_arr; + return result; } Array Array::filter(const Callable &p_callable) const { diff --git a/core/variant/array.h b/core/variant/array.h index 5d2839dda73e..6a68a9b9ff42 100644 --- a/core/variant/array.h +++ b/core/variant/array.h @@ -44,8 +44,6 @@ class Array { void _ref(const Array &p_from) const; void _unref() const; - inline int _clamp_slice_index(int p_index) const; - protected: Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script); bool _assign(const Array &p_array); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 34b8b782d2a1..39db2c681f0e 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1848,7 +1848,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedByteArray, resize, sarray("new_size"), varray()); bind_method(PackedByteArray, has, sarray("value"), varray()); bind_method(PackedByteArray, reverse, sarray(), varray()); - bind_method(PackedByteArray, subarray, sarray("from", "to"), varray()); + bind_method(PackedByteArray, slice, sarray("begin", "end"), varray()); bind_method(PackedByteArray, sort, sarray(), varray()); bind_method(PackedByteArray, bsearch, sarray("value", "before"), varray(true)); bind_method(PackedByteArray, duplicate, sarray(), varray()); @@ -1909,7 +1909,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedInt32Array, resize, sarray("new_size"), varray()); bind_method(PackedInt32Array, has, sarray("value"), varray()); bind_method(PackedInt32Array, reverse, sarray(), varray()); - bind_method(PackedInt32Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedInt32Array, slice, sarray("begin", "end"), varray()); bind_method(PackedInt32Array, to_byte_array, sarray(), varray()); bind_method(PackedInt32Array, sort, sarray(), varray()); bind_method(PackedInt32Array, bsearch, sarray("value", "before"), varray(true)); @@ -1929,7 +1929,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedInt64Array, resize, sarray("new_size"), varray()); bind_method(PackedInt64Array, has, sarray("value"), varray()); bind_method(PackedInt64Array, reverse, sarray(), varray()); - bind_method(PackedInt64Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedInt64Array, slice, sarray("begin", "end"), varray()); bind_method(PackedInt64Array, to_byte_array, sarray(), varray()); bind_method(PackedInt64Array, sort, sarray(), varray()); bind_method(PackedInt64Array, bsearch, sarray("value", "before"), varray(true)); @@ -1949,7 +1949,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedFloat32Array, resize, sarray("new_size"), varray()); bind_method(PackedFloat32Array, has, sarray("value"), varray()); bind_method(PackedFloat32Array, reverse, sarray(), varray()); - bind_method(PackedFloat32Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedFloat32Array, slice, sarray("begin", "end"), varray()); bind_method(PackedFloat32Array, to_byte_array, sarray(), varray()); bind_method(PackedFloat32Array, sort, sarray(), varray()); bind_method(PackedFloat32Array, bsearch, sarray("value", "before"), varray(true)); @@ -1969,7 +1969,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedFloat64Array, resize, sarray("new_size"), varray()); bind_method(PackedFloat64Array, has, sarray("value"), varray()); bind_method(PackedFloat64Array, reverse, sarray(), varray()); - bind_method(PackedFloat64Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedFloat64Array, slice, sarray("begin", "end"), varray()); bind_method(PackedFloat64Array, to_byte_array, sarray(), varray()); bind_method(PackedFloat64Array, sort, sarray(), varray()); bind_method(PackedFloat64Array, bsearch, sarray("value", "before"), varray(true)); @@ -1989,7 +1989,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedStringArray, resize, sarray("new_size"), varray()); bind_method(PackedStringArray, has, sarray("value"), varray()); bind_method(PackedStringArray, reverse, sarray(), varray()); - bind_method(PackedStringArray, subarray, sarray("from", "to"), varray()); + bind_method(PackedStringArray, slice, sarray("begin", "end"), varray()); bind_method(PackedStringArray, to_byte_array, sarray(), varray()); bind_method(PackedStringArray, sort, sarray(), varray()); bind_method(PackedStringArray, bsearch, sarray("value", "before"), varray(true)); @@ -2009,7 +2009,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedVector2Array, resize, sarray("new_size"), varray()); bind_method(PackedVector2Array, has, sarray("value"), varray()); bind_method(PackedVector2Array, reverse, sarray(), varray()); - bind_method(PackedVector2Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedVector2Array, slice, sarray("begin", "end"), varray()); bind_method(PackedVector2Array, to_byte_array, sarray(), varray()); bind_method(PackedVector2Array, sort, sarray(), varray()); bind_method(PackedVector2Array, bsearch, sarray("value", "before"), varray(true)); @@ -2029,7 +2029,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedVector3Array, resize, sarray("new_size"), varray()); bind_method(PackedVector3Array, has, sarray("value"), varray()); bind_method(PackedVector3Array, reverse, sarray(), varray()); - bind_method(PackedVector3Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedVector3Array, slice, sarray("begin", "end"), varray()); bind_method(PackedVector3Array, to_byte_array, sarray(), varray()); bind_method(PackedVector3Array, sort, sarray(), varray()); bind_method(PackedVector3Array, bsearch, sarray("value", "before"), varray(true)); @@ -2049,7 +2049,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedColorArray, resize, sarray("new_size"), varray()); bind_method(PackedColorArray, has, sarray("value"), varray()); bind_method(PackedColorArray, reverse, sarray(), varray()); - bind_method(PackedColorArray, subarray, sarray("from", "to"), varray()); + bind_method(PackedColorArray, slice, sarray("begin", "end"), varray()); bind_method(PackedColorArray, to_byte_array, sarray(), varray()); bind_method(PackedColorArray, sort, sarray(), varray()); bind_method(PackedColorArray, bsearch, sarray("value", "before"), varray(true)); diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml index d505ee98cc09..5b1861bc9ae5 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -449,7 +449,10 @@ - Duplicates the subset described in the function and returns it in an array, deeply copying the array if [code]deep[/code] is [code]true[/code]. Lower and upper index are inclusive, with the [code]step[/code] describing the change between indices while slicing. Wraps around if [code]begin[/code] or [code]end[/code] are out of bounds or negative. Returns an empty array for invalid parameters. + Returns the slice of the [Array], from [code]begin[/code] (inclusive) to [code]end[/code] (exclusive), as a new [Array]. + If [code]end[/code] is negative, it will be relative to the end of the array. + If specified, [code]step[/code] is the relative index between source elements. + If [code]deep[/code] is true, each element will be copied by value rather than by reference. diff --git a/doc/classes/PackedByteArray.xml b/doc/classes/PackedByteArray.xml index fd098481e488..b16d45b8ca62 100644 --- a/doc/classes/PackedByteArray.xml +++ b/doc/classes/PackedByteArray.xml @@ -366,18 +366,19 @@ Returns the size of the array. - - + + + + - Sorts the elements of the array in ascending order. + Returns the slice of the [PackedByteArray], from [code]begin[/code] (inclusive) to [code]end[/code] (exclusive), as a new [PackedByteArray]. + If [code]end[/code]is negative, it will be relative to the end of the array. - - - - + + - Returns the slice of the [PackedByteArray] between indices (inclusive) as a new [PackedByteArray]. Any negative index is considered to be from the end of the array. + Sorts the elements of the array in ascending order. diff --git a/doc/classes/PackedColorArray.xml b/doc/classes/PackedColorArray.xml index f69c5504da2d..13d7440bb964 100644 --- a/doc/classes/PackedColorArray.xml +++ b/doc/classes/PackedColorArray.xml @@ -129,17 +129,17 @@ Returns the size of the array. - - + + + + - Sorts the elements of the array in ascending order. - - - - + + + Sorts the elements of the array in ascending order. diff --git a/doc/classes/PackedFloat32Array.xml b/doc/classes/PackedFloat32Array.xml index ccac6073868f..151014192f04 100644 --- a/doc/classes/PackedFloat32Array.xml +++ b/doc/classes/PackedFloat32Array.xml @@ -130,17 +130,17 @@ Returns the size of the array. - - + + + + - Sorts the elements of the array in ascending order. - - - - + + + Sorts the elements of the array in ascending order. diff --git a/doc/classes/PackedFloat64Array.xml b/doc/classes/PackedFloat64Array.xml index b164283b3ea2..963a02ace860 100644 --- a/doc/classes/PackedFloat64Array.xml +++ b/doc/classes/PackedFloat64Array.xml @@ -130,17 +130,17 @@ Returns the size of the array. - - + + + + - Sorts the elements of the array in ascending order. - - - - + + + Sorts the elements of the array in ascending order. diff --git a/doc/classes/PackedInt32Array.xml b/doc/classes/PackedInt32Array.xml index c6ff31ebdd27..cef113dee9de 100644 --- a/doc/classes/PackedInt32Array.xml +++ b/doc/classes/PackedInt32Array.xml @@ -130,17 +130,17 @@ Returns the array size. - - + + + + - Sorts the elements of the array in ascending order. - - - - + + + Sorts the elements of the array in ascending order. diff --git a/doc/classes/PackedInt64Array.xml b/doc/classes/PackedInt64Array.xml index ff48eb1aad92..072df519c6fb 100644 --- a/doc/classes/PackedInt64Array.xml +++ b/doc/classes/PackedInt64Array.xml @@ -130,17 +130,17 @@ Returns the array size. - - + + + + - Sorts the elements of the array in ascending order. - - - - + + + Sorts the elements of the array in ascending order. diff --git a/doc/classes/PackedStringArray.xml b/doc/classes/PackedStringArray.xml index 4204277ea2fa..0bded150a3e2 100644 --- a/doc/classes/PackedStringArray.xml +++ b/doc/classes/PackedStringArray.xml @@ -130,17 +130,17 @@ Returns the size of the array. - - + + + + - Sorts the elements of the array in ascending order. - - - - + + + Sorts the elements of the array in ascending order. diff --git a/doc/classes/PackedVector2Array.xml b/doc/classes/PackedVector2Array.xml index e6a7b2fa41a9..8e993c41ab8b 100644 --- a/doc/classes/PackedVector2Array.xml +++ b/doc/classes/PackedVector2Array.xml @@ -130,17 +130,17 @@ Returns the size of the array. - - + + + + - Sorts the elements of the array in ascending order. - - - - + + + Sorts the elements of the array in ascending order. diff --git a/doc/classes/PackedVector3Array.xml b/doc/classes/PackedVector3Array.xml index 6992bbca018b..df69e3cd4a4b 100644 --- a/doc/classes/PackedVector3Array.xml +++ b/doc/classes/PackedVector3Array.xml @@ -129,17 +129,17 @@ Returns the size of the array. - - + + + + - Sorts the elements of the array in ascending order. - - - - + + + Sorts the elements of the array in ascending order. diff --git a/servers/rendering/renderer_rd/renderer_scene_gi_rd.cpp b/servers/rendering/renderer_rd/renderer_scene_gi_rd.cpp index b6b5c90b39e1..b31d1062f5fd 100644 --- a/servers/rendering/renderer_rd/renderer_scene_gi_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_scene_gi_rd.cpp @@ -1864,7 +1864,7 @@ void RendererSceneGIRD::SDFGI::render_region(RID p_render_buffers, int p_region, Ref img; img.instantiate(); for (uint32_t i = 0; i < cascade_size; i++) { - Vector subarr = data.subarray(128 * 128 * i, 128 * 128 * (i + 1) - 1); + Vector subarr = data.slice(128 * 128 * i, 128 * 128 * (i + 1)); img->create(cascade_size, cascade_size, false, Image::FORMAT_L8, subarr); img->save_png("res://cascade_sdf_" + itos(cascade) + "_" + itos(i) + ".png"); } @@ -1877,7 +1877,7 @@ void RendererSceneGIRD::SDFGI::render_region(RID p_render_buffers, int p_region, Ref img; img.instantiate(); for (uint32_t i = 0; i < cascade_size; i++) { - Vector subarr = data.subarray(128 * 128 * i * 2, 128 * 128 * (i + 1) * 2 - 1); + Vector subarr = data.slice(128 * 128 * i * 2, 128 * 128 * (i + 1) * 2); img->createcascade_size, cascade_size, false, Image::FORMAT_RGB565, subarr); img->convert(Image::FORMAT_RGBA8); img->save_png("res://cascade_" + itos(cascade) + "_" + itos(i) + ".png"); diff --git a/servers/rendering/renderer_rd/renderer_storage_rd.cpp b/servers/rendering/renderer_rd/renderer_storage_rd.cpp index 29e4a63cbb85..45f321fc904e 100644 --- a/servers/rendering/renderer_rd/renderer_storage_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_storage_rd.cpp @@ -1089,7 +1089,7 @@ Vector> RendererStorageRD::texture_3d_get(RID p_texture) const { const Texture::BufferSlice3D &bs = tex->buffer_slices_3d[i]; ERR_FAIL_COND_V(bs.offset >= (uint32_t)all_data.size(), Vector>()); ERR_FAIL_COND_V(bs.offset + bs.buffer_size > (uint32_t)all_data.size(), Vector>()); - Vector sub_region = all_data.subarray(bs.offset, bs.offset + bs.buffer_size - 1); + Vector sub_region = all_data.slice(bs.offset, bs.offset + bs.buffer_size); Ref img; img.instantiate(); diff --git a/servers/rendering_server.cpp b/servers/rendering_server.cpp index 7a958546b6f6..b9ebe8c8d44d 100644 --- a/servers/rendering_server.cpp +++ b/servers/rendering_server.cpp @@ -1376,7 +1376,7 @@ Array RenderingServer::mesh_surface_get_blend_shape_arrays(RID p_mesh, int p_sur Array blend_shape_array; blend_shape_array.resize(mesh_get_blend_shape_count(p_mesh)); for (uint32_t i = 0; i < blend_shape_count; i++) { - Vector bs_data = blend_shape_data.subarray(i * divisor, (i + 1) * divisor - 1); + Vector bs_data = blend_shape_data.slice(i * divisor, (i + 1) * divisor); Vector unused; blend_shape_array.set(i, _get_array_from_surface(bs_format, bs_data, unused, unused, sd.vertex_count, unused, 0)); } diff --git a/tests/core/templates/test_vector.h b/tests/core/templates/test_vector.h index 658ca5adf150..6ea865dacc60 100644 --- a/tests/core/templates/test_vector.h +++ b/tests/core/templates/test_vector.h @@ -238,7 +238,7 @@ TEST_CASE("[Vector] To byte array") { CHECK(byte_array[15] == 59); } -TEST_CASE("[Vector] Subarray") { +TEST_CASE("[Vector] Slice") { Vector vector; vector.push_back(0); vector.push_back(1); @@ -246,27 +246,27 @@ TEST_CASE("[Vector] Subarray") { vector.push_back(3); vector.push_back(4); - Vector subarray1 = vector.subarray(1, 2); - CHECK(subarray1.size() == 2); - CHECK(subarray1[0] == 1); - CHECK(subarray1[1] == 2); - - Vector subarray2 = vector.subarray(1, -1); - CHECK(subarray2.size() == 4); - CHECK(subarray2[0] == 1); - CHECK(subarray2[1] == 2); - CHECK(subarray2[2] == 3); - CHECK(subarray2[3] == 4); - - Vector subarray3 = vector.subarray(-2, -1); - CHECK(subarray3.size() == 2); - CHECK(subarray3[0] == 3); - CHECK(subarray3[1] == 4); - - Vector subarray4 = vector.subarray(-3, 3); - CHECK(subarray4.size() == 2); - CHECK(subarray4[0] == 2); - CHECK(subarray4[1] == 3); + Vector slice1 = vector.slice(1, 3); + CHECK(slice1.size() == 2); + CHECK(slice1[0] == 1); + CHECK(slice1[1] == 2); + + Vector slice2 = vector.slice(1, -1); + CHECK(slice2.size() == 4); + CHECK(slice2[0] == 1); + CHECK(slice2[1] == 2); + CHECK(slice2[2] == 3); + CHECK(slice2[3] == 4); + + Vector slice3 = vector.slice(3, -1); + CHECK(slice3.size() == 2); + CHECK(slice3[0] == 3); + CHECK(slice3[1] == 4); + + Vector slice4 = vector.slice(2, -2); + CHECK(slice4.size() == 2); + CHECK(slice4[0] == 2); + CHECK(slice4[1] == 3); } TEST_CASE("[Vector] Find, has") { diff --git a/tests/core/variant/test_array.h b/tests/core/variant/test_array.h index e2e84f2962ec..d02b3d0e39b3 100644 --- a/tests/core/variant/test_array.h +++ b/tests/core/variant/test_array.h @@ -246,6 +246,37 @@ TEST_CASE("[Array] max() and min()") { CHECK(min == 2); } +TEST_CASE("[Array] slice()") { + Array array; + array.push_back(0); + array.push_back(1); + array.push_back(2); + array.push_back(3); + array.push_back(4); + + Array slice1 = array.slice(1, 3); + CHECK(slice1.size() == 2); + CHECK(slice1[0] == Variant(1)); + CHECK(slice1[1] == Variant(2)); + + Array slice2 = array.slice(1, -1); + CHECK(slice2.size() == 4); + CHECK(slice2[0] == Variant(1)); + CHECK(slice2[1] == Variant(2)); + CHECK(slice2[2] == Variant(3)); + CHECK(slice2[3] == Variant(4)); + + Array slice3 = array.slice(3, -1); + CHECK(slice3.size() == 2); + CHECK(slice3[0] == Variant(3)); + CHECK(slice3[1] == Variant(4)); + + Array slice4 = array.slice(2, -2); + CHECK(slice4.size() == 2); + CHECK(slice4[0] == Variant(2)); + CHECK(slice4[1] == Variant(3)); +} + TEST_CASE("[Array] Duplicate array") { // a = [1, [2, 2], {3: 3}] Array a = build_array(1, build_array(2, 2), build_dictionary(3, 3));