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 |
|
42 class HighLevelTest : public ::testing::Test { |
|
43 mutable std::unordered_set<CxList *> lists; |
|
44 protected: |
|
45 CxTestingAllocator testingAllocator; |
|
46 |
|
47 void TearDown() override { |
|
48 for (auto &&l: lists) cxListDestroy(l); |
|
49 CX_TEST_ASSERT(testingAllocator.verify()); |
|
50 } |
|
51 |
|
52 static constexpr size_t testdata_len = 250; |
|
53 int_test_data<testdata_len> testdata; |
|
54 |
|
55 auto autofree(CxList *list) const -> CxList * { |
|
56 if (list != NULL) lists.insert(list); |
|
57 return list; |
|
58 } |
|
59 |
|
60 auto linkedListFromTestData() const -> CxList * { |
|
61 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); |
|
62 cxListAddArray(list, testdata.data.data(), testdata_len); |
|
63 return list; |
|
64 } |
|
65 |
|
66 auto pointerLinkedListFromTestData() const -> CxList * { |
|
67 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)); |
|
68 // note: cannot use cxListAddArray() because we don't have a list of pointers |
|
69 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]); |
|
70 return list; |
|
71 } |
|
72 |
|
73 auto arrayListFromTestData() const -> CxList * { |
|
74 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), testdata_len)); |
|
75 cxListAddArray(list, testdata.data.data(), testdata_len); |
|
76 return list; |
|
77 } |
|
78 |
|
79 auto pointerArrayListFromTestData() const -> CxList * { |
|
80 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 256)); |
|
81 // note: cannot use cxListAddArray() because we don't have a list of pointers |
|
82 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]); |
|
83 return list; |
|
84 } |
|
85 |
|
86 void verifyAdd( |
|
87 CxList *list, |
|
88 bool as_pointer |
|
89 ) { |
|
90 auto len = testdata_len; |
|
91 cx_for_n (i, len) EXPECT_EQ(cxListAdd(list, &testdata.data[i]), 0); |
|
92 CX_TEST_ASSERT(cxListSize(list) == len); |
|
93 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]); |
|
94 cx_for_n (i, len) ++testdata.data[i]; |
|
95 if (as_pointer) { |
|
96 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]); |
|
97 } else { |
|
98 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i] - 1); |
|
99 } |
|
100 } |
|
101 |
|
102 static void verifyInsert(CxList *list) { |
|
103 int a = 5, b = 47, c = 13, d = 42; |
|
104 |
|
105 EXPECT_NE(cxListInsert(list, 1, &a), 0); |
|
106 EXPECT_EQ(cxListSize(list), 0); |
|
107 EXPECT_EQ(cxListInsert(list, 0, &a), 0); |
|
108 EXPECT_EQ(cxListSize(list), 1); |
|
109 EXPECT_EQ(cxListInsert(list, 0, &b), 0); |
|
110 EXPECT_EQ(cxListSize(list), 2); |
|
111 EXPECT_EQ(cxListInsert(list, 1, &c), 0); |
|
112 EXPECT_EQ(cxListSize(list), 3); |
|
113 EXPECT_EQ(cxListInsert(list, 3, &d), 0); |
|
114 |
|
115 CX_TEST_ASSERT(cxListSize(list) == 4); |
|
116 |
|
117 EXPECT_EQ(*(int *) cxListAt(list, 0), 47); |
|
118 EXPECT_EQ(*(int *) cxListAt(list, 1), 13); |
|
119 EXPECT_EQ(*(int *) cxListAt(list, 2), 5); |
|
120 EXPECT_EQ(*(int *) cxListAt(list, 3), 42); |
|
121 } |
|
122 |
|
123 static void verifyInsertArray( |
|
124 CxList *list, |
|
125 bool pointers = false |
|
126 ) { |
|
127 int a[5] = {5, 47, 11, 13, 42}; |
|
128 int b[5] = {9, 18, 72, 50, 7}; |
|
129 int *aptr[5]; |
|
130 int *bptr[5]; |
|
131 cx_for_n(i, 5) { |
|
132 aptr[i] = &a[i]; |
|
133 bptr[i] = &b[i]; |
|
134 } |
|
135 |
|
136 size_t inserted; |
|
137 |
|
138 if (pointers) { |
|
139 inserted = cxListInsertArray(list, 0, aptr, 5); |
|
140 } else { |
|
141 inserted = cxListInsertArray(list, 0, a, 5); |
|
142 } |
|
143 CX_TEST_ASSERT(inserted == 5); |
|
144 EXPECT_EQ(*(int *) cxListAt(list, 0), 5); |
|
145 EXPECT_EQ(*(int *) cxListAt(list, 1), 47); |
|
146 EXPECT_EQ(*(int *) cxListAt(list, 2), 11); |
|
147 EXPECT_EQ(*(int *) cxListAt(list, 3), 13); |
|
148 EXPECT_EQ(*(int *) cxListAt(list, 4), 42); |
|
149 if (pointers) { |
|
150 inserted = cxListInsertArray(list, 3, bptr, 5); |
|
151 } else { |
|
152 inserted = cxListInsertArray(list, 3, b, 5); |
|
153 } |
|
154 CX_TEST_ASSERT(inserted == 5); |
|
155 EXPECT_EQ(*(int *) cxListAt(list, 0), 5); |
|
156 EXPECT_EQ(*(int *) cxListAt(list, 1), 47); |
|
157 EXPECT_EQ(*(int *) cxListAt(list, 2), 11); |
|
158 EXPECT_EQ(*(int *) cxListAt(list, 3), 9); |
|
159 EXPECT_EQ(*(int *) cxListAt(list, 4), 18); |
|
160 EXPECT_EQ(*(int *) cxListAt(list, 5), 72); |
|
161 EXPECT_EQ(*(int *) cxListAt(list, 6), 50); |
|
162 EXPECT_EQ(*(int *) cxListAt(list, 7), 7); |
|
163 EXPECT_EQ(*(int *) cxListAt(list, 8), 13); |
|
164 EXPECT_EQ(*(int *) cxListAt(list, 9), 42); |
|
165 } |
|
166 |
|
167 void verifyRemove(CxList *list) const { |
|
168 EXPECT_EQ(cxListRemove(list, 2), 0); |
|
169 EXPECT_EQ(cxListRemove(list, 4), 0); |
|
170 EXPECT_EQ(cxListSize(list), testdata_len - 2); |
|
171 EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[0]); |
|
172 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[1]); |
|
173 EXPECT_EQ(*(int *) cxListAt(list, 2), testdata.data[3]); |
|
174 EXPECT_EQ(*(int *) cxListAt(list, 3), testdata.data[4]); |
|
175 EXPECT_EQ(*(int *) cxListAt(list, 4), testdata.data[6]); |
|
176 |
|
177 EXPECT_EQ(cxListRemove(list, 0), 0); |
|
178 EXPECT_EQ(cxListSize(list), testdata_len - 3); |
|
179 EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[1]); |
|
180 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[3]); |
|
181 |
|
182 EXPECT_NE(cxListRemove(list, testdata_len), 0); |
|
183 } |
|
184 |
|
185 void verifyFindRemove(CxList *list) const { |
|
186 size_t exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) |
|
187 int val = testdata.data[exp]; |
|
188 // randomly picked number could occur earlier in list - find first position |
|
189 cx_for_n (i, exp) { |
|
190 if (testdata.data[i] == val) { |
|
191 exp = i; |
|
192 break; |
|
193 } |
|
194 } |
|
195 EXPECT_EQ(cxListSize(list), testdata_len); |
|
196 EXPECT_EQ(cxListFind(list, &val), exp); |
|
197 EXPECT_EQ(cxListFindRemove(list, &val), exp); |
|
198 EXPECT_EQ(cxListSize(list), testdata_len - 1); |
|
199 EXPECT_NE(cxListFind(list, &val), exp); |
|
200 |
|
201 int notinlist = -1; |
|
202 EXPECT_LT(cxListFindRemove(list, ¬inlist), 0); |
|
203 EXPECT_EQ(cxListSize(list), testdata_len - 1); |
|
204 } |
|
205 |
|
206 static void verifyClear(CxList *list) { |
|
207 cxListClear(list); |
|
208 EXPECT_EQ(0, cxListSize(list)); |
|
209 } |
|
210 |
|
211 static unsigned destr_test_ctr; |
|
212 static int destr_last_value; |
|
213 |
|
214 static void simple_destr_test_fun(void *data) { |
|
215 auto ptr = (int *) data; |
|
216 destr_last_value = *ptr; |
|
217 *ptr = destr_last_value + 1; |
|
218 destr_test_ctr++; |
|
219 } |
|
220 |
|
221 static void advanced_destr_test_fun( |
|
222 [[maybe_unused]] void *u, |
|
223 void *data |
|
224 ) { |
|
225 simple_destr_test_fun(data); |
|
226 } |
|
227 |
|
228 void verifyAnyDestructor(CxList *list) { |
|
229 int off = cxListIsStoringPointers(list) ? 1 : 0; |
|
230 |
|
231 cxListRemove(list, 15); |
|
232 EXPECT_EQ(1, destr_test_ctr); |
|
233 EXPECT_EQ(testdata.data[15], destr_last_value + off); |
|
234 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list)); |
|
235 cxListRemove(list, 47); |
|
236 EXPECT_EQ(2, destr_test_ctr); |
|
237 EXPECT_EQ(testdata.data[48], destr_last_value + off); |
|
238 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list)); |
|
239 |
|
240 auto iter = cxListMutIteratorAt(list, 7); |
|
241 cxIteratorNext(iter); |
|
242 EXPECT_EQ(2, destr_test_ctr); |
|
243 EXPECT_EQ(testdata.data[48], destr_last_value + off); |
|
244 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list)); |
|
245 cxIteratorFlagRemoval(iter); |
|
246 cxIteratorNext(iter); |
|
247 EXPECT_EQ(3, destr_test_ctr); |
|
248 EXPECT_EQ(testdata.data[8], destr_last_value + off); |
|
249 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list)); |
|
250 |
|
251 iter = cxListMutBackwardsIteratorAt(list, 5); |
|
252 cxIteratorNext(iter); |
|
253 EXPECT_EQ(3, destr_test_ctr); |
|
254 EXPECT_EQ(testdata.data[8], destr_last_value + off); |
|
255 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list)); |
|
256 cxIteratorFlagRemoval(iter); |
|
257 cxIteratorNext(iter); |
|
258 EXPECT_EQ(4, destr_test_ctr); |
|
259 EXPECT_EQ(testdata.data[4], destr_last_value + off); |
|
260 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list)); |
|
261 |
|
262 cxListClear(list); |
|
263 EXPECT_EQ(testdata_len, destr_test_ctr); |
|
264 EXPECT_EQ(testdata.data[testdata_len - 1], destr_last_value + off); |
|
265 } |
|
266 |
|
267 void verifySimpleDestructor(CxList *list) { |
|
268 destr_test_ctr = 0; |
|
269 list->simple_destructor = simple_destr_test_fun; |
|
270 verifyAnyDestructor(list); |
|
271 } |
|
272 |
|
273 void verifyAdvancedDestructor(CxList *list) { |
|
274 destr_test_ctr = 0; |
|
275 list->advanced_destructor = advanced_destr_test_fun; |
|
276 verifyAnyDestructor(list); |
|
277 } |
|
278 |
|
279 static void verifySwap(CxList *list) { |
|
280 CX_TEST_ASSERT(cxListSize(list) == 0); |
|
281 |
|
282 int original[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; |
|
283 int swapped[16] = {8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13}; |
|
284 |
|
285 // we have to add the items one by one, because it could be a pointer list |
|
286 cx_for_n(i, 16) { |
|
287 cxListAdd(list, &original[i]); |
|
288 } |
|
289 |
|
290 int result; |
|
291 |
|
292 // execute the test two times with different item sizes |
|
293 result = cxListSwap(list, 1, 4); |
|
294 EXPECT_EQ(0, result); |
|
295 result = cxListSwap(list, 2, 14); |
|
296 EXPECT_EQ(0, result); |
|
297 result = cxListSwap(list, 9, 6); |
|
298 EXPECT_EQ(0, result); |
|
299 result = cxListSwap(list, 3, 3); |
|
300 EXPECT_EQ(0, result); |
|
301 result = cxListSwap(list, 10, 11); |
|
302 EXPECT_EQ(0, result); |
|
303 result = cxListSwap(list, 8, 0); |
|
304 EXPECT_EQ(0, result); |
|
305 result = cxListSwap(list, 7, 12); |
|
306 EXPECT_EQ(0, result); |
|
307 result = cxListSwap(list, 13, 15); |
|
308 EXPECT_EQ(0, result); |
|
309 |
|
310 result = cxListSwap(list, 5, 16); |
|
311 CX_TEST_ASSERT(0 != result); |
|
312 result = cxListSwap(list, 16, 6); |
|
313 CX_TEST_ASSERT(0 != result); |
|
314 result = cxListSwap(list, 16, 17); |
|
315 CX_TEST_ASSERT(0 != result); |
|
316 |
|
317 auto iter = cxListIterator(list); |
|
318 cx_foreach(int*, e, iter) { |
|
319 EXPECT_EQ(*e, swapped[iter.index]); |
|
320 } |
|
321 iter = cxListBackwardsIterator(list); |
|
322 cx_foreach(int*, e, iter) { |
|
323 EXPECT_EQ(*e, swapped[iter.index]); |
|
324 } |
|
325 } |
|
326 |
|
327 void verifyAt(CxList *list) const { |
|
328 auto len = testdata_len; |
|
329 EXPECT_EQ(cxListSize(list), len); |
|
330 cx_for_n (i, len) { |
|
331 EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]); |
|
332 } |
|
333 EXPECT_EQ(cxListAt(list, cxListSize(list)), NULL); |
|
334 } |
|
335 |
|
336 void verifyFind(CxList *list) const { |
|
337 cx_for_n (attempt, 25) { |
|
338 size_t exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) |
|
339 int val = testdata.data[exp]; |
|
340 // randomly picked number could occur earlier in list - find first position |
|
341 cx_for_n (i, exp) { |
|
342 if (testdata.data[i] == val) { |
|
343 exp = i; |
|
344 break; |
|
345 } |
|
346 } |
|
347 EXPECT_EQ(cxListFind(list, &val), exp); |
|
348 } |
|
349 |
|
350 int notinlist = -1; |
|
351 EXPECT_LT(cxListFind(list, ¬inlist), 0); |
|
352 } |
|
353 |
|
354 void verifySort(CxList *list) const { |
|
355 std::array<int, testdata_len> expected{}; |
|
356 std::partial_sort_copy(testdata.data.begin(), testdata.data.end(), expected.begin(), expected.end()); |
|
357 cxListSort(list); |
|
358 cx_for_n (i, testdata_len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]); |
|
359 } |
|
360 |
|
361 void verifyIterator(CxList *list) const { |
|
362 auto iter = cxListIterator(list); |
|
363 size_t i = 0; |
|
364 cx_foreach(int*, x, iter) { |
|
365 CX_TEST_ASSERT(i == iter.index); |
|
366 EXPECT_EQ(*x, testdata.data[iter.index]); |
|
367 i++; |
|
368 } |
|
369 CX_TEST_ASSERT(i == cxListSize(list)); |
|
370 iter = cxListBackwardsIterator(list); |
|
371 cx_foreach(int*, x, iter) { |
|
372 CX_TEST_ASSERT(i - 1 == iter.index); |
|
373 EXPECT_EQ(*x, testdata.data[iter.index]); |
|
374 i--; |
|
375 } |
|
376 CX_TEST_ASSERT(i == 0); |
|
377 auto len = testdata_len; |
|
378 i = len / 2; |
|
379 auto mut_iter = cxListMutIteratorAt(list, i); |
|
380 size_t j = 0; |
|
381 cx_foreach(int*, x, mut_iter) { |
|
382 CX_TEST_ASSERT(mut_iter.index == len / 2 + j / 2); |
|
383 CX_TEST_ASSERT(*x == testdata.data[i]); |
|
384 if (i % 2 == 1) cxIteratorFlagRemoval(mut_iter); |
|
385 i++; |
|
386 j++; |
|
387 } |
|
388 CX_TEST_ASSERT(i == len); |
|
389 i = len / 2; |
|
390 j = 0; |
|
391 mut_iter = cxListMutBackwardsIteratorAt(list, i - 1); |
|
392 cx_foreach(int*, x, mut_iter) { |
|
393 CX_TEST_ASSERT(mut_iter.index == len / 2 - 1 - j); |
|
394 CX_TEST_ASSERT(*x == testdata.data[i - 1]); |
|
395 if (i % 2 == 0) cxIteratorFlagRemoval(mut_iter); |
|
396 i--; |
|
397 j++; |
|
398 } |
|
399 CX_TEST_ASSERT(i == 0); |
|
400 CX_TEST_ASSERT(cxListSize(list) == len / 2); |
|
401 cx_for_n(j, len / 2) ASSERT_EQ(*(int *) cxListAt(list, j), testdata.data[j * 2]); |
|
402 } |
|
403 |
|
404 static void verifyInsertViaIterator(CxList *list) { |
|
405 int newdata[] = {10, 20, 30, 40, 50}; |
|
406 |
|
407 auto iter = cxListMutIteratorAt(list, 2); |
|
408 CX_TEST_ASSERT(cxIteratorValid(iter)); |
|
409 EXPECT_EQ(iter.index, 2); |
|
410 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2); |
|
411 cxListInsertAfter(&iter, &newdata[0]); |
|
412 CX_TEST_ASSERT(cxIteratorValid(iter)); |
|
413 EXPECT_EQ(iter.index, 2); |
|
414 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2); |
|
415 cxListInsertBefore(&iter, &newdata[1]); |
|
416 CX_TEST_ASSERT(cxIteratorValid(iter)); |
|
417 EXPECT_EQ(iter.index, 3); |
|
418 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2); |
|
419 |
|
420 iter = cxListMutIterator(list); |
|
421 cxListInsertBefore(&iter, &newdata[2]); |
|
422 CX_TEST_ASSERT(cxIteratorValid(iter)); |
|
423 EXPECT_EQ(iter.index, 1); |
|
424 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 0); |
|
425 iter = cxListMutIteratorAt(list, cxListSize(list)); |
|
426 cxListInsertBefore(&iter, &newdata[3]); |
|
427 CX_TEST_ASSERT(!cxIteratorValid(iter)); |
|
428 EXPECT_EQ(iter.index, 9); |
|
429 iter = cxListMutIteratorAt(list, cxListSize(list)); |
|
430 cxListInsertAfter(&iter, &newdata[4]); |
|
431 CX_TEST_ASSERT(!cxIteratorValid(iter)); |
|
432 EXPECT_EQ(iter.index, 10); |
|
433 |
|
434 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; |
|
435 cx_for_n (j, 10) EXPECT_EQ(*(int *) cxListAt(list, j), expdata[j]); |
|
436 } |
|
437 |
|
438 void verifyReverse(CxList *list) const { |
|
439 cxListReverse(list); |
|
440 cx_for_n(i, testdata_len) { |
|
441 ASSERT_EQ(*(int *) cxListAt(list, i), testdata.data[testdata_len - 1 - i]); |
|
442 } |
|
443 } |
|
444 |
|
445 static void verifyCompare( |
|
446 CxList *left, |
|
447 CxList *right |
|
448 ) { |
|
449 EXPECT_EQ(cxListCompare(left, right), 0); |
|
450 int x = 42; |
|
451 cxListAdd(left, &x); |
|
452 ASSERT_GT(cxListSize(left), cxListSize(right)); |
|
453 EXPECT_GT(cxListCompare(left, right), 0); |
|
454 EXPECT_LT(cxListCompare(right, left), 0); |
|
455 cxListAdd(right, &x); |
|
456 CX_TEST_ASSERT(cxListSize(left) == cxListSize(right)); |
|
457 EXPECT_EQ(cxListCompare(left, right), 0); |
|
458 int a = 5, b = 10; |
|
459 cxListInsert(left, 15, &a); |
|
460 cxListInsert(right, 15, &b); |
|
461 CX_TEST_ASSERT(cxListSize(left) == cxListSize(right)); |
|
462 EXPECT_LT(cxListCompare(left, right), 0); |
|
463 EXPECT_GT(cxListCompare(right, left), 0); |
|
464 *(int *) cxListAt(left, 15) = 10; |
|
465 EXPECT_EQ(cxListCompare(left, right), 0); |
|
466 } |
|
467 }; |
|
468 |
|
469 unsigned HighLevelTest::destr_test_ctr = 0; |
|
470 int HighLevelTest::destr_last_value = 0; |
|
471 |
|
472 class LinkedList : public HighLevelTest { |
|
473 }; |
|
474 |
|
475 class PointerLinkedList : public HighLevelTest { |
|
476 }; |
|
477 |
|
478 class ArrayList : public HighLevelTest { |
|
479 }; |
|
480 |
|
481 class PointerArrayList : public HighLevelTest { |
|
482 }; |
|
483 |
|
484 |
|
485 TEST_F(LinkedList, cxListAdd) { |
|
486 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); |
|
487 verifyAdd(list, false); |
|
488 } |
|
489 |
|
490 TEST_F(PointerLinkedList, cxListAdd) { |
|
491 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)); |
|
492 verifyAdd(list, true); |
|
493 } |
|
494 |
|
495 TEST_F(ArrayList, cxListAdd) { |
|
496 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8)); |
|
497 verifyAdd(list, false); |
|
498 } |
|
499 |
|
500 TEST_F(PointerArrayList, cxListAdd) { |
|
501 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 8)); |
|
502 verifyAdd(list, true); |
|
503 } |
|
504 |
|
505 TEST_F(LinkedList, cxListInsert) { |
|
506 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); |
|
507 } |
|
508 |
|
509 TEST_F(PointerLinkedList, cxListInsert) { |
|
510 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS))); |
|
511 } |
|
512 |
|
513 TEST_F(ArrayList, cxListInsert) { |
|
514 verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2))); |
|
515 } |
|
516 |
|
517 TEST_F(PointerArrayList, cxListInsert) { |
|
518 verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 2))); |
|
519 } |
|
520 |
|
521 TEST_F(LinkedList, cxListInsertArray) { |
|
522 verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); |
|
523 } |
|
524 |
|
525 TEST_F(PointerLinkedList, cxListInsertArray) { |
|
526 verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)), true); |
|
527 } |
|
528 |
|
529 TEST_F(ArrayList, cxListInsertArray) { |
|
530 verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4))); |
|
531 } |
|
532 |
|
533 TEST_F(PointerArrayList, cxListInsertArray) { |
|
534 verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 4)), true); |
|
535 } |
|
536 |
|
537 TEST_F(LinkedList, cxListRemove) { |
|
538 verifyRemove(linkedListFromTestData()); |
|
539 } |
|
540 |
|
541 TEST_F(PointerLinkedList, cxListRemove) { |
|
542 verifyRemove(pointerLinkedListFromTestData()); |
|
543 } |
|
544 |
|
545 TEST_F(ArrayList, cxListRemove) { |
|
546 verifyRemove(arrayListFromTestData()); |
|
547 } |
|
548 |
|
549 TEST_F(PointerArrayList, cxListRemove) { |
|
550 verifyRemove(pointerArrayListFromTestData()); |
|
551 } |
|
552 |
|
553 TEST_F(LinkedList, cxListFindRemove) { |
|
554 verifyFindRemove(linkedListFromTestData()); |
|
555 } |
|
556 |
|
557 TEST_F(PointerLinkedList, cxListFindRemove) { |
|
558 verifyFindRemove(pointerLinkedListFromTestData()); |
|
559 } |
|
560 |
|
561 TEST_F(ArrayList, cxListFindRemove) { |
|
562 verifyFindRemove(arrayListFromTestData()); |
|
563 } |
|
564 |
|
565 TEST_F(PointerArrayList, cxListFindRemove) { |
|
566 verifyFindRemove(pointerArrayListFromTestData()); |
|
567 } |
|
568 |
|
569 TEST_F(LinkedList, cxListClear) { |
|
570 verifyClear(linkedListFromTestData()); |
|
571 } |
|
572 |
|
573 TEST_F(PointerLinkedList, cxListClear) { |
|
574 verifyClear(pointerLinkedListFromTestData()); |
|
575 } |
|
576 |
|
577 TEST_F(ArrayList, cxListClear) { |
|
578 verifyClear(arrayListFromTestData()); |
|
579 } |
|
580 |
|
581 TEST_F(PointerArrayList, cxListClear) { |
|
582 verifyClear(pointerArrayListFromTestData()); |
|
583 } |
|
584 |
|
585 TEST_F(LinkedList, cxListSwap) { |
|
586 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); |
|
587 } |
|
588 |
|
589 TEST_F(PointerLinkedList, cxListSwap) { |
|
590 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS))); |
|
591 } |
|
592 |
|
593 TEST_F(ArrayList, cxListSwap) { |
|
594 verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 16))); |
|
595 } |
|
596 |
|
597 TEST_F(PointerArrayList, cxListSwap) { |
|
598 verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 16))); |
|
599 } |
|
600 |
|
601 TEST_F(LinkedList, cxListSwapNoSBO) { |
|
602 CX_DISABLE_LINKED_LIST_SWAP_SBO = true; |
|
603 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); |
|
604 CX_DISABLE_LINKED_LIST_SWAP_SBO = false; |
|
605 } |
|
606 |
|
607 TEST_F(PointerLinkedList, cxListSwapNoSBO) { |
|
608 CX_DISABLE_LINKED_LIST_SWAP_SBO = true; |
|
609 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS))); |
|
610 CX_DISABLE_LINKED_LIST_SWAP_SBO = false; |
|
611 } |
|
612 |
|
613 TEST_F(LinkedList, cxListAt) { |
|
614 verifyAt(linkedListFromTestData()); |
|
615 } |
|
616 |
|
617 TEST_F(PointerLinkedList, cxListAt) { |
|
618 verifyAt(pointerLinkedListFromTestData()); |
|
619 } |
|
620 |
|
621 TEST_F(ArrayList, cxListAt) { |
|
622 verifyAt(arrayListFromTestData()); |
|
623 } |
|
624 |
|
625 TEST_F(PointerArrayList, cxListAt) { |
|
626 verifyAt(pointerArrayListFromTestData()); |
|
627 } |
|
628 |
|
629 TEST_F(LinkedList, cxListFind) { |
|
630 verifyFind(linkedListFromTestData()); |
|
631 } |
|
632 |
|
633 TEST_F(PointerLinkedList, cxListFind) { |
|
634 verifyFind(pointerLinkedListFromTestData()); |
|
635 } |
|
636 |
|
637 TEST_F(ArrayList, cxListFind) { |
|
638 verifyFind(arrayListFromTestData()); |
|
639 } |
|
640 |
|
641 TEST_F(PointerArrayList, cxListFind) { |
|
642 verifyFind(pointerArrayListFromTestData()); |
|
643 } |
|
644 |
|
645 TEST_F(LinkedList, cxListSort) { |
|
646 verifySort(linkedListFromTestData()); |
|
647 } |
|
648 |
|
649 TEST_F(PointerLinkedList, cxListSort) { |
|
650 verifySort(pointerLinkedListFromTestData()); |
|
651 } |
|
652 |
|
653 TEST_F(ArrayList, cxListSort) { |
|
654 verifySort(arrayListFromTestData()); |
|
655 } |
|
656 |
|
657 TEST_F(PointerArrayList, cxListSort) { |
|
658 verifySort(pointerArrayListFromTestData()); |
|
659 } |
|
660 |
|
661 TEST_F(LinkedList, Iterator) { |
|
662 verifyIterator(linkedListFromTestData()); |
|
663 } |
|
664 |
|
665 TEST_F(PointerLinkedList, Iterator) { |
|
666 verifyIterator(pointerLinkedListFromTestData()); |
|
667 } |
|
668 |
|
669 TEST_F(ArrayList, Iterator) { |
|
670 verifyIterator(arrayListFromTestData()); |
|
671 } |
|
672 |
|
673 TEST_F(PointerArrayList, Iterator) { |
|
674 verifyIterator(pointerArrayListFromTestData()); |
|
675 } |
|
676 |
|
677 TEST_F(LinkedList, InsertViaIterator) { |
|
678 int fivenums[] = {0, 1, 2, 3, 4, 5}; |
|
679 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); |
|
680 cxListAddArray(list, fivenums, 5); |
|
681 verifyInsertViaIterator(list); |
|
682 } |
|
683 |
|
684 TEST_F(PointerLinkedList, InsertViaIterator) { |
|
685 int fivenums[] = {0, 1, 2, 3, 4, 5}; |
|
686 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)); |
|
687 // note: cannot use cxListAddArray() because we don't have a list of pointers |
|
688 cx_for_n(i, 5) cxListAdd(list, &fivenums[i]); |
|
689 verifyInsertViaIterator(list); |
|
690 } |
|
691 |
|
692 TEST_F(ArrayList, InsertViaIterator) { |
|
693 int fivenums[] = {0, 1, 2, 3, 4, 5}; |
|
694 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)); |
|
695 cxListAddArray(list, fivenums, 5); |
|
696 verifyInsertViaIterator(list); |
|
697 } |
|
698 |
|
699 TEST_F(PointerArrayList, InsertViaIterator) { |
|
700 int fivenums[] = {0, 1, 2, 3, 4, 5}; |
|
701 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 4)); |
|
702 // note: cannot use cxListAddArray() because we don't have a list of pointers |
|
703 cx_for_n(i, 5) cxListAdd(list, &fivenums[i]); |
|
704 verifyInsertViaIterator(list); |
|
705 } |
|
706 |
|
707 TEST_F(LinkedList, cxListReverse) { |
|
708 verifyReverse(linkedListFromTestData()); |
|
709 } |
|
710 |
|
711 TEST_F(PointerLinkedList, cxListReverse) { |
|
712 verifyReverse(pointerLinkedListFromTestData()); |
|
713 } |
|
714 |
|
715 TEST_F(ArrayList, cxListReverse) { |
|
716 verifyReverse(arrayListFromTestData()); |
|
717 } |
|
718 |
|
719 TEST_F(PointerArrayList, cxListReverse) { |
|
720 verifyReverse(pointerArrayListFromTestData()); |
|
721 } |
|
722 |
|
723 TEST_F(LinkedList, cxListCompare) { |
|
724 auto left = linkedListFromTestData(); |
|
725 auto right = linkedListFromTestData(); |
|
726 verifyCompare(left, right); |
|
727 } |
|
728 |
|
729 TEST_F(LinkedList, cxListCompareWithPtrList) { |
|
730 auto left = linkedListFromTestData(); |
|
731 auto right = pointerLinkedListFromTestData(); |
|
732 verifyCompare(left, right); |
|
733 } |
|
734 |
|
735 TEST_F(LinkedList, cxListCompareWithArrayList) { |
|
736 auto left = linkedListFromTestData(); |
|
737 auto right = arrayListFromTestData(); |
|
738 verifyCompare(left, right); |
|
739 } |
|
740 |
|
741 TEST_F(LinkedList, cxListCompareWithPtrArrayList) { |
|
742 auto left = linkedListFromTestData(); |
|
743 auto right = pointerArrayListFromTestData(); |
|
744 verifyCompare(left, right); |
|
745 } |
|
746 |
|
747 TEST_F(PointerLinkedList, cxListCompare) { |
|
748 auto left = pointerLinkedListFromTestData(); |
|
749 auto right = pointerLinkedListFromTestData(); |
|
750 verifyCompare(left, right); |
|
751 } |
|
752 |
|
753 TEST_F(PointerLinkedList, cxListCompareWithNormalList) { |
|
754 auto left = pointerLinkedListFromTestData(); |
|
755 auto right = linkedListFromTestData(); |
|
756 verifyCompare(left, right); |
|
757 } |
|
758 |
|
759 TEST_F(PointerLinkedList, cxListCompareWithArrayList) { |
|
760 auto left = pointerLinkedListFromTestData(); |
|
761 auto right = arrayListFromTestData(); |
|
762 verifyCompare(left, right); |
|
763 } |
|
764 |
|
765 TEST_F(PointerLinkedList, cxListCompareWithPtrArrayList) { |
|
766 auto left = pointerLinkedListFromTestData(); |
|
767 auto right = pointerArrayListFromTestData(); |
|
768 verifyCompare(left, right); |
|
769 } |
|
770 |
|
771 TEST_F(ArrayList, cxListCompare) { |
|
772 auto left = arrayListFromTestData(); |
|
773 auto right = arrayListFromTestData(); |
|
774 verifyCompare(left, right); |
|
775 } |
|
776 |
|
777 TEST_F(ArrayList, cxListCompareWithPtrList) { |
|
778 auto left = arrayListFromTestData(); |
|
779 auto right = pointerLinkedListFromTestData(); |
|
780 verifyCompare(left, right); |
|
781 } |
|
782 |
|
783 TEST_F(ArrayList, cxListCompareWithNormalList) { |
|
784 auto left = arrayListFromTestData(); |
|
785 auto right = linkedListFromTestData(); |
|
786 verifyCompare(left, right); |
|
787 } |
|
788 |
|
789 TEST_F(ArrayList, cxListCompareWithPtrArrayList) { |
|
790 auto left = arrayListFromTestData(); |
|
791 auto right = pointerArrayListFromTestData(); |
|
792 verifyCompare(left, right); |
|
793 } |
|
794 |
|
795 TEST_F(PointerArrayList, cxListCompare) { |
|
796 auto left = pointerArrayListFromTestData(); |
|
797 auto right = pointerArrayListFromTestData(); |
|
798 verifyCompare(left, right); |
|
799 } |
|
800 |
|
801 TEST_F(PointerArrayList, cxListCompareWithPtrList) { |
|
802 auto left = pointerArrayListFromTestData(); |
|
803 auto right = pointerLinkedListFromTestData(); |
|
804 verifyCompare(left, right); |
|
805 } |
|
806 |
|
807 TEST_F(PointerArrayList, cxListCompareWithNormalList) { |
|
808 auto left = pointerArrayListFromTestData(); |
|
809 auto right = linkedListFromTestData(); |
|
810 verifyCompare(left, right); |
|
811 } |
|
812 |
|
813 TEST_F(PointerArrayList, cxListCompareWithNormalArrayList) { |
|
814 auto left = pointerArrayListFromTestData(); |
|
815 auto right = arrayListFromTestData(); |
|
816 verifyCompare(left, right); |
|
817 } |
|
818 |
|
819 TEST_F(LinkedList, SimpleDestructor) { |
|
820 verifySimpleDestructor(linkedListFromTestData()); |
|
821 } |
|
822 |
|
823 TEST_F(PointerLinkedList, SimpleDestructor) { |
|
824 verifySimpleDestructor(pointerLinkedListFromTestData()); |
|
825 } |
|
826 |
|
827 TEST_F(ArrayList, SimpleDestructor) { |
|
828 verifySimpleDestructor(arrayListFromTestData()); |
|
829 } |
|
830 |
|
831 TEST_F(PointerArrayList, SimpleDestructor) { |
|
832 verifySimpleDestructor(pointerArrayListFromTestData()); |
|
833 } |
|
834 |
|
835 TEST_F(LinkedList, AdvancedDestructor) { |
|
836 verifyAdvancedDestructor(linkedListFromTestData()); |
|
837 } |
|
838 |
|
839 TEST_F(PointerLinkedList, AdvancedDestructor) { |
|
840 verifyAdvancedDestructor(pointerLinkedListFromTestData()); |
|
841 } |
|
842 |
|
843 TEST_F(ArrayList, AdvancedDestructor) { |
|
844 verifyAdvancedDestructor(arrayListFromTestData()); |
|
845 } |
|
846 |
|
847 TEST_F(PointerArrayList, AdvancedDestructor) { |
|
848 verifyAdvancedDestructor(pointerArrayListFromTestData()); |
|
849 } |
|
850 |
|
851 |
|
852 |
|