Debian Codegraph @

tests/core/templates/test_paged_array.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_paged_array.h */
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 #pragma once
32
33 #include "core/templates/paged_array.h"
34
35 #include "thirdparty/doctest/doctest.h"
36
37 namespace TestPagedArray {
38
39 // PagedArray
40
41 TEST_CASE("[PagedArray] Simple fill and refill") {
42 PagedArrayPool<uint32_t> pool;
43 PagedArray<uint32_t> array;
44 array.set_page_pool(&pool);
45
46 for (uint32_t i = 0; i < 123456; i++) {
47 array.push_back(i);
48 }
49 CHECK_MESSAGE(
50 array.size() == 123456,
51 "PagedArray should have 123456 elements.");
52
53 bool all_match = true;
54 for (uint32_t i = 0; i < 123456; i++) {
55 if (array[i] != i) {
56 all_match = false;
57 break;
58 }
59 }
60
61 CHECK_MESSAGE(
62 all_match,
63 "PagedArray elements should match from 0 to 123455.");
64
65 array.clear();
66
67 CHECK_MESSAGE(
68 array.size() == 0,
69 "PagedArray elements should be 0 after clear.");
70
71 for (uint32_t i = 0; i < 999; i++) {
72 array.push_back(i);
73 }
74 CHECK_MESSAGE(
75 array.size() == 999,
76 "PagedArray should have 999 elements.");
77
78 all_match = true;
79 for (uint32_t i = 0; i < 999; i++) {
80 if (array[i] != i) {
81 all_match = false;
82 }
83 }
84
85 CHECK_MESSAGE(
86 all_match,
87 "PagedArray elements should match from 0 to 998.");
88
89 array.reset(); //reset so pagepool can be reset
90 pool.reset();
91 }
92
93 TEST_CASE("[PagedArray] Shared pool fill, including merging") {
94 PagedArrayPool<uint32_t> pool;
95 PagedArray<uint32_t> array1;
96 PagedArray<uint32_t> array2;
97 array1.set_page_pool(&pool);
98 array2.set_page_pool(&pool);
99
100 for (uint32_t i = 0; i < 123456; i++) {
101 array1.push_back(i);
102 }
103 CHECK_MESSAGE(
104 array1.size() == 123456,
105 "PagedArray #1 should have 123456 elements.");
106
107 bool all_match = true;
108 for (uint32_t i = 0; i < 123456; i++) {
109 if (array1[i] != i) {
110 all_match = false;
111 }
112 }
113
114 CHECK_MESSAGE(
115 all_match,
116 "PagedArray #1 elements should match from 0 to 123455.");
117
118 for (uint32_t i = 0; i < 999; i++) {
119 array2.push_back(i);
120 }
121 CHECK_MESSAGE(
122 array2.size() == 999,
123 "PagedArray #2 should have 999 elements.");
124
125 all_match = true;
126 for (uint32_t i = 0; i < 999; i++) {
127 if (array2[i] != i) {
128 all_match = false;
129 }
130 }
131
132 CHECK_MESSAGE(
133 all_match,
134 "PagedArray #2 elements should match from 0 to 998.");
135
136 array1.merge_unordered(array2);
137
138 CHECK_MESSAGE(
139 array1.size() == 123456 + 999,
140 "PagedArray #1 should now be 123456 + 999 elements.");
141
142 CHECK_MESSAGE(
143 array2.size() == 0,
144 "PagedArray #2 should now be 0 elements.");
145
146 array1.reset(); //reset so pagepool can be reset
147 array2.reset(); //reset so pagepool can be reset
148 pool.reset();
149 }
150
151 TEST_CASE("[PagedArray] Extensive merge_unordered() test") {
152 for (int page_size = 1; page_size <= 128; page_size *= 2) {
153 PagedArrayPool<uint32_t> pool(page_size);
154 PagedArray<uint32_t> array1;
155 PagedArray<uint32_t> array2;
156 array1.set_page_pool(&pool);
157 array2.set_page_pool(&pool);
158
159 const int max_count = 123;
160 // Test merging arrays of lengths 0+123, 1+122, 2+121, ..., 123+0
161 for (uint32_t j = 0; j < max_count; j++) {
162 CHECK(array1.size() == 0);
163 CHECK(array2.size() == 0);
164
165 uint32_t sum = 12345;
166 for (uint32_t i = 0; i < j; i++) {
167 // Hashing the addend makes it extremely unlikely for any values
168 // other than the original inputs to produce a matching sum
169 uint32_t addend = hash_murmur3_one_32(i) + i;
170 array1.push_back(addend);
171 sum += addend;
172 }
173 for (uint32_t i = j; i < max_count; i++) {
174 // See above
175 uint32_t addend = hash_murmur3_one_32(i) + i;
176 array2.push_back(addend);
177 sum += addend;
178 }
179
180 CHECK(array1.size() == j);
181 CHECK(array2.size() == max_count - j);
182
183 array1.merge_unordered(array2);
184 CHECK_MESSAGE(array1.size() == max_count, "merge_unordered() added/dropped elements while merging");
185
186 // If any elements were altered during merging, the sum will not match up.
187 for (uint32_t i = 0; i < array1.size(); i++) {
188 sum -= array1[i];
189 }
190 CHECK_MESSAGE(sum == 12345, "merge_unordered() altered elements while merging");
191
192 array1.clear();
193 }
194
195 array1.reset();
196 array2.reset();
197 pool.reset();
198 }
199 }
200
201 } // namespace TestPagedArray