5a3b1d4c392b455bb102df4643e0ee13364282df
[pcsx_rearmed.git] / deps / libretro-common / test / lists / test_linked_list.c
1 /* Copyright  (C) 2010-2020 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (test_linked_list.c).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22
23 #include <check.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26
27 #include <lists/linked_list.h>
28
29 #define SUITE_NAME "Linked List"
30
31 static char *_value_1 = "value1";
32 static char *_value_2 = "value2";
33 static char *_value_3 = "value3";
34
35 START_TEST (test_linked_list_create)
36 {
37    linked_list_t *list = linked_list_new();
38    ck_assert_ptr_nonnull(list);
39    linked_list_free(list, NULL);
40 }
41 END_TEST
42
43 START_TEST (test_linked_list_free)
44 {
45    linked_list_t *queue = linked_list_new();
46    linked_list_free(queue, NULL);
47    linked_list_free(NULL, NULL);
48 }
49 END_TEST
50
51 static int _free_alloced_value_count;
52 static void _free_alloced_value(void *value)
53 {
54    _free_alloced_value_count++;
55    free(value);
56 }
57
58 START_TEST (test_linked_list_free_with_fn)
59 {
60    linked_list_t *list = linked_list_new();
61    linked_list_add(list, malloc(1));
62    linked_list_add(list, malloc(1));
63    linked_list_add(list, malloc(1));
64
65    _free_alloced_value_count = 0;
66    linked_list_free(list, &_free_alloced_value);
67
68    ck_assert_int_eq(3, _free_alloced_value_count);
69 }
70 END_TEST
71
72 static void _verify_list(linked_list_t *list, int size, ...)
73 {
74    va_list values_list;
75    void **values;
76    int i;
77    linked_list_iterator_t *iterator;
78
79    values = (void **)malloc(size * sizeof(void *));
80
81    ck_assert_int_eq(linked_list_size(list), size);
82
83    va_start(values_list, size);
84    for (i = 0; i < size; i++)
85    {
86       values[i] = va_arg(values_list, void *);
87       ck_assert_ptr_eq(values[i], linked_list_get(list, i));
88    }
89    va_end(values_list);
90
91    iterator = linked_list_iterator(list, true);
92    for (i = 0; i < size; i++)
93    {
94       ck_assert_ptr_nonnull(iterator);
95       ck_assert_ptr_eq(values[i], linked_list_iterator_value(iterator));
96       iterator = linked_list_iterator_next(iterator);
97    }
98    ck_assert_ptr_null(iterator);
99
100    iterator = linked_list_iterator(list, false);
101    for (i = size - 1; i >= 0; i--)
102    {
103       ck_assert_ptr_nonnull(iterator);
104       ck_assert_ptr_eq(values[i], linked_list_iterator_value(iterator));
105       iterator = linked_list_iterator_next(iterator);
106    }
107    ck_assert_ptr_null(iterator);
108
109    free(values);
110 }
111
112 START_TEST (test_linked_list_add)
113 {
114    linked_list_t *list = linked_list_new();
115    linked_list_add(list, _value_1);
116    linked_list_add(list, _value_2);
117    linked_list_add(list, _value_3);
118
119    _verify_list(list, 3, _value_1, _value_2, _value_3);
120
121    linked_list_free(list, NULL);
122 }
123 END_TEST
124
125 START_TEST (test_linked_list_insert_empty)
126 {
127    linked_list_t *list = linked_list_new();
128    linked_list_insert(list, 0, _value_1);
129
130    ck_assert_int_eq(linked_list_size(list), 1);
131    ck_assert_ptr_eq(linked_list_get(list, 0), _value_1);
132
133    linked_list_free(list, NULL);
134 }
135 END_TEST
136
137 START_TEST (test_linked_list_insert_first)
138 {
139    linked_list_t *list = linked_list_new();
140    linked_list_add(list, _value_2);
141    linked_list_add(list, _value_3);
142    linked_list_insert(list, 0, _value_1);
143
144    _verify_list(list, 3, _value_1, _value_2, _value_3);
145
146    linked_list_free(list, NULL);
147 }
148 END_TEST
149
150 START_TEST (test_linked_list_insert_middle)
151 {
152    linked_list_t *list = linked_list_new();
153    linked_list_add(list, _value_1);
154    linked_list_add(list, _value_3);
155    linked_list_insert(list, 1, _value_2);
156
157    _verify_list(list, 3, _value_1, _value_2, _value_3);
158
159    linked_list_free(list, NULL);
160 }
161 END_TEST
162
163 START_TEST (test_linked_list_insert_last)
164 {
165    linked_list_t *list = linked_list_new();
166    linked_list_add(list, _value_1);
167    linked_list_add(list, _value_2);
168    linked_list_insert(list, 2, _value_3);
169
170    _verify_list(list, 3, _value_1, _value_2, _value_3);
171
172    linked_list_free(list, NULL);
173 }
174 END_TEST
175
176 START_TEST (test_linked_list_insert_invalid)
177 {
178    linked_list_t *list = linked_list_new();
179    linked_list_insert(list, 2, _value_1);
180
181    ck_assert_int_eq(linked_list_size(list), 0);
182
183    linked_list_free(list, NULL);
184 }
185 END_TEST
186
187 START_TEST (test_linked_list_insert_null)
188 {
189    linked_list_insert(NULL, 0, _value_1);
190 }
191 END_TEST
192
193 START_TEST (test_linked_list_get_invalid)
194 {
195    linked_list_t *list = linked_list_new();
196    ck_assert_ptr_null(linked_list_get(list, 2));
197
198    linked_list_free(list, NULL);
199 }
200 END_TEST
201
202 START_TEST (test_linked_list_get_null)
203 {
204    ck_assert_ptr_null(linked_list_get(NULL, 0));
205 }
206 END_TEST
207
208 START_TEST (test_linked_list_get_first_matching_null)
209 {
210    ck_assert_ptr_null(linked_list_get_first_matching(NULL, NULL, NULL));
211 }
212 END_TEST
213
214 START_TEST (test_linked_list_get_first_matching_function_null)
215 {
216    linked_list_t *list = linked_list_new();
217    ck_assert_ptr_null(linked_list_get_first_matching(list, NULL, NULL));
218
219    linked_list_free(list, NULL);
220 }
221
222 bool _matches_function(void *value, void *state)
223 {
224    ck_assert_ptr_eq(_value_1, state);
225    return value == _value_2;
226 }
227
228 START_TEST (test_linked_list_get_first_matching_no_match)
229 {
230    linked_list_t *list = linked_list_new();
231    ck_assert_ptr_null(linked_list_get_first_matching(list, &_matches_function, _value_1));
232
233    linked_list_free(list, NULL);
234 }
235 END_TEST
236
237 START_TEST (test_linked_list_get_first_matching_with_match)
238 {
239    linked_list_t *list = linked_list_new();
240    linked_list_add(list, _value_1);
241    linked_list_add(list, _value_2);
242    linked_list_add(list, _value_3);
243
244    ck_assert_ptr_eq(_value_2, linked_list_get_first_matching(list, &_matches_function, _value_1));
245
246    linked_list_free(list, NULL);
247 }
248 END_TEST
249
250 START_TEST (test_linked_list_get_last_matching_null)
251 {
252    ck_assert_ptr_null(linked_list_get_last_matching(NULL, NULL, NULL));
253 }
254 END_TEST
255
256 START_TEST (test_linked_list_get_last_matching_function_null)
257 {
258    linked_list_t *list = linked_list_new();
259    ck_assert_ptr_null(linked_list_get_last_matching(list, NULL, NULL));
260
261    linked_list_free(list, NULL);
262 }
263
264 START_TEST (test_linked_list_get_last_matching_no_match)
265 {
266    linked_list_t *list = linked_list_new();
267    ck_assert_ptr_null(linked_list_get_last_matching(list, &_matches_function, _value_1));
268
269    linked_list_free(list, NULL);
270 }
271 END_TEST
272
273 START_TEST (test_linked_list_get_last_matching_with_match)
274 {
275    linked_list_t *list = linked_list_new();
276    linked_list_add(list, _value_1);
277    linked_list_add(list, _value_2);
278    linked_list_add(list, _value_3);
279
280    ck_assert_ptr_eq(_value_2, linked_list_get_last_matching(list, &_matches_function, _value_1));
281
282    linked_list_free(list, NULL);
283 }
284 END_TEST
285
286 START_TEST (test_linked_list_remove_at_null)
287 {
288    ck_assert_ptr_null(linked_list_remove_at(NULL, 0));
289 }
290 END_TEST
291
292 START_TEST (test_linked_list_remove_at_empty)
293 {
294    linked_list_t *list = linked_list_new();
295    ck_assert_ptr_null(linked_list_remove_at(list, 0));
296
297    linked_list_free(list, NULL);
298 }
299 END_TEST
300
301 START_TEST (test_linked_list_remove_at_invalid)
302 {
303    linked_list_t *list = linked_list_new();
304    linked_list_add(list, _value_1);
305    linked_list_add(list, _value_2);
306    linked_list_add(list, _value_3);
307
308    linked_list_remove_at(list, 3);
309
310    _verify_list(list, 3, _value_1, _value_2, _value_3);
311
312    linked_list_free(list, NULL);
313 }
314 END_TEST
315
316 START_TEST (test_linked_list_remove_at_first)
317 {
318    linked_list_t *list = linked_list_new();
319    linked_list_add(list, _value_1);
320    linked_list_add(list, _value_2);
321    linked_list_add(list, _value_3);
322
323    linked_list_remove_at(list, 0);
324
325    _verify_list(list, 2, _value_2, _value_3);
326
327    linked_list_free(list, NULL);
328 }
329 END_TEST
330
331 START_TEST (test_linked_list_remove_at_middle)
332 {
333    linked_list_t *list = linked_list_new();
334    linked_list_add(list, _value_1);
335    linked_list_add(list, _value_2);
336    linked_list_add(list, _value_3);
337
338    linked_list_remove_at(list, 1);
339
340    _verify_list(list, 2, _value_1, _value_3);
341
342    linked_list_free(list, NULL);
343 }
344 END_TEST
345
346 START_TEST (test_linked_list_remove_at_last)
347 {
348    linked_list_t *list = linked_list_new();
349    linked_list_add(list, _value_1);
350    linked_list_add(list, _value_2);
351    linked_list_add(list, _value_3);
352
353    linked_list_remove_at(list, 2);
354
355    _verify_list(list, 2, _value_1, _value_2);
356
357    linked_list_free(list, NULL);
358 }
359 END_TEST
360
361 START_TEST (test_linked_list_remove_at_only)
362 {
363    linked_list_t *list = linked_list_new();
364    linked_list_add(list, _value_1);
365
366    linked_list_remove_at(list, 0);
367
368    _verify_list(list, 0);
369
370    linked_list_free(list, NULL);
371 }
372 END_TEST
373
374 START_TEST (test_linked_list_remove_first_null)
375 {
376    ck_assert_ptr_null(linked_list_remove_first(NULL, _value_1));
377 }
378 END_TEST
379
380 START_TEST (test_linked_list_remove_first_empty)
381 {
382    linked_list_t *list = linked_list_new();
383    ck_assert_ptr_null(linked_list_remove_first(list, _value_1));
384
385    linked_list_free(list, NULL);
386 }
387 END_TEST
388
389 START_TEST (test_linked_list_remove_first_not_found)
390 {
391    linked_list_t *list = linked_list_new();
392    linked_list_add(list, _value_1);
393    linked_list_add(list, _value_2);
394    linked_list_add(list, _value_3);
395
396    ck_assert_ptr_null(linked_list_remove_first(list, "foo"));
397
398    _verify_list(list, 3, _value_1, _value_2, _value_3);
399
400    linked_list_free(list, NULL);
401 }
402 END_TEST
403
404 START_TEST (test_linked_list_remove_first_first)
405 {
406    linked_list_t *list = linked_list_new();
407    linked_list_add(list, _value_1);
408    linked_list_add(list, _value_2);
409    linked_list_add(list, _value_3);
410
411    ck_assert_ptr_eq(linked_list_remove_first(list, _value_1), _value_1);
412
413    _verify_list(list, 2, _value_2, _value_3);
414
415    linked_list_free(list, NULL);
416 }
417 END_TEST
418
419 START_TEST (test_linked_list_remove_first_middle)
420 {
421    linked_list_t *list = linked_list_new();
422    linked_list_add(list, _value_1);
423    linked_list_add(list, _value_2);
424    linked_list_add(list, _value_3);
425
426    ck_assert_ptr_eq(linked_list_remove_first(list, _value_2), _value_2);
427
428    _verify_list(list, 2, _value_1, _value_3);
429
430    linked_list_free(list, NULL);
431 }
432 END_TEST
433
434 START_TEST (test_linked_list_remove_first_last)
435 {
436    linked_list_t *list = linked_list_new();
437    linked_list_add(list, _value_1);
438    linked_list_add(list, _value_2);
439    linked_list_add(list, _value_3);
440
441    ck_assert_ptr_eq(linked_list_remove_first(list, _value_3), _value_3);
442
443    _verify_list(list, 2, _value_1, _value_2);
444
445    linked_list_free(list, NULL);
446 }
447 END_TEST
448
449 START_TEST (test_linked_list_remove_first_only)
450 {
451    linked_list_t *list = linked_list_new();
452    linked_list_add(list, _value_1);
453
454    ck_assert_ptr_eq(linked_list_remove_first(list, _value_1), _value_1);
455
456    _verify_list(list, 0);
457
458    linked_list_free(list, NULL);
459 }
460 END_TEST
461
462 START_TEST (test_linked_list_remove_first_multiple)
463 {
464    linked_list_t *list = linked_list_new();
465    linked_list_add(list, _value_1);
466    linked_list_add(list, _value_2);
467    linked_list_add(list, _value_1);
468
469    ck_assert_ptr_eq(linked_list_remove_first(list, _value_1), _value_1);
470
471    _verify_list(list, 2, _value_2, _value_1);
472
473    linked_list_free(list, NULL);
474 }
475 END_TEST
476
477 START_TEST (test_linked_list_remove_last_null)
478 {
479    ck_assert_ptr_null(linked_list_remove_last(NULL, _value_1));
480 }
481 END_TEST
482
483 START_TEST (test_linked_list_remove_last_empty)
484 {
485    linked_list_t *list = linked_list_new();
486    ck_assert_ptr_null(linked_list_remove_last(list, _value_1));
487
488    linked_list_free(list, NULL);
489 }
490 END_TEST
491
492 START_TEST (test_linked_list_remove_last_not_found)
493 {
494    linked_list_t *list = linked_list_new();
495    linked_list_add(list, _value_1);
496    linked_list_add(list, _value_2);
497    linked_list_add(list, _value_3);
498
499    ck_assert_ptr_null(linked_list_remove_last(list, "foo"));
500
501    _verify_list(list, 3, _value_1, _value_2, _value_3);
502
503    linked_list_free(list, NULL);
504 }
505 END_TEST
506
507 START_TEST (test_linked_list_remove_last_first)
508 {
509    linked_list_t *list = linked_list_new();
510    linked_list_add(list, _value_1);
511    linked_list_add(list, _value_2);
512    linked_list_add(list, _value_3);
513
514    ck_assert_ptr_eq(linked_list_remove_last(list, _value_1), _value_1);
515
516    _verify_list(list, 2, _value_2, _value_3);
517
518    linked_list_free(list, NULL);
519 }
520 END_TEST
521
522 START_TEST (test_linked_list_remove_last_middle)
523 {
524    linked_list_t *list = linked_list_new();
525    linked_list_add(list, _value_1);
526    linked_list_add(list, _value_2);
527    linked_list_add(list, _value_3);
528
529    ck_assert_ptr_eq(linked_list_remove_last(list, _value_2), _value_2);
530
531    _verify_list(list, 2, _value_1, _value_3);
532
533    linked_list_free(list, NULL);
534 }
535 END_TEST
536
537 START_TEST (test_linked_list_remove_last_last)
538 {
539    linked_list_t *list = linked_list_new();
540    linked_list_add(list, _value_1);
541    linked_list_add(list, _value_2);
542    linked_list_add(list, _value_3);
543
544    ck_assert_ptr_eq(linked_list_remove_last(list, _value_3), _value_3);
545
546    _verify_list(list, 2, _value_1, _value_2);
547
548    linked_list_free(list, NULL);
549 }
550 END_TEST
551
552 START_TEST (test_linked_list_remove_last_only)
553 {
554    linked_list_t *list = linked_list_new();
555    linked_list_add(list, _value_1);
556
557    ck_assert_ptr_eq(linked_list_remove_last(list, _value_1), _value_1);
558
559    _verify_list(list, 0);
560
561    linked_list_free(list, NULL);
562 }
563 END_TEST
564
565 START_TEST (test_linked_list_remove_last_multiple)
566 {
567    linked_list_t *list = linked_list_new();
568    linked_list_add(list, _value_1);
569    linked_list_add(list, _value_2);
570    linked_list_add(list, _value_1);
571
572    ck_assert_ptr_eq(linked_list_remove_last(list, _value_1), _value_1);
573
574    _verify_list(list, 2, _value_1, _value_2);
575
576    linked_list_free(list, NULL);
577 }
578 END_TEST
579
580 START_TEST (test_linked_list_remove_all_null)
581 {
582    ck_assert_ptr_null(linked_list_remove_all(NULL, _value_1));
583 }
584 END_TEST
585
586 START_TEST (test_linked_list_remove_all_empty)
587 {
588    linked_list_t *list = linked_list_new();
589    ck_assert_ptr_null(linked_list_remove_all(list, _value_1));
590
591    linked_list_free(list, NULL);
592 }
593 END_TEST
594
595 START_TEST (test_linked_list_remove_all_not_found)
596 {
597    linked_list_t *list = linked_list_new();
598    linked_list_add(list, _value_1);
599    linked_list_add(list, _value_2);
600    linked_list_add(list, _value_3);
601
602    ck_assert_ptr_null(linked_list_remove_all(list, "foo"));
603
604    _verify_list(list, 3, _value_1, _value_2, _value_3);
605
606    linked_list_free(list, NULL);
607 }
608 END_TEST
609
610 START_TEST (test_linked_list_remove_all_first)
611 {
612    linked_list_t *list = linked_list_new();
613    linked_list_add(list, _value_1);
614    linked_list_add(list, _value_2);
615    linked_list_add(list, _value_3);
616
617    ck_assert_ptr_eq(linked_list_remove_all(list, _value_1), _value_1);
618
619    _verify_list(list, 2, _value_2, _value_3);
620
621    linked_list_free(list, NULL);
622 }
623 END_TEST
624
625 START_TEST (test_linked_list_remove_all_middle)
626 {
627    linked_list_t *list = linked_list_new();
628    linked_list_add(list, _value_1);
629    linked_list_add(list, _value_2);
630    linked_list_add(list, _value_3);
631
632    ck_assert_ptr_eq(linked_list_remove_all(list, _value_2), _value_2);
633
634    _verify_list(list, 2, _value_1, _value_3);
635
636    linked_list_free(list, NULL);
637 }
638 END_TEST
639
640 START_TEST (test_linked_list_remove_all_last)
641 {
642    linked_list_t *list = linked_list_new();
643    linked_list_add(list, _value_1);
644    linked_list_add(list, _value_2);
645    linked_list_add(list, _value_3);
646
647    ck_assert_ptr_eq(linked_list_remove_all(list, _value_3), _value_3);
648
649    _verify_list(list, 2, _value_1, _value_2);
650
651    linked_list_free(list, NULL);
652 }
653 END_TEST
654
655 START_TEST (test_linked_list_remove_all_only)
656 {
657    linked_list_t *list = linked_list_new();
658    linked_list_add(list, _value_1);
659
660    ck_assert_ptr_eq(linked_list_remove_all(list, _value_1), _value_1);
661
662    _verify_list(list, 0);
663
664    linked_list_free(list, NULL);
665 }
666 END_TEST
667
668 START_TEST (test_linked_list_remove_all_multiple)
669 {
670    linked_list_t *list = linked_list_new();
671    linked_list_add(list, _value_1);
672    linked_list_add(list, _value_2);
673    linked_list_add(list, _value_1);
674
675    ck_assert_ptr_eq(linked_list_remove_all(list, _value_1), _value_1);
676
677    _verify_list(list, 1, _value_2);
678
679    linked_list_free(list, NULL);
680 }
681 END_TEST
682
683 bool _match_value_1(void *value)
684 {
685    return _value_1 == value;
686 }
687
688 bool _no_match(void *value)
689 {
690    return false;
691 }
692
693 START_TEST (test_linked_list_remove_first_matching_null)
694 {
695    ck_assert_ptr_null(linked_list_remove_first_matching(NULL, &_match_value_1));
696 }
697 END_TEST
698
699 START_TEST (test_linked_list_remove_first_matching_empty)
700 {
701    linked_list_t *list = linked_list_new();
702    ck_assert_ptr_null(linked_list_remove_first_matching(list, &_match_value_1));
703
704    linked_list_free(list, NULL);
705 }
706 END_TEST
707
708 START_TEST (test_linked_list_remove_first_matching_not_found)
709 {
710    linked_list_t *list = linked_list_new();
711    linked_list_add(list, _value_1);
712    linked_list_add(list, _value_2);
713    linked_list_add(list, _value_3);
714
715    ck_assert_ptr_null(linked_list_remove_first_matching(list, &_no_match));
716
717    _verify_list(list, 3, _value_1, _value_2, _value_3);
718
719    linked_list_free(list, NULL);
720 }
721 END_TEST
722
723 START_TEST (test_linked_list_remove_first_matching_first)
724 {
725    linked_list_t *list = linked_list_new();
726    linked_list_add(list, _value_1);
727    linked_list_add(list, _value_2);
728    linked_list_add(list, _value_3);
729
730    ck_assert_ptr_eq(linked_list_remove_first_matching(list, &_match_value_1), _value_1);
731
732    _verify_list(list, 2, _value_2, _value_3);
733
734    linked_list_free(list, NULL);
735 }
736 END_TEST
737
738 START_TEST (test_linked_list_remove_first_matching_middle)
739 {
740    linked_list_t *list = linked_list_new();
741    linked_list_add(list, _value_2);
742    linked_list_add(list, _value_1);
743    linked_list_add(list, _value_3);
744
745    ck_assert_ptr_eq(linked_list_remove_first_matching(list, &_match_value_1), _value_1);
746
747    _verify_list(list, 2, _value_2, _value_3);
748
749    linked_list_free(list, NULL);
750 }
751 END_TEST
752
753 START_TEST (test_linked_list_remove_first_matching_last)
754 {
755    linked_list_t *list = linked_list_new();
756    linked_list_add(list, _value_2);
757    linked_list_add(list, _value_3);
758    linked_list_add(list, _value_1);
759
760    ck_assert_ptr_eq(linked_list_remove_first_matching(list, &_match_value_1), _value_1);
761
762    _verify_list(list, 2, _value_2, _value_3);
763
764    linked_list_free(list, NULL);
765 }
766 END_TEST
767
768 START_TEST (test_linked_list_remove_first_matching_only)
769 {
770    linked_list_t *list = linked_list_new();
771    linked_list_add(list, _value_1);
772
773    ck_assert_ptr_eq(linked_list_remove_first_matching(list, &_match_value_1), _value_1);
774
775    _verify_list(list, 0);
776
777    linked_list_free(list, NULL);
778 }
779 END_TEST
780
781 START_TEST (test_linked_list_remove_first_matching_multiple)
782 {
783    linked_list_t *list = linked_list_new();
784    linked_list_add(list, _value_1);
785    linked_list_add(list, _value_2);
786    linked_list_add(list, _value_1);
787
788    ck_assert_ptr_eq(linked_list_remove_first_matching(list, &_match_value_1), _value_1);
789
790    _verify_list(list, 2, _value_2, _value_1);
791
792    linked_list_free(list, NULL);
793 }
794 END_TEST
795
796 START_TEST (test_linked_list_remove_last_matching_null)
797 {
798    ck_assert_ptr_null(linked_list_remove_last_matching(NULL, &_match_value_1));
799 }
800 END_TEST
801
802 START_TEST (test_linked_list_remove_last_matching_empty)
803 {
804    linked_list_t *list = linked_list_new();
805    ck_assert_ptr_null(linked_list_remove_last_matching(list, &_match_value_1));
806
807    linked_list_free(list, NULL);
808 }
809 END_TEST
810
811 START_TEST (test_linked_list_remove_last_matching_not_found)
812 {
813    linked_list_t *list = linked_list_new();
814    linked_list_add(list, _value_1);
815    linked_list_add(list, _value_2);
816    linked_list_add(list, _value_3);
817
818    ck_assert_ptr_null(linked_list_remove_last_matching(list, &_no_match));
819
820    _verify_list(list, 3, _value_1, _value_2, _value_3);
821
822    linked_list_free(list, NULL);
823 }
824 END_TEST
825
826 START_TEST (test_linked_list_remove_last_matching_first)
827 {
828    linked_list_t *list = linked_list_new();
829    linked_list_add(list, _value_1);
830    linked_list_add(list, _value_2);
831    linked_list_add(list, _value_3);
832
833    ck_assert_ptr_eq(linked_list_remove_last_matching(list, &_match_value_1), _value_1);
834
835    _verify_list(list, 2, _value_2, _value_3);
836
837    linked_list_free(list, NULL);
838 }
839 END_TEST
840
841 START_TEST (test_linked_list_remove_last_matching_middle)
842 {
843    linked_list_t *list = linked_list_new();
844    linked_list_add(list, _value_2);
845    linked_list_add(list, _value_1);
846    linked_list_add(list, _value_3);
847
848    ck_assert_ptr_eq(linked_list_remove_last_matching(list, &_match_value_1), _value_1);
849
850    _verify_list(list, 2, _value_2, _value_3);
851
852    linked_list_free(list, NULL);
853 }
854 END_TEST
855
856 START_TEST (test_linked_list_remove_last_matching_last)
857 {
858    linked_list_t *list = linked_list_new();
859    linked_list_add(list, _value_2);
860    linked_list_add(list, _value_3);
861    linked_list_add(list, _value_1);
862
863    ck_assert_ptr_eq(linked_list_remove_last_matching(list, &_match_value_1), _value_1);
864
865    _verify_list(list, 2, _value_2, _value_3);
866
867    linked_list_free(list, NULL);
868 }
869 END_TEST
870
871 START_TEST (test_linked_list_remove_last_matching_only)
872 {
873    linked_list_t *list = linked_list_new();
874    linked_list_add(list, _value_1);
875
876    ck_assert_ptr_eq(linked_list_remove_last_matching(list, &_match_value_1), _value_1);
877
878    _verify_list(list, 0);
879
880    linked_list_free(list, NULL);
881 }
882 END_TEST
883
884 START_TEST (test_linked_list_remove_last_matching_multiple)
885 {
886    linked_list_t *list = linked_list_new();
887    linked_list_add(list, _value_1);
888    linked_list_add(list, _value_2);
889    linked_list_add(list, _value_1);
890
891    ck_assert_ptr_eq(linked_list_remove_last_matching(list, &_match_value_1), _value_1);
892
893    _verify_list(list, 2, _value_1, _value_2);
894
895    linked_list_free(list, NULL);
896 }
897 END_TEST
898
899 START_TEST (test_linked_list_remove_all_matching_null)
900 {
901    linked_list_remove_all_matching(NULL, &_match_value_1);
902 }
903 END_TEST
904
905 START_TEST (test_linked_list_remove_all_matching_empty)
906 {
907    linked_list_t *list = linked_list_new();
908    linked_list_remove_all_matching(list, &_match_value_1);
909
910    linked_list_free(list, NULL);
911 }
912 END_TEST
913
914 START_TEST (test_linked_list_remove_all_matching_not_found)
915 {
916    linked_list_t *list = linked_list_new();
917    linked_list_add(list, _value_1);
918    linked_list_add(list, _value_2);
919    linked_list_add(list, _value_3);
920
921    linked_list_remove_all_matching(list, &_no_match);
922
923    _verify_list(list, 3, _value_1, _value_2, _value_3);
924
925    linked_list_free(list, NULL);
926 }
927 END_TEST
928
929 START_TEST (test_linked_list_remove_all_matching_first)
930 {
931    linked_list_t *list = linked_list_new();
932    linked_list_add(list, _value_1);
933    linked_list_add(list, _value_2);
934    linked_list_add(list, _value_3);
935
936    linked_list_remove_all_matching(list, &_match_value_1);
937
938    _verify_list(list, 2, _value_2, _value_3);
939
940    linked_list_free(list, NULL);
941 }
942 END_TEST
943
944 START_TEST (test_linked_list_remove_all_matching_middle)
945 {
946    linked_list_t *list = linked_list_new();
947    linked_list_add(list, _value_2);
948    linked_list_add(list, _value_1);
949    linked_list_add(list, _value_3);
950
951    linked_list_remove_all_matching(list, &_match_value_1);
952
953    _verify_list(list, 2, _value_2, _value_3);
954
955    linked_list_free(list, NULL);
956 }
957 END_TEST
958
959 START_TEST (test_linked_list_remove_all_matching_last)
960 {
961    linked_list_t *list = linked_list_new();
962    linked_list_add(list, _value_2);
963    linked_list_add(list, _value_3);
964    linked_list_add(list, _value_1);
965
966    linked_list_remove_all_matching(list, &_match_value_1);
967
968    _verify_list(list, 2, _value_2, _value_3);
969
970    linked_list_free(list, NULL);
971 }
972 END_TEST
973
974 START_TEST (test_linked_list_remove_all_matching_only)
975 {
976    linked_list_t *list = linked_list_new();
977    linked_list_add(list, _value_1);
978
979    linked_list_remove_all_matching(list, &_match_value_1);
980
981    _verify_list(list, 0);
982
983    linked_list_free(list, NULL);
984 }
985 END_TEST
986
987 START_TEST (test_linked_list_remove_all_matching_multiple)
988 {
989    linked_list_t *list = linked_list_new();
990    linked_list_add(list, _value_1);
991    linked_list_add(list, _value_2);
992    linked_list_add(list, _value_1);
993
994    linked_list_remove_all_matching(list, &_match_value_1);
995
996    _verify_list(list, 1, _value_2);
997
998    linked_list_free(list, NULL);
999 }
1000 END_TEST
1001
1002 START_TEST (test_linked_list_set_at_null)
1003 {
1004    ck_assert_int_eq(linked_list_set_at(NULL, 0, _value_1) == true, 0);
1005 }
1006 END_TEST
1007
1008 START_TEST (test_linked_list_set_at_empty)
1009 {
1010    linked_list_t *list = linked_list_new();
1011    ck_assert_int_eq(linked_list_set_at(list, 0, _value_1) == true, 0);
1012
1013    linked_list_free(list, NULL);
1014 }
1015 END_TEST
1016
1017 START_TEST (test_linked_list_set_at_invalid)
1018 {
1019    linked_list_t *list = linked_list_new();
1020    linked_list_add(list, _value_1);
1021    ck_assert_int_eq(linked_list_set_at(list, 1, _value_2) == true, 0);
1022
1023    linked_list_free(list, NULL);
1024 }
1025 END_TEST
1026
1027 static char *_replacement_value = "foo";
1028
1029 START_TEST (test_linked_list_set_at_first)
1030 {
1031    linked_list_t *list = linked_list_new();
1032    linked_list_add(list, _value_1);
1033    linked_list_add(list, _value_2);
1034    linked_list_add(list, _value_3);
1035
1036    ck_assert_int_eq(linked_list_set_at(list, 0, _replacement_value) == false, 0);
1037
1038    _verify_list(list, 3, _replacement_value, _value_2, _value_3);
1039
1040    linked_list_free(list, NULL);
1041 }
1042 END_TEST
1043
1044 START_TEST (test_linked_list_set_at_middle)
1045 {
1046    linked_list_t *list = linked_list_new();
1047    linked_list_add(list, _value_1);
1048    linked_list_add(list, _value_2);
1049    linked_list_add(list, _value_3);
1050
1051    ck_assert_int_eq(linked_list_set_at(list, 1, _replacement_value) == false, 0);
1052
1053    _verify_list(list, 3, _value_1, _replacement_value, _value_3);
1054
1055    linked_list_free(list, NULL);
1056 }
1057 END_TEST
1058
1059 START_TEST (test_linked_list_set_at_last)
1060 {
1061    linked_list_t *list = linked_list_new();
1062    linked_list_add(list, _value_1);
1063    linked_list_add(list, _value_2);
1064    linked_list_add(list, _value_3);
1065
1066    ck_assert_int_eq(linked_list_set_at(list, 2, _replacement_value) == false, 0);
1067
1068    _verify_list(list, 3, _value_1, _value_2, _replacement_value);
1069
1070    linked_list_free(list, NULL);
1071 }
1072 END_TEST
1073
1074 START_TEST (test_linked_list_iterator_remove_null)
1075 {
1076    ck_assert_ptr_null(linked_list_iterator_remove(NULL));
1077 }
1078 END_TEST
1079
1080 START_TEST (test_linked_list_iterator_remove_first)
1081 {
1082    linked_list_t *list;
1083    linked_list_iterator_t *iterator;
1084
1085    list = linked_list_new();
1086    linked_list_add(list, _value_1);
1087    linked_list_add(list, _value_2);
1088    linked_list_add(list, _value_3);
1089
1090    iterator = linked_list_iterator(list, true);
1091    iterator = linked_list_iterator_remove(iterator);
1092
1093    ck_assert_ptr_nonnull(iterator);
1094    ck_assert_ptr_eq(linked_list_iterator_value(iterator), _value_2);
1095    _verify_list(list, 2, _value_2, _value_3);
1096
1097    linked_list_iterator_free(iterator);
1098    linked_list_free(list, NULL);
1099 }
1100 END_TEST
1101
1102 START_TEST (test_linked_list_iterator_remove_middle)
1103 {
1104    linked_list_t *list;
1105    linked_list_iterator_t *iterator;
1106
1107    list = linked_list_new();
1108    linked_list_add(list, _value_1);
1109    linked_list_add(list, _value_2);
1110    linked_list_add(list, _value_3);
1111
1112    iterator = linked_list_iterator(list, true);
1113    iterator = linked_list_iterator_next(iterator);
1114    iterator = linked_list_iterator_remove(iterator);
1115
1116    ck_assert_ptr_nonnull(iterator);
1117    ck_assert_ptr_eq(linked_list_iterator_value(iterator), _value_3);
1118    _verify_list(list, 2, _value_1, _value_3);
1119
1120    linked_list_iterator_free(iterator);
1121    linked_list_free(list, NULL);
1122 }
1123 END_TEST
1124
1125 START_TEST (test_linked_list_iterator_remove_last)
1126 {
1127    linked_list_t *list;
1128    linked_list_iterator_t *iterator;
1129
1130    list = linked_list_new();
1131    linked_list_add(list, _value_1);
1132    linked_list_add(list, _value_2);
1133    linked_list_add(list, _value_3);
1134
1135    iterator = linked_list_iterator(list, true);
1136    iterator = linked_list_iterator_next(iterator);
1137    iterator = linked_list_iterator_next(iterator);
1138    iterator = linked_list_iterator_remove(iterator);
1139
1140    ck_assert_ptr_null(iterator);
1141    _verify_list(list, 2, _value_1, _value_2);
1142
1143    linked_list_free(list, NULL);
1144 }
1145 END_TEST
1146
1147 START_TEST (test_linked_list_iterator_free_null)
1148 {
1149    linked_list_iterator_free(NULL);
1150 }
1151 END_TEST
1152
1153 static size_t _foreach_count;
1154 static void _foreach_fn(size_t index, void *value)
1155 {
1156    _foreach_count++;
1157 }
1158
1159 START_TEST (test_linked_list_foreach_null_list)
1160 {
1161    linked_list_foreach(NULL, _foreach_fn);
1162 }
1163 END_TEST
1164
1165 START_TEST (test_linked_list_foreach_null_fn)
1166 {
1167    linked_list_t *list = linked_list_new();
1168    linked_list_add(list, _value_1);
1169    linked_list_add(list, _value_2);
1170    linked_list_add(list, _value_3);
1171
1172    linked_list_foreach(list, NULL);
1173
1174    linked_list_free(list, NULL);
1175 }
1176 END_TEST
1177
1178 START_TEST (test_linked_list_foreach_valid)
1179 {
1180    linked_list_t *list = linked_list_new();
1181    linked_list_add(list, _value_1);
1182    linked_list_add(list, _value_2);
1183    linked_list_add(list, _value_3);
1184
1185    _foreach_count = 0;
1186    linked_list_foreach(list, &_foreach_fn);
1187    ck_assert_uint_eq(3, _foreach_count);
1188
1189    linked_list_free(list, NULL);
1190 }
1191
1192 Suite *create_suite(void)
1193 {
1194    Suite *s = suite_create(SUITE_NAME);
1195
1196    TCase *tc_core = tcase_create("Core");
1197    tcase_add_test(tc_core, test_linked_list_create);
1198    tcase_add_test(tc_core, test_linked_list_free);
1199    tcase_add_test(tc_core, test_linked_list_free_with_fn);
1200    tcase_add_test(tc_core, test_linked_list_add);
1201    tcase_add_test(tc_core, test_linked_list_insert_empty);
1202    tcase_add_test(tc_core, test_linked_list_insert_first);
1203    tcase_add_test(tc_core, test_linked_list_insert_middle);
1204    tcase_add_test(tc_core, test_linked_list_insert_last);
1205    tcase_add_test(tc_core, test_linked_list_insert_invalid);
1206    tcase_add_test(tc_core, test_linked_list_insert_null);
1207    tcase_add_test(tc_core, test_linked_list_get_invalid);
1208    tcase_add_test(tc_core, test_linked_list_get_null);
1209    tcase_add_test(tc_core, test_linked_list_get_first_matching_null);
1210    tcase_add_test(tc_core, test_linked_list_get_first_matching_function_null);
1211    tcase_add_test(tc_core, test_linked_list_get_first_matching_no_match);
1212    tcase_add_test(tc_core, test_linked_list_get_first_matching_with_match);
1213    tcase_add_test(tc_core, test_linked_list_get_last_matching_null);
1214    tcase_add_test(tc_core, test_linked_list_get_last_matching_function_null);
1215    tcase_add_test(tc_core, test_linked_list_get_last_matching_no_match);
1216    tcase_add_test(tc_core, test_linked_list_get_last_matching_with_match);
1217    tcase_add_test(tc_core, test_linked_list_remove_at_null);
1218    tcase_add_test(tc_core, test_linked_list_remove_at_empty);
1219    tcase_add_test(tc_core, test_linked_list_remove_at_invalid);
1220    tcase_add_test(tc_core, test_linked_list_remove_at_first);
1221    tcase_add_test(tc_core, test_linked_list_remove_at_middle);
1222    tcase_add_test(tc_core, test_linked_list_remove_at_last);
1223    tcase_add_test(tc_core, test_linked_list_remove_at_only);
1224    tcase_add_test(tc_core, test_linked_list_remove_first_null);
1225    tcase_add_test(tc_core, test_linked_list_remove_first_empty);
1226    tcase_add_test(tc_core, test_linked_list_remove_first_not_found);
1227    tcase_add_test(tc_core, test_linked_list_remove_first_first);
1228    tcase_add_test(tc_core, test_linked_list_remove_first_middle);
1229    tcase_add_test(tc_core, test_linked_list_remove_first_last);
1230    tcase_add_test(tc_core, test_linked_list_remove_first_only);
1231    tcase_add_test(tc_core, test_linked_list_remove_first_multiple);
1232    tcase_add_test(tc_core, test_linked_list_remove_last_null);
1233    tcase_add_test(tc_core, test_linked_list_remove_last_empty);
1234    tcase_add_test(tc_core, test_linked_list_remove_last_not_found);
1235    tcase_add_test(tc_core, test_linked_list_remove_last_first);
1236    tcase_add_test(tc_core, test_linked_list_remove_last_middle);
1237    tcase_add_test(tc_core, test_linked_list_remove_last_last);
1238    tcase_add_test(tc_core, test_linked_list_remove_last_only);
1239    tcase_add_test(tc_core, test_linked_list_remove_last_multiple);
1240    tcase_add_test(tc_core, test_linked_list_remove_all_null);
1241    tcase_add_test(tc_core, test_linked_list_remove_all_empty);
1242    tcase_add_test(tc_core, test_linked_list_remove_all_not_found);
1243    tcase_add_test(tc_core, test_linked_list_remove_all_first);
1244    tcase_add_test(tc_core, test_linked_list_remove_all_middle);
1245    tcase_add_test(tc_core, test_linked_list_remove_all_last);
1246    tcase_add_test(tc_core, test_linked_list_remove_all_only);
1247    tcase_add_test(tc_core, test_linked_list_remove_all_multiple);
1248    tcase_add_test(tc_core, test_linked_list_remove_first_matching_null);
1249    tcase_add_test(tc_core, test_linked_list_remove_first_matching_empty);
1250    tcase_add_test(tc_core, test_linked_list_remove_first_matching_not_found);
1251    tcase_add_test(tc_core, test_linked_list_remove_first_matching_first);
1252    tcase_add_test(tc_core, test_linked_list_remove_first_matching_middle);
1253    tcase_add_test(tc_core, test_linked_list_remove_first_matching_last);
1254    tcase_add_test(tc_core, test_linked_list_remove_first_matching_only);
1255    tcase_add_test(tc_core, test_linked_list_remove_first_matching_multiple);
1256    tcase_add_test(tc_core, test_linked_list_remove_last_matching_null);
1257    tcase_add_test(tc_core, test_linked_list_remove_last_matching_empty);
1258    tcase_add_test(tc_core, test_linked_list_remove_last_matching_not_found);
1259    tcase_add_test(tc_core, test_linked_list_remove_last_matching_first);
1260    tcase_add_test(tc_core, test_linked_list_remove_last_matching_middle);
1261    tcase_add_test(tc_core, test_linked_list_remove_last_matching_last);
1262    tcase_add_test(tc_core, test_linked_list_remove_last_matching_only);
1263    tcase_add_test(tc_core, test_linked_list_remove_last_matching_multiple);
1264    tcase_add_test(tc_core, test_linked_list_remove_all_matching_null);
1265    tcase_add_test(tc_core, test_linked_list_remove_all_matching_empty);
1266    tcase_add_test(tc_core, test_linked_list_remove_all_matching_not_found);
1267    tcase_add_test(tc_core, test_linked_list_remove_all_matching_first);
1268    tcase_add_test(tc_core, test_linked_list_remove_all_matching_middle);
1269    tcase_add_test(tc_core, test_linked_list_remove_all_matching_last);
1270    tcase_add_test(tc_core, test_linked_list_remove_all_matching_only);
1271    tcase_add_test(tc_core, test_linked_list_remove_all_matching_multiple);
1272    tcase_add_test(tc_core, test_linked_list_set_at_null);
1273    tcase_add_test(tc_core, test_linked_list_set_at_empty);
1274    tcase_add_test(tc_core, test_linked_list_set_at_invalid);
1275    tcase_add_test(tc_core, test_linked_list_set_at_first);
1276    tcase_add_test(tc_core, test_linked_list_set_at_middle);
1277    tcase_add_test(tc_core, test_linked_list_set_at_last);
1278    tcase_add_test(tc_core, test_linked_list_iterator_remove_null);
1279    tcase_add_test(tc_core, test_linked_list_iterator_remove_first);
1280    tcase_add_test(tc_core, test_linked_list_iterator_remove_middle);
1281    tcase_add_test(tc_core, test_linked_list_iterator_remove_last);
1282    tcase_add_test(tc_core, test_linked_list_iterator_free_null);
1283    tcase_add_test(tc_core, test_linked_list_foreach_null_list);
1284    tcase_add_test(tc_core, test_linked_list_foreach_null_fn);
1285    tcase_add_test(tc_core, test_linked_list_foreach_valid);
1286    suite_add_tcase(s, tc_core);
1287
1288    return s;
1289 }
1290
1291 int main(void)
1292 {
1293    int num_fail;
1294    Suite *s = create_suite();
1295    SRunner *sr = srunner_create(s);
1296    srunner_run_all(sr, CK_NORMAL);
1297    num_fail = srunner_ntests_failed(sr);
1298    srunner_free(sr);
1299    return (num_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1300 }