diff options
author | David Walter Seikel | 2012-01-23 23:30:42 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-23 23:30:42 +1000 |
commit | 825a3d837a33f226c879cd02ad15c3fba57e8b2c (patch) | |
tree | 75f57bd9c4253508d338dc79ba8e57a7abc42255 /libraries/eina/src/tests/eina_test_inarray.c | |
parent | Add ability to disable the test harness, or the Lua compile test. (diff) | |
download | SledjHamr-825a3d837a33f226c879cd02ad15c3fba57e8b2c.zip SledjHamr-825a3d837a33f226c879cd02ad15c3fba57e8b2c.tar.gz SledjHamr-825a3d837a33f226c879cd02ad15c3fba57e8b2c.tar.bz2 SledjHamr-825a3d837a33f226c879cd02ad15c3fba57e8b2c.tar.xz |
Update the EFL to what I'm actually using, coz I'm using some stuff not yet released.
Diffstat (limited to 'libraries/eina/src/tests/eina_test_inarray.c')
-rw-r--r-- | libraries/eina/src/tests/eina_test_inarray.c | 401 |
1 files changed, 401 insertions, 0 deletions
diff --git a/libraries/eina/src/tests/eina_test_inarray.c b/libraries/eina/src/tests/eina_test_inarray.c new file mode 100644 index 0000000..22ba763 --- /dev/null +++ b/libraries/eina/src/tests/eina_test_inarray.c | |||
@@ -0,0 +1,401 @@ | |||
1 | /* EINA - EFL data type library | ||
2 | * Copyright (C) 2012 ProFUSION embedded systems | ||
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 <stdio.h> | ||
24 | |||
25 | #include "eina_suite.h" | ||
26 | #include "Eina.h" | ||
27 | |||
28 | START_TEST(eina_inarray_test_simple) | ||
29 | { | ||
30 | const int test_members = 5; | ||
31 | Eina_Inarray *array; | ||
32 | int i, pos, *member; | ||
33 | const struct spec { | ||
34 | int pos, value; | ||
35 | } *s, specs[] = { | ||
36 | {test_members, 1234}, | ||
37 | {5, 0x1337}, | ||
38 | {0, 0xbeef}, | ||
39 | {-1, -1} | ||
40 | }; | ||
41 | |||
42 | eina_init(); | ||
43 | |||
44 | array = eina_inarray_new(sizeof(int), 2); | ||
45 | fail_unless(array != NULL); | ||
46 | |||
47 | for (i = 0; i < test_members; i++) | ||
48 | { | ||
49 | pos = eina_inarray_append(array, &i); | ||
50 | fail_unless(pos == i); | ||
51 | } | ||
52 | fail_unless(eina_inarray_count(array) == (unsigned)test_members); | ||
53 | |||
54 | for (i = 0; i < test_members; i++) | ||
55 | { | ||
56 | member = eina_inarray_nth(array, i); | ||
57 | fail_unless(*member == i); | ||
58 | } | ||
59 | |||
60 | for (s = specs; s->pos >= 0; s++) | ||
61 | { | ||
62 | fail_unless(eina_inarray_insert_at(array, s->pos, &s->value)); | ||
63 | |||
64 | for (i = 0; i < s->pos; i++) | ||
65 | { | ||
66 | member = eina_inarray_nth(array, i); | ||
67 | fail_unless(*member == i); | ||
68 | } | ||
69 | member = eina_inarray_nth(array, s->pos); | ||
70 | fail_unless(*member == s->value); | ||
71 | for (i = s->pos + 1; i < test_members + 1; i++) | ||
72 | { | ||
73 | member = eina_inarray_nth(array, i); | ||
74 | fail_unless(*member == i - 1); | ||
75 | } | ||
76 | |||
77 | fail_unless(eina_inarray_remove_at(array, s->pos)); | ||
78 | for (i = 0; i < test_members; i++) | ||
79 | { | ||
80 | member = eina_inarray_nth(array, i); | ||
81 | fail_unless(*member == i); | ||
82 | } | ||
83 | } | ||
84 | |||
85 | eina_inarray_free(array); | ||
86 | eina_shutdown(); | ||
87 | } | ||
88 | END_TEST | ||
89 | |||
90 | |||
91 | START_TEST(eina_inarray_test_alloc_at) | ||
92 | { | ||
93 | Eina_Inarray *array; | ||
94 | int *member; | ||
95 | int i; | ||
96 | |||
97 | eina_init(); | ||
98 | |||
99 | array = eina_inarray_new(sizeof(int), 2); | ||
100 | fail_unless(array != NULL); | ||
101 | |||
102 | member = eina_inarray_alloc_at(array, 0, 4); | ||
103 | fail_unless(member != NULL); | ||
104 | fail_unless(eina_inarray_count(array) == 4); | ||
105 | |||
106 | for (i = 0; i < 4; i++) | ||
107 | member[i] = i + 2; | ||
108 | |||
109 | member = eina_inarray_alloc_at(array, 0, 2); | ||
110 | fail_unless(member != NULL); | ||
111 | fail_unless(eina_inarray_count(array) == 6); | ||
112 | for (i = 0; i < 2; i++) | ||
113 | member[i] = i; | ||
114 | |||
115 | member = eina_inarray_alloc_at(array, 6, 2); | ||
116 | fail_unless(member != NULL); | ||
117 | fail_unless(eina_inarray_count(array) == 8); | ||
118 | for (i = 0; i < 2; i++) | ||
119 | member[i] = i + 6; | ||
120 | |||
121 | member = array->members; | ||
122 | for (i = 0; i < 8; i++) | ||
123 | fail_unless(member[i] == i); | ||
124 | |||
125 | eina_inarray_free(array); | ||
126 | eina_shutdown(); | ||
127 | } | ||
128 | END_TEST | ||
129 | |||
130 | static const short rand_numbers[] = { | ||
131 | 9, 0, 2, 3, 6, 5, 4, 7, 8, 1, 10 | ||
132 | }; | ||
133 | static const int numbers_count = sizeof(rand_numbers)/sizeof(rand_numbers[0]); | ||
134 | |||
135 | static void | ||
136 | show_sort_array(const Eina_Inarray *array) | ||
137 | { | ||
138 | int i, len = eina_inarray_count(array); | ||
139 | printf("array with %d members:\n", len); | ||
140 | for (i = 0; i < len; i++) | ||
141 | { | ||
142 | short *member = eina_inarray_nth(array, i); | ||
143 | printf("\tarray[%2d]=%5hd\n", i, *member); | ||
144 | } | ||
145 | } | ||
146 | |||
147 | static Eina_Bool | ||
148 | check_short_sorted(const Eina_Inarray *array) | ||
149 | { | ||
150 | int i; | ||
151 | for (i = 0; i < numbers_count; i++) | ||
152 | { | ||
153 | short *member = eina_inarray_nth(array, i); | ||
154 | if (*member != (short)i) | ||
155 | { | ||
156 | show_sort_array(array); | ||
157 | fprintf(stderr, "not sorted at %d: %hd\n", i, *member); | ||
158 | return EINA_FALSE; | ||
159 | } | ||
160 | } | ||
161 | return EINA_TRUE; | ||
162 | } | ||
163 | |||
164 | static int | ||
165 | short_cmp(const void *pa, const void *pb) | ||
166 | { | ||
167 | const short *a = pa, *b = pb; | ||
168 | return *a - *b; | ||
169 | } | ||
170 | |||
171 | START_TEST(eina_inarray_test_insert_sort) | ||
172 | { | ||
173 | Eina_Inarray *array; | ||
174 | int i, pos; | ||
175 | |||
176 | eina_init(); | ||
177 | |||
178 | array = eina_inarray_new(sizeof(short), 1); | ||
179 | fail_unless(array != NULL); | ||
180 | |||
181 | /* insert sorted and already sorted sequence */ | ||
182 | for (i = 0; i < numbers_count; i++) | ||
183 | { | ||
184 | short val = i; | ||
185 | pos = eina_inarray_insert(array, &val, short_cmp); | ||
186 | fail_unless(pos == (int)val); | ||
187 | } | ||
188 | fail_unless(check_short_sorted(array)); | ||
189 | eina_inarray_flush(array); | ||
190 | |||
191 | for (i = 0; i < numbers_count; i++) | ||
192 | { | ||
193 | short val = i; | ||
194 | pos = eina_inarray_insert_sorted(array, &val, short_cmp); | ||
195 | fail_unless(pos == (int)val); | ||
196 | } | ||
197 | fail_unless(check_short_sorted(array)); | ||
198 | eina_inarray_flush(array); | ||
199 | |||
200 | /* insert sorted the reverse sequence */ | ||
201 | for (i = 0; i < numbers_count; i++) | ||
202 | { | ||
203 | short val = numbers_count - i - 1; | ||
204 | pos = eina_inarray_insert(array, &val, short_cmp); | ||
205 | fail_unless(pos == 0); | ||
206 | } | ||
207 | fail_unless(check_short_sorted(array)); | ||
208 | eina_inarray_flush(array); | ||
209 | |||
210 | for (i = 0; i < numbers_count; i++) | ||
211 | { | ||
212 | short val = numbers_count - i - 1; | ||
213 | pos = eina_inarray_insert_sorted(array, &val, short_cmp); | ||
214 | fail_unless(pos == 0); | ||
215 | } | ||
216 | fail_unless(check_short_sorted(array)); | ||
217 | eina_inarray_flush(array); | ||
218 | |||
219 | /* insert sorted random numbers */ | ||
220 | for (i = 0; i < numbers_count; i++) | ||
221 | { | ||
222 | short val = rand_numbers[i]; | ||
223 | eina_inarray_insert(array, &val, short_cmp); | ||
224 | } | ||
225 | fail_unless(check_short_sorted(array)); | ||
226 | eina_inarray_flush(array); | ||
227 | |||
228 | for (i = 0; i < numbers_count; i++) | ||
229 | { | ||
230 | short val = rand_numbers[i]; | ||
231 | eina_inarray_insert_sorted(array, &val, short_cmp); | ||
232 | } | ||
233 | fail_unless(check_short_sorted(array)); | ||
234 | eina_inarray_flush(array); | ||
235 | |||
236 | eina_inarray_free(array); | ||
237 | eina_shutdown(); | ||
238 | } | ||
239 | END_TEST | ||
240 | |||
241 | START_TEST(eina_inarray_test_sort) | ||
242 | { | ||
243 | Eina_Inarray *array; | ||
244 | int i; | ||
245 | |||
246 | eina_init(); | ||
247 | |||
248 | array = eina_inarray_new(sizeof(short), 1); | ||
249 | fail_unless(array != NULL); | ||
250 | |||
251 | for (i = 0; i < numbers_count; i++) | ||
252 | { | ||
253 | short val = rand_numbers[i]; | ||
254 | eina_inarray_append(array, &val); | ||
255 | } | ||
256 | eina_inarray_sort(array, short_cmp); | ||
257 | fail_unless(check_short_sorted(array)); | ||
258 | eina_inarray_free(array); | ||
259 | eina_shutdown(); | ||
260 | } | ||
261 | END_TEST | ||
262 | |||
263 | START_TEST(eina_inarray_test_reverse) | ||
264 | { | ||
265 | Eina_Inarray *array; | ||
266 | int i; | ||
267 | |||
268 | eina_init(); | ||
269 | |||
270 | array = eina_inarray_new(sizeof(short), 1); | ||
271 | fail_unless(array != NULL); | ||
272 | |||
273 | for (i = 0; i < numbers_count; i++) | ||
274 | { | ||
275 | short val = i; | ||
276 | eina_inarray_append(array, &val); | ||
277 | } | ||
278 | eina_inarray_reverse(array); | ||
279 | |||
280 | for (i = 0; i < numbers_count; i++) | ||
281 | { | ||
282 | short *member = eina_inarray_nth(array, i); | ||
283 | fail_unless(*member == (numbers_count - i - 1)); | ||
284 | } | ||
285 | |||
286 | eina_inarray_free(array); | ||
287 | eina_shutdown(); | ||
288 | } | ||
289 | END_TEST | ||
290 | |||
291 | static Eina_Bool | ||
292 | array_foreach(const void *array __UNUSED__, void *p, void *user_data __UNUSED__) | ||
293 | { | ||
294 | short *member = p; | ||
295 | int *i = user_data; | ||
296 | fail_unless(*i == *member); | ||
297 | (*i)++; | ||
298 | return EINA_TRUE; | ||
299 | } | ||
300 | |||
301 | static Eina_Bool | ||
302 | array_foreach_stop_2nd(const void *array __UNUSED__, void *p, void *user_data __UNUSED__) | ||
303 | { | ||
304 | short *member = p; | ||
305 | int *i = user_data; | ||
306 | fail_unless(*i == *member); | ||
307 | if (*i == 1) | ||
308 | return EINA_FALSE; | ||
309 | (*i)++; | ||
310 | return EINA_TRUE; | ||
311 | } | ||
312 | |||
313 | START_TEST(eina_inarray_test_itr) | ||
314 | { | ||
315 | Eina_Inarray *array; | ||
316 | Eina_Iterator *it; | ||
317 | Eina_Accessor *ac; | ||
318 | short *member; | ||
319 | int i; | ||
320 | |||
321 | eina_init(); | ||
322 | |||
323 | array = eina_inarray_new(sizeof(short), 1); | ||
324 | fail_unless(array != NULL); | ||
325 | |||
326 | for (i = 0; i < numbers_count; i++) | ||
327 | { | ||
328 | short val = i; | ||
329 | eina_inarray_append(array, &val); | ||
330 | } | ||
331 | i = 0; | ||
332 | EINA_INARRAY_FOREACH(array, member) | ||
333 | { | ||
334 | fail_unless(*member == i); | ||
335 | i++; | ||
336 | } | ||
337 | fail_unless(i == numbers_count); | ||
338 | |||
339 | i--; | ||
340 | EINA_INARRAY_REVERSE_FOREACH(array, member) | ||
341 | { | ||
342 | fail_unless(*member == i); | ||
343 | i--; | ||
344 | } | ||
345 | fail_unless(i == -1); | ||
346 | |||
347 | i = 0; | ||
348 | fail_unless(eina_inarray_foreach(array, array_foreach, &i)); | ||
349 | fail_unless(i == numbers_count); | ||
350 | |||
351 | i = 0; | ||
352 | fail_if(eina_inarray_foreach(array, array_foreach_stop_2nd, &i)); | ||
353 | fail_unless(i == 1); | ||
354 | |||
355 | it = eina_inarray_iterator_new(array); | ||
356 | fail_unless(it != NULL); | ||
357 | i = 0; | ||
358 | EINA_ITERATOR_FOREACH(it, member) | ||
359 | { | ||
360 | fail_unless(*member == i); | ||
361 | i++; | ||
362 | } | ||
363 | fail_unless(i == numbers_count); | ||
364 | eina_iterator_free(it); | ||
365 | |||
366 | it = eina_inarray_iterator_reversed_new(array); | ||
367 | fail_unless(it != NULL); | ||
368 | i--; | ||
369 | EINA_ITERATOR_FOREACH(it, member) | ||
370 | { | ||
371 | fail_unless(*member == i); | ||
372 | i--; | ||
373 | } | ||
374 | fail_unless(i == -1); | ||
375 | eina_iterator_free(it); | ||
376 | |||
377 | ac = eina_inarray_accessor_new(array); | ||
378 | fail_unless(ac != NULL); | ||
379 | for (i = 0; i < numbers_count; i++) | ||
380 | { | ||
381 | fail_unless(eina_accessor_data_get(ac, i, (void **)&member)); | ||
382 | fail_unless(*member == i); | ||
383 | } | ||
384 | fail_unless(i == numbers_count); | ||
385 | eina_accessor_free(ac); | ||
386 | |||
387 | eina_inarray_free(array); | ||
388 | eina_shutdown(); | ||
389 | } | ||
390 | END_TEST | ||
391 | |||
392 | void | ||
393 | eina_test_inarray(TCase *tc) | ||
394 | { | ||
395 | tcase_add_test(tc, eina_inarray_test_simple); | ||
396 | tcase_add_test(tc, eina_inarray_test_alloc_at); | ||
397 | tcase_add_test(tc, eina_inarray_test_insert_sort); | ||
398 | tcase_add_test(tc, eina_inarray_test_sort); | ||
399 | tcase_add_test(tc, eina_inarray_test_reverse); | ||
400 | tcase_add_test(tc, eina_inarray_test_itr); | ||
401 | } | ||