1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (test_linked_list.c).
5 * ---------------------------------------------------------------------------------------
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:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
27 #include <lists/linked_list.h>
29 #define SUITE_NAME "Linked List"
31 static char *_value_1 = "value1";
32 static char *_value_2 = "value2";
33 static char *_value_3 = "value3";
35 START_TEST (test_linked_list_create)
37 linked_list_t *list = linked_list_new();
38 ck_assert_ptr_nonnull(list);
39 linked_list_free(list, NULL);
43 START_TEST (test_linked_list_free)
45 linked_list_t *queue = linked_list_new();
46 linked_list_free(queue, NULL);
47 linked_list_free(NULL, NULL);
51 static int _free_alloced_value_count;
52 static void _free_alloced_value(void *value)
54 _free_alloced_value_count++;
58 START_TEST (test_linked_list_free_with_fn)
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));
65 _free_alloced_value_count = 0;
66 linked_list_free(list, &_free_alloced_value);
68 ck_assert_int_eq(3, _free_alloced_value_count);
72 static void _verify_list(linked_list_t *list, int size, ...)
77 linked_list_iterator_t *iterator;
79 values = (void **)malloc(size * sizeof(void *));
81 ck_assert_int_eq(linked_list_size(list), size);
83 va_start(values_list, size);
84 for (i = 0; i < size; i++)
86 values[i] = va_arg(values_list, void *);
87 ck_assert_ptr_eq(values[i], linked_list_get(list, i));
91 iterator = linked_list_iterator(list, true);
92 for (i = 0; i < size; i++)
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);
98 ck_assert_ptr_null(iterator);
100 iterator = linked_list_iterator(list, false);
101 for (i = size - 1; i >= 0; i--)
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);
107 ck_assert_ptr_null(iterator);
112 START_TEST (test_linked_list_add)
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);
119 _verify_list(list, 3, _value_1, _value_2, _value_3);
121 linked_list_free(list, NULL);
125 START_TEST (test_linked_list_insert_empty)
127 linked_list_t *list = linked_list_new();
128 linked_list_insert(list, 0, _value_1);
130 ck_assert_int_eq(linked_list_size(list), 1);
131 ck_assert_ptr_eq(linked_list_get(list, 0), _value_1);
133 linked_list_free(list, NULL);
137 START_TEST (test_linked_list_insert_first)
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);
144 _verify_list(list, 3, _value_1, _value_2, _value_3);
146 linked_list_free(list, NULL);
150 START_TEST (test_linked_list_insert_middle)
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);
157 _verify_list(list, 3, _value_1, _value_2, _value_3);
159 linked_list_free(list, NULL);
163 START_TEST (test_linked_list_insert_last)
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);
170 _verify_list(list, 3, _value_1, _value_2, _value_3);
172 linked_list_free(list, NULL);
176 START_TEST (test_linked_list_insert_invalid)
178 linked_list_t *list = linked_list_new();
179 linked_list_insert(list, 2, _value_1);
181 ck_assert_int_eq(linked_list_size(list), 0);
183 linked_list_free(list, NULL);
187 START_TEST (test_linked_list_insert_null)
189 linked_list_insert(NULL, 0, _value_1);
193 START_TEST (test_linked_list_get_invalid)
195 linked_list_t *list = linked_list_new();
196 ck_assert_ptr_null(linked_list_get(list, 2));
198 linked_list_free(list, NULL);
202 START_TEST (test_linked_list_get_null)
204 ck_assert_ptr_null(linked_list_get(NULL, 0));
208 START_TEST (test_linked_list_get_first_matching_null)
210 ck_assert_ptr_null(linked_list_get_first_matching(NULL, NULL, NULL));
214 START_TEST (test_linked_list_get_first_matching_function_null)
216 linked_list_t *list = linked_list_new();
217 ck_assert_ptr_null(linked_list_get_first_matching(list, NULL, NULL));
219 linked_list_free(list, NULL);
222 bool _matches_function(void *value, void *state)
224 ck_assert_ptr_eq(_value_1, state);
225 return value == _value_2;
228 START_TEST (test_linked_list_get_first_matching_no_match)
230 linked_list_t *list = linked_list_new();
231 ck_assert_ptr_null(linked_list_get_first_matching(list, &_matches_function, _value_1));
233 linked_list_free(list, NULL);
237 START_TEST (test_linked_list_get_first_matching_with_match)
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);
244 ck_assert_ptr_eq(_value_2, linked_list_get_first_matching(list, &_matches_function, _value_1));
246 linked_list_free(list, NULL);
250 START_TEST (test_linked_list_get_last_matching_null)
252 ck_assert_ptr_null(linked_list_get_last_matching(NULL, NULL, NULL));
256 START_TEST (test_linked_list_get_last_matching_function_null)
258 linked_list_t *list = linked_list_new();
259 ck_assert_ptr_null(linked_list_get_last_matching(list, NULL, NULL));
261 linked_list_free(list, NULL);
264 START_TEST (test_linked_list_get_last_matching_no_match)
266 linked_list_t *list = linked_list_new();
267 ck_assert_ptr_null(linked_list_get_last_matching(list, &_matches_function, _value_1));
269 linked_list_free(list, NULL);
273 START_TEST (test_linked_list_get_last_matching_with_match)
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);
280 ck_assert_ptr_eq(_value_2, linked_list_get_last_matching(list, &_matches_function, _value_1));
282 linked_list_free(list, NULL);
286 START_TEST (test_linked_list_remove_at_null)
288 ck_assert_ptr_null(linked_list_remove_at(NULL, 0));
292 START_TEST (test_linked_list_remove_at_empty)
294 linked_list_t *list = linked_list_new();
295 ck_assert_ptr_null(linked_list_remove_at(list, 0));
297 linked_list_free(list, NULL);
301 START_TEST (test_linked_list_remove_at_invalid)
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);
308 linked_list_remove_at(list, 3);
310 _verify_list(list, 3, _value_1, _value_2, _value_3);
312 linked_list_free(list, NULL);
316 START_TEST (test_linked_list_remove_at_first)
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);
323 linked_list_remove_at(list, 0);
325 _verify_list(list, 2, _value_2, _value_3);
327 linked_list_free(list, NULL);
331 START_TEST (test_linked_list_remove_at_middle)
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);
338 linked_list_remove_at(list, 1);
340 _verify_list(list, 2, _value_1, _value_3);
342 linked_list_free(list, NULL);
346 START_TEST (test_linked_list_remove_at_last)
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);
353 linked_list_remove_at(list, 2);
355 _verify_list(list, 2, _value_1, _value_2);
357 linked_list_free(list, NULL);
361 START_TEST (test_linked_list_remove_at_only)
363 linked_list_t *list = linked_list_new();
364 linked_list_add(list, _value_1);
366 linked_list_remove_at(list, 0);
368 _verify_list(list, 0);
370 linked_list_free(list, NULL);
374 START_TEST (test_linked_list_remove_first_null)
376 ck_assert_ptr_null(linked_list_remove_first(NULL, _value_1));
380 START_TEST (test_linked_list_remove_first_empty)
382 linked_list_t *list = linked_list_new();
383 ck_assert_ptr_null(linked_list_remove_first(list, _value_1));
385 linked_list_free(list, NULL);
389 START_TEST (test_linked_list_remove_first_not_found)
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);
396 ck_assert_ptr_null(linked_list_remove_first(list, "foo"));
398 _verify_list(list, 3, _value_1, _value_2, _value_3);
400 linked_list_free(list, NULL);
404 START_TEST (test_linked_list_remove_first_first)
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);
411 ck_assert_ptr_eq(linked_list_remove_first(list, _value_1), _value_1);
413 _verify_list(list, 2, _value_2, _value_3);
415 linked_list_free(list, NULL);
419 START_TEST (test_linked_list_remove_first_middle)
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);
426 ck_assert_ptr_eq(linked_list_remove_first(list, _value_2), _value_2);
428 _verify_list(list, 2, _value_1, _value_3);
430 linked_list_free(list, NULL);
434 START_TEST (test_linked_list_remove_first_last)
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);
441 ck_assert_ptr_eq(linked_list_remove_first(list, _value_3), _value_3);
443 _verify_list(list, 2, _value_1, _value_2);
445 linked_list_free(list, NULL);
449 START_TEST (test_linked_list_remove_first_only)
451 linked_list_t *list = linked_list_new();
452 linked_list_add(list, _value_1);
454 ck_assert_ptr_eq(linked_list_remove_first(list, _value_1), _value_1);
456 _verify_list(list, 0);
458 linked_list_free(list, NULL);
462 START_TEST (test_linked_list_remove_first_multiple)
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);
469 ck_assert_ptr_eq(linked_list_remove_first(list, _value_1), _value_1);
471 _verify_list(list, 2, _value_2, _value_1);
473 linked_list_free(list, NULL);
477 START_TEST (test_linked_list_remove_last_null)
479 ck_assert_ptr_null(linked_list_remove_last(NULL, _value_1));
483 START_TEST (test_linked_list_remove_last_empty)
485 linked_list_t *list = linked_list_new();
486 ck_assert_ptr_null(linked_list_remove_last(list, _value_1));
488 linked_list_free(list, NULL);
492 START_TEST (test_linked_list_remove_last_not_found)
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);
499 ck_assert_ptr_null(linked_list_remove_last(list, "foo"));
501 _verify_list(list, 3, _value_1, _value_2, _value_3);
503 linked_list_free(list, NULL);
507 START_TEST (test_linked_list_remove_last_first)
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);
514 ck_assert_ptr_eq(linked_list_remove_last(list, _value_1), _value_1);
516 _verify_list(list, 2, _value_2, _value_3);
518 linked_list_free(list, NULL);
522 START_TEST (test_linked_list_remove_last_middle)
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);
529 ck_assert_ptr_eq(linked_list_remove_last(list, _value_2), _value_2);
531 _verify_list(list, 2, _value_1, _value_3);
533 linked_list_free(list, NULL);
537 START_TEST (test_linked_list_remove_last_last)
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);
544 ck_assert_ptr_eq(linked_list_remove_last(list, _value_3), _value_3);
546 _verify_list(list, 2, _value_1, _value_2);
548 linked_list_free(list, NULL);
552 START_TEST (test_linked_list_remove_last_only)
554 linked_list_t *list = linked_list_new();
555 linked_list_add(list, _value_1);
557 ck_assert_ptr_eq(linked_list_remove_last(list, _value_1), _value_1);
559 _verify_list(list, 0);
561 linked_list_free(list, NULL);
565 START_TEST (test_linked_list_remove_last_multiple)
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);
572 ck_assert_ptr_eq(linked_list_remove_last(list, _value_1), _value_1);
574 _verify_list(list, 2, _value_1, _value_2);
576 linked_list_free(list, NULL);
580 START_TEST (test_linked_list_remove_all_null)
582 ck_assert_ptr_null(linked_list_remove_all(NULL, _value_1));
586 START_TEST (test_linked_list_remove_all_empty)
588 linked_list_t *list = linked_list_new();
589 ck_assert_ptr_null(linked_list_remove_all(list, _value_1));
591 linked_list_free(list, NULL);
595 START_TEST (test_linked_list_remove_all_not_found)
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);
602 ck_assert_ptr_null(linked_list_remove_all(list, "foo"));
604 _verify_list(list, 3, _value_1, _value_2, _value_3);
606 linked_list_free(list, NULL);
610 START_TEST (test_linked_list_remove_all_first)
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);
617 ck_assert_ptr_eq(linked_list_remove_all(list, _value_1), _value_1);
619 _verify_list(list, 2, _value_2, _value_3);
621 linked_list_free(list, NULL);
625 START_TEST (test_linked_list_remove_all_middle)
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);
632 ck_assert_ptr_eq(linked_list_remove_all(list, _value_2), _value_2);
634 _verify_list(list, 2, _value_1, _value_3);
636 linked_list_free(list, NULL);
640 START_TEST (test_linked_list_remove_all_last)
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);
647 ck_assert_ptr_eq(linked_list_remove_all(list, _value_3), _value_3);
649 _verify_list(list, 2, _value_1, _value_2);
651 linked_list_free(list, NULL);
655 START_TEST (test_linked_list_remove_all_only)
657 linked_list_t *list = linked_list_new();
658 linked_list_add(list, _value_1);
660 ck_assert_ptr_eq(linked_list_remove_all(list, _value_1), _value_1);
662 _verify_list(list, 0);
664 linked_list_free(list, NULL);
668 START_TEST (test_linked_list_remove_all_multiple)
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);
675 ck_assert_ptr_eq(linked_list_remove_all(list, _value_1), _value_1);
677 _verify_list(list, 1, _value_2);
679 linked_list_free(list, NULL);
683 bool _match_value_1(void *value)
685 return _value_1 == value;
688 bool _no_match(void *value)
693 START_TEST (test_linked_list_remove_first_matching_null)
695 ck_assert_ptr_null(linked_list_remove_first_matching(NULL, &_match_value_1));
699 START_TEST (test_linked_list_remove_first_matching_empty)
701 linked_list_t *list = linked_list_new();
702 ck_assert_ptr_null(linked_list_remove_first_matching(list, &_match_value_1));
704 linked_list_free(list, NULL);
708 START_TEST (test_linked_list_remove_first_matching_not_found)
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);
715 ck_assert_ptr_null(linked_list_remove_first_matching(list, &_no_match));
717 _verify_list(list, 3, _value_1, _value_2, _value_3);
719 linked_list_free(list, NULL);
723 START_TEST (test_linked_list_remove_first_matching_first)
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);
730 ck_assert_ptr_eq(linked_list_remove_first_matching(list, &_match_value_1), _value_1);
732 _verify_list(list, 2, _value_2, _value_3);
734 linked_list_free(list, NULL);
738 START_TEST (test_linked_list_remove_first_matching_middle)
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);
745 ck_assert_ptr_eq(linked_list_remove_first_matching(list, &_match_value_1), _value_1);
747 _verify_list(list, 2, _value_2, _value_3);
749 linked_list_free(list, NULL);
753 START_TEST (test_linked_list_remove_first_matching_last)
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);
760 ck_assert_ptr_eq(linked_list_remove_first_matching(list, &_match_value_1), _value_1);
762 _verify_list(list, 2, _value_2, _value_3);
764 linked_list_free(list, NULL);
768 START_TEST (test_linked_list_remove_first_matching_only)
770 linked_list_t *list = linked_list_new();
771 linked_list_add(list, _value_1);
773 ck_assert_ptr_eq(linked_list_remove_first_matching(list, &_match_value_1), _value_1);
775 _verify_list(list, 0);
777 linked_list_free(list, NULL);
781 START_TEST (test_linked_list_remove_first_matching_multiple)
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);
788 ck_assert_ptr_eq(linked_list_remove_first_matching(list, &_match_value_1), _value_1);
790 _verify_list(list, 2, _value_2, _value_1);
792 linked_list_free(list, NULL);
796 START_TEST (test_linked_list_remove_last_matching_null)
798 ck_assert_ptr_null(linked_list_remove_last_matching(NULL, &_match_value_1));
802 START_TEST (test_linked_list_remove_last_matching_empty)
804 linked_list_t *list = linked_list_new();
805 ck_assert_ptr_null(linked_list_remove_last_matching(list, &_match_value_1));
807 linked_list_free(list, NULL);
811 START_TEST (test_linked_list_remove_last_matching_not_found)
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);
818 ck_assert_ptr_null(linked_list_remove_last_matching(list, &_no_match));
820 _verify_list(list, 3, _value_1, _value_2, _value_3);
822 linked_list_free(list, NULL);
826 START_TEST (test_linked_list_remove_last_matching_first)
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);
833 ck_assert_ptr_eq(linked_list_remove_last_matching(list, &_match_value_1), _value_1);
835 _verify_list(list, 2, _value_2, _value_3);
837 linked_list_free(list, NULL);
841 START_TEST (test_linked_list_remove_last_matching_middle)
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);
848 ck_assert_ptr_eq(linked_list_remove_last_matching(list, &_match_value_1), _value_1);
850 _verify_list(list, 2, _value_2, _value_3);
852 linked_list_free(list, NULL);
856 START_TEST (test_linked_list_remove_last_matching_last)
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);
863 ck_assert_ptr_eq(linked_list_remove_last_matching(list, &_match_value_1), _value_1);
865 _verify_list(list, 2, _value_2, _value_3);
867 linked_list_free(list, NULL);
871 START_TEST (test_linked_list_remove_last_matching_only)
873 linked_list_t *list = linked_list_new();
874 linked_list_add(list, _value_1);
876 ck_assert_ptr_eq(linked_list_remove_last_matching(list, &_match_value_1), _value_1);
878 _verify_list(list, 0);
880 linked_list_free(list, NULL);
884 START_TEST (test_linked_list_remove_last_matching_multiple)
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);
891 ck_assert_ptr_eq(linked_list_remove_last_matching(list, &_match_value_1), _value_1);
893 _verify_list(list, 2, _value_1, _value_2);
895 linked_list_free(list, NULL);
899 START_TEST (test_linked_list_remove_all_matching_null)
901 linked_list_remove_all_matching(NULL, &_match_value_1);
905 START_TEST (test_linked_list_remove_all_matching_empty)
907 linked_list_t *list = linked_list_new();
908 linked_list_remove_all_matching(list, &_match_value_1);
910 linked_list_free(list, NULL);
914 START_TEST (test_linked_list_remove_all_matching_not_found)
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);
921 linked_list_remove_all_matching(list, &_no_match);
923 _verify_list(list, 3, _value_1, _value_2, _value_3);
925 linked_list_free(list, NULL);
929 START_TEST (test_linked_list_remove_all_matching_first)
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);
936 linked_list_remove_all_matching(list, &_match_value_1);
938 _verify_list(list, 2, _value_2, _value_3);
940 linked_list_free(list, NULL);
944 START_TEST (test_linked_list_remove_all_matching_middle)
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);
951 linked_list_remove_all_matching(list, &_match_value_1);
953 _verify_list(list, 2, _value_2, _value_3);
955 linked_list_free(list, NULL);
959 START_TEST (test_linked_list_remove_all_matching_last)
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);
966 linked_list_remove_all_matching(list, &_match_value_1);
968 _verify_list(list, 2, _value_2, _value_3);
970 linked_list_free(list, NULL);
974 START_TEST (test_linked_list_remove_all_matching_only)
976 linked_list_t *list = linked_list_new();
977 linked_list_add(list, _value_1);
979 linked_list_remove_all_matching(list, &_match_value_1);
981 _verify_list(list, 0);
983 linked_list_free(list, NULL);
987 START_TEST (test_linked_list_remove_all_matching_multiple)
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);
994 linked_list_remove_all_matching(list, &_match_value_1);
996 _verify_list(list, 1, _value_2);
998 linked_list_free(list, NULL);
1002 START_TEST (test_linked_list_set_at_null)
1004 ck_assert_int_eq(linked_list_set_at(NULL, 0, _value_1) == true, 0);
1008 START_TEST (test_linked_list_set_at_empty)
1010 linked_list_t *list = linked_list_new();
1011 ck_assert_int_eq(linked_list_set_at(list, 0, _value_1) == true, 0);
1013 linked_list_free(list, NULL);
1017 START_TEST (test_linked_list_set_at_invalid)
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);
1023 linked_list_free(list, NULL);
1027 static char *_replacement_value = "foo";
1029 START_TEST (test_linked_list_set_at_first)
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);
1036 ck_assert_int_eq(linked_list_set_at(list, 0, _replacement_value) == false, 0);
1038 _verify_list(list, 3, _replacement_value, _value_2, _value_3);
1040 linked_list_free(list, NULL);
1044 START_TEST (test_linked_list_set_at_middle)
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);
1051 ck_assert_int_eq(linked_list_set_at(list, 1, _replacement_value) == false, 0);
1053 _verify_list(list, 3, _value_1, _replacement_value, _value_3);
1055 linked_list_free(list, NULL);
1059 START_TEST (test_linked_list_set_at_last)
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);
1066 ck_assert_int_eq(linked_list_set_at(list, 2, _replacement_value) == false, 0);
1068 _verify_list(list, 3, _value_1, _value_2, _replacement_value);
1070 linked_list_free(list, NULL);
1074 START_TEST (test_linked_list_iterator_remove_null)
1076 ck_assert_ptr_null(linked_list_iterator_remove(NULL));
1080 START_TEST (test_linked_list_iterator_remove_first)
1082 linked_list_t *list;
1083 linked_list_iterator_t *iterator;
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);
1090 iterator = linked_list_iterator(list, true);
1091 iterator = linked_list_iterator_remove(iterator);
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);
1097 linked_list_iterator_free(iterator);
1098 linked_list_free(list, NULL);
1102 START_TEST (test_linked_list_iterator_remove_middle)
1104 linked_list_t *list;
1105 linked_list_iterator_t *iterator;
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);
1112 iterator = linked_list_iterator(list, true);
1113 iterator = linked_list_iterator_next(iterator);
1114 iterator = linked_list_iterator_remove(iterator);
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);
1120 linked_list_iterator_free(iterator);
1121 linked_list_free(list, NULL);
1125 START_TEST (test_linked_list_iterator_remove_last)
1127 linked_list_t *list;
1128 linked_list_iterator_t *iterator;
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);
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);
1140 ck_assert_ptr_null(iterator);
1141 _verify_list(list, 2, _value_1, _value_2);
1143 linked_list_free(list, NULL);
1147 START_TEST (test_linked_list_iterator_free_null)
1149 linked_list_iterator_free(NULL);
1153 static size_t _foreach_count;
1154 static void _foreach_fn(size_t index, void *value)
1159 START_TEST (test_linked_list_foreach_null_list)
1161 linked_list_foreach(NULL, _foreach_fn);
1165 START_TEST (test_linked_list_foreach_null_fn)
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);
1172 linked_list_foreach(list, NULL);
1174 linked_list_free(list, NULL);
1178 START_TEST (test_linked_list_foreach_valid)
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);
1186 linked_list_foreach(list, &_foreach_fn);
1187 ck_assert_uint_eq(3, _foreach_count);
1189 linked_list_free(list, NULL);
1192 Suite *create_suite(void)
1194 Suite *s = suite_create(SUITE_NAME);
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);
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);
1299 return (num_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;