feat: updated engine version to 4.4-rc1

This commit is contained in:
Sara 2025-02-23 14:38:14 +01:00
parent ee00efde1f
commit 21ba8e33af
5459 changed files with 1128836 additions and 198305 deletions

View file

@ -0,0 +1,313 @@
/**************************************************************************/
/* test_a_hash_map.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. */
/**************************************************************************/
#ifndef TEST_A_HASH_MAP_H
#define TEST_A_HASH_MAP_H
#include "core/templates/a_hash_map.h"
#include "tests/test_macros.h"
namespace TestAHashMap {
TEST_CASE("[AHashMap] List initialization") {
AHashMap<int, String> map{ { 0, "A" }, { 1, "B" }, { 2, "C" }, { 3, "D" }, { 4, "E" } };
CHECK(map.size() == 5);
CHECK(map[0] == "A");
CHECK(map[1] == "B");
CHECK(map[2] == "C");
CHECK(map[3] == "D");
CHECK(map[4] == "E");
}
TEST_CASE("[AHashMap] List initialization with existing elements") {
AHashMap<int, String> map{ { 0, "A" }, { 0, "B" }, { 0, "C" }, { 0, "D" }, { 0, "E" } };
CHECK(map.size() == 1);
CHECK(map[0] == "E");
}
TEST_CASE("[AHashMap] Insert element") {
AHashMap<int, int> map;
AHashMap<int, int>::Iterator e = map.insert(42, 84);
CHECK(e);
CHECK(e->key == 42);
CHECK(e->value == 84);
CHECK(map[42] == 84);
CHECK(map.has(42));
CHECK(map.find(42));
}
TEST_CASE("[AHashMap] Overwrite element") {
AHashMap<int, int> map;
map.insert(42, 84);
map.insert(42, 1234);
CHECK(map[42] == 1234);
}
TEST_CASE("[AHashMap] Erase via element") {
AHashMap<int, int> map;
AHashMap<int, int>::Iterator e = map.insert(42, 84);
map.remove(e);
CHECK(!map.has(42));
CHECK(!map.find(42));
}
TEST_CASE("[AHashMap] Erase via key") {
AHashMap<int, int> map;
map.insert(42, 84);
map.erase(42);
CHECK(!map.has(42));
CHECK(!map.find(42));
}
TEST_CASE("[AHashMap] Size") {
AHashMap<int, int> map;
map.insert(42, 84);
map.insert(123, 84);
map.insert(123, 84);
map.insert(0, 84);
map.insert(123485, 84);
CHECK(map.size() == 4);
}
TEST_CASE("[AHashMap] Iteration") {
AHashMap<int, int> map;
map.insert(42, 84);
map.insert(123, 12385);
map.insert(0, 12934);
map.insert(123485, 1238888);
map.insert(123, 111111);
Vector<Pair<int, int>> expected;
expected.push_back(Pair<int, int>(42, 84));
expected.push_back(Pair<int, int>(123, 111111));
expected.push_back(Pair<int, int>(0, 12934));
expected.push_back(Pair<int, int>(123485, 1238888));
int idx = 0;
for (const KeyValue<int, int> &E : map) {
CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
idx++;
}
idx--;
for (AHashMap<int, int>::Iterator it = map.last(); it; --it) {
CHECK(expected[idx] == Pair<int, int>(it->key, it->value));
idx--;
}
}
TEST_CASE("[AHashMap] Const iteration") {
AHashMap<int, int> map;
map.insert(42, 84);
map.insert(123, 12385);
map.insert(0, 12934);
map.insert(123485, 1238888);
map.insert(123, 111111);
const AHashMap<int, int> const_map = map;
Vector<Pair<int, int>> expected;
expected.push_back(Pair<int, int>(42, 84));
expected.push_back(Pair<int, int>(123, 111111));
expected.push_back(Pair<int, int>(0, 12934));
expected.push_back(Pair<int, int>(123485, 1238888));
expected.push_back(Pair<int, int>(123, 111111));
int idx = 0;
for (const KeyValue<int, int> &E : const_map) {
CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
idx++;
}
idx--;
for (AHashMap<int, int>::ConstIterator it = const_map.last(); it; --it) {
CHECK(expected[idx] == Pair<int, int>(it->key, it->value));
idx--;
}
}
TEST_CASE("[AHashMap] Replace key") {
AHashMap<int, int> map;
map.insert(42, 84);
map.insert(0, 12934);
CHECK(map.replace_key(0, 1));
CHECK(map.has(1));
CHECK(map[1] == 12934);
}
TEST_CASE("[AHashMap] Clear") {
AHashMap<int, int> map;
map.insert(42, 84);
map.insert(123, 12385);
map.insert(0, 12934);
map.clear();
CHECK(!map.has(42));
CHECK(map.size() == 0);
CHECK(map.is_empty());
}
TEST_CASE("[AHashMap] Get") {
AHashMap<int, int> map;
map.insert(42, 84);
map.insert(123, 12385);
map.insert(0, 12934);
CHECK(map.get(123) == 12385);
map.get(123) = 10;
CHECK(map.get(123) == 10);
CHECK(*map.getptr(0) == 12934);
*map.getptr(0) = 1;
CHECK(*map.getptr(0) == 1);
CHECK(map.get(42) == 84);
CHECK(map.getptr(-10) == nullptr);
}
TEST_CASE("[AHashMap] Insert, iterate and remove many elements") {
const int elem_max = 1234;
AHashMap<int, int> map;
for (int i = 0; i < elem_max; i++) {
map.insert(i, i);
}
//insert order should have been kept
int idx = 0;
for (auto &K : map) {
CHECK(idx == K.key);
CHECK(idx == K.value);
CHECK(map.has(idx));
idx++;
}
Vector<int> elems_still_valid;
for (int i = 0; i < elem_max; i++) {
if ((i % 5) == 0) {
map.erase(i);
} else {
elems_still_valid.push_back(i);
}
}
CHECK(elems_still_valid.size() == map.size());
for (int i = 0; i < elems_still_valid.size(); i++) {
CHECK(map.has(elems_still_valid[i]));
}
}
TEST_CASE("[AHashMap] Insert, iterate and remove many strings") {
const int elem_max = 432;
AHashMap<String, String> map;
for (int i = 0; i < elem_max; i++) {
map.insert(itos(i), itos(i));
}
//insert order should have been kept
int idx = 0;
for (auto &K : map) {
CHECK(itos(idx) == K.key);
CHECK(itos(idx) == K.value);
CHECK(map.has(itos(idx)));
idx++;
}
Vector<String> elems_still_valid;
for (int i = 0; i < elem_max; i++) {
if ((i % 5) == 0) {
map.erase(itos(i));
} else {
elems_still_valid.push_back(itos(i));
}
}
CHECK(elems_still_valid.size() == map.size());
for (int i = 0; i < elems_still_valid.size(); i++) {
CHECK(map.has(elems_still_valid[i]));
}
elems_still_valid.clear();
}
TEST_CASE("[AHashMap] Copy constructor") {
AHashMap<int, int> map0;
const uint32_t count = 5;
for (uint32_t i = 0; i < count; i++) {
map0.insert(i, i);
}
AHashMap<int, int> map1(map0);
CHECK(map0.size() == map1.size());
CHECK(map0.get_capacity() == map1.get_capacity());
CHECK(*map0.getptr(0) == *map1.getptr(0));
}
TEST_CASE("[AHashMap] Operator =") {
AHashMap<int, int> map0;
AHashMap<int, int> map1;
const uint32_t count = 5;
map1.insert(1234, 1234);
for (uint32_t i = 0; i < count; i++) {
map0.insert(i, i);
}
map1 = map0;
CHECK(map0.size() == map1.size());
CHECK(map0.get_capacity() == map1.get_capacity());
CHECK(*map0.getptr(0) == *map1.getptr(0));
}
TEST_CASE("[AHashMap] Array methods") {
AHashMap<int, int> map;
for (int i = 0; i < 100; i++) {
map.insert(100 - i, i);
}
for (int i = 0; i < 100; i++) {
CHECK(map.get_by_index(i).value == i);
}
int index = map.get_index(1);
CHECK(map.get_by_index(index).value == 99);
CHECK(map.erase_by_index(index));
CHECK(!map.erase_by_index(index));
CHECK(map.get_index(1) == -1);
}
} // namespace TestAHashMap
#endif // TEST_A_HASH_MAP_H

View file

@ -201,10 +201,10 @@ public:
command_queue.push_and_sync(this, &SharedThreadState::func2, tr, f);
break;
case TEST_MSGRET_FUNC1_TRANSFORM:
command_queue.push_and_ret(this, &SharedThreadState::func1r, tr, &otr);
command_queue.push_and_ret(this, &SharedThreadState::func1r, &otr, tr);
break;
case TEST_MSGRET_FUNC2_TRANSFORM_FLOAT:
command_queue.push_and_ret(this, &SharedThreadState::func2r, tr, f, &otr);
command_queue.push_and_ret(this, &SharedThreadState::func2r, &otr, tr, f);
break;
default:
break;
@ -244,6 +244,44 @@ public:
}
writer_thread.wait_to_finish();
}
struct CopyMoveTestType {
inline static int copy_count;
inline static int move_count;
int value = 0;
CopyMoveTestType(int p_value = 0) :
value(p_value) {}
CopyMoveTestType(const CopyMoveTestType &p_other) :
value(p_other.value) {
copy_count++;
}
CopyMoveTestType(CopyMoveTestType &&p_other) :
value(p_other.value) {
move_count++;
}
CopyMoveTestType &operator=(const CopyMoveTestType &p_other) {
value = p_other.value;
copy_count++;
return *this;
}
CopyMoveTestType &operator=(CopyMoveTestType &&p_other) {
value = p_other.value;
move_count++;
return *this;
}
};
void copy_move_test_copy(CopyMoveTestType p_test_type) {
}
void copy_move_test_ref(const CopyMoveTestType &p_test_type) {
}
void copy_move_test_move(CopyMoveTestType &&p_test_type) {
}
};
static void test_command_queue_basic(bool p_use_thread_pool_sync) {
@ -446,6 +484,83 @@ TEST_CASE("[Stress][CommandQueue] Stress test command queue") {
ProjectSettings::get_singleton()->set_setting(COMMAND_QUEUE_SETTING,
ProjectSettings::get_singleton()->property_get_revert(COMMAND_QUEUE_SETTING));
}
TEST_CASE("[CommandQueue] Test Parameter Passing Semantics") {
SharedThreadState sts;
sts.init_threads();
SUBCASE("Testing with lvalue") {
SharedThreadState::CopyMoveTestType::copy_count = 0;
SharedThreadState::CopyMoveTestType::move_count = 0;
SharedThreadState::CopyMoveTestType lvalue(42);
SUBCASE("Pass by copy") {
sts.command_queue.push(&sts, &SharedThreadState::copy_move_test_copy, lvalue);
sts.message_count_to_read = -1;
sts.reader_threadwork.main_start_work();
sts.reader_threadwork.main_wait_for_done();
CHECK(SharedThreadState::CopyMoveTestType::copy_count == 1);
CHECK(SharedThreadState::CopyMoveTestType::move_count == 1);
}
SUBCASE("Pass by reference") {
sts.command_queue.push(&sts, &SharedThreadState::copy_move_test_ref, lvalue);
sts.message_count_to_read = -1;
sts.reader_threadwork.main_start_work();
sts.reader_threadwork.main_wait_for_done();
CHECK(SharedThreadState::CopyMoveTestType::copy_count == 1);
CHECK(SharedThreadState::CopyMoveTestType::move_count == 0);
}
}
SUBCASE("Testing with rvalue") {
SharedThreadState::CopyMoveTestType::copy_count = 0;
SharedThreadState::CopyMoveTestType::move_count = 0;
SUBCASE("Pass by copy") {
sts.command_queue.push(&sts, &SharedThreadState::copy_move_test_copy,
SharedThreadState::CopyMoveTestType(43));
sts.message_count_to_read = -1;
sts.reader_threadwork.main_start_work();
sts.reader_threadwork.main_wait_for_done();
CHECK(SharedThreadState::CopyMoveTestType::copy_count == 0);
CHECK(SharedThreadState::CopyMoveTestType::move_count == 2);
}
SUBCASE("Pass by reference") {
sts.command_queue.push(&sts, &SharedThreadState::copy_move_test_ref,
SharedThreadState::CopyMoveTestType(43));
sts.message_count_to_read = -1;
sts.reader_threadwork.main_start_work();
sts.reader_threadwork.main_wait_for_done();
CHECK(SharedThreadState::CopyMoveTestType::copy_count == 0);
CHECK(SharedThreadState::CopyMoveTestType::move_count == 1);
}
SUBCASE("Pass by rvalue reference") {
sts.command_queue.push(&sts, &SharedThreadState::copy_move_test_move,
SharedThreadState::CopyMoveTestType(43));
sts.message_count_to_read = -1;
sts.reader_threadwork.main_start_work();
sts.reader_threadwork.main_wait_for_done();
CHECK(SharedThreadState::CopyMoveTestType::copy_count == 0);
CHECK(SharedThreadState::CopyMoveTestType::move_count == 1);
}
}
sts.destroy_threads();
}
} // namespace TestCommandQueue
#endif // TEST_COMMAND_QUEUE_H

View file

@ -37,6 +37,24 @@
namespace TestHashMap {
TEST_CASE("[HashMap] List initialization") {
HashMap<int, String> map{ { 0, "A" }, { 1, "B" }, { 2, "C" }, { 3, "D" }, { 4, "E" } };
CHECK(map.size() == 5);
CHECK(map[0] == "A");
CHECK(map[1] == "B");
CHECK(map[2] == "C");
CHECK(map[3] == "D");
CHECK(map[4] == "E");
}
TEST_CASE("[HashMap] List initialization with existing elements") {
HashMap<int, String> map{ { 0, "A" }, { 0, "B" }, { 0, "C" }, { 0, "D" }, { 0, "E" } };
CHECK(map.size() == 1);
CHECK(map[0] == "E");
}
TEST_CASE("[HashMap] Insert element") {
HashMap<int, int> map;
HashMap<int, int>::Iterator e = map.insert(42, 84);

View file

@ -37,6 +37,24 @@
namespace TestHashSet {
TEST_CASE("[HashSet] List initialization") {
HashSet<int> set{ 0, 1, 2, 3, 4 };
CHECK(set.size() == 5);
CHECK(set.has(0));
CHECK(set.has(1));
CHECK(set.has(2));
CHECK(set.has(3));
CHECK(set.has(4));
}
TEST_CASE("[HashSet] List initialization with existing elements") {
HashSet<int> set{ 0, 0, 0, 0, 0 };
CHECK(set.size() == 1);
CHECK(set.has(0));
}
TEST_CASE("[HashSet] Insert element") {
HashSet<int> set;
HashSet<int>::Iterator e = set.insert(42);

View file

@ -45,6 +45,17 @@ static void populate_integers(List<int> &p_list, List<int>::Element *r_elements[
}
}
TEST_CASE("[List] List initialization") {
List<int> list{ 0, 1, 2, 3, 4 };
CHECK(list.size() == 5);
CHECK(list.get(0) == 0);
CHECK(list.get(1) == 1);
CHECK(list.get(2) == 2);
CHECK(list.get(3) == 3);
CHECK(list.get(4) == 4);
}
TEST_CASE("[List] Push/pop back") {
List<String> list;

View file

@ -38,6 +38,32 @@
namespace TestOAHashMap {
TEST_CASE("[OAHashMap] List initialization") {
OAHashMap<int, String> map{ { 0, "A" }, { 1, "B" }, { 2, "C" }, { 3, "D" }, { 4, "E" } };
CHECK(map.get_num_elements() == 5);
String value;
CHECK(map.lookup(0, value));
CHECK(value == "A");
CHECK(map.lookup(1, value));
CHECK(value == "B");
CHECK(map.lookup(2, value));
CHECK(value == "C");
CHECK(map.lookup(3, value));
CHECK(value == "D");
CHECK(map.lookup(4, value));
CHECK(value == "E");
}
TEST_CASE("[OAHashMap] List initialization with existing elements") {
OAHashMap<int, String> map{ { 0, "A" }, { 0, "B" }, { 0, "C" }, { 0, "D" }, { 0, "E" } };
CHECK(map.get_num_elements() == 1);
String value;
CHECK(map.lookup(0, value));
CHECK(value == "E");
}
TEST_CASE("[OAHashMap] Insert element") {
OAHashMap<int, int> map;
map.insert(42, 84);

View file

@ -31,10 +31,27 @@
#ifndef TEST_RID_H
#define TEST_RID_H
#include "core/os/thread.h"
#include "core/templates/local_vector.h"
#include "core/templates/rid.h"
#include "core/templates/rid_owner.h"
#include "tests/test_macros.h"
#ifdef SANITIZERS_ENABLED
#ifdef __has_feature
#if __has_feature(thread_sanitizer)
#define TSAN_ENABLED
#endif
#elif defined(__SANITIZE_THREAD__)
#define TSAN_ENABLED
#endif
#endif
#ifdef TSAN_ENABLED
#include <sanitizer/tsan_interface.h>
#endif
namespace TestRID {
TEST_CASE("[RID] Default Constructor") {
RID rid;
@ -96,6 +113,142 @@ TEST_CASE("[RID] 'get_local_index'") {
CHECK(RID::from_uint64(4'294'967'295).get_local_index() == 4'294'967'295);
CHECK(RID::from_uint64(4'294'967'297).get_local_index() == 1);
}
// This case would let sanitizers realize data races.
// Additionally, on purely weakly ordered architectures, it would detect synchronization issues
// if RID_Alloc failed to impose proper memory ordering and the test's threads are distributed
// among multiple L1 caches.
TEST_CASE("[RID_Owner] Thread safety") {
struct DataHolder {
char data[Thread::CACHE_LINE_BYTES];
};
struct RID_OwnerTester {
uint32_t thread_count = 0;
RID_Owner<DataHolder, true> rid_owner;
TightLocalVector<Thread> threads;
SafeNumeric<uint32_t> next_thread_idx;
// Using std::atomic directly since SafeNumeric doesn't support relaxed ordering.
TightLocalVector<std::atomic<uint64_t>> rids;
std::atomic<uint32_t> sync[2] = {};
std::atomic<uint32_t> correct = 0;
// A barrier that doesn't introduce memory ordering constraints, only compiler ones.
// The idea is not to cause any sync traffic that would make the code we want to test
// seem correct as a side effect.
void lockstep(uint32_t p_step) {
uint32_t buf_idx = p_step % 2;
uint32_t target = (p_step / 2 + 1) * threads.size();
sync[buf_idx].fetch_add(1, std::memory_order_relaxed);
do {
std::this_thread::yield();
} while (sync[buf_idx].load(std::memory_order_relaxed) != target);
}
explicit RID_OwnerTester(bool p_chunk_for_all, bool p_chunks_preallocated) :
thread_count(OS::get_singleton()->get_processor_count()),
rid_owner(sizeof(DataHolder) * (p_chunk_for_all ? thread_count : 1)) {
threads.resize(thread_count);
rids.resize(threads.size());
if (p_chunks_preallocated) {
LocalVector<RID> prealloc_rids;
for (uint32_t i = 0; i < (p_chunk_for_all ? 1 : threads.size()); i++) {
prealloc_rids.push_back(rid_owner.make_rid());
}
for (uint32_t i = 0; i < prealloc_rids.size(); i++) {
rid_owner.free(prealloc_rids[i]);
}
}
}
~RID_OwnerTester() {
for (uint32_t i = 0; i < threads.size(); i++) {
rid_owner.free(RID::from_uint64(rids[i].load(std::memory_order_relaxed)));
}
}
void test() {
for (uint32_t i = 0; i < threads.size(); i++) {
threads[i].start(
[](void *p_data) {
RID_OwnerTester *rot = (RID_OwnerTester *)p_data;
auto _compute_thread_unique_byte = [](uint32_t p_idx) -> char {
return ((p_idx & 0xff) ^ (0b11111110 << (p_idx % 8)));
};
// 1. Each thread gets a zero-based index.
uint32_t self_th_idx = rot->next_thread_idx.postincrement();
rot->lockstep(0);
// 2. Each thread makes a RID holding unique data.
DataHolder initial_data;
memset(&initial_data, _compute_thread_unique_byte(self_th_idx), Thread::CACHE_LINE_BYTES);
RID my_rid = rot->rid_owner.make_rid(initial_data);
rot->rids[self_th_idx].store(my_rid.get_id(), std::memory_order_relaxed);
rot->lockstep(1);
// 3. Each thread verifies all the others.
uint32_t local_correct = 0;
for (uint32_t th_idx = 0; th_idx < rot->threads.size(); th_idx++) {
if (th_idx == self_th_idx) {
continue;
}
char expected_unique_byte = _compute_thread_unique_byte(th_idx);
RID rid = RID::from_uint64(rot->rids[th_idx].load(std::memory_order_relaxed));
DataHolder *data = rot->rid_owner.get_or_null(rid);
#ifdef TSAN_ENABLED
__tsan_acquire(data); // We know not a race in practice.
#endif
bool ok = true;
for (uint32_t j = 0; j < Thread::CACHE_LINE_BYTES; j++) {
if (data->data[j] != expected_unique_byte) {
ok = false;
break;
}
}
if (ok) {
local_correct++;
}
#ifdef TSAN_ENABLED
__tsan_release(data);
#endif
}
rot->lockstep(2);
rot->correct.fetch_add(local_correct, std::memory_order_acq_rel);
},
this);
}
for (uint32_t i = 0; i < threads.size(); i++) {
threads[i].wait_to_finish();
}
CHECK_EQ(correct.load(), threads.size() * (threads.size() - 1));
}
};
SUBCASE("All items in one chunk, pre-allocated") {
RID_OwnerTester tester(true, true);
tester.test();
}
SUBCASE("All items in one chunk, NOT pre-allocated") {
RID_OwnerTester tester(true, false);
tester.test();
}
SUBCASE("One item per chunk, pre-allocated") {
RID_OwnerTester tester(false, true);
tester.test();
}
SUBCASE("One item per chunk, NOT pre-allocated") {
RID_OwnerTester tester(false, false);
tester.test();
}
}
} // namespace TestRID
#endif // TEST_RID_H

View file

@ -215,6 +215,154 @@ TEST_CASE("[Vector] Get, set") {
CHECK(vector.get(4) == 4);
}
TEST_CASE("[Vector] To byte array (variant call)") {
// PackedInt32Array.
{
PackedInt32Array vector[] = { { 0, -1, 2008 }, {} };
PackedByteArray out[] = { { /* 0 */ 0x00, 0x00, 0x00, 0x00, /* -1 */ 0xFF, 0xFF, 0xFF, 0xFF, /* 2008 */ 0xD8, 0x07, 0x00, 0x00 }, {} };
for (size_t i = 0; i < std::size(vector); i++) {
Callable::CallError err;
Variant v_ret;
Variant v_vector = vector[i];
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
CHECK(v_ret.operator PackedByteArray() == out[i]);
}
}
// PackedInt64Array.
{
PackedInt64Array vector[] = { { 0, -1, 2008 }, {} };
PackedByteArray out[] = { { /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* -1 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 2008 */ 0xD8, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, {} };
for (size_t i = 0; i < std::size(vector); i++) {
Callable::CallError err;
Variant v_ret;
Variant v_vector = vector[i];
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
CHECK(v_ret.operator PackedByteArray() == out[i]);
}
}
// PackedFloat32Array.
{
PackedFloat32Array vector[] = { { 0.0, -1.0, 200e24 }, {} };
PackedByteArray out[] = { { /* 0.0 */ 0x00, 0x00, 0x00, 0x00, /* -1.0 */ 0x00, 0x00, 0x80, 0xBF, /* 200e24 */ 0xA6, 0x6F, 0x25, 0x6B }, {} };
for (size_t i = 0; i < std::size(vector); i++) {
Callable::CallError err;
Variant v_ret;
Variant v_vector = vector[i];
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
CHECK(v_ret.operator PackedByteArray() == out[i]);
}
}
// PackedFloat64Array.
{
PackedFloat64Array vector[] = { { 0.0, -1.0, 200e24 }, {} };
PackedByteArray out[] = { { /* 0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* -1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF, /* 200e24 */ 0x35, 0x03, 0x32, 0xB7, 0xF4, 0xAD, 0x64, 0x45 }, {} };
for (size_t i = 0; i < std::size(vector); i++) {
Callable::CallError err;
Variant v_ret;
Variant v_vector = vector[i];
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
CHECK(v_ret.operator PackedByteArray() == out[i]);
}
}
// PackedStringArray.
{
PackedStringArray vector[] = { { "test", "string" }, {}, { "", "test" } };
PackedByteArray out[] = { { /* test */ 0x74, 0x65, 0x73, 0x74, /* null */ 0x00, /* string */ 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67, /* null */ 0x00 }, {}, { /* null */ 0x00, /* test */ 0x74, 0x65, 0x73, 0x74, /* null */ 0x00 } };
for (size_t i = 0; i < std::size(vector); i++) {
Callable::CallError err;
Variant v_ret;
Variant v_vector = vector[i];
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
CHECK(v_ret.operator PackedByteArray() == out[i]);
}
}
// PackedVector2Array.
{
PackedVector2Array vector[] = { { Vector2(), Vector2(1, -1) }, {} };
#ifdef REAL_T_IS_DOUBLE
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* X=1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, /* Y=-1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF }, {} };
#else
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, /* X=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* Y=-1.0 */ 0x00, 0x00, 0x80, 0xBF }, {} };
#endif
for (size_t i = 0; i < std::size(vector); i++) {
Callable::CallError err;
Variant v_ret;
Variant v_vector = vector[i];
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
CHECK(v_ret.operator PackedByteArray() == out[i]);
}
}
// PackedVector3Array.
{
PackedVector3Array vector[] = { { Vector3(), Vector3(1, 1, -1) }, {} };
#ifdef REAL_T_IS_DOUBLE
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Z=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* X=1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, /* Y=1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, /* Z=-1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF }, {} };
#else
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, /* Z=0.0 */ 0x00, 0x00, 0x00, 0x00, /* X=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* Y=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* Z=-1.0 */ 0x00, 0x00, 0x80, 0xBF }, {} };
#endif
for (size_t i = 0; i < std::size(vector); i++) {
Callable::CallError err;
Variant v_ret;
Variant v_vector = vector[i];
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
CHECK(v_ret.operator PackedByteArray() == out[i]);
}
}
// PackedColorArray.
{
PackedColorArray vector[] = { { Color(), Color(1, 1, 1) }, {} };
PackedByteArray out[] = { { /* R=0.0 */ 0x00, 0x00, 0x00, 0x00, /* G=0.0 */ 0x00, 0x00, 0x00, 0x00, /* B=0.0 */ 0x00, 0x00, 0x00, 0x00, /* A=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* R=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* G=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* B=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* A=1.0 */ 0x00, 0x00, 0x80, 0x3F }, {} };
for (size_t i = 0; i < std::size(vector); i++) {
Callable::CallError err;
Variant v_ret;
Variant v_vector = vector[i];
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
CHECK(v_ret.operator PackedByteArray() == out[i]);
}
}
// PackedVector4Array.
{
PackedVector4Array vector[] = { { Vector4(), Vector4(1, -1, 1, -1) }, {} };
#ifdef REAL_T_IS_DOUBLE
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Z 0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* W=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* X=1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, /* Y=-1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF, /* Z=1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, /* W=-1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF }, {} };
#else
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, /* Z=0.0 */ 0x00, 0x00, 0x00, 0x00, /* W 0.0 */ 0x00, 0x00, 0x00, 0x00, /* X 1.0 */ 0x00, 0x00, 0x80, 0x3F, /* Y=-1.0 */ 0x00, 0x00, 0x80, 0xBF, /* Z=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* W=-1.0 */ 0x00, 0x00, 0x80, 0xBF }, {} };
#endif
for (size_t i = 0; i < std::size(vector); i++) {
Callable::CallError err;
Variant v_ret;
Variant v_vector = vector[i];
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
CHECK(v_ret.operator PackedByteArray() == out[i]);
}
}
}
TEST_CASE("[Vector] To byte array") {
Vector<int> vector;
vector.push_back(0);
@ -532,6 +680,31 @@ TEST_CASE("[Vector] Operators") {
CHECK(vector != vector_other);
}
struct CyclicVectorHolder {
Vector<CyclicVectorHolder> *vector = nullptr;
bool is_destructing = false;
~CyclicVectorHolder() {
if (is_destructing) {
// The vector must exist and not expose its backing array at this point.
CHECK_NE(vector, nullptr);
CHECK_EQ(vector->ptr(), nullptr);
}
}
};
TEST_CASE("[Vector] Cyclic Reference") {
// Create a stack-space vector.
Vector<CyclicVectorHolder> vector;
// Add a new (empty) element.
vector.resize(1);
// Expose the vector to its element through CyclicVectorHolder.
// This is questionable behavior, but should still behave graciously.
vector.ptrw()[0] = CyclicVectorHolder{ &vector };
vector.ptrw()[0].is_destructing = true;
// The vector goes out of scope and destructs, calling CyclicVectorHolder's destructor.
}
} // namespace TestVector
#endif // TEST_VECTOR_H