1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
/*************************************************************************/
/* test_array.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef TEST_ARRAY_H
#define TEST_ARRAY_H
#include "core/variant/array.h"
#include "tests/test_macros.h"
#include "tests/test_tools.h"
namespace TestArray {
static inline Array build_array() {
return Array();
}
template <typename... Targs>
static inline Array build_array(Variant item, Targs... Fargs) {
Array a = build_array(Fargs...);
a.push_front(item);
return a;
}
static inline Dictionary build_dictionary() {
return Dictionary();
}
template <typename... Targs>
static inline Dictionary build_dictionary(Variant key, Variant item, Targs... Fargs) {
Dictionary d = build_dictionary(Fargs...);
d[key] = item;
return d;
}
TEST_CASE("[Array] size(), clear(), and is_empty()") {
Array arr;
CHECK(arr.size() == 0);
CHECK(arr.is_empty());
arr.push_back(1);
CHECK(arr.size() == 1);
arr.clear();
CHECK(arr.is_empty());
CHECK(arr.size() == 0);
}
TEST_CASE("[Array] Assignment and comparison operators") {
Array arr1;
Array arr2;
arr1.push_back(1);
CHECK(arr1 != arr2);
CHECK(arr1 > arr2);
CHECK(arr1 >= arr2);
arr2.push_back(2);
CHECK(arr1 != arr2);
CHECK(arr1 < arr2);
CHECK(arr1 <= arr2);
CHECK(arr2 > arr1);
CHECK(arr2 >= arr1);
Array arr3 = arr2;
CHECK(arr3 == arr2);
}
TEST_CASE("[Array] append_array()") {
Array arr1;
Array arr2;
arr1.push_back(1);
arr1.append_array(arr2);
CHECK(arr1.size() == 1);
arr2.push_back(2);
arr1.append_array(arr2);
CHECK(arr1.size() == 2);
CHECK(int(arr1[0]) == 1);
CHECK(int(arr1[1]) == 2);
}
TEST_CASE("[Array] resize(), insert(), and erase()") {
Array arr;
arr.resize(2);
CHECK(arr.size() == 2);
arr.insert(0, 1);
CHECK(int(arr[0]) == 1);
arr.insert(0, 2);
CHECK(int(arr[0]) == 2);
arr.erase(2);
CHECK(int(arr[0]) == 1);
}
TEST_CASE("[Array] front() and back()") {
Array arr;
arr.push_back(1);
CHECK(int(arr.front()) == 1);
CHECK(int(arr.back()) == 1);
arr.push_back(3);
CHECK(int(arr.front()) == 1);
CHECK(int(arr.back()) == 3);
}
TEST_CASE("[Array] has() and count()") {
Array arr;
arr.push_back(1);
arr.push_back(1);
CHECK(arr.has(1));
CHECK(!arr.has(2));
CHECK(arr.count(1) == 2);
CHECK(arr.count(2) == 0);
}
TEST_CASE("[Array] remove_at()") {
Array arr;
arr.push_back(1);
arr.push_back(2);
arr.remove_at(0);
CHECK(arr.size() == 1);
CHECK(int(arr[0]) == 2);
arr.remove_at(0);
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;
arr.push_back(1);
CHECK(int(arr.get(0)) == 1);
}
TEST_CASE("[Array] sort()") {
Array arr;
arr.push_back(3);
arr.push_back(4);
arr.push_back(2);
arr.push_back(1);
arr.sort();
int val = 1;
for (int i = 0; i < arr.size(); i++) {
CHECK(int(arr[i]) == val);
val++;
}
}
TEST_CASE("[Array] push_front(), pop_front(), pop_back()") {
Array arr;
arr.push_front(1);
arr.push_front(2);
CHECK(int(arr[0]) == 2);
arr.pop_front();
CHECK(int(arr[0]) == 1);
CHECK(arr.size() == 1);
arr.push_front(2);
arr.push_front(3);
arr.pop_back();
CHECK(int(arr[1]) == 2);
CHECK(arr.size() == 2);
}
TEST_CASE("[Array] pop_at()") {
ErrorDetector ed;
Array arr;
arr.push_back(2);
arr.push_back(4);
arr.push_back(6);
arr.push_back(8);
arr.push_back(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;
array.push_back(0);
array.push_back(1);
array.push_back(2);
array.push_back(3);
array.push_back(4);
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, -1);
CHECK(slice3.size() == 2);
CHECK(slice3[0] == Variant(3));
CHECK(slice3[1] == Variant(4));
Array slice4 = array.slice(2, -2);
CHECK(slice4.size() == 2);
CHECK(slice4[0] == Variant(2));
CHECK(slice4[1] == Variant(3));
}
TEST_CASE("[Array] Duplicate array") {
// a = [1, [2, 2], {3: 3}]
Array a = build_array(1, build_array(2, 2), build_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 endup 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 = build_array(1, build_array(2, 2), build_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 = build_array(1);
Array a2 = build_array(1);
Array other_a = build_array(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 = build_array(build_array(build_array(1), 2), 3);
Array a2 = a1.duplicate(true);
// other_a = [[[1, 0], 2], 3]
Array other_a = build_array(build_array(build_array(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 = build_array(build_dictionary(1, 2), 3);
Array a2 = a1.duplicate(true);
// other_a = [{1: 0}, 3]
Array other_a = build_array(build_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();
}
} // namespace TestArray
#endif // TEST_ARRAY_H
|