Merge pull request #83027 from rarysson/array-negative-indexes

Add negative index to `Array.remove_at` and `Array.insert`
This commit is contained in:
Thaddeus Crews 2025-04-09 08:51:40 -05:00
commit e6b2a42053
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
3 changed files with 33 additions and 2 deletions

View file

@ -126,6 +126,12 @@ TEST_CASE("[Array] resize(), insert(), and erase()") {
CHECK(int(arr[0]) == 2);
arr.erase(2);
CHECK(int(arr[0]) == 1);
// Negative index on insert.
CHECK(arr.size() == 3);
arr.insert(-1, 3);
CHECK(int(arr[2]) == 3);
CHECK(arr.size() == 4);
}
TEST_CASE("[Array] front() and back()") {
@ -154,6 +160,15 @@ TEST_CASE("[Array] remove_at()") {
arr.remove_at(0);
CHECK(arr.size() == 0);
// Negative index.
arr.push_back(3);
arr.push_back(4);
arr.remove_at(-1);
CHECK(arr.size() == 1);
CHECK(int(arr[0]) == 3);
arr.remove_at(-1);
CHECK(arr.size() == 0);
// The array is now empty; try to use `remove_at()` again.
// Normally, this prints an error message so we silence it.
ERR_PRINT_OFF;