27
#include "eina_safety_checks.h"
29
#include "eina_suite.h"
31
typedef struct _Eina_Test_Inlist Eina_Test_Inlist;
32
struct _Eina_Test_Inlist
38
#ifdef EINA_SAFETY_CHECKS
49
_eina_test_safety_print_cb(const Eina_Log_Domain *d, Eina_Log_Level level, const char *file, const char *fnc, int line, const char *fmt, void *data, va_list args EINA_UNUSED)
51
struct log_ctx *ctx = data;
55
va_copy(cp_args, args);
56
str = va_arg(cp_args, const char *);
59
ck_assert_int_eq(level, EINA_LOG_LEVEL_ERR);
60
ck_assert_str_eq(fmt, "%s");
61
ck_assert_str_eq(ctx->msg, str);
62
ck_assert_str_eq(ctx->fnc, fnc);
66
eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args);
75
static Eina_Test_Inlist *
76
_eina_test_inlist_build(int i)
78
Eina_Test_Inlist *tmp;
80
tmp = malloc(sizeof(Eina_Test_Inlist));
87
EFL_START_TEST(eina_inlist_simple)
89
Eina_Inlist *lst = NULL;
90
Eina_Inlist *tmpl = NULL;
91
Eina_Test_Inlist *tmp;
92
Eina_Test_Inlist *prev;
94
#ifdef EINA_SAFETY_CHECKS
99
tmp = _eina_test_inlist_build(42);
100
lst = eina_inlist_append(lst, EINA_INLIST_GET(tmp));
103
lst = eina_inlist_remove(lst, EINA_INLIST_GET(tmp));
104
lst = eina_inlist_prepend(lst, EINA_INLIST_GET(tmp));
106
tmp = _eina_test_inlist_build(1664);
107
lst = eina_inlist_append_relative(lst, EINA_INLIST_GET(tmp), lst);
109
fail_if(EINA_INLIST_CONTAINER_GET(lst, Eina_Test_Inlist)->i != 42);
112
tmp = _eina_test_inlist_build(3227);
113
lst = eina_inlist_prepend_relative(lst, EINA_INLIST_GET(
114
tmp), EINA_INLIST_GET(prev));
116
fail_if(EINA_INLIST_CONTAINER_GET(lst, Eina_Test_Inlist)->i != 42);
118
lst = eina_inlist_remove(lst, EINA_INLIST_GET(tmp));
120
lst = eina_inlist_append_relative(lst, EINA_INLIST_GET(tmp), lst);
121
lst = eina_inlist_remove(lst, EINA_INLIST_GET(tmp));
123
lst = eina_inlist_prepend_relative(lst, EINA_INLIST_GET(tmp), lst);
125
tmp = _eina_test_inlist_build(27);
126
lst = eina_inlist_prepend_relative(lst, EINA_INLIST_GET(tmp), NULL);
128
tmp = _eina_test_inlist_build(81);
129
lst = eina_inlist_append_relative(lst, EINA_INLIST_GET(tmp), NULL);
131
EINA_INLIST_FOREACH(lst, tmp)
135
case 0: fail_if(tmp->i != 27); break;
137
case 1: fail_if(tmp->i != 3227); break;
139
case 2: fail_if(tmp->i != 42); break;
141
case 3: fail_if(tmp->i != 1664); break;
143
case 4: fail_if(tmp->i != 81); break;
149
#ifdef EINA_SAFETY_CHECKS
151
eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx);
153
#define TEST_MAGIC_SAFETY(fn, _msg) \
159
fprintf(stderr, "you should have a safety check failure below:\n");
164
TEST_MAGIC_SAFETY("eina_inlist_remove",
165
"safety check failed: list == NULL");
167
tmp2 = eina_inlist_remove(NULL, EINA_INLIST_GET(tmp));
168
fail_if(tmp2 != NULL);
169
fail_unless(ctx.did);
173
fprintf(stderr, "you should have a safety check failure below:\n");
175
TEST_MAGIC_SAFETY("eina_inlist_remove",
176
"safety check failed: item == NULL");
177
lst = eina_inlist_remove(lst, NULL);
178
fail_unless(ctx.did);
181
fprintf(stderr, "you should have a safety check failure below:\n");
183
TEST_MAGIC_SAFETY("eina_inlist_append",
184
"safety check failed: new_l == NULL");
185
lst = eina_inlist_append(lst, NULL);
186
fail_unless(ctx.did);
189
fprintf(stderr, "you should have a safety check failure below:\n");
191
TEST_MAGIC_SAFETY("eina_inlist_append_relative",
192
"safety check failed: new_l == NULL");
193
lst = eina_inlist_append_relative(lst, NULL, NULL);
194
fail_unless(ctx.did);
197
fprintf(stderr, "you should have a safety check failure below:\n");
199
TEST_MAGIC_SAFETY("eina_inlist_prepend",
200
"safety check failed: new_l == NULL");
201
lst = eina_inlist_prepend(lst, NULL);
202
fail_unless(ctx.did);
205
fprintf(stderr, "you should have a safety check failure below:\n");
207
TEST_MAGIC_SAFETY("eina_inlist_prepend_relative",
208
"safety check failed: new_l == NULL");
209
lst = eina_inlist_prepend_relative(lst, NULL, NULL);
210
fail_unless(ctx.did);
213
fprintf(stderr, "you should have a safety check failure below:\n");
215
TEST_MAGIC_SAFETY("eina_inlist_find",
216
"safety check failed: item == NULL");
217
lst = eina_inlist_find(lst, NULL);
218
fail_unless(ctx.did);
221
fprintf(stderr, "you should have a safety check failure below:\n");
223
TEST_MAGIC_SAFETY("eina_inlist_demote",
224
"safety check failed: list == NULL");
225
lst = eina_inlist_demote(NULL, NULL);
226
fail_unless(ctx.did);
229
fprintf(stderr, "you should have a safety check failure below:\n");
231
TEST_MAGIC_SAFETY("eina_inlist_demote",
232
"safety check failed: item == NULL");
233
lst = eina_inlist_demote((void*)1L, NULL);
234
fail_unless(ctx.did);
238
fprintf(stderr, "you should have a safety check failure below:\n");
240
TEST_MAGIC_SAFETY("eina_inlist_promote",
241
"safety check failed: list == NULL");
242
lst = eina_inlist_promote(NULL, NULL);
243
fail_unless(ctx.did);
246
fprintf(stderr, "you should have a safety check failure below:\n");
248
TEST_MAGIC_SAFETY("eina_inlist_promote",
249
"safety check failed: item == NULL");
250
lst = eina_inlist_promote((void*)1L, NULL);
251
fail_unless(ctx.did);
255
fprintf(stderr, "you should have a safety check failure below:\n");
257
TEST_MAGIC_SAFETY("eina_inlist_sorted_insert",
258
"safety check failed: item == NULL");
259
lst = eina_inlist_sorted_insert(NULL, NULL, NULL);
260
fail_unless(ctx.did);
263
fprintf(stderr, "you should have a safety check failure below:\n");
265
TEST_MAGIC_SAFETY("eina_inlist_sorted_insert",
266
"safety check failed: func == NULL");
267
lst = eina_inlist_sorted_insert(NULL, (void*)1L, NULL);
268
fail_unless(ctx.did);
271
eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
275
tmpl = eina_inlist_last(lst);
276
fail_if(tmpl == lst);
277
tmpl = eina_inlist_first(tmpl);
278
fail_if(tmpl != lst);
280
tmp = EINA_INLIST_CONTAINER_GET(lst, Eina_Test_Inlist);
281
lst = eina_inlist_demote(lst, lst);
282
fail_if(EINA_INLIST_CONTAINER_GET(lst, Eina_Test_Inlist) == tmp);
284
lst = eina_inlist_promote(lst, EINA_INLIST_GET(tmp));
285
fail_if(lst != EINA_INLIST_GET(tmp));
287
tmp = EINA_INLIST_CONTAINER_GET(eina_inlist_find(lst, EINA_INLIST_GET(
288
prev)), Eina_Test_Inlist);
289
lst = eina_inlist_remove(lst, EINA_INLIST_GET(tmp));
290
prev = (Eina_Test_Inlist *)eina_inlist_find(lst, EINA_INLIST_GET(tmp));
291
tmp = prev ? EINA_INLIST_CONTAINER_GET(prev, Eina_Test_Inlist) : NULL;
292
fail_if(tmp != NULL);
295
lst = eina_inlist_remove(lst, lst);
300
typedef struct _Eina_Test_Inlist_Sorted Eina_Test_Inlist_Sorted;
301
struct _Eina_Test_Inlist_Sorted
309
_eina_test_inlist_cmp(const void *d1, const void *d2)
311
const Eina_Test_Inlist_Sorted *t1 = d1;
312
const Eina_Test_Inlist_Sorted *t2 = d2;
314
return t1->value - t2->value;
318
_eina_test_inlist_cmp2(const void *d1, const void *d2)
320
const Eina_Test_Inlist_Sorted *t1 = d1;
321
const Eina_Test_Inlist_Sorted *t2 = d2;
323
return t2->value - t1->value;
326
#define _eina_test_inlist_check(LIST) \
328
const Eina_Test_Inlist_Sorted *_t; \
331
EINA_INLIST_FOREACH(LIST, _t) \
333
ck_assert_int_eq(_t->value, values[_i]); \
338
#define _eina_test_inlist_check_reverse(LIST) \
340
const Eina_Test_Inlist_Sorted *_t; \
341
int _i = EINA_C_ARRAY_LENGTH(values) - 1; \
343
EINA_INLIST_FOREACH(LIST, _t) \
345
ck_assert_int_eq(_t->value, values[_i]); \
350
static int values_unsorted[] =
382
_eina_test_inlist_check_insert(const Eina_Inlist *list)
384
const Eina_Test_Inlist_Sorted *t;
387
EINA_INLIST_FOREACH(list, t)
389
ck_assert_int_ge(t->value, last_value);
390
last_value = t->value;
395
_eina_test_inlist_check_insert_reverse(const Eina_Inlist *list)
397
const Eina_Test_Inlist_Sorted *t;
400
EINA_INLIST_FOREACH(list, t)
403
ck_assert_int_le(t->value, last_value);
404
last_value = t->value;
408
EFL_START_TEST(eina_inlist_sorted)
410
Eina_Test_Inlist_Sorted *t, tmp[EINA_C_ARRAY_LENGTH(values_unsorted)];
411
Eina_Inlist *list = NULL;
412
Eina_Inlist *sorted = NULL;
415
for (i = 0; i < EINA_C_ARRAY_LENGTH(values_unsorted); ++i)
417
tmp[i].value = values_unsorted[i];
419
list = eina_inlist_prepend(list, EINA_INLIST_GET(&tmp[i]));
422
list = eina_inlist_sort(list, _eina_test_inlist_cmp);
424
_eina_test_inlist_check(list);
426
i = EINA_C_ARRAY_LENGTH(values_unsorted) - 1;
427
EINA_INLIST_FOREACH(list, t)
429
t->value = values_unsorted[i];
435
Eina_Inlist *p = list;
437
list = eina_inlist_remove(list, list);
439
sorted = eina_inlist_sorted_insert(sorted, p, _eina_test_inlist_cmp);
440
_eina_test_inlist_check_insert(sorted);
442
_eina_test_inlist_check(sorted);
446
EFL_START_TEST(eina_inlist_sorted_state)
448
Eina_Test_Inlist_Sorted tmp[EINA_C_ARRAY_LENGTH(values_unsorted)];
449
Eina_Inlist_Sorted_State *state;
450
Eina_Inlist *list = NULL;
453
state = eina_inlist_sorted_state_new();
456
for (i = 0; i < EINA_C_ARRAY_LENGTH(values_unsorted); ++i)
458
tmp[i].value = values_unsorted[i];
460
list = eina_inlist_sorted_state_insert(list, EINA_INLIST_GET(&tmp[i]), _eina_test_inlist_cmp, state);
461
_eina_test_inlist_check_insert(list);
463
eina_inlist_sorted_state_free(state);
464
_eina_test_inlist_check(list);
468
EFL_START_TEST(eina_inlist_sorted2)
470
Eina_Test_Inlist_Sorted *t, tmp[EINA_C_ARRAY_LENGTH(values_unsorted)];
471
Eina_Inlist *list = NULL;
472
Eina_Inlist *sorted = NULL;
475
for (i = 0; i < EINA_C_ARRAY_LENGTH(values_unsorted); ++i)
477
tmp[i].value = values_unsorted[i];
479
list = eina_inlist_prepend(list, EINA_INLIST_GET(&tmp[i]));
482
list = eina_inlist_sort(list, _eina_test_inlist_cmp2);
484
_eina_test_inlist_check_reverse(list);
486
i = EINA_C_ARRAY_LENGTH(values_unsorted) - 1;
487
EINA_INLIST_FOREACH(list, t)
489
t->value = values_unsorted[i];
495
Eina_Inlist *p = list;
497
list = eina_inlist_remove(list, list);
499
sorted = eina_inlist_sorted_insert(sorted, p, _eina_test_inlist_cmp2);
500
_eina_test_inlist_check_insert_reverse(sorted);
502
_eina_test_inlist_check_reverse(sorted);
506
EFL_START_TEST(eina_inlist_sorted_state2)
508
Eina_Test_Inlist_Sorted tmp[EINA_C_ARRAY_LENGTH(values_unsorted)];
509
Eina_Inlist_Sorted_State *state;
510
Eina_Inlist *list = NULL;
513
state = eina_inlist_sorted_state_new();
516
for (i = 0; i < EINA_C_ARRAY_LENGTH(values_unsorted); ++i)
518
tmp[i].value = values_unsorted[i];
520
list = eina_inlist_sorted_state_insert(list, EINA_INLIST_GET(&tmp[i]), _eina_test_inlist_cmp2, state);
521
_eina_test_inlist_check_insert_reverse(list);
523
eina_inlist_sorted_state_free(state);
524
_eina_test_inlist_check_reverse(list);
529
eina_test_inlist(TCase *tc)
531
tcase_add_test(tc, eina_inlist_simple);
532
tcase_add_test(tc, eina_inlist_sorted);
533
tcase_add_test(tc, eina_inlist_sorted_state);
534
tcase_add_test(tc, eina_inlist_sorted2);
535
tcase_add_test(tc, eina_inlist_sorted_state2);