tests/test_list.cpp

changeset 653
e081643aae2a
parent 641
d402fead3386
child 654
c9d008861178
equal deleted inserted replaced
652:bf817b825ed2 653:e081643aae2a
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "cx/linked_list.h"
30 #include "cx/array_list.h"
31 #include "cx/utils.h"
32 #include "cx/compare.h"
33 #include "util_allocator.h"
34
35 #include <gtest/gtest.h>
36 #include <array>
37 #include <vector>
38 #include <unordered_set>
39 #include <algorithm>
40
41 struct node {
42 node *next = nullptr;
43 node *prev = nullptr;
44 int data = 0;
45 };
46
47 const ptrdiff_t loc_prev = offsetof(struct node, prev);
48 const ptrdiff_t loc_next = offsetof(struct node, next);
49 const ptrdiff_t loc_data = offsetof(struct node, data);
50
51 struct node_test_data {
52 node *begin = nullptr;
53
54 explicit node_test_data(node *begin) : begin(begin) {
55 auto n = begin;
56 while (n != nullptr) {
57 nodes.push_back(n);
58 n = n->next;
59 }
60 }
61
62 node_test_data(node_test_data &) = delete;
63
64 node_test_data(node_test_data &&) = default;
65
66 ~node_test_data() {
67 for (auto &&n: nodes) delete n;
68 }
69
70 private:
71 std::vector<node *> nodes;
72 };
73
74 static node_test_data create_nodes_test_data(size_t len) {
75 if (len == 0) return node_test_data{nullptr};
76 auto begin = new node;
77 auto prev = begin;
78 for (size_t i = 1; i < len; i++) {
79 auto n = new node;
80 cx_linked_list_link(prev, n, loc_prev, loc_next);
81 prev = n;
82 }
83 return node_test_data{begin};
84 }
85
86 template<typename InputIter>
87 static node_test_data create_nodes_test_data(
88 InputIter begin,
89 InputIter end
90 ) {
91 if (begin == end) return node_test_data{nullptr};
92 node *first = new node;
93 first->data = *begin;
94 node *prev = first;
95 begin++;
96 for (; begin != end; begin++) {
97 auto n = new node;
98 n->data = *begin;
99 cx_linked_list_link(prev, n, loc_prev, loc_next);
100 prev = n;
101 }
102 return node_test_data{first};
103 }
104
105 static node_test_data create_nodes_test_data(std::initializer_list<int> data) {
106 return create_nodes_test_data(data.begin(), data.end());
107 }
108
109 template<size_t N>
110 struct int_test_data {
111 std::array<int, N> data;
112
113 int_test_data() {
114 cx_for_n (i, N) data[i] = ::rand(); // NOLINT(cert-msc50-cpp)
115 }
116 };
117
118 TEST(LinkedList_LowLevel, link_unlink) {
119 node a, b, c;
120
121 cx_linked_list_link(&a, &b, loc_prev, loc_next);
122 EXPECT_EQ(a.prev, nullptr);
123 EXPECT_EQ(a.next, &b);
124 EXPECT_EQ(b.prev, &a);
125 EXPECT_EQ(b.next, nullptr);
126
127 cx_linked_list_unlink(&a, &b, loc_prev, loc_next);
128 EXPECT_EQ(a.prev, nullptr);
129 EXPECT_EQ(a.next, nullptr);
130 EXPECT_EQ(b.prev, nullptr);
131 EXPECT_EQ(b.next, nullptr);
132
133 cx_linked_list_link(&b, &c, loc_prev, loc_next);
134 cx_linked_list_link(&a, &b, loc_prev, loc_next);
135 cx_linked_list_unlink(&b, &c, loc_prev, loc_next);
136 EXPECT_EQ(a.prev, nullptr);
137 EXPECT_EQ(a.next, &b);
138 EXPECT_EQ(b.prev, &a);
139 EXPECT_EQ(b.next, nullptr);
140 EXPECT_EQ(c.prev, nullptr);
141 EXPECT_EQ(c.next, nullptr);
142 }
143
144 TEST(LinkedList_LowLevel, cx_linked_list_at) {
145 node a, b, c, d;
146 cx_linked_list_link(&a, &b, loc_prev, loc_next);
147 cx_linked_list_link(&b, &c, loc_prev, loc_next);
148 cx_linked_list_link(&c, &d, loc_prev, loc_next);
149
150 EXPECT_EQ(cx_linked_list_at(&a, 0, loc_next, 0), &a);
151 EXPECT_EQ(cx_linked_list_at(&a, 0, loc_next, 1), &b);
152 EXPECT_EQ(cx_linked_list_at(&a, 0, loc_next, 2), &c);
153 EXPECT_EQ(cx_linked_list_at(&a, 0, loc_next, 3), &d);
154 EXPECT_EQ(cx_linked_list_at(&a, 0, loc_next, 4), nullptr);
155
156 EXPECT_EQ(cx_linked_list_at(&b, 1, loc_prev, 0), &a);
157 EXPECT_EQ(cx_linked_list_at(&b, 1, loc_next, 1), &b);
158 EXPECT_EQ(cx_linked_list_at(&b, 1, loc_next, 2), &c);
159 EXPECT_EQ(cx_linked_list_at(&b, 1, loc_next, 3), &d);
160 EXPECT_EQ(cx_linked_list_at(&b, 1, loc_next, 4), nullptr);
161
162 EXPECT_EQ(cx_linked_list_at(&d, 3, loc_prev, 0), &a);
163 EXPECT_EQ(cx_linked_list_at(&d, 3, loc_prev, 1), &b);
164 }
165
166 TEST(LinkedList_LowLevel, cx_linked_list_find) {
167 auto testdata = create_nodes_test_data({2, 4, 6, 8});
168 auto list = testdata.begin;
169 int s;
170
171 s = 2;
172 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 0);
173 s = 4;
174 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 1);
175 s = 6;
176 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 2);
177 s = 8;
178 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 3);
179 s = 10;
180 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 4);
181 s = -2;
182 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 4);
183 }
184
185 TEST(LinkedList_LowLevel, cx_linked_list_compare) {
186 auto ta = create_nodes_test_data({2, 4, 6, 8});
187 auto tb = create_nodes_test_data({2, 4, 6});
188 auto tc = create_nodes_test_data({2, 4, 6, 9});
189 auto la = ta.begin, lb = tb.begin, lc = tc.begin;
190
191 EXPECT_GT(cx_linked_list_compare(la, lb, loc_next, loc_data, cx_cmp_int), 0);
192 EXPECT_LT(cx_linked_list_compare(lb, la, loc_next, loc_data, cx_cmp_int), 0);
193 EXPECT_GT(cx_linked_list_compare(lc, la, loc_next, loc_data, cx_cmp_int), 0);
194 EXPECT_LT(cx_linked_list_compare(la, lc, loc_next, loc_data, cx_cmp_int), 0);
195 EXPECT_EQ(cx_linked_list_compare(la, la, loc_next, loc_data, cx_cmp_int), 0);
196 }
197
198 TEST(LinkedList_LowLevel, cx_linked_list_add) {
199 // test with begin, end / prev, next
200 {
201 node nodes[4];
202 void *begin = nullptr, *end = nullptr;
203
204 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
205 EXPECT_EQ(begin, &nodes[0]);
206 EXPECT_EQ(end, &nodes[0]);
207 EXPECT_EQ(nodes[0].prev, nullptr);
208 EXPECT_EQ(nodes[0].next, nullptr);
209
210 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
211 EXPECT_EQ(begin, &nodes[0]);
212 EXPECT_EQ(end, &nodes[1]);
213 EXPECT_EQ(nodes[0].next, &nodes[1]);
214 EXPECT_EQ(nodes[1].prev, &nodes[0]);
215 }
216
217 // test with begin only / prev, next
218 {
219 node nodes[4];
220 void *begin = nullptr;
221
222 cx_linked_list_add(&begin, nullptr, loc_prev, loc_next, &nodes[0]);
223 EXPECT_EQ(begin, &nodes[0]);
224 cx_linked_list_add(&begin, nullptr, loc_prev, loc_next, &nodes[1]);
225 EXPECT_EQ(begin, &nodes[0]);
226 EXPECT_EQ(nodes[0].next, &nodes[1]);
227 EXPECT_EQ(nodes[1].prev, &nodes[0]);
228
229 cx_linked_list_add(&begin, nullptr, loc_prev, loc_next, &nodes[2]);
230 EXPECT_EQ(nodes[1].next, &nodes[2]);
231 EXPECT_EQ(nodes[2].prev, &nodes[1]);
232 }
233
234 // test with end only / prev, next
235 {
236 node nodes[4];
237 void *end = nullptr;
238
239 cx_linked_list_add(nullptr, &end, loc_prev, loc_next, &nodes[0]);
240 EXPECT_EQ(end, &nodes[0]);
241 cx_linked_list_add(nullptr, &end, loc_prev, loc_next, &nodes[1]);
242 EXPECT_EQ(end, &nodes[1]);
243 EXPECT_EQ(nodes[0].next, &nodes[1]);
244 EXPECT_EQ(nodes[1].prev, &nodes[0]);
245
246 cx_linked_list_add(nullptr, &end, loc_prev, loc_next, &nodes[2]);
247 EXPECT_EQ(end, &nodes[2]);
248 EXPECT_EQ(nodes[1].next, &nodes[2]);
249 EXPECT_EQ(nodes[2].prev, &nodes[1]);
250 }
251
252 // test with begin, end / next
253 {
254 node nodes[4];
255 void *begin = nullptr, *end = nullptr;
256
257 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
258 EXPECT_EQ(begin, &nodes[0]);
259 EXPECT_EQ(end, &nodes[0]);
260 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
261 EXPECT_EQ(end, &nodes[1]);
262 EXPECT_EQ(nodes[0].next, &nodes[1]);
263 EXPECT_EQ(nodes[1].prev, nullptr);
264 }
265 }
266
267 TEST(LinkedList_LowLevel, cx_linked_list_prepend) {
268 // test with begin, end / prev, next
269 {
270 node nodes[4];
271 void *begin = nullptr, *end = nullptr;
272
273 cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]);
274 EXPECT_EQ(begin, &nodes[0]);
275 EXPECT_EQ(end, &nodes[0]);
276 EXPECT_EQ(nodes[0].prev, nullptr);
277 EXPECT_EQ(nodes[0].next, nullptr);
278
279 cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]);
280 EXPECT_EQ(begin, &nodes[1]);
281 EXPECT_EQ(end, &nodes[0]);
282 EXPECT_EQ(nodes[1].next, &nodes[0]);
283 EXPECT_EQ(nodes[0].prev, &nodes[1]);
284 }
285
286 // test with begin only / prev, next
287 {
288 node nodes[4];
289 void *begin = nullptr;
290
291 cx_linked_list_prepend(&begin, nullptr, loc_prev, loc_next, &nodes[0]);
292 EXPECT_EQ(begin, &nodes[0]);
293 cx_linked_list_prepend(&begin, nullptr, loc_prev, loc_next, &nodes[1]);
294 EXPECT_EQ(begin, &nodes[1]);
295 EXPECT_EQ(nodes[1].next, &nodes[0]);
296 EXPECT_EQ(nodes[0].prev, &nodes[1]);
297
298 cx_linked_list_prepend(&begin, nullptr, loc_prev, loc_next, &nodes[2]);
299 EXPECT_EQ(begin, &nodes[2]);
300 EXPECT_EQ(nodes[2].next, &nodes[1]);
301 EXPECT_EQ(nodes[1].prev, &nodes[2]);
302 }
303
304 // test with end only / prev, next
305 {
306 node nodes[4];
307 void *end = nullptr;
308
309 cx_linked_list_prepend(nullptr, &end, loc_prev, loc_next, &nodes[0]);
310 EXPECT_EQ(end, &nodes[0]);
311 cx_linked_list_prepend(nullptr, &end, loc_prev, loc_next, &nodes[1]);
312 EXPECT_EQ(end, &nodes[0]);
313 EXPECT_EQ(nodes[1].next, &nodes[0]);
314 EXPECT_EQ(nodes[0].prev, &nodes[1]);
315
316 cx_linked_list_prepend(nullptr, &end, loc_prev, loc_next, &nodes[2]);
317 EXPECT_EQ(end, &nodes[0]);
318 EXPECT_EQ(nodes[2].next, &nodes[1]);
319 EXPECT_EQ(nodes[1].prev, &nodes[2]);
320 }
321
322 // test with begin, end / next
323 {
324 node nodes[4];
325 void *begin = nullptr, *end = nullptr;
326
327 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]);
328 EXPECT_EQ(begin, &nodes[0]);
329 EXPECT_EQ(end, &nodes[0]);
330 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]);
331 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]);
332 EXPECT_EQ(begin, &nodes[2]);
333 EXPECT_EQ(end, &nodes[0]);
334 EXPECT_EQ(nodes[1].next, &nodes[0]);
335 EXPECT_EQ(nodes[2].next, &nodes[1]);
336 EXPECT_EQ(nodes[1].prev, nullptr);
337 EXPECT_EQ(nodes[0].prev, nullptr);
338 }
339 }
340
341 TEST(LinkedList_LowLevel, cx_linked_list_insert) {
342 // insert mid list
343 {
344 node nodes[4];
345 void *begin = &nodes[0], *end = &nodes[2];
346
347 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
348 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
349
350 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3]);
351 EXPECT_EQ(begin, &nodes[0]);
352 EXPECT_EQ(end, &nodes[2]);
353 EXPECT_EQ(nodes[1].next, &nodes[3]);
354 EXPECT_EQ(nodes[2].prev, &nodes[3]);
355 EXPECT_EQ(nodes[3].prev, &nodes[1]);
356 EXPECT_EQ(nodes[3].next, &nodes[2]);
357 }
358
359 // insert end
360 {
361 node nodes[4];
362 void *begin = &nodes[0], *end = &nodes[2];
363
364 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
365 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
366
367 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3]);
368 EXPECT_EQ(begin, &nodes[0]);
369 EXPECT_EQ(end, &nodes[3]);
370 EXPECT_EQ(nodes[2].next, &nodes[3]);
371 EXPECT_EQ(nodes[3].prev, &nodes[2]);
372 EXPECT_EQ(nodes[3].next, nullptr);
373 }
374
375 // insert begin
376 {
377 node nodes[4];
378 void *begin = &nodes[0], *end = &nodes[2];
379
380 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
381 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
382
383 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, nullptr, &nodes[3]);
384 EXPECT_EQ(begin, &nodes[3]);
385 EXPECT_EQ(end, &nodes[2]);
386 EXPECT_EQ(nodes[0].prev, &nodes[3]);
387 EXPECT_EQ(nodes[3].prev, nullptr);
388 EXPECT_EQ(nodes[3].next, &nodes[0]);
389 }
390 }
391
392 TEST(LinkedList_LowLevel, cx_linked_list_insert_chain) {
393 // insert mid list
394 {
395 node nodes[5];
396 void *begin = &nodes[0], *end = &nodes[2];
397
398 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
399 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
400 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
401
402 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3], nullptr);
403 EXPECT_EQ(begin, &nodes[0]);
404 EXPECT_EQ(end, &nodes[2]);
405 EXPECT_EQ(nodes[1].next, &nodes[3]);
406 EXPECT_EQ(nodes[2].prev, &nodes[4]);
407 EXPECT_EQ(nodes[3].prev, &nodes[1]);
408 EXPECT_EQ(nodes[4].next, &nodes[2]);
409 }
410
411 // insert end
412 {
413 node nodes[5];
414 void *begin = &nodes[0], *end = &nodes[2];
415
416 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
417 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
418 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
419
420 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3], nullptr);
421 EXPECT_EQ(begin, &nodes[0]);
422 EXPECT_EQ(end, &nodes[4]);
423 EXPECT_EQ(nodes[2].next, &nodes[3]);
424 EXPECT_EQ(nodes[3].prev, &nodes[2]);
425 EXPECT_EQ(nodes[4].next, nullptr);
426 }
427
428 // insert begin
429 {
430 node nodes[5];
431 void *begin = &nodes[0], *end = &nodes[2];
432
433 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
434 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
435 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
436
437 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, nullptr, &nodes[3], nullptr);
438 EXPECT_EQ(begin, &nodes[3]);
439 EXPECT_EQ(end, &nodes[2]);
440 EXPECT_EQ(nodes[0].prev, &nodes[4]);
441 EXPECT_EQ(nodes[3].prev, nullptr);
442 EXPECT_EQ(nodes[4].next, &nodes[0]);
443 }
444 }
445
446 TEST(LinkedList_LowLevel, cx_linked_list_first) {
447 auto testdata = create_nodes_test_data(3);
448 auto begin = testdata.begin;
449 EXPECT_EQ(cx_linked_list_first(begin, loc_prev), begin);
450 EXPECT_EQ(cx_linked_list_first(begin->next, loc_prev), begin);
451 EXPECT_EQ(cx_linked_list_first(begin->next->next, loc_prev), begin);
452 }
453
454 TEST(LinkedList_LowLevel, cx_linked_list_last) {
455 auto testdata = create_nodes_test_data(3);
456 auto begin = testdata.begin;
457 auto end = begin->next->next;
458 EXPECT_EQ(cx_linked_list_last(begin, loc_next), end);
459 EXPECT_EQ(cx_linked_list_last(begin->next, loc_next), end);
460 EXPECT_EQ(cx_linked_list_last(begin->next->next, loc_next), end);
461 }
462
463 TEST(LinkedList_LowLevel, cx_linked_list_prev) {
464 auto testdata = create_nodes_test_data(3);
465 auto begin = testdata.begin;
466 EXPECT_EQ(cx_linked_list_prev(begin, loc_next, begin), nullptr);
467 EXPECT_EQ(cx_linked_list_prev(begin, loc_next, begin->next), begin);
468 EXPECT_EQ(cx_linked_list_prev(begin, loc_next, begin->next->next), begin->next);
469 }
470
471 TEST(LinkedList_LowLevel, cx_linked_list_remove) {
472 auto testdata = create_nodes_test_data({2, 4, 6});
473 auto begin = reinterpret_cast<void *>(testdata.begin);
474 auto first = testdata.begin;
475 auto second = first->next;
476 auto third = second->next;
477 auto end = reinterpret_cast<void *>(third);
478
479 cx_linked_list_remove(&begin, &end, loc_prev, loc_next, second);
480 EXPECT_EQ(begin, first);
481 EXPECT_EQ(end, third);
482 EXPECT_EQ(first->prev, nullptr);
483 EXPECT_EQ(first->next, third);
484 EXPECT_EQ(third->prev, first);
485 EXPECT_EQ(third->next, nullptr);
486
487 cx_linked_list_remove(&begin, &end, loc_prev, loc_next, third);
488 EXPECT_EQ(begin, first);
489 EXPECT_EQ(end, first);
490 EXPECT_EQ(first->prev, nullptr);
491 EXPECT_EQ(first->next, nullptr);
492
493 cx_linked_list_remove(&begin, &end, loc_prev, loc_next, first);
494 EXPECT_EQ(begin, nullptr);
495 EXPECT_EQ(end, nullptr);
496 }
497
498 TEST(LinkedList_LowLevel, cx_linked_list_size) {
499 EXPECT_EQ(cx_linked_list_size(nullptr, loc_next), 0);
500
501 {
502 auto testdata = create_nodes_test_data(5);
503 EXPECT_EQ(cx_linked_list_size(testdata.begin, loc_next), 5);
504 }
505
506 {
507 auto testdata = create_nodes_test_data(13);
508 EXPECT_EQ(cx_linked_list_size(testdata.begin, loc_next), 13);
509 }
510 }
511
512 TEST(LinkedList_LowLevel, cx_linked_list_sort) {
513 int_test_data<1500> testdata;
514 std::array<int, 1500> sorted{};
515 std::partial_sort_copy(testdata.data.begin(), testdata.data.end(), sorted.begin(), sorted.end());
516
517 auto scrambled = create_nodes_test_data(testdata.data.begin(), testdata.data.end());
518 void *begin = scrambled.begin;
519 void *end = cx_linked_list_last(begin, loc_next);
520
521 cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data, cx_cmp_int);
522
523 node *check = reinterpret_cast<node *>(begin);
524 node *check_last = nullptr;
525 cx_for_n (i, sorted.size()) {
526 EXPECT_EQ(check->data, sorted[i]);
527 EXPECT_EQ(check->prev, check_last);
528 if (i < sorted.size() - 1) {
529 ASSERT_NE(check->next, nullptr);
530 }
531 check_last = check;
532 check = check->next;
533 }
534 EXPECT_EQ(check, nullptr);
535 EXPECT_EQ(end, check_last);
536 }
537
538 TEST(LinkedList_LowLevel, cx_linked_list_reverse) {
539 auto testdata = create_nodes_test_data({2, 4, 6, 8});
540 auto expected = create_nodes_test_data({8, 6, 4, 2});
541
542 auto begin = reinterpret_cast<void *>(testdata.begin);
543 auto end = cx_linked_list_last(begin, loc_next);
544 auto orig_begin = begin, orig_end = end;
545
546 cx_linked_list_reverse(&begin, &end, loc_prev, loc_next);
547 EXPECT_EQ(end, orig_begin);
548 EXPECT_EQ(begin, orig_end);
549 EXPECT_EQ(cx_linked_list_compare(begin, expected.begin, loc_next, loc_data, cx_cmp_int), 0);
550 }
551
552 class HighLevelTest : public ::testing::Test {
553 mutable std::unordered_set<CxList *> lists;
554 protected:
555 CxTestingAllocator testingAllocator;
556
557 void TearDown() override {
558 for (auto &&l: lists) cxListDestroy(l);
559 EXPECT_TRUE(testingAllocator.verify());
560 }
561
562 static constexpr size_t testdata_len = 250;
563 int_test_data<testdata_len> testdata;
564
565 auto autofree(CxList *list) const -> CxList * {
566 if (list != nullptr) lists.insert(list);
567 return list;
568 }
569
570 auto linkedListFromTestData() const -> CxList * {
571 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
572 cxListAddArray(list, testdata.data.data(), testdata_len);
573 return list;
574 }
575
576 auto pointerLinkedListFromTestData() const -> CxList * {
577 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
578 cxListStorePointers(list);
579 // note: cannot use cxListAddArray() because we don't have a list of pointers
580 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
581 return list;
582 }
583
584 auto arrayListFromTestData() const -> CxList * {
585 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), testdata_len));
586 cxListAddArray(list, testdata.data.data(), testdata_len);
587 return list;
588 }
589
590 void verifyCreate(CxList *list) const {
591 EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE);
592 EXPECT_EQ(list->size, 0);
593 EXPECT_EQ(list->allocator, &testingAllocator);
594 EXPECT_EQ(list->cmpfunc, cx_cmp_int);
595 }
596
597 void verifyAdd(
598 CxList *list,
599 bool as_pointer
600 ) {
601 auto len = testdata_len;
602 cx_for_n (i, len) EXPECT_EQ(cxListAdd(list, &testdata.data[i]), 0);
603 EXPECT_EQ(list->size, len);
604 EXPECT_GE(list->capacity, list->size);
605 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
606 cx_for_n (i, len) ++testdata.data[i];
607 if (as_pointer) {
608 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
609 } else {
610 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i] - 1);
611 }
612 }
613
614 static void verifyInsert(CxList *list) {
615 int a = 5, b = 47, c = 13, d = 42;
616
617 EXPECT_NE(cxListInsert(list, 1, &a), 0);
618 EXPECT_EQ(list->size, 0);
619 EXPECT_EQ(cxListInsert(list, 0, &a), 0);
620 EXPECT_EQ(list->size, 1);
621 EXPECT_EQ(cxListInsert(list, 0, &b), 0);
622 EXPECT_EQ(list->size, 2);
623 EXPECT_EQ(cxListInsert(list, 1, &c), 0);
624 EXPECT_EQ(list->size, 3);
625 EXPECT_EQ(cxListInsert(list, 3, &d), 0);
626
627 ASSERT_EQ(list->size, 4);
628 EXPECT_GE(list->capacity, list->size);
629
630 EXPECT_EQ(*(int *) cxListAt(list, 0), 47);
631 EXPECT_EQ(*(int *) cxListAt(list, 1), 13);
632 EXPECT_EQ(*(int *) cxListAt(list, 2), 5);
633 EXPECT_EQ(*(int *) cxListAt(list, 3), 42);
634 }
635
636 static void verifyInsertArray(
637 CxList *list,
638 bool pointers = false
639 ) {
640 int a[5] = {5, 47, 11, 13, 42};
641 int b[5] = {9, 18, 72, 50, 7};
642 int *aptr[5];
643 int *bptr[5];
644 cx_for_n(i, 5) {
645 aptr[i] = &a[i];
646 bptr[i] = &b[i];
647 }
648
649 size_t inserted;
650
651 if (pointers) {
652 inserted = cxListInsertArray(list, 0, aptr, 5);
653 } else {
654 inserted = cxListInsertArray(list, 0, a, 5);
655 }
656 EXPECT_EQ(inserted, 5);
657 EXPECT_EQ(*(int *) cxListAt(list, 0), 5);
658 EXPECT_EQ(*(int *) cxListAt(list, 1), 47);
659 EXPECT_EQ(*(int *) cxListAt(list, 2), 11);
660 EXPECT_EQ(*(int *) cxListAt(list, 3), 13);
661 EXPECT_EQ(*(int *) cxListAt(list, 4), 42);
662 if (pointers) {
663 inserted = cxListInsertArray(list, 3, bptr, 5);
664 } else {
665 inserted = cxListInsertArray(list, 3, b, 5);
666 }
667 EXPECT_EQ(inserted, 5);
668 EXPECT_EQ(*(int *) cxListAt(list, 0), 5);
669 EXPECT_EQ(*(int *) cxListAt(list, 1), 47);
670 EXPECT_EQ(*(int *) cxListAt(list, 2), 11);
671 EXPECT_EQ(*(int *) cxListAt(list, 3), 9);
672 EXPECT_EQ(*(int *) cxListAt(list, 4), 18);
673 EXPECT_EQ(*(int *) cxListAt(list, 5), 72);
674 EXPECT_EQ(*(int *) cxListAt(list, 6), 50);
675 EXPECT_EQ(*(int *) cxListAt(list, 7), 7);
676 EXPECT_EQ(*(int *) cxListAt(list, 8), 13);
677 EXPECT_EQ(*(int *) cxListAt(list, 9), 42);
678 }
679
680 void verifyRemove(CxList *list) const {
681 EXPECT_EQ(cxListRemove(list, 2), 0);
682 EXPECT_EQ(cxListRemove(list, 4), 0);
683 EXPECT_EQ(list->size, testdata_len - 2);
684 EXPECT_GE(list->capacity, list->size);
685 EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[0]);
686 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[1]);
687 EXPECT_EQ(*(int *) cxListAt(list, 2), testdata.data[3]);
688 EXPECT_EQ(*(int *) cxListAt(list, 3), testdata.data[4]);
689 EXPECT_EQ(*(int *) cxListAt(list, 4), testdata.data[6]);
690
691 EXPECT_EQ(cxListRemove(list, 0), 0);
692 EXPECT_EQ(list->size, testdata_len - 3);
693 EXPECT_GE(list->capacity, list->size);
694 EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[1]);
695 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[3]);
696
697 EXPECT_NE(cxListRemove(list, testdata_len), 0);
698 }
699
700 void verifyAt(CxList *list) const {
701 auto len = testdata_len;
702 EXPECT_EQ(list->size, len);
703 cx_for_n (i, len) {
704 EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
705 }
706 EXPECT_EQ(cxListAt(list, list->size), nullptr);
707 }
708
709 void verifyFind(CxList *list) const {
710 cx_for_n (attempt, 25) {
711 size_t exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
712 int val = testdata.data[exp];
713 // randomly picked number could occur earlier in list - find first position
714 cx_for_n (i, exp) {
715 if (testdata.data[i] == val) {
716 exp = i;
717 break;
718 }
719 }
720 EXPECT_EQ(cxListFind(list, &val), exp);
721 }
722 }
723
724 void verifySort(CxList *list) const {
725 std::array<int, testdata_len> expected{};
726 std::partial_sort_copy(testdata.data.begin(), testdata.data.end(), expected.begin(), expected.end());
727 cxListSort(list);
728 cx_for_n (i, testdata_len) ASSERT_EQ(*(int *) cxListAt(list, i), expected[i]);
729 }
730
731 void verifyIterator(CxList *list) const {
732 int i = 0;
733 auto iter = cxListBeginMut(list);
734 cx_foreach(int*, x, iter) {
735 ASSERT_EQ(iter.index, (size_t) (i + 1) / 2);
736 ASSERT_EQ(*x, testdata.data[i]);
737 if (i % 2 == 1) cxIteratorFlagRemoval(iter);
738 i++;
739 }
740 auto len = testdata_len;
741 EXPECT_EQ(i, len);
742 ASSERT_EQ(list->size, len / 2);
743 cx_for_n(j, len / 2) ASSERT_EQ(*(int *) cxListAt(list, j), testdata.data[j * 2]);
744 }
745
746 static void verifyInsertViaIterator(CxList *list) {
747 int newdata[] = {10, 20, 30, 40, 50};
748
749 auto iter = cxListMutIterator(list, 2);
750 EXPECT_TRUE(cxIteratorValid(iter));
751 EXPECT_EQ(iter.index, 2);
752 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2);
753 cxListInsertAfter(&iter, &newdata[0]);
754 EXPECT_TRUE(cxIteratorValid(iter));
755 EXPECT_EQ(iter.index, 2);
756 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2);
757 cxListInsertBefore(&iter, &newdata[1]);
758 EXPECT_TRUE(cxIteratorValid(iter));
759 EXPECT_EQ(iter.index, 3);
760 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2);
761
762 iter = cxListBeginMut(list);
763 cxListInsertBefore(&iter, &newdata[2]);
764 EXPECT_TRUE(cxIteratorValid(iter));
765 EXPECT_EQ(iter.index, 1);
766 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 0);
767 iter = cxListMutIterator(list, list->size);
768 cxListInsertBefore(&iter, &newdata[3]);
769 EXPECT_FALSE(cxIteratorValid(iter));
770 EXPECT_EQ(iter.index, 9);
771 iter = cxListMutIterator(list, list->size);
772 cxListInsertAfter(&iter, &newdata[4]);
773 EXPECT_FALSE(cxIteratorValid(iter));
774 EXPECT_EQ(iter.index, 10);
775
776 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
777 cx_for_n (j, 10) EXPECT_EQ(*(int *) cxListAt(list, j), expdata[j]);
778 }
779
780 void verifyReverse(CxList *list) const {
781 cxListReverse(list);
782 cx_for_n(i, testdata_len) {
783 ASSERT_EQ(*(int *) cxListAt(list, i), testdata.data[testdata_len - 1 - i]);
784 }
785 }
786
787 static void verifyCompare(
788 CxList *left,
789 CxList *right
790 ) {
791 EXPECT_EQ(cxListCompare(left, right), 0);
792 int x = 42;
793 cxListAdd(left, &x);
794 ASSERT_GT(left->size, right->size);
795 EXPECT_GT(cxListCompare(left, right), 0);
796 EXPECT_LT(cxListCompare(right, left), 0);
797 cxListAdd(right, &x);
798 ASSERT_EQ(left->size, right->size);
799 EXPECT_EQ(cxListCompare(left, right), 0);
800 int a = 5, b = 10;
801 cxListInsert(left, 15, &a);
802 cxListInsert(right, 15, &b);
803 ASSERT_EQ(left->size, right->size);
804 EXPECT_LT(cxListCompare(left, right), 0);
805 EXPECT_GT(cxListCompare(right, left), 0);
806 *(int *) cxListAt(left, 15) = 10;
807 EXPECT_EQ(cxListCompare(left, right), 0);
808 }
809 };
810
811 class LinkedList : public HighLevelTest {
812 };
813
814 class PointerLinkedList : public HighLevelTest {
815 };
816
817 class ArrayList : public HighLevelTest {
818 };
819
820 TEST_F(PointerLinkedList, cxListStorePointers) {
821 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, 47));
822 EXPECT_FALSE(cxListIsStoringPointers(list));
823 cxListStorePointers(list);
824 EXPECT_EQ(list->itemsize, sizeof(void *));
825 EXPECT_NE(list->cl, nullptr);
826 EXPECT_NE(list->climpl, nullptr);
827 EXPECT_TRUE(cxListIsStoringPointers(list));
828 cxListStoreObjects(list);
829 EXPECT_NE(list->cl, nullptr);
830 EXPECT_EQ(list->climpl, nullptr);
831 EXPECT_FALSE(cxListIsStoringPointers(list));
832 }
833
834 TEST_F(LinkedList, cxLinkedListCreate) {
835 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
836 ASSERT_NE(list, nullptr);
837 EXPECT_EQ(list->itemsize, sizeof(int));
838 EXPECT_EQ(list->capacity, (size_t) -1);
839 verifyCreate(list);
840 }
841
842 TEST_F(ArrayList, cxArrayListCreate) {
843 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
844 ASSERT_NE(list, nullptr);
845 EXPECT_EQ(list->itemsize, sizeof(int));
846 EXPECT_EQ(list->capacity, 8);
847 verifyCreate(list);
848 }
849
850 TEST_F(LinkedList, cxListAdd) {
851 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
852 verifyAdd(list, false);
853 }
854
855 TEST_F(PointerLinkedList, cxListAdd) {
856 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
857 cxListStorePointers(list);
858 verifyAdd(list, true);
859 }
860
861 TEST_F(ArrayList, cxListAdd) {
862 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
863 verifyAdd(list, false);
864 }
865
866 TEST_F(LinkedList, cxListInsert) {
867 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
868 }
869
870 TEST_F(PointerLinkedList, cxListInsert) {
871 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
872 cxListStorePointers(list);
873 verifyInsert(list);
874 }
875
876 TEST_F(ArrayList, cxListInsert) {
877 verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2)));
878 }
879
880 TEST_F(LinkedList, cxListInsertArray) {
881 verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
882 }
883
884 TEST_F(PointerLinkedList, cxListInsertArray) {
885 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
886 cxListStorePointers(list);
887 verifyInsertArray(list, true);
888 }
889
890 TEST_F(ArrayList, cxListInsertArray) {
891 verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)));
892 }
893
894 TEST_F(LinkedList, cxListRemove) {
895 verifyRemove(linkedListFromTestData());
896 }
897
898 TEST_F(PointerLinkedList, cxListRemove) {
899 verifyRemove(pointerLinkedListFromTestData());
900 }
901
902 TEST_F(ArrayList, cxListRemove) {
903 verifyRemove(arrayListFromTestData());
904 }
905
906 TEST_F(LinkedList, cxListAt) {
907 verifyAt(linkedListFromTestData());
908 }
909
910 TEST_F(PointerLinkedList, cxListAt) {
911 verifyAt(pointerLinkedListFromTestData());
912 }
913
914 TEST_F(ArrayList, cxListAt) {
915 verifyAt(arrayListFromTestData());
916 }
917
918 TEST_F(LinkedList, cxListFind) {
919 verifyFind(linkedListFromTestData());
920 }
921
922 TEST_F(PointerLinkedList, cxListFind) {
923 verifyFind(pointerLinkedListFromTestData());
924 }
925
926 TEST_F(ArrayList, cxListFind) {
927 verifyFind(arrayListFromTestData());
928 }
929
930 TEST_F(LinkedList, cxListSort) {
931 verifySort(linkedListFromTestData());
932 }
933
934 TEST_F(PointerLinkedList, cxListSort) {
935 verifySort(pointerLinkedListFromTestData());
936 }
937
938 TEST_F(ArrayList, cxListSort) {
939 verifySort(arrayListFromTestData());
940 }
941
942 TEST_F(LinkedList, Iterator) {
943 verifyIterator(linkedListFromTestData());
944 }
945
946 TEST_F(PointerLinkedList, Iterator) {
947 verifyIterator(pointerLinkedListFromTestData());
948 }
949
950 TEST_F(ArrayList, Iterator) {
951 verifyIterator(arrayListFromTestData());
952 }
953
954 TEST_F(LinkedList, InsertViaIterator) {
955 int fivenums[] = {0, 1, 2, 3, 4, 5};
956 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
957 cxListAddArray(list, fivenums, 5);
958 verifyInsertViaIterator(list);
959 }
960
961 TEST_F(PointerLinkedList, InsertViaIterator) {
962 int fivenums[] = {0, 1, 2, 3, 4, 5};
963 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
964 cxListStorePointers(list);
965 // note: cannot use cxListAddArray() because we don't have a list of pointers
966 cx_for_n(i, 5) cxListAdd(list, &fivenums[i]);
967 verifyInsertViaIterator(list);
968 }
969
970 TEST_F(ArrayList, InsertViaIterator) {
971 int fivenums[] = {0, 1, 2, 3, 4, 5};
972 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
973 cxListAddArray(list, fivenums, 5);
974 verifyInsertViaIterator(list);
975 }
976
977 TEST_F(LinkedList, cxListReverse) {
978 verifyReverse(linkedListFromTestData());
979 }
980
981 TEST_F(PointerLinkedList, cxListReverse) {
982 verifyReverse(pointerLinkedListFromTestData());
983 }
984
985 TEST_F(ArrayList, cxListReverse) {
986 verifyReverse(arrayListFromTestData());
987 }
988
989 TEST_F(LinkedList, cxListCompare) {
990 auto left = linkedListFromTestData();
991 auto right = linkedListFromTestData();
992 verifyCompare(left, right);
993 }
994
995 TEST_F(LinkedList, cxListCompareWithPtrList) {
996 auto left = linkedListFromTestData();
997 auto right = pointerLinkedListFromTestData();
998 verifyCompare(left, right);
999 }
1000
1001 TEST_F(LinkedList, cxListCompareWithArrayList) {
1002 auto left = linkedListFromTestData();
1003 auto right = arrayListFromTestData();
1004 verifyCompare(left, right);
1005 }
1006
1007 TEST_F(PointerLinkedList, cxListCompare) {
1008 auto left = pointerLinkedListFromTestData();
1009 auto right = pointerLinkedListFromTestData();
1010 verifyCompare(left, right);
1011 }
1012
1013 TEST_F(PointerLinkedList, cxListCompareWithNormalList) {
1014 auto left = pointerLinkedListFromTestData();
1015 auto right = linkedListFromTestData();
1016 verifyCompare(left, right);
1017 }
1018
1019 TEST_F(PointerLinkedList, cxListCompareWithArrayList) {
1020 auto left = pointerLinkedListFromTestData();
1021 auto right = arrayListFromTestData();
1022 verifyCompare(left, right);
1023 }
1024
1025 TEST_F(ArrayList, cxListCompare) {
1026 auto left = arrayListFromTestData();
1027 auto right = arrayListFromTestData();
1028 verifyCompare(left, right);
1029 }
1030
1031 TEST_F(ArrayList, cxListCompareWithPtrList) {
1032 auto left = arrayListFromTestData();
1033 auto right = pointerLinkedListFromTestData();
1034 verifyCompare(left, right);
1035 }
1036
1037 TEST_F(ArrayList, cxListCompareWithNormalList) {
1038 auto left = arrayListFromTestData();
1039 auto right = linkedListFromTestData();
1040 verifyCompare(left, right);
1041 }
1042
1043 TEST_F(PointerLinkedList, NoDestructor) {
1044 void *item = cxMalloc(&testingAllocator, sizeof(int));
1045 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
1046 cxListStorePointers(list);
1047 cxListAdd(list, item);
1048 ASSERT_FALSE(testingAllocator.verify());
1049 cxListDestroy(list);
1050 EXPECT_FALSE(testingAllocator.verify());
1051 cxFree(&testingAllocator, item);
1052 EXPECT_TRUE(testingAllocator.verify());
1053 }
1054
1055 TEST_F(PointerLinkedList, SimpleDestructor) {
1056 int item = 0;
1057 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
1058 cxListStorePointers(list);
1059 list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
1060 list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
1061 cxListAdd(list, &item);
1062 cxListDestroy(list);
1063 EXPECT_EQ(item, 42);
1064 }
1065
1066 TEST_F(PointerLinkedList, AdvancedDestructor) {
1067 void *item = cxMalloc(&testingAllocator, sizeof(int));
1068 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
1069 cxListStorePointers(list);
1070 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
1071 list->advanced_destructor.data = &testingAllocator;
1072 list->advanced_destructor.func = (cx_destructor_func2) cxFree;
1073 cxListAdd(list, item);
1074 ASSERT_FALSE(testingAllocator.verify());
1075 cxListDestroy(list);
1076 EXPECT_TRUE(testingAllocator.verify());
1077 }

mercurial