summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNinni Pipping <over999ships@gmail.com>2023-03-14 14:11:05 +0100
committerYuri Sizov <yuris@humnom.net>2023-03-27 17:31:05 +0200
commitb43f84780882358d451bece176de2c854f666f30 (patch)
tree081a216b96b3574a37956cb95cebe70d859baba3
parentac1b26c7c51e71e5d98a18fb4ad779de1b572a5f (diff)
Fix `Array.slice()` rounding for `abs(step) != 1`
(cherry picked from commit 10f385fb792dc74ffb8be221fec88ba9ef7aaa25)
-rw-r--r--core/variant/array.cpp2
-rw-r--r--tests/core/variant/test_array.h31
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;
}