diff options
-rw-r--r-- | core/variant/array.cpp | 2 | ||||
-rw-r--r-- | tests/core/variant/test_array.h | 31 |
2 files changed, 22 insertions, 11 deletions
diff --git a/core/variant/array.cpp b/core/variant/array.cpp index d156c35343..5215142dd3 100644 --- a/core/variant/array.cpp +++ b/core/variant/array.cpp @@ -466,7 +466,7 @@ Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { ERR_FAIL_COND_V_MSG(p_step > 0 && begin > end, result, "Slice is positive, but bounds is decreasing."); ERR_FAIL_COND_V_MSG(p_step < 0 && begin < end, result, "Slice is negative, but bounds is increasing."); - int result_size = (end - begin) / p_step; + int result_size = (end - begin) / p_step + (((end - begin) % p_step != 0) ? 1 : 0); result.resize(result_size); for (int src_idx = begin, dest_idx = 0; dest_idx < result_size; ++dest_idx) { diff --git a/tests/core/variant/test_array.h b/tests/core/variant/test_array.h index 04d5bb5107..ccb02ed5fa 100644 --- a/tests/core/variant/test_array.h +++ b/tests/core/variant/test_array.h @@ -253,6 +253,7 @@ TEST_CASE("[Array] slice()") { array.push_back(2); array.push_back(3); array.push_back(4); + array.push_back(5); Array slice0 = array.slice(0, 0); CHECK(slice0.size() == 0); @@ -263,42 +264,52 @@ TEST_CASE("[Array] slice()") { CHECK(slice1[1] == Variant(2)); Array slice2 = array.slice(1, -1); - CHECK(slice2.size() == 3); + 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); - CHECK(slice3.size() == 2); + CHECK(slice3.size() == 3); CHECK(slice3[0] == Variant(3)); CHECK(slice3[1] == Variant(4)); + CHECK(slice3[2] == Variant(5)); Array slice4 = array.slice(2, -2); - CHECK(slice4.size() == 1); + CHECK(slice4.size() == 2); CHECK(slice4[0] == Variant(2)); + CHECK(slice4[1] == Variant(3)); Array slice5 = array.slice(-2); CHECK(slice5.size() == 2); - CHECK(slice5[0] == Variant(3)); - CHECK(slice5[1] == Variant(4)); + CHECK(slice5[0] == Variant(4)); + CHECK(slice5[1] == Variant(5)); Array slice6 = array.slice(2, 42); - CHECK(slice6.size() == 3); + CHECK(slice6.size() == 4); CHECK(slice6[0] == Variant(2)); CHECK(slice6[1] == Variant(3)); CHECK(slice6[2] == Variant(4)); + CHECK(slice6[3] == Variant(5)); Array slice7 = array.slice(4, 0, -2); CHECK(slice7.size() == 2); CHECK(slice7[0] == Variant(4)); CHECK(slice7[1] == Variant(2)); - ERR_PRINT_OFF; - Array slice8 = array.slice(4, 1); - CHECK(slice8.size() == 0); + Array slice8 = array.slice(5, 0, -2); + CHECK(slice8.size() == 3); + CHECK(slice8[0] == Variant(5)); + CHECK(slice8[1] == Variant(3)); + CHECK(slice8[2] == Variant(1)); - Array slice9 = array.slice(3, -4); + ERR_PRINT_OFF; + Array slice9 = array.slice(4, 1); CHECK(slice9.size() == 0); + + Array slice10 = array.slice(3, -4); + CHECK(slice10.size() == 0); ERR_PRINT_ON; } |