qt: playlist: use item title if available
[vlc.git] / src / test / list.c
blob583adc1f0d94d394e04850d1e15b1a023003903e
1 /*****************************************************************************
2 * list.c: Test for vlc_list
3 *****************************************************************************
4 * Copyright (C) 2018 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #undef NDEBUG
29 #include <assert.h>
31 #include <vlc_common.h>
32 #include <vlc_list.h>
34 struct test_elem
36 int i;
37 struct vlc_list node;
40 static struct vlc_list *make_elem(int i)
42 struct test_elem *e = malloc(sizeof (*e));
43 if (e == NULL)
44 abort();
46 e->i = i;
47 return &e->node;
50 int main (void)
52 struct vlc_list head, *back;
53 struct test_elem *elem;
54 int count;
56 vlc_list_init(&head);
57 vlc_list_foreach(elem, &head, node)
58 assert(0); /* No iteration on an empty list */
59 assert(vlc_list_is_empty(&head));
61 vlc_list_init(&head); /* List can be reinitialized */
62 vlc_list_prepend(make_elem(1), &head);
63 count = 0;
64 vlc_list_foreach(elem, &head, node)
65 assert(elem->i == 1), count++;
66 assert(count == 1);
68 back = make_elem(2);
69 vlc_list_append(back, &head);
70 count = 0;
71 vlc_list_foreach(elem, &head, node)
72 assert(elem->i == count + 1), count++;
73 assert(count == 2);
75 vlc_list_prepend(make_elem(3), &head);
76 vlc_list_remove(head.prev); /* remove number 2 */
77 free(vlc_list_entry(back, struct test_elem, node));
78 count = 0;
79 vlc_list_foreach(elem, &head, node)
80 assert(elem->i == (count ? 1 : 3)), count++;
81 assert(count == 2);
83 vlc_list_foreach(elem, &head, node)
85 vlc_list_remove(&elem->node);
86 free(elem);
88 assert(vlc_list_is_empty(&head));
90 for (int i = 20; i < 30; i++)
91 vlc_list_append(make_elem(i), &head);
92 for (int i = 19; i >= 10; i--)
93 vlc_list_prepend(make_elem(i), &head);
95 count = 0;
96 vlc_list_foreach(elem, &head, node)
97 assert(elem->i == count + 10), count++;
98 assert(count == 20);
100 count = 0;
101 for (elem = vlc_list_first_entry_or_null(&head, struct test_elem, node);
102 elem != NULL;
103 elem = vlc_list_next_entry_or_null(&head, elem,
104 struct test_elem, node))
105 assert(elem->i == count + 10), count++;
106 assert(count == 20);
108 count = 0;
109 for (elem = vlc_list_last_entry_or_null(&head, struct test_elem, node);
110 elem != NULL;
111 elem = vlc_list_prev_entry_or_null(&head, elem,
112 struct test_elem, node))
113 assert(elem->i == 29 - count), count++;
114 assert(count == 20);
116 count = 0;
117 vlc_list_foreach(elem, &head, node)
119 if (count & 1)
121 vlc_list_remove(&elem->node);
122 free(elem);
124 count++;
126 assert(count == 20);
128 count = 0;
129 vlc_list_foreach(elem, &head, node)
131 assert(elem->i == count * 2 + 10);
132 vlc_list_remove(&elem->node);
133 free(elem);
134 count++;
136 assert(count == 10);
137 assert(vlc_list_is_empty(&head));
138 return 0;