Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/templates/test_paged_array.cpp
45991 views
1
/**************************************************************************/
2
/* test_paged_array.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_paged_array)
34
35
#include "core/templates/paged_array.h"
36
37
namespace TestPagedArray {
38
39
TEST_CASE("[PagedArray] Simple fill and refill") {
40
PagedArrayPool<uint32_t> pool;
41
PagedArray<uint32_t> array;
42
array.set_page_pool(&pool);
43
44
for (uint32_t i = 0; i < 123456; i++) {
45
array.push_back(i);
46
}
47
CHECK_MESSAGE(
48
array.size() == 123456,
49
"PagedArray should have 123456 elements.");
50
51
bool all_match = true;
52
for (uint32_t i = 0; i < 123456; i++) {
53
if (array[i] != i) {
54
all_match = false;
55
break;
56
}
57
}
58
59
CHECK_MESSAGE(
60
all_match,
61
"PagedArray elements should match from 0 to 123455.");
62
63
array.clear();
64
65
CHECK_MESSAGE(
66
array.size() == 0,
67
"PagedArray elements should be 0 after clear.");
68
69
for (uint32_t i = 0; i < 999; i++) {
70
array.push_back(i);
71
}
72
CHECK_MESSAGE(
73
array.size() == 999,
74
"PagedArray should have 999 elements.");
75
76
all_match = true;
77
for (uint32_t i = 0; i < 999; i++) {
78
if (array[i] != i) {
79
all_match = false;
80
}
81
}
82
83
CHECK_MESSAGE(
84
all_match,
85
"PagedArray elements should match from 0 to 998.");
86
87
array.reset(); //reset so pagepool can be reset
88
pool.reset();
89
}
90
91
TEST_CASE("[PagedArray] Shared pool fill, including merging") {
92
PagedArrayPool<uint32_t> pool;
93
PagedArray<uint32_t> array1;
94
PagedArray<uint32_t> array2;
95
array1.set_page_pool(&pool);
96
array2.set_page_pool(&pool);
97
98
for (uint32_t i = 0; i < 123456; i++) {
99
array1.push_back(i);
100
}
101
CHECK_MESSAGE(
102
array1.size() == 123456,
103
"PagedArray #1 should have 123456 elements.");
104
105
bool all_match = true;
106
for (uint32_t i = 0; i < 123456; i++) {
107
if (array1[i] != i) {
108
all_match = false;
109
}
110
}
111
112
CHECK_MESSAGE(
113
all_match,
114
"PagedArray #1 elements should match from 0 to 123455.");
115
116
for (uint32_t i = 0; i < 999; i++) {
117
array2.push_back(i);
118
}
119
CHECK_MESSAGE(
120
array2.size() == 999,
121
"PagedArray #2 should have 999 elements.");
122
123
all_match = true;
124
for (uint32_t i = 0; i < 999; i++) {
125
if (array2[i] != i) {
126
all_match = false;
127
}
128
}
129
130
CHECK_MESSAGE(
131
all_match,
132
"PagedArray #2 elements should match from 0 to 998.");
133
134
array1.merge_unordered(array2);
135
136
CHECK_MESSAGE(
137
array1.size() == 123456 + 999,
138
"PagedArray #1 should now be 123456 + 999 elements.");
139
140
CHECK_MESSAGE(
141
array2.size() == 0,
142
"PagedArray #2 should now be 0 elements.");
143
144
array1.reset(); //reset so pagepool can be reset
145
array2.reset(); //reset so pagepool can be reset
146
pool.reset();
147
}
148
149
TEST_CASE("[PagedArray] Extensive merge_unordered() test") {
150
for (int page_size = 1; page_size <= 128; page_size *= 2) {
151
PagedArrayPool<uint32_t> pool(page_size);
152
PagedArray<uint32_t> array1;
153
PagedArray<uint32_t> array2;
154
array1.set_page_pool(&pool);
155
array2.set_page_pool(&pool);
156
157
const int max_count = 123;
158
// Test merging arrays of lengths 0+123, 1+122, 2+121, ..., 123+0
159
for (uint32_t j = 0; j < max_count; j++) {
160
CHECK(array1.size() == 0);
161
CHECK(array2.size() == 0);
162
163
uint32_t sum = 12345;
164
for (uint32_t i = 0; i < j; i++) {
165
// Hashing the addend makes it extremely unlikely for any values
166
// other than the original inputs to produce a matching sum
167
uint32_t addend = hash_murmur3_one_32(i) + i;
168
array1.push_back(addend);
169
sum += addend;
170
}
171
for (uint32_t i = j; i < max_count; i++) {
172
// See above
173
uint32_t addend = hash_murmur3_one_32(i) + i;
174
array2.push_back(addend);
175
sum += addend;
176
}
177
178
CHECK(array1.size() == j);
179
CHECK(array2.size() == max_count - j);
180
181
array1.merge_unordered(array2);
182
CHECK_MESSAGE(array1.size() == max_count, "merge_unordered() added/dropped elements while merging");
183
184
// If any elements were altered during merging, the sum will not match up.
185
for (uint32_t i = 0; i < array1.size(); i++) {
186
sum -= array1[i];
187
}
188
CHECK_MESSAGE(sum == 12345, "merge_unordered() altered elements while merging");
189
190
array1.clear();
191
}
192
193
array1.reset();
194
array2.reset();
195
pool.reset();
196
}
197
}
198
199
} // namespace TestPagedArray
200
201