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 *****************************************************************************/
31 #include <vlc_common.h>
40 static struct vlc_list
*make_elem(int i
)
42 struct test_elem
*e
= malloc(sizeof (*e
));
52 struct vlc_list head
, *back
;
53 struct test_elem
*elem
;
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
);
64 vlc_list_foreach(elem
, &head
, node
)
65 assert(elem
->i
== 1), count
++;
69 vlc_list_append(back
, &head
);
71 vlc_list_foreach(elem
, &head
, node
)
72 assert(elem
->i
== count
+ 1), count
++;
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
));
79 vlc_list_foreach(elem
, &head
, node
)
80 assert(elem
->i
== (count
? 1 : 3)), count
++;
83 vlc_list_foreach(elem
, &head
, node
)
85 vlc_list_remove(&elem
->node
);
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
);
96 vlc_list_foreach(elem
, &head
, node
)
97 assert(elem
->i
== count
+ 10), count
++;
101 for (elem
= vlc_list_first_entry_or_null(&head
, struct test_elem
, node
);
103 elem
= vlc_list_next_entry_or_null(&head
, elem
,
104 struct test_elem
, node
))
105 assert(elem
->i
== count
+ 10), count
++;
109 for (elem
= vlc_list_last_entry_or_null(&head
, struct test_elem
, node
);
111 elem
= vlc_list_prev_entry_or_null(&head
, elem
,
112 struct test_elem
, node
))
113 assert(elem
->i
== 29 - count
), count
++;
117 vlc_list_foreach(elem
, &head
, node
)
121 vlc_list_remove(&elem
->node
);
129 vlc_list_foreach(elem
, &head
, node
)
131 assert(elem
->i
== count
* 2 + 10);
132 vlc_list_remove(&elem
->node
);
137 assert(vlc_list_is_empty(&head
));