aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/tests/eina_test_inlist.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eina/src/tests/eina_test_inlist.c')
-rw-r--r--libraries/eina/src/tests/eina_test_inlist.c254
1 files changed, 254 insertions, 0 deletions
diff --git a/libraries/eina/src/tests/eina_test_inlist.c b/libraries/eina/src/tests/eina_test_inlist.c
new file mode 100644
index 0000000..c27f393
--- /dev/null
+++ b/libraries/eina/src/tests/eina_test_inlist.c
@@ -0,0 +1,254 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2008 Cedric Bail
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifdef HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include <stdlib.h>
24#include <stdio.h>
25
26#include "eina_suite.h"
27#include "Eina.h"
28#include "eina_safety_checks.h"
29
30typedef struct _Eina_Test_Inlist Eina_Test_Inlist;
31struct _Eina_Test_Inlist
32{
33 int i;
34 EINA_INLIST;
35};
36
37static Eina_Test_Inlist *
38_eina_test_inlist_build(int i)
39{
40 Eina_Test_Inlist *tmp;
41
42 tmp = malloc(sizeof(Eina_Test_Inlist));
43 fail_if(!tmp);
44 tmp->i = i;
45
46 return tmp;
47}
48
49START_TEST(eina_inlist_simple)
50{
51 Eina_Inlist *lst = NULL;
52 Eina_Test_Inlist *tmp;
53 Eina_Test_Inlist *prev;
54 int i = 0;
55
56 fail_if(!eina_init());
57
58 tmp = _eina_test_inlist_build(42);
59 lst = eina_inlist_append(lst, EINA_INLIST_GET(tmp));
60 fail_if(!lst);
61
62 lst = eina_inlist_remove(lst, EINA_INLIST_GET(tmp));
63 lst = eina_inlist_prepend(lst, EINA_INLIST_GET(tmp));
64
65 tmp = _eina_test_inlist_build(1664);
66 lst = eina_inlist_append_relative(lst, EINA_INLIST_GET(tmp), lst);
67 fail_if(!lst);
68 fail_if(EINA_INLIST_CONTAINER_GET(lst, Eina_Test_Inlist)->i != 42);
69
70 prev = tmp;
71 tmp = _eina_test_inlist_build(3227);
72 lst = eina_inlist_prepend_relative(lst, EINA_INLIST_GET(
73 tmp), EINA_INLIST_GET(prev));
74 fail_if(!lst);
75 fail_if(EINA_INLIST_CONTAINER_GET(lst, Eina_Test_Inlist)->i != 42);
76
77 lst = eina_inlist_remove(lst, EINA_INLIST_GET(tmp));
78
79 lst = eina_inlist_append_relative(lst, EINA_INLIST_GET(tmp), lst);
80 lst = eina_inlist_remove(lst, EINA_INLIST_GET(tmp));
81
82 lst = eina_inlist_prepend_relative(lst, EINA_INLIST_GET(tmp), lst);
83
84 tmp = _eina_test_inlist_build(27);
85 lst = eina_inlist_prepend_relative(lst, EINA_INLIST_GET(tmp), NULL);
86
87 tmp = _eina_test_inlist_build(81);
88 lst = eina_inlist_append_relative(lst, EINA_INLIST_GET(tmp), NULL);
89
90 EINA_INLIST_FOREACH(lst, tmp)
91 {
92 switch (i)
93 {
94 case 0: fail_if(tmp->i != 27); break;
95
96 case 1: fail_if(tmp->i != 3227); break;
97
98 case 2: fail_if(tmp->i != 42); break;
99
100 case 3: fail_if(tmp->i != 1664); break;
101
102 case 4: fail_if(tmp->i != 81); break;
103 }
104
105 ++i;
106 }
107
108#ifdef EINA_SAFETY_CHECKS
109 fprintf(stderr, "you should have a safety check failure below:\n");
110 {
111 Eina_Inlist *tmp2 = eina_inlist_remove(NULL, EINA_INLIST_GET(tmp));
112 fail_if(tmp2 != NULL);
113 fail_if(eina_error_get() != EINA_ERROR_SAFETY_FAILED);
114 }
115
116 fprintf(stderr, "you should have a safety check failure below:\n");
117 lst = eina_inlist_remove(lst, NULL);
118 fail_if(eina_error_get() != EINA_ERROR_SAFETY_FAILED);
119#endif
120
121 tmp = EINA_INLIST_CONTAINER_GET(lst, Eina_Test_Inlist);
122 lst = eina_inlist_demote(lst, lst);
123 fail_if(EINA_INLIST_CONTAINER_GET(lst, Eina_Test_Inlist) == tmp);
124
125 lst = eina_inlist_promote(lst, EINA_INLIST_GET(tmp));
126 fail_if(lst != EINA_INLIST_GET(tmp));
127
128 tmp = EINA_INLIST_CONTAINER_GET(eina_inlist_find(lst, EINA_INLIST_GET(
129 prev)), Eina_Test_Inlist);
130 lst = eina_inlist_remove(lst, EINA_INLIST_GET(tmp));
131 prev = (Eina_Test_Inlist *)eina_inlist_find(lst, EINA_INLIST_GET(tmp));
132 tmp = prev ? EINA_INLIST_CONTAINER_GET(prev, Eina_Test_Inlist) : NULL;
133 fail_if(tmp != NULL);
134
135 while (lst)
136 lst = eina_inlist_remove(lst, lst);
137
138 eina_shutdown();
139}
140END_TEST
141
142typedef struct _Eina_Test_Inlist_Sorted Eina_Test_Inlist_Sorted;
143struct _Eina_Test_Inlist_Sorted
144{
145 EINA_INLIST;
146
147 int value;
148};
149
150static int
151_eina_test_inlist_cmp(const void *d1, const void *d2)
152{
153 const Eina_Test_Inlist_Sorted *t1 = d1;
154 const Eina_Test_Inlist_Sorted *t2 = d2;
155
156 return t1->value - t2->value;
157}
158
159static void
160_eina_test_inlist_check(const Eina_Inlist *list)
161{
162 const Eina_Test_Inlist_Sorted *t;
163 int last_value = 0;
164
165 EINA_INLIST_FOREACH(list, t)
166 {
167 fail_if(t->value < last_value);
168 last_value = t->value;
169 }
170}
171
172START_TEST(eina_inlist_sorted)
173{
174 Eina_Test_Inlist_Sorted *tmp;
175 Eina_Inlist *list = NULL;
176 Eina_Inlist *sorted = NULL;
177 int i;
178
179 fail_if(!eina_init());
180
181 srand(time(NULL));
182
183 for (i = 0; i < 2000; ++i)
184 {
185 tmp = malloc(sizeof (Eina_Test_Inlist_Sorted));
186 if (!tmp) continue ;
187
188 tmp->value = rand();
189
190 list = eina_inlist_prepend(list, EINA_INLIST_GET(tmp));
191 }
192
193 list = eina_inlist_sort(list, _eina_test_inlist_cmp);
194
195 _eina_test_inlist_check(list);
196
197 EINA_INLIST_FOREACH(list, tmp)
198 tmp->value = rand();
199
200 i = 0;
201 while (list)
202 {
203 Eina_Inlist *p = list;
204
205 list = eina_inlist_remove(list, list);
206
207 sorted = eina_inlist_sorted_insert(sorted, p, _eina_test_inlist_cmp);
208 _eina_test_inlist_check(sorted);
209 }
210
211 _eina_test_inlist_check(sorted);
212
213 eina_shutdown();
214}
215END_TEST
216
217START_TEST(eina_inlist_sorted_state)
218{
219 Eina_Test_Inlist_Sorted *tmp;
220 Eina_Inlist_Sorted_State *state;
221 Eina_Inlist *list = NULL;
222 int i;
223
224 fail_if(!eina_init());
225
226 state = eina_inlist_sorted_state_new();
227 fail_if(!state);
228
229 for (i = 0; i < 2000; ++i)
230 {
231 tmp = malloc(sizeof (Eina_Test_Inlist_Sorted));
232 if (!tmp) continue ;
233
234 tmp->value = rand();
235
236 list = eina_inlist_sorted_state_insert(list, EINA_INLIST_GET(tmp), _eina_test_inlist_cmp, state);
237 _eina_test_inlist_check(list);
238 }
239
240 _eina_test_inlist_check(list);
241
242 eina_inlist_sorted_state_free(state);
243
244 eina_shutdown();
245}
246END_TEST
247
248void
249eina_test_inlist(TCase *tc)
250{
251 tcase_add_test(tc, eina_inlist_simple);
252 tcase_add_test(tc, eina_inlist_sorted);
253 tcase_add_test(tc, eina_inlist_sorted_state);
254}