Fix `Array.slice()` rounding for `abs(step) != 1`
This commit is contained in:
parent
79454bfd3b
commit
10f385fb79
|
@ -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 positive, but bounds is decreasing.");
|
||||||
ERR_FAIL_COND_V_MSG(p_step < 0 && begin < end, result, "Slice is negative, but bounds is increasing.");
|
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);
|
result.resize(result_size);
|
||||||
|
|
||||||
for (int src_idx = begin, dest_idx = 0; dest_idx < result_size; ++dest_idx) {
|
for (int src_idx = begin, dest_idx = 0; dest_idx < result_size; ++dest_idx) {
|
||||||
|
|
|
@ -253,6 +253,7 @@ TEST_CASE("[Array] slice()") {
|
||||||
array.push_back(2);
|
array.push_back(2);
|
||||||
array.push_back(3);
|
array.push_back(3);
|
||||||
array.push_back(4);
|
array.push_back(4);
|
||||||
|
array.push_back(5);
|
||||||
|
|
||||||
Array slice0 = array.slice(0, 0);
|
Array slice0 = array.slice(0, 0);
|
||||||
CHECK(slice0.size() == 0);
|
CHECK(slice0.size() == 0);
|
||||||
|
@ -263,42 +264,52 @@ TEST_CASE("[Array] slice()") {
|
||||||
CHECK(slice1[1] == Variant(2));
|
CHECK(slice1[1] == Variant(2));
|
||||||
|
|
||||||
Array slice2 = array.slice(1, -1);
|
Array slice2 = array.slice(1, -1);
|
||||||
CHECK(slice2.size() == 3);
|
CHECK(slice2.size() == 4);
|
||||||
CHECK(slice2[0] == Variant(1));
|
CHECK(slice2[0] == Variant(1));
|
||||||
CHECK(slice2[1] == Variant(2));
|
CHECK(slice2[1] == Variant(2));
|
||||||
CHECK(slice2[2] == Variant(3));
|
CHECK(slice2[2] == Variant(3));
|
||||||
|
CHECK(slice2[3] == Variant(4));
|
||||||
|
|
||||||
Array slice3 = array.slice(3);
|
Array slice3 = array.slice(3);
|
||||||
CHECK(slice3.size() == 2);
|
CHECK(slice3.size() == 3);
|
||||||
CHECK(slice3[0] == Variant(3));
|
CHECK(slice3[0] == Variant(3));
|
||||||
CHECK(slice3[1] == Variant(4));
|
CHECK(slice3[1] == Variant(4));
|
||||||
|
CHECK(slice3[2] == Variant(5));
|
||||||
|
|
||||||
Array slice4 = array.slice(2, -2);
|
Array slice4 = array.slice(2, -2);
|
||||||
CHECK(slice4.size() == 1);
|
CHECK(slice4.size() == 2);
|
||||||
CHECK(slice4[0] == Variant(2));
|
CHECK(slice4[0] == Variant(2));
|
||||||
|
CHECK(slice4[1] == Variant(3));
|
||||||
|
|
||||||
Array slice5 = array.slice(-2);
|
Array slice5 = array.slice(-2);
|
||||||
CHECK(slice5.size() == 2);
|
CHECK(slice5.size() == 2);
|
||||||
CHECK(slice5[0] == Variant(3));
|
CHECK(slice5[0] == Variant(4));
|
||||||
CHECK(slice5[1] == Variant(4));
|
CHECK(slice5[1] == Variant(5));
|
||||||
|
|
||||||
Array slice6 = array.slice(2, 42);
|
Array slice6 = array.slice(2, 42);
|
||||||
CHECK(slice6.size() == 3);
|
CHECK(slice6.size() == 4);
|
||||||
CHECK(slice6[0] == Variant(2));
|
CHECK(slice6[0] == Variant(2));
|
||||||
CHECK(slice6[1] == Variant(3));
|
CHECK(slice6[1] == Variant(3));
|
||||||
CHECK(slice6[2] == Variant(4));
|
CHECK(slice6[2] == Variant(4));
|
||||||
|
CHECK(slice6[3] == Variant(5));
|
||||||
|
|
||||||
Array slice7 = array.slice(4, 0, -2);
|
Array slice7 = array.slice(4, 0, -2);
|
||||||
CHECK(slice7.size() == 2);
|
CHECK(slice7.size() == 2);
|
||||||
CHECK(slice7[0] == Variant(4));
|
CHECK(slice7[0] == Variant(4));
|
||||||
CHECK(slice7[1] == Variant(2));
|
CHECK(slice7[1] == Variant(2));
|
||||||
|
|
||||||
ERR_PRINT_OFF;
|
Array slice8 = array.slice(5, 0, -2);
|
||||||
Array slice8 = array.slice(4, 1);
|
CHECK(slice8.size() == 3);
|
||||||
CHECK(slice8.size() == 0);
|
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);
|
CHECK(slice9.size() == 0);
|
||||||
|
|
||||||
|
Array slice10 = array.slice(3, -4);
|
||||||
|
CHECK(slice10.size() == 0);
|
||||||
ERR_PRINT_ON;
|
ERR_PRINT_ON;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue