tests/test_list.cpp

changeset 654
c9d008861178
parent 647
2e6e9d9f2159
parent 653
e081643aae2a
child 655
7340c4255f1f
equal deleted inserted replaced
647:2e6e9d9f2159 654:c9d008861178
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 static void verifySwap(CxList *list) {
701 ASSERT_EQ(list->size, 0);
702
703 int original[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
704 int swapped[16] = {8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13};
705
706 // we have to add the items one by one, because it could be a pointer list
707 cx_for_n(i, 16) {
708 cxListAdd(list, &original[i]);
709 }
710
711 int result;
712
713 // execute the test two times with different item sizes
714 result = cxListSwap(list, 1, 4);
715 EXPECT_EQ(0, result);
716 result = cxListSwap(list, 2, 14);
717 EXPECT_EQ(0, result);
718 result = cxListSwap(list, 9, 6);
719 EXPECT_EQ(0, result);
720 result = cxListSwap(list, 3, 3);
721 EXPECT_EQ(0, result);
722 result = cxListSwap(list, 10, 11);
723 EXPECT_EQ(0, result);
724 result = cxListSwap(list, 8, 0);
725 EXPECT_EQ(0, result);
726 result = cxListSwap(list, 7, 12);
727 EXPECT_EQ(0, result);
728 result = cxListSwap(list, 13, 15);
729 EXPECT_EQ(0, result);
730
731 result = cxListSwap(list, 5, 16);
732 EXPECT_NE(0, result);
733 result = cxListSwap(list, 16, 6);
734 EXPECT_NE(0, result);
735 result = cxListSwap(list, 16, 17);
736 EXPECT_NE(0, result);
737
738 auto iter = cxListBegin(list);
739 cx_foreach(int*, e, iter) {
740 EXPECT_EQ(*e, swapped[iter.index]);
741 }
742 // TODO: replace with backward iterator
743 cx_for_n(i, 16) {
744 EXPECT_EQ(*((int *) cxListAt(list, i)), swapped[i]);
745 }
746 }
747
748 void verifyAt(CxList *list) const {
749 auto len = testdata_len;
750 EXPECT_EQ(list->size, len);
751 cx_for_n (i, len) {
752 EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
753 }
754 EXPECT_EQ(cxListAt(list, list->size), nullptr);
755 }
756
757 void verifyFind(CxList *list) const {
758 cx_for_n (attempt, 25) {
759 size_t exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
760 int val = testdata.data[exp];
761 // randomly picked number could occur earlier in list - find first position
762 cx_for_n (i, exp) {
763 if (testdata.data[i] == val) {
764 exp = i;
765 break;
766 }
767 }
768 EXPECT_EQ(cxListFind(list, &val), exp);
769 }
770 }
771
772 void verifySort(CxList *list) const {
773 std::array<int, testdata_len> expected{};
774 std::partial_sort_copy(testdata.data.begin(), testdata.data.end(), expected.begin(), expected.end());
775 cxListSort(list);
776 cx_for_n (i, testdata_len) ASSERT_EQ(*(int *) cxListAt(list, i), expected[i]);
777 }
778
779 void verifyIterator(CxList *list) const {
780 int i = 0;
781 auto iter = cxListBeginMut(list);
782 cx_foreach(int*, x, iter) {
783 ASSERT_EQ(iter.index, (size_t) (i + 1) / 2);
784 ASSERT_EQ(*x, testdata.data[i]);
785 if (i % 2 == 1) cxIteratorFlagRemoval(iter);
786 i++;
787 }
788 auto len = testdata_len;
789 EXPECT_EQ(i, len);
790 ASSERT_EQ(list->size, len / 2);
791 cx_for_n(j, len / 2) ASSERT_EQ(*(int *) cxListAt(list, j), testdata.data[j * 2]);
792 }
793
794 static void verifyInsertViaIterator(CxList *list) {
795 int newdata[] = {10, 20, 30, 40, 50};
796
797 auto iter = cxListMutIterator(list, 2);
798 EXPECT_TRUE(cxIteratorValid(iter));
799 EXPECT_EQ(iter.index, 2);
800 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2);
801 cxListInsertAfter(&iter, &newdata[0]);
802 EXPECT_TRUE(cxIteratorValid(iter));
803 EXPECT_EQ(iter.index, 2);
804 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2);
805 cxListInsertBefore(&iter, &newdata[1]);
806 EXPECT_TRUE(cxIteratorValid(iter));
807 EXPECT_EQ(iter.index, 3);
808 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2);
809
810 iter = cxListBeginMut(list);
811 cxListInsertBefore(&iter, &newdata[2]);
812 EXPECT_TRUE(cxIteratorValid(iter));
813 EXPECT_EQ(iter.index, 1);
814 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 0);
815 iter = cxListMutIterator(list, list->size);
816 cxListInsertBefore(&iter, &newdata[3]);
817 EXPECT_FALSE(cxIteratorValid(iter));
818 EXPECT_EQ(iter.index, 9);
819 iter = cxListMutIterator(list, list->size);
820 cxListInsertAfter(&iter, &newdata[4]);
821 EXPECT_FALSE(cxIteratorValid(iter));
822 EXPECT_EQ(iter.index, 10);
823
824 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
825 cx_for_n (j, 10) EXPECT_EQ(*(int *) cxListAt(list, j), expdata[j]);
826 }
827
828 void verifyReverse(CxList *list) const {
829 cxListReverse(list);
830 cx_for_n(i, testdata_len) {
831 ASSERT_EQ(*(int *) cxListAt(list, i), testdata.data[testdata_len - 1 - i]);
832 }
833 }
834
835 static void verifyCompare(
836 CxList *left,
837 CxList *right
838 ) {
839 EXPECT_EQ(cxListCompare(left, right), 0);
840 int x = 42;
841 cxListAdd(left, &x);
842 ASSERT_GT(left->size, right->size);
843 EXPECT_GT(cxListCompare(left, right), 0);
844 EXPECT_LT(cxListCompare(right, left), 0);
845 cxListAdd(right, &x);
846 ASSERT_EQ(left->size, right->size);
847 EXPECT_EQ(cxListCompare(left, right), 0);
848 int a = 5, b = 10;
849 cxListInsert(left, 15, &a);
850 cxListInsert(right, 15, &b);
851 ASSERT_EQ(left->size, right->size);
852 EXPECT_LT(cxListCompare(left, right), 0);
853 EXPECT_GT(cxListCompare(right, left), 0);
854 *(int *) cxListAt(left, 15) = 10;
855 EXPECT_EQ(cxListCompare(left, right), 0);
856 }
857 };
858
859 class LinkedList : public HighLevelTest {
860 };
861
862 class PointerLinkedList : public HighLevelTest {
863 };
864
865 class ArrayList : public HighLevelTest {
866 };
867
868 TEST_F(PointerLinkedList, cxListStorePointers) {
869 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, 47));
870 EXPECT_FALSE(cxListIsStoringPointers(list));
871 cxListStorePointers(list);
872 EXPECT_EQ(list->itemsize, sizeof(void *));
873 EXPECT_NE(list->cl, nullptr);
874 EXPECT_NE(list->climpl, nullptr);
875 EXPECT_TRUE(cxListIsStoringPointers(list));
876 cxListStoreObjects(list);
877 EXPECT_NE(list->cl, nullptr);
878 EXPECT_EQ(list->climpl, nullptr);
879 EXPECT_FALSE(cxListIsStoringPointers(list));
880 }
881
882 TEST_F(LinkedList, cxLinkedListCreate) {
883 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
884 ASSERT_NE(list, nullptr);
885 EXPECT_EQ(list->itemsize, sizeof(int));
886 EXPECT_EQ(list->capacity, (size_t) -1);
887 verifyCreate(list);
888 }
889
890 TEST_F(ArrayList, cxArrayListCreate) {
891 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
892 ASSERT_NE(list, nullptr);
893 EXPECT_EQ(list->itemsize, sizeof(int));
894 EXPECT_EQ(list->capacity, 8);
895 verifyCreate(list);
896 }
897
898 TEST_F(LinkedList, cxListAdd) {
899 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
900 verifyAdd(list, false);
901 }
902
903 TEST_F(PointerLinkedList, cxListAdd) {
904 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
905 cxListStorePointers(list);
906 verifyAdd(list, true);
907 }
908
909 TEST_F(ArrayList, cxListAdd) {
910 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
911 verifyAdd(list, false);
912 }
913
914 TEST_F(LinkedList, cxListInsert) {
915 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
916 }
917
918 TEST_F(PointerLinkedList, cxListInsert) {
919 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
920 cxListStorePointers(list);
921 verifyInsert(list);
922 }
923
924 TEST_F(ArrayList, cxListInsert) {
925 verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2)));
926 }
927
928 TEST_F(LinkedList, cxListInsertArray) {
929 verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
930 }
931
932 TEST_F(PointerLinkedList, cxListInsertArray) {
933 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
934 cxListStorePointers(list);
935 verifyInsertArray(list, true);
936 }
937
938 TEST_F(ArrayList, cxListInsertArray) {
939 verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)));
940 }
941
942 TEST_F(LinkedList, cxListRemove) {
943 verifyRemove(linkedListFromTestData());
944 }
945
946 TEST_F(PointerLinkedList, cxListRemove) {
947 verifyRemove(pointerLinkedListFromTestData());
948 }
949
950 TEST_F(ArrayList, cxListRemove) {
951 verifyRemove(arrayListFromTestData());
952 }
953
954 TEST_F(LinkedList, cxListSwap) {
955 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
956 }
957
958 TEST_F(PointerLinkedList, cxListSwap) {
959 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
960 cxListStorePointers(list);
961 verifySwap(list);
962 }
963
964 TEST_F(ArrayList, cxListSwap) {
965 verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 16)));
966 }
967
968 TEST_F(LinkedList, cxListSwapNoSBO) {
969 CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
970 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
971 CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
972 }
973
974 TEST_F(PointerLinkedList, cxListSwapNoSBO) {
975 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
976 cxListStorePointers(list);
977 CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
978 verifySwap(list);
979 CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
980 }
981
982 TEST_F(ArrayList, cxListSwapNoSBO) {
983 CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
984 verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 16)));
985 CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
986 }
987
988 TEST_F(LinkedList, cxListAt) {
989 verifyAt(linkedListFromTestData());
990 }
991
992 TEST_F(PointerLinkedList, cxListAt) {
993 verifyAt(pointerLinkedListFromTestData());
994 }
995
996 TEST_F(ArrayList, cxListAt) {
997 verifyAt(arrayListFromTestData());
998 }
999
1000 TEST_F(LinkedList, cxListFind) {
1001 verifyFind(linkedListFromTestData());
1002 }
1003
1004 TEST_F(PointerLinkedList, cxListFind) {
1005 verifyFind(pointerLinkedListFromTestData());
1006 }
1007
1008 TEST_F(ArrayList, cxListFind) {
1009 verifyFind(arrayListFromTestData());
1010 }
1011
1012 TEST_F(LinkedList, cxListSort) {
1013 verifySort(linkedListFromTestData());
1014 }
1015
1016 TEST_F(PointerLinkedList, cxListSort) {
1017 verifySort(pointerLinkedListFromTestData());
1018 }
1019
1020 TEST_F(ArrayList, cxListSort) {
1021 verifySort(arrayListFromTestData());
1022 }
1023
1024 TEST_F(LinkedList, Iterator) {
1025 verifyIterator(linkedListFromTestData());
1026 }
1027
1028 TEST_F(PointerLinkedList, Iterator) {
1029 verifyIterator(pointerLinkedListFromTestData());
1030 }
1031
1032 TEST_F(ArrayList, Iterator) {
1033 verifyIterator(arrayListFromTestData());
1034 }
1035
1036 TEST_F(LinkedList, InsertViaIterator) {
1037 int fivenums[] = {0, 1, 2, 3, 4, 5};
1038 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
1039 cxListAddArray(list, fivenums, 5);
1040 verifyInsertViaIterator(list);
1041 }
1042
1043 TEST_F(PointerLinkedList, InsertViaIterator) {
1044 int fivenums[] = {0, 1, 2, 3, 4, 5};
1045 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
1046 cxListStorePointers(list);
1047 // note: cannot use cxListAddArray() because we don't have a list of pointers
1048 cx_for_n(i, 5) cxListAdd(list, &fivenums[i]);
1049 verifyInsertViaIterator(list);
1050 }
1051
1052 TEST_F(ArrayList, InsertViaIterator) {
1053 int fivenums[] = {0, 1, 2, 3, 4, 5};
1054 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
1055 cxListAddArray(list, fivenums, 5);
1056 verifyInsertViaIterator(list);
1057 }
1058
1059 TEST_F(LinkedList, cxListReverse) {
1060 verifyReverse(linkedListFromTestData());
1061 }
1062
1063 TEST_F(PointerLinkedList, cxListReverse) {
1064 verifyReverse(pointerLinkedListFromTestData());
1065 }
1066
1067 TEST_F(ArrayList, cxListReverse) {
1068 verifyReverse(arrayListFromTestData());
1069 }
1070
1071 TEST_F(LinkedList, cxListCompare) {
1072 auto left = linkedListFromTestData();
1073 auto right = linkedListFromTestData();
1074 verifyCompare(left, right);
1075 }
1076
1077 TEST_F(LinkedList, cxListCompareWithPtrList) {
1078 auto left = linkedListFromTestData();
1079 auto right = pointerLinkedListFromTestData();
1080 verifyCompare(left, right);
1081 }
1082
1083 TEST_F(LinkedList, cxListCompareWithArrayList) {
1084 auto left = linkedListFromTestData();
1085 auto right = arrayListFromTestData();
1086 verifyCompare(left, right);
1087 }
1088
1089 TEST_F(PointerLinkedList, cxListCompare) {
1090 auto left = pointerLinkedListFromTestData();
1091 auto right = pointerLinkedListFromTestData();
1092 verifyCompare(left, right);
1093 }
1094
1095 TEST_F(PointerLinkedList, cxListCompareWithNormalList) {
1096 auto left = pointerLinkedListFromTestData();
1097 auto right = linkedListFromTestData();
1098 verifyCompare(left, right);
1099 }
1100
1101 TEST_F(PointerLinkedList, cxListCompareWithArrayList) {
1102 auto left = pointerLinkedListFromTestData();
1103 auto right = arrayListFromTestData();
1104 verifyCompare(left, right);
1105 }
1106
1107 TEST_F(ArrayList, cxListCompare) {
1108 auto left = arrayListFromTestData();
1109 auto right = arrayListFromTestData();
1110 verifyCompare(left, right);
1111 }
1112
1113 TEST_F(ArrayList, cxListCompareWithPtrList) {
1114 auto left = arrayListFromTestData();
1115 auto right = pointerLinkedListFromTestData();
1116 verifyCompare(left, right);
1117 }
1118
1119 TEST_F(ArrayList, cxListCompareWithNormalList) {
1120 auto left = arrayListFromTestData();
1121 auto right = linkedListFromTestData();
1122 verifyCompare(left, right);
1123 }
1124
1125 TEST_F(PointerLinkedList, NoDestructor) {
1126 void *item = cxMalloc(&testingAllocator, sizeof(int));
1127 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
1128 cxListStorePointers(list);
1129 cxListAdd(list, item);
1130 ASSERT_FALSE(testingAllocator.verify());
1131 cxListDestroy(list);
1132 EXPECT_FALSE(testingAllocator.verify());
1133 cxFree(&testingAllocator, item);
1134 EXPECT_TRUE(testingAllocator.verify());
1135 }
1136
1137 TEST_F(PointerLinkedList, SimpleDestructor) {
1138 int item = 0;
1139 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
1140 cxListStorePointers(list);
1141 list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
1142 list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
1143 cxListAdd(list, &item);
1144 cxListDestroy(list);
1145 EXPECT_EQ(item, 42);
1146 }
1147
1148 TEST_F(PointerLinkedList, AdvancedDestructor) {
1149 void *item = cxMalloc(&testingAllocator, sizeof(int));
1150 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
1151 cxListStorePointers(list);
1152 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
1153 list->advanced_destructor.data = &testingAllocator;
1154 list->advanced_destructor.func = (cx_destructor_func2) cxFree;
1155 cxListAdd(list, item);
1156 ASSERT_FALSE(testingAllocator.verify());
1157 cxListDestroy(list);
1158 EXPECT_TRUE(testingAllocator.verify());
1159 }

mercurial