Merge pull request #46227 from Calinou/test-add-array

Add a test suite for Array
This commit is contained in:
Rémi Verschelde 2021-02-19 19:08:52 +01:00 committed by GitHub
commit 5be6610db8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 187 additions and 0 deletions

186
tests/test_array.h Normal file
View file

@ -0,0 +1,186 @@
/*************************************************************************/
/* test_array.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef TEST_ARRAY_H
#define TEST_ARRAY_H
#include "core/object/class_db.h"
#include "core/object/script_language.h"
#include "core/templates/hashfuncs.h"
#include "core/templates/vector.h"
#include "core/variant/array.h"
#include "core/variant/container_type_validate.h"
#include "core/variant/variant.h"
#include "tests/test_macros.h"
namespace TestArray {
TEST_CASE("[Array] size(), clear(), and is_empty()") {
Array arr;
CHECK(arr.size() == 0);
CHECK(arr.is_empty());
arr.push_back(1);
CHECK(arr.size() == 1);
arr.clear();
CHECK(arr.is_empty());
CHECK(arr.size() == 0);
}
TEST_CASE("[Array] Assignment and comparison operators") {
Array arr1;
Array arr2;
arr1.push_back(1);
CHECK(arr1 != arr2);
CHECK(arr1 > arr2);
CHECK(arr1 >= arr2);
arr2.push_back(2);
CHECK(arr1 != arr2);
CHECK(arr1 < arr2);
CHECK(arr1 <= arr2);
CHECK(arr2 > arr1);
CHECK(arr2 >= arr1);
Array arr3 = arr2;
CHECK(arr3 == arr2);
}
TEST_CASE("[Array] append_array()") {
Array arr1;
Array arr2;
arr1.push_back(1);
arr1.append_array(arr2);
CHECK(arr1.size() == 1);
arr2.push_back(2);
arr1.append_array(arr2);
CHECK(arr1.size() == 2);
CHECK(int(arr1[0]) == 1);
CHECK(int(arr1[1]) == 2);
}
TEST_CASE("[Array] resize(), insert(), and erase()") {
Array arr;
arr.resize(2);
CHECK(arr.size() == 2);
arr.insert(0, 1);
CHECK(int(arr[0]) == 1);
arr.insert(0, 2);
CHECK(int(arr[0]) == 2);
arr.erase(2);
CHECK(int(arr[0]) == 1);
}
TEST_CASE("[Array] front() and back()") {
Array arr;
arr.push_back(1);
CHECK(int(arr.front()) == 1);
CHECK(int(arr.back()) == 1);
arr.push_back(3);
CHECK(int(arr.front()) == 1);
CHECK(int(arr.back()) == 3);
}
TEST_CASE("[Array] has() and count()") {
Array arr;
arr.push_back(1);
arr.push_back(1);
CHECK(arr.has(1));
CHECK(!arr.has(2));
CHECK(arr.count(1) == 2);
CHECK(arr.count(2) == 0);
}
TEST_CASE("[Array] remove()") {
Array arr;
arr.push_back(1);
arr.push_back(2);
arr.remove(0);
CHECK(arr.size() == 1);
CHECK(int(arr[0]) == 2);
arr.remove(0);
CHECK(arr.size() == 0);
// The array is now empty; try to use `remove()` again.
// Normally, this prints an error message so we silence it.
ERR_PRINT_OFF;
arr.remove(0);
ERR_PRINT_ON;
CHECK(arr.size() == 0);
}
TEST_CASE("[Array] get()") {
Array arr;
arr.push_back(1);
CHECK(int(arr.get(0)) == 1);
}
TEST_CASE("[Array] sort()") {
Array arr;
arr.push_back(3);
arr.push_back(4);
arr.push_back(2);
arr.push_back(1);
arr.sort();
int val = 1;
for (int i = 0; i < arr.size(); i++) {
CHECK(int(arr[i]) == val);
val++;
}
}
TEST_CASE("[Array] push_front(), pop_front(), pop_back()") {
Array arr;
arr.push_front(1);
arr.push_front(2);
CHECK(int(arr[0]) == 2);
arr.pop_front();
CHECK(int(arr[0]) == 1);
CHECK(arr.size() == 1);
arr.push_front(2);
arr.push_front(3);
arr.pop_back();
CHECK(int(arr[1]) == 2);
CHECK(arr.size() == 2);
}
TEST_CASE("[Array] max() and min()") {
Array arr;
arr.push_back(3);
arr.push_front(4);
arr.push_back(5);
arr.push_back(2);
int max = int(arr.max());
int min = int(arr.min());
CHECK(max == 5);
CHECK(min == 2);
}
} // namespace TestArray
#endif // TEST_ARRAY_H

View file

@ -33,6 +33,7 @@
#include "core/templates/list.h" #include "core/templates/list.h"
#include "test_aabb.h" #include "test_aabb.h"
#include "test_array.h"
#include "test_astar.h" #include "test_astar.h"
#include "test_basis.h" #include "test_basis.h"
#include "test_class_db.h" #include "test_class_db.h"