diff options
author | Rémi Verschelde <remi@verschelde.fr> | 2021-09-14 13:01:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-14 13:01:20 +0200 |
commit | d2808b5a5128dd9e6631fb6e07da2fd500e161f7 (patch) | |
tree | f6d07860a9842e5b394316e2031828aa54e47150 /tests/test_array.h | |
parent | 7774ec1634374a2c6d10eba4d4c885688747deff (diff) | |
parent | a8cea5353f88d1dcc7c2c08600710844d9cd67e7 (diff) |
Merge pull request #52153 from timothyqiu/test-array-pop-at
Add unit tests for `Array.pop_at()`
Diffstat (limited to 'tests/test_array.h')
-rw-r--r-- | tests/test_array.h | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/test_array.h b/tests/test_array.h index 52da256860..3bd476fd27 100644 --- a/tests/test_array.h +++ b/tests/test_array.h @@ -39,6 +39,7 @@ #include "core/variant/container_type_validate.h" #include "core/variant/variant.h" #include "tests/test_macros.h" +#include "tests/test_tools.h" namespace TestArray { @@ -170,6 +171,56 @@ TEST_CASE("[Array] push_front(), pop_front(), pop_back()") { CHECK(arr.size() == 2); } +TEST_CASE("[Array] pop_at()") { + ErrorDetector ed; + + Array arr; + arr.push_back(2); + arr.push_back(4); + arr.push_back(6); + arr.push_back(8); + arr.push_back(10); + + REQUIRE(int(arr.pop_at(2)) == 6); + REQUIRE(arr.size() == 4); + CHECK(int(arr[0]) == 2); + CHECK(int(arr[1]) == 4); + CHECK(int(arr[2]) == 8); + CHECK(int(arr[3]) == 10); + + REQUIRE(int(arr.pop_at(2)) == 8); + REQUIRE(arr.size() == 3); + CHECK(int(arr[0]) == 2); + CHECK(int(arr[1]) == 4); + CHECK(int(arr[2]) == 10); + + // Negative index. + REQUIRE(int(arr.pop_at(-1)) == 10); + REQUIRE(arr.size() == 2); + CHECK(int(arr[0]) == 2); + CHECK(int(arr[1]) == 4); + + // Invalid pop. + ed.clear(); + ERR_PRINT_OFF; + const Variant ret = arr.pop_at(-15); + ERR_PRINT_ON; + REQUIRE(ret.is_null()); + CHECK(ed.has_error); + + REQUIRE(int(arr.pop_at(0)) == 2); + REQUIRE(arr.size() == 1); + CHECK(int(arr[0]) == 4); + + REQUIRE(int(arr.pop_at(0)) == 4); + REQUIRE(arr.is_empty()); + + // Pop from empty array. + ed.clear(); + REQUIRE(arr.pop_at(24).is_null()); + CHECK_FALSE(ed.has_error); +} + TEST_CASE("[Array] max() and min()") { Array arr; arr.push_back(3); |