Debian Codegraph @

tests/core/templates/test_list.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_list.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/list.h"
34
35 #include "tests/test_macros.h"
36
37 namespace TestList {
38
39 static void populate_integers(List<int> &p_list, List<int>::Element *r_elements[], int num_elements) {
40 p_list.clear();
41 for (int i = 0; i < num_elements; ++i) {
42 List<int>::Element *n = p_list.push_back(i);
43 r_elements[i] = n;
44 }
45 }
46
47 TEST_CASE("[List] List initialization") {
48 List<int> list{ 0, 1, 2, 3, 4 };
49
50 CHECK(list.size() == 5);
51 CHECK(list.get(0) == 0);
52 CHECK(list.get(1) == 1);
53 CHECK(list.get(2) == 2);
54 CHECK(list.get(3) == 3);
55 CHECK(list.get(4) == 4);
56 }
57
58 TEST_CASE("[List] Push/pop back") {
59 List<String> list;
60
61 List<String>::Element *n;
62 n = list.push_back("A");
63 CHECK(n->get() == "A");
64 n = list.push_back("B");
65 CHECK(n->get() == "B");
66 n = list.push_back("C");
67 CHECK(n->get() == "C");
68
69 CHECK(list.size() == 3);
70 CHECK(!list.is_empty());
71
72 String v;
73 v = list.back()->get();
74 list.pop_back();
75 CHECK(v == "C");
76 v = list.back()->get();
77 list.pop_back();
78 CHECK(v == "B");
79 v = list.back()->get();
80 list.pop_back();
81 CHECK(v == "A");
82
83 CHECK(list.size() == 0);
84 CHECK(list.is_empty());
85
86 CHECK(list.back() == nullptr);
87 CHECK(list.front() == nullptr);
88 }
89
90 TEST_CASE("[List] Push/pop front") {
91 List<String> list;
92
93 List<String>::Element *n;
94 n = list.push_front("A");
95 CHECK(n->get() == "A");
96 n = list.push_front("B");
97 CHECK(n->get() == "B");
98 n = list.push_front("C");
99 CHECK(n->get() == "C");
100
101 CHECK(list.size() == 3);
102 CHECK(!list.is_empty());
103
104 String v;
105 v = list.front()->get();
106 list.pop_front();
107 CHECK(v == "C");
108 v = list.front()->get();
109 list.pop_front();
110 CHECK(v == "B");
111 v = list.front()->get();
112 list.pop_front();
113 CHECK(v == "A");
114
115 CHECK(list.size() == 0);
116 CHECK(list.is_empty());
117
118 CHECK(list.back() == nullptr);
119 CHECK(list.front() == nullptr);
120 }
121
122 TEST_CASE("[List] Set and get") {
123 List<String> list;
124 list.push_back("A");
125
126 List<String>::Element *n = list.front();
127 CHECK(n->get() == "A");
128
129 n->set("X");
130 CHECK(n->get() == "X");
131 }
132
133 TEST_CASE("[List] Insert before") {
134 List<String> list;
135 List<String>::Element *a = list.push_back("A");
136 List<String>::Element *b = list.push_back("B");
137 List<String>::Element *c = list.push_back("C");
138
139 list.insert_before(b, "I");
140
141 CHECK(a->next()->get() == "I");
142 CHECK(c->prev()->prev()->get() == "I");
143 CHECK(list.front()->next()->get() == "I");
144 CHECK(list.back()->prev()->prev()->get() == "I");
145 }
146
147 TEST_CASE("[List] Insert after") {
148 List<String> list;
149 List<String>::Element *a = list.push_back("A");
150 List<String>::Element *b = list.push_back("B");
151 List<String>::Element *c = list.push_back("C");
152
153 list.insert_after(b, "I");
154
155 CHECK(a->next()->next()->get() == "I");
156 CHECK(c->prev()->get() == "I");
157 CHECK(list.front()->next()->next()->get() == "I");
158 CHECK(list.back()->prev()->get() == "I");
159 }
160
161 TEST_CASE("[List] Insert before null") {
162 List<String> list;
163 List<String>::Element *a = list.push_back("A");
164 List<String>::Element *b = list.push_back("B");
165 List<String>::Element *c = list.push_back("C");
166
167 list.insert_before(nullptr, "I");
168
169 CHECK(a->next()->get() == "B");
170 CHECK(b->get() == "B");
171 CHECK(c->prev()->prev()->get() == "A");
172 CHECK(list.front()->next()->get() == "B");
173 CHECK(list.back()->prev()->prev()->get() == "B");
174 CHECK(list.back()->get() == "I");
175 }
176
177 TEST_CASE("[List] Insert after null") {
178 List<String> list;
179 List<String>::Element *a = list.push_back("A");
180 List<String>::Element *b = list.push_back("B");
181 List<String>::Element *c = list.push_back("C");
182
183 list.insert_after(nullptr, "I");
184
185 CHECK(a->next()->get() == "B");
186 CHECK(b->get() == "B");
187 CHECK(c->prev()->prev()->get() == "A");
188 CHECK(list.front()->next()->get() == "B");
189 CHECK(list.back()->prev()->prev()->get() == "B");
190 CHECK(list.back()->get() == "I");
191 }
192
193 TEST_CASE("[List] Find") {
194 List<int> list;
195 List<int>::Element *n[10];
196 // Indices match values.
197 populate_integers(list, n, 10);
198
199 for (int i = 0; i < 10; ++i) {
200 CHECK(n[i]->get() == list.find(i)->get());
201 }
202 }
203
204 TEST_CASE("[List] Erase (by value)") {
205 List<int> list;
206 List<int>::Element *n[4];
207 // Indices match values.
208 populate_integers(list, n, 4);
209
210 CHECK(list.front()->next()->next()->get() == 2);
211 bool erased = list.erase(2); // 0, 1, 3.
212 CHECK(erased);
213 CHECK(list.size() == 3);
214
215 // The pointer n[2] points to the freed memory which is not reset to zero,
216 // so the below assertion may pass, but this relies on undefined behavior.
217 // CHECK(n[2]->get() == 2);
218
219 CHECK(list.front()->get() == 0);
220 CHECK(list.front()->next()->next()->get() == 3);
221 CHECK(list.back()->get() == 3);
222 CHECK(list.back()->prev()->get() == 1);
223
224 CHECK(n[1]->next()->get() == 3);
225 CHECK(n[3]->prev()->get() == 1);
226
227 erased = list.erase(9000); // Doesn't exist.
228 CHECK(!erased);
229 }
230
231 TEST_CASE("[List] Erase (by element)") {
232 List<int> list;
233 List<int>::Element *n[4];
234 // Indices match values.
235 populate_integers(list, n, 4);
236
237 bool erased = list.erase(n[2]);
238 CHECK(erased);
239 CHECK(list.size() == 3);
240 CHECK(n[1]->next()->get() == 3);
241 CHECK(n[3]->prev()->get() == 1);
242 }
243
244 TEST_CASE("[List] Element erase") {
245 List<int> list;
246 List<int>::Element *n[4];
247 // Indices match values.
248 populate_integers(list, n, 4);
249
250 n[2]->erase();
251
252 CHECK(list.size() == 3);
253 CHECK(n[1]->next()->get() == 3);
254 CHECK(n[3]->prev()->get() == 1);
255 }
256
257 TEST_CASE("[List] Clear") {
258 List<int> list;
259 List<int>::Element *n[100];
260 populate_integers(list, n, 100);
261
262 list.clear();
263
264 CHECK(list.size() == 0);
265 CHECK(list.is_empty());
266 }
267
268 TEST_CASE("[List] Invert") {
269 List<int> list;
270 List<int>::Element *n[4];
271 populate_integers(list, n, 4);
272
273 list.reverse();
274
275 CHECK(list.front()->get() == 3);
276 CHECK(list.front()->next()->get() == 2);
277 CHECK(list.back()->prev()->get() == 1);
278 CHECK(list.back()->get() == 0);
279 }
280
281 TEST_CASE("[List] Move to front") {
282 List<int> list;
283 List<int>::Element *n[4];
284 populate_integers(list, n, 4);
285
286 list.move_to_front(n[3]);
287
288 CHECK(list.front()->get() == 3);
289 CHECK(list.back()->get() == 2);
290 }
291
292 TEST_CASE("[List] Move to back") {
293 List<int> list;
294 List<int>::Element *n[4];
295 populate_integers(list, n, 4);
296
297 list.move_to_back(n[0]);
298
299 CHECK(list.back()->get() == 0);
300 CHECK(list.front()->get() == 1);
301 }
302
303 TEST_CASE("[List] Move before") {
304 List<int> list;
305 List<int>::Element *n[4];
306 populate_integers(list, n, 4);
307
308 list.move_before(n[3], n[1]);
309
310 CHECK(list.front()->next()->get() == n[3]->get());
311 }
312
313 template <typename T>
314 static void compare_lists(const List<T> &p_result, const List<T> &p_expected) {
315 CHECK_EQ(p_result.size(), p_expected.size());
316 const typename List<T>::Element *result_it = p_result.front();
317 const typename List<T>::Element *expected_it = p_expected.front();
318 for (int i = 0; i < p_result.size(); i++) {
319 CHECK(result_it);
320 CHECK(expected_it);
321 CHECK_EQ(result_it->get(), expected_it->get());
322 result_it = result_it->next();
323 expected_it = expected_it->next();
324 }
325 CHECK(!result_it);
326 CHECK(!expected_it);
327
328 result_it = p_result.back();
329 expected_it = p_expected.back();
330 for (int i = 0; i < p_result.size(); i++) {
331 CHECK(result_it);
332 CHECK(expected_it);
333 CHECK_EQ(result_it->get(), expected_it->get());
334 result_it = result_it->prev();
335 expected_it = expected_it->prev();
336 }
337 CHECK(!result_it);
338 CHECK(!expected_it);
339 }
340
341 TEST_CASE("[List] Sort") {
342 List<String> result{ "D", "B", "A", "C" };
343 result.sort();
344 List<String> expected{ "A", "B", "C", "D" };
345 compare_lists(result, expected);
346
347 List<int> empty_result{};
348 empty_result.sort();
349 List<int> empty_expected{};
350 compare_lists(empty_result, empty_expected);
351
352 List<int> one_result{ 1 };
353 one_result.sort();
354 List<int> one_expected{ 1 };
355 compare_lists(one_result, one_expected);
356
357 List<float> reversed_result{ 2.0, 1.5, 1.0 };
358 reversed_result.sort();
359 List<float> reversed_expected{ 1.0, 1.5, 2.0 };
360 compare_lists(reversed_result, reversed_expected);
361
362 List<int> already_sorted_result{ 1, 2, 3, 4, 5 };
363 already_sorted_result.sort();
364 List<int> already_sorted_expected{ 1, 2, 3, 4, 5 };
365 compare_lists(already_sorted_result, already_sorted_expected);
366
367 List<int> with_duplicates_result{ 1, 2, 3, 1, 2, 3 };
368 with_duplicates_result.sort();
369 List<int> with_duplicates_expected{ 1, 1, 2, 2, 3, 3 };
370 compare_lists(with_duplicates_result, with_duplicates_expected);
371 }
372
373 TEST_CASE("[List] Swap adjacent front and back") {
374 List<int> list;
375 List<int>::Element *n[2];
376 populate_integers(list, n, 2);
377
378 list.swap(list.front(), list.back());
379
380 CHECK(list.front()->prev() == nullptr);
381 CHECK(list.front() != list.front()->next());
382
383 CHECK(list.front() == n[1]);
384 CHECK(list.back() == n[0]);
385
386 CHECK(list.back()->next() == nullptr);
387 CHECK(list.back() != list.back()->prev());
388 }
389
390 TEST_CASE("[List] Swap first adjacent pair") {
391 List<int> list;
392 List<int>::Element *n[4];
393 populate_integers(list, n, 4);
394
395 list.swap(n[0], n[1]);
396
397 CHECK(list.front()->prev() == nullptr);
398 CHECK(list.front() != list.front()->next());
399
400 CHECK(list.front() == n[1]);
401 CHECK(list.front()->next() == n[0]);
402 CHECK(list.back()->prev() == n[2]);
403 CHECK(list.back() == n[3]);
404
405 CHECK(list.back()->next() == nullptr);
406 CHECK(list.back() != list.back()->prev());
407 }
408
409 TEST_CASE("[List] Swap middle adjacent pair") {
410 List<int> list;
411 List<int>::Element *n[4];
412 populate_integers(list, n, 4);
413
414 list.swap(n[1], n[2]);
415
416 CHECK(list.front()->prev() == nullptr);
417
418 CHECK(list.front() == n[0]);
419 CHECK(list.front()->next() == n[2]);
420 CHECK(list.back()->prev() == n[1]);
421 CHECK(list.back() == n[3]);
422
423 CHECK(list.back()->next() == nullptr);
424 }
425
426 TEST_CASE("[List] Swap last adjacent pair") {
427 List<int> list;
428 List<int>::Element *n[4];
429 populate_integers(list, n, 4);
430
431 list.swap(n[2], n[3]);
432
433 CHECK(list.front()->prev() == nullptr);
434
435 CHECK(list.front() == n[0]);
436 CHECK(list.front()->next() == n[1]);
437 CHECK(list.back()->prev() == n[3]);
438 CHECK(list.back() == n[2]);
439
440 CHECK(list.back()->next() == nullptr);
441 }
442
443 TEST_CASE("[List] Swap first cross pair") {
444 List<int> list;
445 List<int>::Element *n[4];
446 populate_integers(list, n, 4);
447
448 list.swap(n[0], n[2]);
449
450 CHECK(list.front()->prev() == nullptr);
451
452 CHECK(list.front() == n[2]);
453 CHECK(list.front()->next() == n[1]);
454 CHECK(list.back()->prev() == n[0]);
455 CHECK(list.back() == n[3]);
456
457 CHECK(list.back()->next() == nullptr);
458 }
459
460 TEST_CASE("[List] Swap last cross pair") {
461 List<int> list;
462 List<int>::Element *n[4];
463 populate_integers(list, n, 4);
464
465 list.swap(n[1], n[3]);
466
467 CHECK(list.front()->prev() == nullptr);
468
469 CHECK(list.front() == n[0]);
470 CHECK(list.front()->next() == n[3]);
471 CHECK(list.back()->prev() == n[2]);
472 CHECK(list.back() == n[1]);
473
474 CHECK(list.back()->next() == nullptr);
475 }
476
477 TEST_CASE("[List] Swap edges") {
478 List<int> list;
479 List<int>::Element *n[4];
480 populate_integers(list, n, 4);
481
482 list.swap(n[1], n[3]);
483
484 CHECK(list.front()->prev() == nullptr);
485
486 CHECK(list.front() == n[0]);
487 CHECK(list.front()->next() == n[3]);
488 CHECK(list.back()->prev() == n[2]);
489 CHECK(list.back() == n[1]);
490
491 CHECK(list.back()->next() == nullptr);
492 }
493
494 TEST_CASE("[List] Swap middle (values check)") {
495 List<String> list;
496 List<String>::Element *n_str1 = list.push_back("Still");
497 List<String>::Element *n_str2 = list.push_back("waiting");
498 List<String>::Element *n_str3 = list.push_back("for");
499 List<String>::Element *n_str4 = list.push_back("Godot.");
500
501 CHECK(n_str1->get() == "Still");
502 CHECK(n_str4->get() == "Godot.");
503
504 CHECK(list.front()->get() == "Still");
505 CHECK(list.front()->next()->get() == "waiting");
506 CHECK(list.back()->prev()->get() == "for");
507 CHECK(list.back()->get() == "Godot.");
508
509 list.swap(n_str2, n_str3);
510
511 CHECK(list.front()->next()->get() == "for");
512 CHECK(list.back()->prev()->get() == "waiting");
513 }
514
515 TEST_CASE("[List] Swap front and back (values check)") {
516 List<Variant> list;
517 Variant str = "Godot";
518 List<Variant>::Element *n_str = list.push_back(str);
519 Variant color = Color(0, 0, 1);
520 List<Variant>::Element *n_color = list.push_back(color);
521
522 CHECK(list.front()->get() == "Godot");
523 CHECK(list.back()->get() == Color(0, 0, 1));
524
525 list.swap(n_str, n_color);
526
527 CHECK(list.front()->get() == Color(0, 0, 1));
528 CHECK(list.back()->get() == "Godot");
529 }
530
531 TEST_CASE("[List] Swap adjacent back and front (reverse order of elements)") {
532 List<int> list;
533 List<int>::Element *n[2];
534 populate_integers(list, n, 2);
535
536 list.swap(n[1], n[0]);
537
538 List<int>::Element *it = list.front();
539 while (it) {
540 List<int>::Element *prev_it = it;
541 it = it->next();
542 if (it == prev_it) {
543 FAIL_CHECK("Infinite loop detected.");
544 break;
545 }
546 }
547 }
548 } // namespace TestList