initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
647
tests/core/variant/test_array.h
Normal file
647
tests/core/variant/test_array.h
Normal file
@@ -0,0 +1,647 @@
|
||||
/**************************************************************************/
|
||||
/* test_array.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/variant/array.h"
|
||||
#include "tests/test_macros.h"
|
||||
#include "tests/test_tools.h"
|
||||
|
||||
namespace TestArray {
|
||||
TEST_CASE("[Array] initializer list") {
|
||||
Array arr = { 0, 1, "test", true, { 0.0, 1.0 } };
|
||||
CHECK(arr.size() == 5);
|
||||
CHECK(arr[0] == Variant(0));
|
||||
CHECK(arr[1] == Variant(1));
|
||||
CHECK(arr[2] == Variant("test"));
|
||||
CHECK(arr[3] == Variant(true));
|
||||
CHECK(arr[4] == Variant({ 0.0, 1.0 }));
|
||||
|
||||
arr = { "reassign" };
|
||||
CHECK(arr.size() == 1);
|
||||
CHECK(arr[0] == Variant("reassign"));
|
||||
|
||||
TypedArray<int> typed_arr = { 0, 1, 2 };
|
||||
CHECK(typed_arr.size() == 3);
|
||||
CHECK(typed_arr[0] == Variant(0));
|
||||
CHECK(typed_arr[1] == Variant(1));
|
||||
CHECK(typed_arr[2] == Variant(2));
|
||||
}
|
||||
|
||||
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);
|
||||
arr.resize(0);
|
||||
CHECK(arr.size() == 0);
|
||||
arr.insert(0, 8);
|
||||
CHECK(arr.size() == 1);
|
||||
arr.insert(1, 16);
|
||||
CHECK(int(arr[1]) == 16);
|
||||
arr.insert(-1, 3);
|
||||
CHECK(int(arr[1]) == 3);
|
||||
}
|
||||
|
||||
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 = { 1, 1 };
|
||||
CHECK(arr.has(1));
|
||||
CHECK(!arr.has(2));
|
||||
CHECK(arr.count(1) == 2);
|
||||
CHECK(arr.count(2) == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] remove_at()") {
|
||||
Array arr = { 1, 2 };
|
||||
arr.remove_at(0);
|
||||
CHECK(arr.size() == 1);
|
||||
CHECK(int(arr[0]) == 2);
|
||||
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;
|
||||
arr.remove_at(0);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
CHECK(arr.size() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] get()") {
|
||||
Array arr = { 1 };
|
||||
CHECK(int(arr.get(0)) == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] sort()") {
|
||||
Array arr = { 3, 4, 2, 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] pop_at()") {
|
||||
ErrorDetector ed;
|
||||
|
||||
Array arr = { 2, 4, 6, 8, 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);
|
||||
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);
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] slice()") {
|
||||
Array array = { 0, 1, 2, 3, 4, 5 };
|
||||
|
||||
Array slice0 = array.slice(0, 0);
|
||||
CHECK(slice0.size() == 0);
|
||||
|
||||
Array slice1 = array.slice(1, 3);
|
||||
CHECK(slice1.size() == 2);
|
||||
CHECK(slice1[0] == Variant(1));
|
||||
CHECK(slice1[1] == Variant(2));
|
||||
|
||||
Array slice2 = array.slice(1, -1);
|
||||
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() == 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() == 2);
|
||||
CHECK(slice4[0] == Variant(2));
|
||||
CHECK(slice4[1] == Variant(3));
|
||||
|
||||
Array slice5 = array.slice(-2);
|
||||
CHECK(slice5.size() == 2);
|
||||
CHECK(slice5[0] == Variant(4));
|
||||
CHECK(slice5[1] == Variant(5));
|
||||
|
||||
Array slice6 = array.slice(2, 42);
|
||||
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));
|
||||
|
||||
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(10, 0, -2);
|
||||
CHECK(slice9.size() == 3);
|
||||
CHECK(slice9[0] == Variant(5));
|
||||
CHECK(slice9[1] == Variant(3));
|
||||
CHECK(slice9[2] == Variant(1));
|
||||
|
||||
Array slice10 = array.slice(2, -10, -1);
|
||||
CHECK(slice10.size() == 3);
|
||||
CHECK(slice10[0] == Variant(2));
|
||||
CHECK(slice10[1] == Variant(1));
|
||||
CHECK(slice10[2] == Variant(0));
|
||||
|
||||
ERR_PRINT_OFF;
|
||||
Array slice11 = array.slice(4, 1);
|
||||
CHECK(slice11.size() == 0);
|
||||
|
||||
Array slice12 = array.slice(3, -4);
|
||||
CHECK(slice12.size() == 0);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
Array slice13 = Array().slice(1);
|
||||
CHECK(slice13.size() == 0);
|
||||
|
||||
Array slice14 = array.slice(6);
|
||||
CHECK(slice14.size() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Duplicate array") {
|
||||
// a = [1, [2, 2], {3: 3}]
|
||||
Array a = { 1, { 2, 2 }, Dictionary({ { 3, 3 } }) };
|
||||
|
||||
// Deep copy
|
||||
Array deep_a = a.duplicate(true);
|
||||
CHECK_MESSAGE(deep_a.id() != a.id(), "Should create a new array");
|
||||
CHECK_MESSAGE(Array(deep_a[1]).id() != Array(a[1]).id(), "Should clone nested array");
|
||||
CHECK_MESSAGE(Dictionary(deep_a[2]).id() != Dictionary(a[2]).id(), "Should clone nested dictionary");
|
||||
CHECK_EQ(deep_a, a);
|
||||
deep_a.push_back(1);
|
||||
CHECK_NE(deep_a, a);
|
||||
deep_a.pop_back();
|
||||
Array(deep_a[1]).push_back(1);
|
||||
CHECK_NE(deep_a, a);
|
||||
Array(deep_a[1]).pop_back();
|
||||
CHECK_EQ(deep_a, a);
|
||||
|
||||
// Shallow copy
|
||||
Array shallow_a = a.duplicate(false);
|
||||
CHECK_MESSAGE(shallow_a.id() != a.id(), "Should create a new array");
|
||||
CHECK_MESSAGE(Array(shallow_a[1]).id() == Array(a[1]).id(), "Should keep nested array");
|
||||
CHECK_MESSAGE(Dictionary(shallow_a[2]).id() == Dictionary(a[2]).id(), "Should keep nested dictionary");
|
||||
CHECK_EQ(shallow_a, a);
|
||||
Array(shallow_a).push_back(1);
|
||||
CHECK_NE(shallow_a, a);
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Duplicate recursive array") {
|
||||
// Self recursive
|
||||
Array a;
|
||||
a.push_back(a);
|
||||
|
||||
Array a_shallow = a.duplicate(false);
|
||||
CHECK_EQ(a, a_shallow);
|
||||
|
||||
// Deep copy of recursive array ends up with recursion limit and return
|
||||
// an invalid result (multiple nested arrays), the point is we should
|
||||
// not end up with a segfault and an error log should be printed
|
||||
ERR_PRINT_OFF;
|
||||
a.duplicate(true);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Nested recursive
|
||||
Array a1;
|
||||
Array a2;
|
||||
a2.push_back(a1);
|
||||
a1.push_back(a2);
|
||||
|
||||
Array a1_shallow = a1.duplicate(false);
|
||||
CHECK_EQ(a1, a1_shallow);
|
||||
|
||||
// Same deep copy issue as above
|
||||
ERR_PRINT_OFF;
|
||||
a1.duplicate(true);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Break the recursivity otherwise Array teardown will leak memory
|
||||
a.clear();
|
||||
a1.clear();
|
||||
a2.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Hash array") {
|
||||
// a = [1, [2, 2], {3: 3}]
|
||||
Array a = { 1, { 2, 2 }, Dictionary({ { 3, 3 } }) };
|
||||
uint32_t original_hash = a.hash();
|
||||
|
||||
a.push_back(1);
|
||||
CHECK_NE(a.hash(), original_hash);
|
||||
|
||||
a.pop_back();
|
||||
CHECK_EQ(a.hash(), original_hash);
|
||||
|
||||
Array(a[1]).push_back(1);
|
||||
CHECK_NE(a.hash(), original_hash);
|
||||
Array(a[1]).pop_back();
|
||||
CHECK_EQ(a.hash(), original_hash);
|
||||
|
||||
(Dictionary(a[2]))[1] = 1;
|
||||
CHECK_NE(a.hash(), original_hash);
|
||||
Dictionary(a[2]).erase(1);
|
||||
CHECK_EQ(a.hash(), original_hash);
|
||||
|
||||
Array a2 = a.duplicate(true);
|
||||
CHECK_EQ(a2.hash(), a.hash());
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Hash recursive array") {
|
||||
Array a1;
|
||||
a1.push_back(a1);
|
||||
|
||||
Array a2;
|
||||
a2.push_back(a2);
|
||||
|
||||
// Hash should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
CHECK_EQ(a1.hash(), a2.hash());
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Break the recursivity otherwise Array teardown will leak memory
|
||||
a1.clear();
|
||||
a2.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Empty comparison") {
|
||||
Array a1;
|
||||
Array a2;
|
||||
|
||||
// test both operator== and operator!=
|
||||
CHECK_EQ(a1, a2);
|
||||
CHECK_FALSE(a1 != a2);
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Flat comparison") {
|
||||
Array a1 = { 1 };
|
||||
Array a2 = { 1 };
|
||||
Array other_a = { 2 };
|
||||
|
||||
// test both operator== and operator!=
|
||||
CHECK_EQ(a1, a1); // compare self
|
||||
CHECK_FALSE(a1 != a1);
|
||||
CHECK_EQ(a1, a2); // different equivalent arrays
|
||||
CHECK_FALSE(a1 != a2);
|
||||
CHECK_NE(a1, other_a); // different arrays with different content
|
||||
CHECK_FALSE(a1 == other_a);
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Nested array comparison") {
|
||||
// a1 = [[[1], 2], 3]
|
||||
Array a1 = { { { 1 }, 2 }, 3 };
|
||||
|
||||
Array a2 = a1.duplicate(true);
|
||||
|
||||
// other_a = [[[1, 0], 2], 3]
|
||||
Array other_a = { { { 1, 0 }, 2 }, 3 };
|
||||
|
||||
// test both operator== and operator!=
|
||||
CHECK_EQ(a1, a1); // compare self
|
||||
CHECK_FALSE(a1 != a1);
|
||||
CHECK_EQ(a1, a2); // different equivalent arrays
|
||||
CHECK_FALSE(a1 != a2);
|
||||
CHECK_NE(a1, other_a); // different arrays with different content
|
||||
CHECK_FALSE(a1 == other_a);
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Nested dictionary comparison") {
|
||||
// a1 = [{1: 2}, 3]
|
||||
Array a1 = { Dictionary({ { 1, 2 } }), 3 };
|
||||
|
||||
Array a2 = a1.duplicate(true);
|
||||
|
||||
// other_a = [{1: 0}, 3]
|
||||
Array other_a = { Dictionary({ { 1, 0 } }), 3 };
|
||||
|
||||
// test both operator== and operator!=
|
||||
CHECK_EQ(a1, a1); // compare self
|
||||
CHECK_FALSE(a1 != a1);
|
||||
CHECK_EQ(a1, a2); // different equivalent arrays
|
||||
CHECK_FALSE(a1 != a2);
|
||||
CHECK_NE(a1, other_a); // different arrays with different content
|
||||
CHECK_FALSE(a1 == other_a);
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Recursive comparison") {
|
||||
Array a1;
|
||||
a1.push_back(a1);
|
||||
|
||||
Array a2;
|
||||
a2.push_back(a2);
|
||||
|
||||
// Comparison should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
CHECK_EQ(a1, a2);
|
||||
CHECK_FALSE(a1 != a2);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
a1.push_back(1);
|
||||
a2.push_back(1);
|
||||
|
||||
// Comparison should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
CHECK_EQ(a1, a2);
|
||||
CHECK_FALSE(a1 != a2);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
a1.push_back(1);
|
||||
a2.push_back(2);
|
||||
|
||||
// Comparison should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
CHECK_NE(a1, a2);
|
||||
CHECK_FALSE(a1 == a2);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Break the recursivity otherwise Array tearndown will leak memory
|
||||
a1.clear();
|
||||
a2.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Recursive self comparison") {
|
||||
Array a1;
|
||||
Array a2;
|
||||
a2.push_back(a1);
|
||||
a1.push_back(a2);
|
||||
|
||||
CHECK_EQ(a1, a1);
|
||||
CHECK_FALSE(a1 != a1);
|
||||
|
||||
// Break the recursivity otherwise Array tearndown will leak memory
|
||||
a1.clear();
|
||||
a2.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Iteration") {
|
||||
Array a1 = { 1, 2, 3 };
|
||||
Array a2 = { 1, 2, 3 };
|
||||
|
||||
int idx = 0;
|
||||
for (Variant &E : a1) {
|
||||
CHECK_EQ(int(a2[idx]), int(E));
|
||||
idx++;
|
||||
}
|
||||
|
||||
CHECK_EQ(idx, a1.size());
|
||||
|
||||
idx = 0;
|
||||
|
||||
for (const Variant &E : (const Array &)a1) {
|
||||
CHECK_EQ(int(a2[idx]), int(E));
|
||||
idx++;
|
||||
}
|
||||
|
||||
CHECK_EQ(idx, a1.size());
|
||||
|
||||
a1.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Iteration and modification") {
|
||||
Array a1 = { 1, 2, 3 };
|
||||
Array a2 = { 2, 3, 4 };
|
||||
Array a3 = { 1, 2, 3 };
|
||||
Array a4 = { 1, 2, 3 };
|
||||
a3.make_read_only();
|
||||
|
||||
int idx = 0;
|
||||
for (Variant &E : a1) {
|
||||
E = a2[idx];
|
||||
idx++;
|
||||
}
|
||||
|
||||
CHECK_EQ(a1, a2);
|
||||
|
||||
// Ensure read-only is respected.
|
||||
idx = 0;
|
||||
for (Variant &E : a3) {
|
||||
E = a2[idx];
|
||||
}
|
||||
|
||||
CHECK_EQ(a3, a4);
|
||||
|
||||
a1.clear();
|
||||
a2.clear();
|
||||
a4.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Typed copying") {
|
||||
TypedArray<int> a1 = { 1 };
|
||||
TypedArray<double> a2 = { 1.0 };
|
||||
|
||||
Array a3 = a1;
|
||||
TypedArray<int> a4 = a3;
|
||||
|
||||
Array a5 = a2;
|
||||
TypedArray<int> a6 = a5;
|
||||
|
||||
a3[0] = 2;
|
||||
a4[0] = 3;
|
||||
|
||||
// Same typed TypedArray should be shared.
|
||||
CHECK_EQ(a1[0], Variant(3));
|
||||
CHECK_EQ(a3[0], Variant(3));
|
||||
CHECK_EQ(a4[0], Variant(3));
|
||||
|
||||
a5[0] = 2.0;
|
||||
a6[0] = 3.0;
|
||||
|
||||
// Different typed TypedArray should not be shared.
|
||||
CHECK_EQ(a2[0], Variant(2.0));
|
||||
CHECK_EQ(a5[0], Variant(2.0));
|
||||
CHECK_EQ(a6[0], Variant(3.0));
|
||||
|
||||
a1.clear();
|
||||
a2.clear();
|
||||
a3.clear();
|
||||
a4.clear();
|
||||
a5.clear();
|
||||
a6.clear();
|
||||
}
|
||||
|
||||
static bool _find_custom_callable(const Variant &p_val) {
|
||||
return (int)p_val % 2 == 0;
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Test find_custom") {
|
||||
Array a1 = { 1, 3, 4, 5, 8, 9 };
|
||||
// Find first even number.
|
||||
int index = a1.find_custom(callable_mp_static(_find_custom_callable));
|
||||
CHECK_EQ(index, 2);
|
||||
}
|
||||
|
||||
TEST_CASE("[Array] Test rfind_custom") {
|
||||
Array a1 = { 1, 3, 4, 5, 8, 9 };
|
||||
// Find last even number.
|
||||
int index = a1.rfind_custom(callable_mp_static(_find_custom_callable));
|
||||
CHECK_EQ(index, 4);
|
||||
}
|
||||
|
||||
} // namespace TestArray
|
197
tests/core/variant/test_callable.h
Normal file
197
tests/core/variant/test_callable.h
Normal file
@@ -0,0 +1,197 @@
|
||||
/**************************************************************************/
|
||||
/* test_callable.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/object/class_db.h"
|
||||
#include "core/object/object.h"
|
||||
|
||||
#include "tests/test_macros.h"
|
||||
|
||||
namespace TestCallable {
|
||||
|
||||
class TestClass : public Object {
|
||||
GDCLASS(TestClass, Object);
|
||||
|
||||
protected:
|
||||
static void _bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("test_func_1", "foo", "bar"), &TestClass::test_func_1);
|
||||
ClassDB::bind_method(D_METHOD("test_func_2", "foo", "bar", "baz"), &TestClass::test_func_2);
|
||||
ClassDB::bind_static_method("TestClass", D_METHOD("test_func_5", "foo", "bar"), &TestClass::test_func_5);
|
||||
ClassDB::bind_static_method("TestClass", D_METHOD("test_func_6", "foo", "bar", "baz"), &TestClass::test_func_6);
|
||||
|
||||
{
|
||||
MethodInfo mi;
|
||||
mi.name = "test_func_7";
|
||||
mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));
|
||||
mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));
|
||||
|
||||
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_7", &TestClass::test_func_7, mi, varray(), false);
|
||||
}
|
||||
|
||||
{
|
||||
MethodInfo mi;
|
||||
mi.name = "test_func_8";
|
||||
mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));
|
||||
mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));
|
||||
mi.arguments.push_back(PropertyInfo(Variant::INT, "baz"));
|
||||
|
||||
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_8", &TestClass::test_func_8, mi, varray(), false);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void test_func_1(int p_foo, int p_bar) {}
|
||||
void test_func_2(int p_foo, int p_bar, int p_baz) {}
|
||||
|
||||
int test_func_3(int p_foo, int p_bar) const { return 0; }
|
||||
int test_func_4(int p_foo, int p_bar, int p_baz) const { return 0; }
|
||||
|
||||
static void test_func_5(int p_foo, int p_bar) {}
|
||||
static void test_func_6(int p_foo, int p_bar, int p_baz) {}
|
||||
|
||||
void test_func_7(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}
|
||||
void test_func_8(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}
|
||||
};
|
||||
|
||||
TEST_CASE("[Callable] Argument count") {
|
||||
TestClass *my_test = memnew(TestClass);
|
||||
|
||||
// Bound methods tests.
|
||||
|
||||
// Test simple methods.
|
||||
Callable callable_1 = Callable(my_test, "test_func_1");
|
||||
CHECK_EQ(callable_1.get_argument_count(), 2);
|
||||
Callable callable_2 = Callable(my_test, "test_func_2");
|
||||
CHECK_EQ(callable_2.get_argument_count(), 3);
|
||||
Callable callable_3 = Callable(my_test, "test_func_5");
|
||||
CHECK_EQ(callable_3.get_argument_count(), 2);
|
||||
Callable callable_4 = Callable(my_test, "test_func_6");
|
||||
CHECK_EQ(callable_4.get_argument_count(), 3);
|
||||
|
||||
// Test vararg methods.
|
||||
Callable callable_vararg_1 = Callable(my_test, "test_func_7");
|
||||
CHECK_MESSAGE(callable_vararg_1.get_argument_count() == 2, "vararg Callable should return the number of declared arguments");
|
||||
Callable callable_vararg_2 = Callable(my_test, "test_func_8");
|
||||
CHECK_MESSAGE(callable_vararg_2.get_argument_count() == 3, "vararg Callable should return the number of declared arguments");
|
||||
|
||||
// Callable MP tests.
|
||||
|
||||
// Test simple methods.
|
||||
Callable callable_mp_1 = callable_mp(my_test, &TestClass::test_func_1);
|
||||
CHECK_EQ(callable_mp_1.get_argument_count(), 2);
|
||||
Callable callable_mp_2 = callable_mp(my_test, &TestClass::test_func_2);
|
||||
CHECK_EQ(callable_mp_2.get_argument_count(), 3);
|
||||
Callable callable_mp_3 = callable_mp(my_test, &TestClass::test_func_3);
|
||||
CHECK_EQ(callable_mp_3.get_argument_count(), 2);
|
||||
Callable callable_mp_4 = callable_mp(my_test, &TestClass::test_func_4);
|
||||
CHECK_EQ(callable_mp_4.get_argument_count(), 3);
|
||||
|
||||
// Test static methods.
|
||||
Callable callable_mp_static_1 = callable_mp_static(&TestClass::test_func_5);
|
||||
CHECK_EQ(callable_mp_static_1.get_argument_count(), 2);
|
||||
Callable callable_mp_static_2 = callable_mp_static(&TestClass::test_func_6);
|
||||
CHECK_EQ(callable_mp_static_2.get_argument_count(), 3);
|
||||
|
||||
// Test bind.
|
||||
Callable callable_mp_bind_1 = callable_mp_2.bind(1);
|
||||
CHECK_MESSAGE(callable_mp_bind_1.get_argument_count() == 2, "bind should subtract from the argument count");
|
||||
Callable callable_mp_bind_2 = callable_mp_2.bind(1, 2);
|
||||
CHECK_MESSAGE(callable_mp_bind_2.get_argument_count() == 1, "bind should subtract from the argument count");
|
||||
|
||||
// Test unbind.
|
||||
Callable callable_mp_unbind_1 = callable_mp_2.unbind(1);
|
||||
CHECK_MESSAGE(callable_mp_unbind_1.get_argument_count() == 4, "unbind should add to the argument count");
|
||||
Callable callable_mp_unbind_2 = callable_mp_2.unbind(2);
|
||||
CHECK_MESSAGE(callable_mp_unbind_2.get_argument_count() == 5, "unbind should add to the argument count");
|
||||
|
||||
memdelete(my_test);
|
||||
}
|
||||
|
||||
class TestBoundUnboundArgumentCount : public Object {
|
||||
GDCLASS(TestBoundUnboundArgumentCount, Object);
|
||||
|
||||
protected:
|
||||
static void _bind_methods() {
|
||||
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func", &TestBoundUnboundArgumentCount::test_func, MethodInfo("test_func"));
|
||||
}
|
||||
|
||||
public:
|
||||
Variant test_func(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
|
||||
Array result;
|
||||
result.resize(p_argcount);
|
||||
for (int i = 0; i < p_argcount; i++) {
|
||||
result[i] = *p_args[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static String get_output(const Callable &p_callable) {
|
||||
Array effective_args = { 7, 8, 9 };
|
||||
effective_args.resize(3 - p_callable.get_unbound_arguments_count());
|
||||
effective_args.append_array(p_callable.get_bound_arguments());
|
||||
|
||||
return vformat(
|
||||
"%d %d %s %s %s",
|
||||
p_callable.get_unbound_arguments_count(),
|
||||
p_callable.get_bound_arguments_count(),
|
||||
p_callable.get_bound_arguments(),
|
||||
p_callable.call(7, 8, 9),
|
||||
effective_args);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("[Callable] Bound and unbound argument count") {
|
||||
String (*get_output)(const Callable &) = TestBoundUnboundArgumentCount::get_output;
|
||||
|
||||
TestBoundUnboundArgumentCount *test_instance = memnew(TestBoundUnboundArgumentCount);
|
||||
|
||||
Callable test_func = Callable(test_instance, "test_func");
|
||||
|
||||
CHECK(get_output(test_func) == "0 0 [] [7, 8, 9] [7, 8, 9]");
|
||||
CHECK(get_output(test_func.bind(1, 2)) == "0 2 [1, 2] [7, 8, 9, 1, 2] [7, 8, 9, 1, 2]");
|
||||
CHECK(get_output(test_func.bind(1, 2).unbind(1)) == "1 2 [1, 2] [7, 8, 1, 2] [7, 8, 1, 2]");
|
||||
CHECK(get_output(test_func.bind(1, 2).unbind(1).bind(3, 4)) == "0 3 [3, 1, 2] [7, 8, 9, 3, 1, 2] [7, 8, 9, 3, 1, 2]");
|
||||
CHECK(get_output(test_func.bind(1, 2).unbind(1).bind(3, 4).unbind(1)) == "1 3 [3, 1, 2] [7, 8, 3, 1, 2] [7, 8, 3, 1, 2]");
|
||||
|
||||
CHECK(get_output(test_func.bind(1).bind(2).bind(3).unbind(1)) == "1 3 [3, 2, 1] [7, 8, 3, 2, 1] [7, 8, 3, 2, 1]");
|
||||
CHECK(get_output(test_func.bind(1).bind(2).unbind(1).bind(3)) == "0 2 [2, 1] [7, 8, 9, 2, 1] [7, 8, 9, 2, 1]");
|
||||
CHECK(get_output(test_func.bind(1).unbind(1).bind(2).bind(3)) == "0 2 [3, 1] [7, 8, 9, 3, 1] [7, 8, 9, 3, 1]");
|
||||
CHECK(get_output(test_func.unbind(1).bind(1).bind(2).bind(3)) == "0 2 [3, 2] [7, 8, 9, 3, 2] [7, 8, 9, 3, 2]");
|
||||
|
||||
CHECK(get_output(test_func.unbind(1).unbind(1).unbind(1).bind(1, 2, 3)) == "0 0 [] [7, 8, 9] [7, 8, 9]");
|
||||
CHECK(get_output(test_func.unbind(1).unbind(1).bind(1, 2, 3).unbind(1)) == "1 1 [1] [7, 8, 1] [7, 8, 1]");
|
||||
CHECK(get_output(test_func.unbind(1).bind(1, 2, 3).unbind(1).unbind(1)) == "2 2 [1, 2] [7, 1, 2] [7, 1, 2]");
|
||||
CHECK(get_output(test_func.bind(1, 2, 3).unbind(1).unbind(1).unbind(1)) == "3 3 [1, 2, 3] [1, 2, 3] [1, 2, 3]");
|
||||
|
||||
memdelete(test_instance);
|
||||
}
|
||||
|
||||
} // namespace TestCallable
|
620
tests/core/variant/test_dictionary.h
Normal file
620
tests/core/variant/test_dictionary.h
Normal file
@@ -0,0 +1,620 @@
|
||||
/**************************************************************************/
|
||||
/* test_dictionary.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/variant/typed_dictionary.h"
|
||||
#include "tests/test_macros.h"
|
||||
|
||||
namespace TestDictionary {
|
||||
TEST_CASE("[Dictionary] Assignment using bracket notation ([])") {
|
||||
Dictionary map;
|
||||
map["Hello"] = 0;
|
||||
CHECK(int(map["Hello"]) == 0);
|
||||
map["Hello"] = 3;
|
||||
CHECK(int(map["Hello"]) == 3);
|
||||
map["World!"] = 4;
|
||||
CHECK(int(map["World!"]) == 4);
|
||||
|
||||
map[StringName("HelloName")] = 6;
|
||||
CHECK(int(map[StringName("HelloName")]) == 6);
|
||||
CHECK(int(map.find_key(6).get_type()) == Variant::STRING_NAME);
|
||||
map[StringName("HelloName")] = 7;
|
||||
CHECK(int(map[StringName("HelloName")]) == 7);
|
||||
|
||||
// Test String and StringName are equivalent.
|
||||
map[StringName("Hello")] = 8;
|
||||
CHECK(int(map["Hello"]) == 8);
|
||||
map["Hello"] = 9;
|
||||
CHECK(int(map[StringName("Hello")]) == 9);
|
||||
|
||||
// Test non-string keys, since keys can be of any Variant type.
|
||||
map[12345] = -5;
|
||||
CHECK(int(map[12345]) == -5);
|
||||
map[false] = 128;
|
||||
CHECK(int(map[false]) == 128);
|
||||
map[Vector2(10, 20)] = 30;
|
||||
CHECK(int(map[Vector2(10, 20)]) == 30);
|
||||
map[0] = 400;
|
||||
CHECK(int(map[0]) == 400);
|
||||
// Check that assigning 0 doesn't overwrite the value for `false`.
|
||||
CHECK(int(map[false]) == 128);
|
||||
|
||||
// Ensure read-only maps aren't modified by non-existing keys.
|
||||
const int length = map.size();
|
||||
map.make_read_only();
|
||||
CHECK(int(map["This key does not exist"].get_type()) == Variant::NIL);
|
||||
CHECK(map.size() == length);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] List init") {
|
||||
Dictionary dict{
|
||||
{ 0, "int" },
|
||||
{ "packed_string_array", PackedStringArray({ "array", "of", "values" }) },
|
||||
{ "key", Dictionary({ { "nested", 200 } }) },
|
||||
{ Vector2(), "v2" },
|
||||
};
|
||||
CHECK(dict.size() == 4);
|
||||
CHECK(dict[0] == "int");
|
||||
CHECK(PackedStringArray(dict["packed_string_array"])[2] == "values");
|
||||
CHECK(Dictionary(dict["key"])["nested"] == Variant(200));
|
||||
CHECK(dict[Vector2()] == "v2");
|
||||
|
||||
TypedDictionary<double, double> tdict{
|
||||
{ 0.0, 1.0 },
|
||||
{ 5.0, 2.0 },
|
||||
};
|
||||
CHECK_EQ(tdict[0.0], Variant(1.0));
|
||||
CHECK_EQ(tdict[5.0], Variant(2.0));
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] get_key_list()") {
|
||||
Dictionary map;
|
||||
LocalVector<Variant> keys;
|
||||
keys = map.get_key_list();
|
||||
CHECK(keys.is_empty());
|
||||
map[1] = 3;
|
||||
keys = map.get_key_list();
|
||||
CHECK(keys.size() == 1);
|
||||
CHECK(int(keys[0]) == 1);
|
||||
map[2] = 4;
|
||||
keys = map.get_key_list();
|
||||
CHECK(keys.size() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] get_key_at_index()") {
|
||||
Dictionary map;
|
||||
map[4] = 3;
|
||||
Variant val = map.get_key_at_index(0);
|
||||
CHECK(int(val) == 4);
|
||||
map[3] = 1;
|
||||
val = map.get_key_at_index(0);
|
||||
CHECK(int(val) == 4);
|
||||
val = map.get_key_at_index(1);
|
||||
CHECK(int(val) == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] getptr()") {
|
||||
Dictionary map;
|
||||
map[1] = 3;
|
||||
Variant *key = map.getptr(1);
|
||||
CHECK(int(*key) == 3);
|
||||
key = map.getptr(2);
|
||||
CHECK(key == nullptr);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] get_valid()") {
|
||||
Dictionary map;
|
||||
map[1] = 3;
|
||||
Variant val = map.get_valid(1);
|
||||
CHECK(int(val) == 3);
|
||||
}
|
||||
TEST_CASE("[Dictionary] get()") {
|
||||
Dictionary map;
|
||||
map[1] = 3;
|
||||
Variant val = map.get(1, -1);
|
||||
CHECK(int(val) == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] size(), empty() and clear()") {
|
||||
Dictionary map;
|
||||
CHECK(map.size() == 0);
|
||||
CHECK(map.is_empty());
|
||||
map[1] = 3;
|
||||
CHECK(map.size() == 1);
|
||||
CHECK(!map.is_empty());
|
||||
map.clear();
|
||||
CHECK(map.size() == 0);
|
||||
CHECK(map.is_empty());
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] has() and has_all()") {
|
||||
Dictionary map;
|
||||
CHECK(map.has(1) == false);
|
||||
map[1] = 3;
|
||||
CHECK(map.has(1));
|
||||
Array keys;
|
||||
keys.push_back(1);
|
||||
CHECK(map.has_all(keys));
|
||||
keys.push_back(2);
|
||||
CHECK(map.has_all(keys) == false);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] keys() and values()") {
|
||||
Dictionary map;
|
||||
Array keys = map.keys();
|
||||
Array values = map.values();
|
||||
CHECK(keys.is_empty());
|
||||
CHECK(values.is_empty());
|
||||
map[1] = 3;
|
||||
keys = map.keys();
|
||||
values = map.values();
|
||||
CHECK(int(keys[0]) == 1);
|
||||
CHECK(int(values[0]) == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] Duplicate dictionary") {
|
||||
// d = {1: {1: 1}, {2: 2}: [2], [3]: 3}
|
||||
Dictionary k2 = { { 2, 2 } };
|
||||
Array k3 = { 3 };
|
||||
Dictionary d = {
|
||||
{ 1, Dictionary({ { 1, 1 } }) },
|
||||
{ k2, Array({ 2 }) },
|
||||
{ k3, 3 }
|
||||
};
|
||||
|
||||
// Deep copy
|
||||
Dictionary deep_d = d.duplicate(true);
|
||||
CHECK_MESSAGE(deep_d.id() != d.id(), "Should create a new dictionary");
|
||||
CHECK_MESSAGE(Dictionary(deep_d[1]).id() != Dictionary(d[1]).id(), "Should clone nested dictionary");
|
||||
CHECK_MESSAGE(Array(deep_d[k2]).id() != Array(d[k2]).id(), "Should clone nested array");
|
||||
CHECK_EQ(deep_d, d);
|
||||
deep_d[0] = 0;
|
||||
CHECK_NE(deep_d, d);
|
||||
deep_d.erase(0);
|
||||
Dictionary(deep_d[1]).operator[](0) = 0;
|
||||
CHECK_NE(deep_d, d);
|
||||
Dictionary(deep_d[1]).erase(0);
|
||||
CHECK_EQ(deep_d, d);
|
||||
// Keys should also be copied
|
||||
k2[0] = 0;
|
||||
CHECK_NE(deep_d, d);
|
||||
k2.erase(0);
|
||||
CHECK_EQ(deep_d, d);
|
||||
k3.push_back(0);
|
||||
CHECK_NE(deep_d, d);
|
||||
k3.pop_back();
|
||||
CHECK_EQ(deep_d, d);
|
||||
|
||||
// Shallow copy
|
||||
Dictionary shallow_d = d.duplicate(false);
|
||||
CHECK_MESSAGE(shallow_d.id() != d.id(), "Should create a new array");
|
||||
CHECK_MESSAGE(Dictionary(shallow_d[1]).id() == Dictionary(d[1]).id(), "Should keep nested dictionary");
|
||||
CHECK_MESSAGE(Array(shallow_d[k2]).id() == Array(d[k2]).id(), "Should keep nested array");
|
||||
CHECK_EQ(shallow_d, d);
|
||||
shallow_d[0] = 0;
|
||||
CHECK_NE(shallow_d, d);
|
||||
shallow_d.erase(0);
|
||||
#if 0 // TODO: recursion in dict key currently is buggy
|
||||
// Keys should also be shallowed
|
||||
k2[0] = 0;
|
||||
CHECK_EQ(shallow_d, d);
|
||||
k2.erase(0);
|
||||
k3.push_back(0);
|
||||
CHECK_EQ(shallow_d, d);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] Duplicate recursive dictionary") {
|
||||
// Self recursive
|
||||
Dictionary d;
|
||||
d[1] = d;
|
||||
|
||||
Dictionary d_shallow = d.duplicate(false);
|
||||
CHECK_EQ(d, d_shallow);
|
||||
|
||||
// Deep copy of recursive dictionary endup with recursion limit and return
|
||||
// an invalid result (multiple nested dictionaries), the point is we should
|
||||
// not end up with a segfault and an error log should be printed
|
||||
ERR_PRINT_OFF;
|
||||
d.duplicate(true);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Nested recursive
|
||||
Dictionary d1;
|
||||
Dictionary d2;
|
||||
d1[2] = d2;
|
||||
d2[1] = d1;
|
||||
|
||||
Dictionary d1_shallow = d1.duplicate(false);
|
||||
CHECK_EQ(d1, d1_shallow);
|
||||
|
||||
// Same deep copy issue as above
|
||||
ERR_PRINT_OFF;
|
||||
d1.duplicate(true);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Break the recursivity otherwise Dictionary teardown will leak memory
|
||||
d.clear();
|
||||
d1.clear();
|
||||
d2.clear();
|
||||
}
|
||||
|
||||
#if 0 // TODO: duplicate recursion in dict key is currently buggy
|
||||
TEST_CASE("[Dictionary] Duplicate recursive dictionary on keys") {
|
||||
// Self recursive
|
||||
Dictionary d;
|
||||
d[d] = d;
|
||||
|
||||
Dictionary d_shallow = d.duplicate(false);
|
||||
CHECK_EQ(d, d_shallow);
|
||||
|
||||
// Deep copy of recursive dictionary endup with recursion limit and return
|
||||
// an invalid result (multiple nested dictionaries), the point is we should
|
||||
// not end up with a segfault and an error log should be printed
|
||||
ERR_PRINT_OFF;
|
||||
d.duplicate(true);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Nested recursive
|
||||
Dictionary d1;
|
||||
Dictionary d2;
|
||||
d1[d2] = d2;
|
||||
d2[d1] = d1;
|
||||
|
||||
Dictionary d1_shallow = d1.duplicate(false);
|
||||
CHECK_EQ(d1, d1_shallow);
|
||||
|
||||
// Same deep copy issue as above
|
||||
ERR_PRINT_OFF;
|
||||
d1.duplicate(true);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Break the recursivity otherwise Dictionary teardown will leak memory
|
||||
d.clear();
|
||||
d1.clear();
|
||||
d2.clear();
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("[Dictionary] Hash dictionary") {
|
||||
// d = {1: {1: 1}, {2: 2}: [2], [3]: 3}
|
||||
Dictionary k2 = { { 2, 2 } };
|
||||
Array k3 = { 3 };
|
||||
Dictionary d = {
|
||||
{ 1, Dictionary({ { 1, 1 } }) },
|
||||
{ k2, Array({ 2 }) },
|
||||
{ k3, 3 }
|
||||
};
|
||||
uint32_t original_hash = d.hash();
|
||||
|
||||
// Modify dict change the hash
|
||||
d[0] = 0;
|
||||
CHECK_NE(d.hash(), original_hash);
|
||||
d.erase(0);
|
||||
CHECK_EQ(d.hash(), original_hash);
|
||||
|
||||
// Modify nested item change the hash
|
||||
Dictionary(d[1]).operator[](0) = 0;
|
||||
CHECK_NE(d.hash(), original_hash);
|
||||
Dictionary(d[1]).erase(0);
|
||||
Array(d[k2]).push_back(0);
|
||||
CHECK_NE(d.hash(), original_hash);
|
||||
Array(d[k2]).pop_back();
|
||||
|
||||
// Modify a key change the hash
|
||||
k2[0] = 0;
|
||||
CHECK_NE(d.hash(), original_hash);
|
||||
k2.erase(0);
|
||||
CHECK_EQ(d.hash(), original_hash);
|
||||
k3.push_back(0);
|
||||
CHECK_NE(d.hash(), original_hash);
|
||||
k3.pop_back();
|
||||
CHECK_EQ(d.hash(), original_hash);
|
||||
|
||||
// Duplication doesn't change the hash
|
||||
Dictionary d2 = d.duplicate(true);
|
||||
CHECK_EQ(d2.hash(), original_hash);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] Hash recursive dictionary") {
|
||||
Dictionary d;
|
||||
d[1] = d;
|
||||
|
||||
// Hash should reach recursion limit, we just make sure this doesn't blow up
|
||||
ERR_PRINT_OFF;
|
||||
d.hash();
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Break the recursivity otherwise Dictionary teardown will leak memory
|
||||
d.clear();
|
||||
}
|
||||
|
||||
#if 0 // TODO: recursion in dict key is currently buggy
|
||||
TEST_CASE("[Dictionary] Hash recursive dictionary on keys") {
|
||||
Dictionary d;
|
||||
d[d] = 1;
|
||||
|
||||
// Hash should reach recursion limit, we just make sure this doesn't blow up
|
||||
ERR_PRINT_OFF;
|
||||
d.hash();
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Break the recursivity otherwise Dictionary teardown will leak memory
|
||||
d.clear();
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("[Dictionary] Empty comparison") {
|
||||
Dictionary d1;
|
||||
Dictionary d2;
|
||||
|
||||
// test both operator== and operator!=
|
||||
CHECK_EQ(d1, d2);
|
||||
CHECK_FALSE(d1 != d2);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] Flat comparison") {
|
||||
Dictionary d1 = { { 1, 1 } };
|
||||
Dictionary d2 = { { 1, 1 } };
|
||||
Dictionary other_d = { { 2, 1 } };
|
||||
|
||||
// test both operator== and operator!=
|
||||
CHECK_EQ(d1, d1); // compare self
|
||||
CHECK_FALSE(d1 != d1);
|
||||
CHECK_EQ(d1, d2); // different equivalent arrays
|
||||
CHECK_FALSE(d1 != d2);
|
||||
CHECK_NE(d1, other_d); // different arrays with different content
|
||||
CHECK_FALSE(d1 == other_d);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] Nested dictionary comparison") {
|
||||
// d1 = {1: {2: {3: 4}}}
|
||||
Dictionary d1 = { { 1, Dictionary({ { 2, Dictionary({ { 3, 4 } }) } }) } };
|
||||
|
||||
Dictionary d2 = d1.duplicate(true);
|
||||
|
||||
// other_d = {1: {2: {3: 0}}}
|
||||
Dictionary other_d = { { 1, Dictionary({ { 2, Dictionary({ { 3, 0 } }) } }) } };
|
||||
|
||||
// test both operator== and operator!=
|
||||
CHECK_EQ(d1, d1); // compare self
|
||||
CHECK_FALSE(d1 != d1);
|
||||
CHECK_EQ(d1, d2); // different equivalent arrays
|
||||
CHECK_FALSE(d1 != d2);
|
||||
CHECK_NE(d1, other_d); // different arrays with different content
|
||||
CHECK_FALSE(d1 == other_d);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] Nested array comparison") {
|
||||
// d1 = {1: [2, 3]}
|
||||
Dictionary d1 = { { 1, { 2, 3 } } };
|
||||
|
||||
Dictionary d2 = d1.duplicate(true);
|
||||
|
||||
// other_d = {1: [2, 0]}
|
||||
Dictionary other_d = { { 1, { 2, 0 } } };
|
||||
|
||||
// test both operator== and operator!=
|
||||
CHECK_EQ(d1, d1); // compare self
|
||||
CHECK_FALSE(d1 != d1);
|
||||
CHECK_EQ(d1, d2); // different equivalent arrays
|
||||
CHECK_FALSE(d1 != d2);
|
||||
CHECK_NE(d1, other_d); // different arrays with different content
|
||||
CHECK_FALSE(d1 == other_d);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] Recursive comparison") {
|
||||
Dictionary d1;
|
||||
d1[1] = d1;
|
||||
|
||||
Dictionary d2;
|
||||
d2[1] = d2;
|
||||
|
||||
// Comparison should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
CHECK_EQ(d1, d2);
|
||||
CHECK_FALSE(d1 != d2);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
d1[2] = 2;
|
||||
d2[2] = 2;
|
||||
|
||||
// Comparison should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
CHECK_EQ(d1, d2);
|
||||
CHECK_FALSE(d1 != d2);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
d1[3] = 3;
|
||||
d2[3] = 0;
|
||||
|
||||
// Comparison should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
CHECK_NE(d1, d2);
|
||||
CHECK_FALSE(d1 == d2);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Break the recursivity otherwise Dictionary teardown will leak memory
|
||||
d1.clear();
|
||||
d2.clear();
|
||||
}
|
||||
|
||||
#if 0 // TODO: recursion in dict key is currently buggy
|
||||
TEST_CASE("[Dictionary] Recursive comparison on keys") {
|
||||
Dictionary d1;
|
||||
// Hash computation should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
d1[d1] = 1;
|
||||
ERR_PRINT_ON;
|
||||
|
||||
Dictionary d2;
|
||||
// Hash computation should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
d2[d2] = 1;
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Comparison should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
CHECK_EQ(d1, d2);
|
||||
CHECK_FALSE(d1 != d2);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
d1[2] = 2;
|
||||
d2[2] = 2;
|
||||
|
||||
// Comparison should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
CHECK_EQ(d1, d2);
|
||||
CHECK_FALSE(d1 != d2);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
d1[3] = 3;
|
||||
d2[3] = 0;
|
||||
|
||||
// Comparison should reach recursion limit
|
||||
ERR_PRINT_OFF;
|
||||
CHECK_NE(d1, d2);
|
||||
CHECK_FALSE(d1 == d2);
|
||||
ERR_PRINT_ON;
|
||||
|
||||
// Break the recursivity otherwise Dictionary teardown will leak memory
|
||||
d1.clear();
|
||||
d2.clear();
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("[Dictionary] Recursive self comparison") {
|
||||
Dictionary d1;
|
||||
Dictionary d2;
|
||||
d1[1] = d2;
|
||||
d2[1] = d1;
|
||||
|
||||
CHECK_EQ(d1, d1);
|
||||
CHECK_FALSE(d1 != d1);
|
||||
|
||||
// Break the recursivity otherwise Dictionary teardown will leak memory
|
||||
d1.clear();
|
||||
d2.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] Order and find") {
|
||||
Dictionary d;
|
||||
d[4] = "four";
|
||||
d[8] = "eight";
|
||||
d[12] = "twelve";
|
||||
d["4"] = "four";
|
||||
|
||||
Array keys = { 4, 8, 12, "4" };
|
||||
|
||||
CHECK_EQ(d.keys(), keys);
|
||||
CHECK_EQ(d.find_key("four"), Variant(4));
|
||||
CHECK_EQ(d.find_key("does not exist"), Variant());
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] Typed copying") {
|
||||
TypedDictionary<int, int> d1;
|
||||
d1[0] = 1;
|
||||
|
||||
TypedDictionary<double, double> d2;
|
||||
d2[0] = 1.0;
|
||||
|
||||
Dictionary d3 = d1;
|
||||
TypedDictionary<int, int> d4 = d3;
|
||||
|
||||
Dictionary d5 = d2;
|
||||
TypedDictionary<int, int> d6 = d5;
|
||||
|
||||
d3[0] = 2;
|
||||
d4[0] = 3;
|
||||
|
||||
// Same typed TypedDictionary should be shared.
|
||||
CHECK_EQ(d1[0], Variant(3));
|
||||
CHECK_EQ(d3[0], Variant(3));
|
||||
CHECK_EQ(d4[0], Variant(3));
|
||||
|
||||
d5[0] = 2.0;
|
||||
d6[0] = 3.0;
|
||||
|
||||
// Different typed TypedDictionary should not be shared.
|
||||
CHECK_EQ(d2[0], Variant(2.0));
|
||||
CHECK_EQ(d5[0], Variant(2.0));
|
||||
CHECK_EQ(d6[0], Variant(3.0));
|
||||
|
||||
d1.clear();
|
||||
d2.clear();
|
||||
d3.clear();
|
||||
d4.clear();
|
||||
d5.clear();
|
||||
d6.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] Iteration") {
|
||||
Dictionary a1 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
|
||||
Dictionary a2 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
|
||||
|
||||
int idx = 0;
|
||||
|
||||
for (const KeyValue<Variant, Variant> &kv : (const Dictionary &)a1) {
|
||||
CHECK_EQ(int(a2[kv.key]), int(kv.value));
|
||||
idx++;
|
||||
}
|
||||
|
||||
CHECK_EQ(idx, a1.size());
|
||||
|
||||
a1.clear();
|
||||
a2.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] Object value init") {
|
||||
Object *a = memnew(Object);
|
||||
Object *b = memnew(Object);
|
||||
TypedDictionary<double, Object *> tdict = {
|
||||
{ 0.0, a },
|
||||
{ 5.0, b },
|
||||
};
|
||||
CHECK_EQ(tdict[0.0], Variant(a));
|
||||
CHECK_EQ(tdict[5.0], Variant(b));
|
||||
memdelete(a);
|
||||
memdelete(b);
|
||||
}
|
||||
|
||||
TEST_CASE("[Dictionary] RefCounted value init") {
|
||||
Ref<RefCounted> a = memnew(RefCounted);
|
||||
Ref<RefCounted> b = memnew(RefCounted);
|
||||
TypedDictionary<double, Ref<RefCounted>> tdict = {
|
||||
{ 0.0, a },
|
||||
{ 5.0, b },
|
||||
};
|
||||
CHECK_EQ(tdict[0.0], Variant(a));
|
||||
CHECK_EQ(tdict[5.0], Variant(b));
|
||||
}
|
||||
|
||||
} // namespace TestDictionary
|
2248
tests/core/variant/test_variant.h
Normal file
2248
tests/core/variant/test_variant.h
Normal file
File diff suppressed because it is too large
Load Diff
131
tests/core/variant/test_variant_utility.h
Normal file
131
tests/core/variant/test_variant_utility.h
Normal file
@@ -0,0 +1,131 @@
|
||||
/**************************************************************************/
|
||||
/* test_variant_utility.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/variant/variant_utility.h"
|
||||
|
||||
#include "tests/test_macros.h"
|
||||
|
||||
namespace TestVariantUtility {
|
||||
|
||||
TEST_CASE("[VariantUtility] Type conversion") {
|
||||
Variant converted;
|
||||
converted = VariantUtilityFunctions::type_convert("Hi!", Variant::Type::NIL);
|
||||
CHECK(converted.get_type() == Variant::Type::NIL);
|
||||
CHECK(converted == Variant());
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert("Hi!", Variant::Type::INT);
|
||||
CHECK(converted.get_type() == Variant::Type::INT);
|
||||
CHECK(converted == Variant(0));
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert("123", Variant::Type::INT);
|
||||
CHECK(converted.get_type() == Variant::Type::INT);
|
||||
CHECK(converted == Variant(123));
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert(123, Variant::Type::STRING);
|
||||
CHECK(converted.get_type() == Variant::Type::STRING);
|
||||
CHECK(converted == Variant("123"));
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert(123.4, Variant::Type::INT);
|
||||
CHECK(converted.get_type() == Variant::Type::INT);
|
||||
CHECK(converted == Variant(123));
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert(5, Variant::Type::VECTOR2);
|
||||
CHECK(converted.get_type() == Variant::Type::VECTOR2);
|
||||
CHECK(converted == Variant(Vector2(0, 0)));
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert(Vector3(1, 2, 3), Variant::Type::VECTOR2);
|
||||
CHECK(converted.get_type() == Variant::Type::VECTOR2);
|
||||
CHECK(converted == Variant(Vector2(1, 2)));
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert(Vector2(1, 2), Variant::Type::VECTOR4);
|
||||
CHECK(converted.get_type() == Variant::Type::VECTOR4);
|
||||
CHECK(converted == Variant(Vector4(1, 2, 0, 0)));
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert(Vector4(1.2, 3.4, 5.6, 7.8), Variant::Type::VECTOR3I);
|
||||
CHECK(converted.get_type() == Variant::Type::VECTOR3I);
|
||||
CHECK(converted == Variant(Vector3i(1, 3, 5)));
|
||||
|
||||
{
|
||||
Basis basis = Basis::from_scale(Vector3(1.2, 3.4, 5.6));
|
||||
Transform3D transform = Transform3D(basis, Vector3());
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert(transform, Variant::Type::BASIS);
|
||||
CHECK(converted.get_type() == Variant::Type::BASIS);
|
||||
CHECK(converted == basis);
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert(basis, Variant::Type::TRANSFORM3D);
|
||||
CHECK(converted.get_type() == Variant::Type::TRANSFORM3D);
|
||||
CHECK(converted == transform);
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert(basis, Variant::Type::STRING);
|
||||
CHECK(converted.get_type() == Variant::Type::STRING);
|
||||
CHECK(converted == Variant("[X: (1.2, 0.0, 0.0), Y: (0.0, 3.4, 0.0), Z: (0.0, 0.0, 5.6)]"));
|
||||
}
|
||||
|
||||
{
|
||||
Array arr = { 1.2, 3.4, 5.6 };
|
||||
PackedFloat64Array packed = { 1.2, 3.4, 5.6 };
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert(arr, Variant::Type::PACKED_FLOAT64_ARRAY);
|
||||
CHECK(converted.get_type() == Variant::Type::PACKED_FLOAT64_ARRAY);
|
||||
CHECK(converted == packed);
|
||||
|
||||
converted = VariantUtilityFunctions::type_convert(packed, Variant::Type::ARRAY);
|
||||
CHECK(converted.get_type() == Variant::Type::ARRAY);
|
||||
CHECK(converted == arr);
|
||||
}
|
||||
|
||||
{
|
||||
// Check that using Variant::call_utility_function also works.
|
||||
Vector<const Variant *> args;
|
||||
Variant data_arg = "Hi!";
|
||||
args.push_back(&data_arg);
|
||||
Variant type_arg = Variant::Type::NIL;
|
||||
args.push_back(&type_arg);
|
||||
Callable::CallError call_error;
|
||||
Variant::call_utility_function("type_convert", &converted, (const Variant **)args.ptr(), 2, call_error);
|
||||
CHECK(converted.get_type() == Variant::Type::NIL);
|
||||
CHECK(converted == Variant());
|
||||
|
||||
type_arg = Variant::Type::INT;
|
||||
Variant::call_utility_function("type_convert", &converted, (const Variant **)args.ptr(), 2, call_error);
|
||||
CHECK(converted.get_type() == Variant::Type::INT);
|
||||
CHECK(converted == Variant(0));
|
||||
|
||||
data_arg = "123";
|
||||
Variant::call_utility_function("type_convert", &converted, (const Variant **)args.ptr(), 2, call_error);
|
||||
CHECK(converted.get_type() == Variant::Type::INT);
|
||||
CHECK(converted == Variant(123));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace TestVariantUtility
|
Reference in New Issue
Block a user