efl

Форк
0
/
eina_test_inlist.c 
536 строк · 13.5 Кб
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.h>
27
#include "eina_safety_checks.h"
28

29
#include "eina_suite.h"
30

31
typedef struct _Eina_Test_Inlist Eina_Test_Inlist;
32
struct _Eina_Test_Inlist
33
{
34
   int i;
35
   EINA_INLIST;
36
};
37

38
#ifdef EINA_SAFETY_CHECKS
39
struct log_ctx {
40
   const char *msg;
41
   const char *fnc;
42
   Eina_Bool did;
43
};
44

45
/* tests should not output on success, just uncomment this for debugging */
46
//#define SHOW_LOG 1
47

48
static void
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)
50
{
51
   struct log_ctx *ctx = data;
52
   va_list cp_args;
53
   const char *str;
54

55
   va_copy(cp_args, args);
56
   str = va_arg(cp_args, const char *);
57
   va_end(cp_args);
58

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);
63
   ctx->did = EINA_TRUE;
64

65
#ifdef SHOW_LOG
66
   eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args);
67
#else
68
   (void)d;
69
   (void)file;
70
   (void)line;
71
#endif
72
}
73
#endif
74

75
static Eina_Test_Inlist *
76
_eina_test_inlist_build(int i)
77
{
78
   Eina_Test_Inlist *tmp;
79

80
   tmp = malloc(sizeof(Eina_Test_Inlist));
81
   fail_if(!tmp);
82
   tmp->i = i;
83

84
   return tmp;
85
}
86

87
EFL_START_TEST(eina_inlist_simple)
88
{
89
   Eina_Inlist *lst = NULL;
90
   Eina_Inlist *tmpl = NULL;
91
   Eina_Test_Inlist *tmp;
92
   Eina_Test_Inlist *prev;
93
   int i = 0;
94
#ifdef EINA_SAFETY_CHECKS
95
   Eina_Inlist *bkp;
96
   struct log_ctx ctx;
97
#endif
98

99
   tmp = _eina_test_inlist_build(42);
100
   lst = eina_inlist_append(lst, EINA_INLIST_GET(tmp));
101
   fail_if(!lst);
102

103
   lst = eina_inlist_remove(lst, EINA_INLIST_GET(tmp));
104
   lst = eina_inlist_prepend(lst, EINA_INLIST_GET(tmp));
105

106
   tmp = _eina_test_inlist_build(1664);
107
   lst = eina_inlist_append_relative(lst, EINA_INLIST_GET(tmp), lst);
108
   fail_if(!lst);
109
   fail_if(EINA_INLIST_CONTAINER_GET(lst, Eina_Test_Inlist)->i != 42);
110

111
   prev = tmp;
112
   tmp = _eina_test_inlist_build(3227);
113
   lst = eina_inlist_prepend_relative(lst, EINA_INLIST_GET(
114
                                         tmp), EINA_INLIST_GET(prev));
115
   fail_if(!lst);
116
   fail_if(EINA_INLIST_CONTAINER_GET(lst, Eina_Test_Inlist)->i != 42);
117

118
   lst = eina_inlist_remove(lst, EINA_INLIST_GET(tmp));
119

120
   lst = eina_inlist_append_relative(lst, EINA_INLIST_GET(tmp), lst);
121
   lst = eina_inlist_remove(lst, EINA_INLIST_GET(tmp));
122

123
   lst = eina_inlist_prepend_relative(lst, EINA_INLIST_GET(tmp), lst);
124

125
   tmp = _eina_test_inlist_build(27);
126
   lst = eina_inlist_prepend_relative(lst, EINA_INLIST_GET(tmp), NULL);
127

128
   tmp = _eina_test_inlist_build(81);
129
   lst = eina_inlist_append_relative(lst, EINA_INLIST_GET(tmp), NULL);
130

131
   EINA_INLIST_FOREACH(lst, tmp)
132
   {
133
      switch (i)
134
        {
135
         case 0: fail_if(tmp->i != 27); break;
136

137
         case 1: fail_if(tmp->i != 3227); break;
138

139
         case 2: fail_if(tmp->i != 42); break;
140

141
         case 3: fail_if(tmp->i != 1664); break;
142

143
         case 4: fail_if(tmp->i != 81); break;
144
        }
145

146
      ++i;
147
   }
148

149
#ifdef EINA_SAFETY_CHECKS
150
   bkp = lst;
151
   eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx);
152

153
#define TEST_MAGIC_SAFETY(fn, _msg)              \
154
   ctx.msg = _msg;                               \
155
   ctx.fnc = fn;                                 \
156
   ctx.did = EINA_FALSE
157

158
#ifdef SHOW_LOG
159
   fprintf(stderr, "you should have a safety check failure below:\n");
160
#endif
161
   {
162
      Eina_Inlist *tmp2;
163

164
      TEST_MAGIC_SAFETY("eina_inlist_remove",
165
                        "safety check failed: list == NULL");
166

167
      tmp2 = eina_inlist_remove(NULL, EINA_INLIST_GET(tmp));
168
      fail_if(tmp2 != NULL);
169
      fail_unless(ctx.did);
170
   }
171

172
#ifdef SHOW_LOG
173
   fprintf(stderr, "you should have a safety check failure below:\n");
174
#endif
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);
179

180
#ifdef SHOW_LOG
181
   fprintf(stderr, "you should have a safety check failure below:\n");
182
#endif
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);
187

188
#ifdef SHOW_LOG
189
   fprintf(stderr, "you should have a safety check failure below:\n");
190
#endif
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);
195

196
#ifdef SHOW_LOG
197
   fprintf(stderr, "you should have a safety check failure below:\n");
198
#endif
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);
203

204
#ifdef SHOW_LOG
205
   fprintf(stderr, "you should have a safety check failure below:\n");
206
#endif
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);
211

212
#ifdef SHOW_LOG
213
   fprintf(stderr, "you should have a safety check failure below:\n");
214
#endif
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);
219

220
#ifdef SHOW_LOG
221
   fprintf(stderr, "you should have a safety check failure below:\n");
222
#endif
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);
227

228
#ifdef SHOW_LOG
229
   fprintf(stderr, "you should have a safety check failure below:\n");
230
#endif
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);
235
   lst = NULL;
236

237
#ifdef SHOW_LOG
238
   fprintf(stderr, "you should have a safety check failure below:\n");
239
#endif
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);
244

245
#ifdef SHOW_LOG
246
   fprintf(stderr, "you should have a safety check failure below:\n");
247
#endif
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);
252
   lst = NULL;
253

254
#ifdef SHOW_LOG
255
   fprintf(stderr, "you should have a safety check failure below:\n");
256
#endif
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);
261

262
#ifdef SHOW_LOG
263
   fprintf(stderr, "you should have a safety check failure below:\n");
264
#endif
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);
269
   lst = NULL;
270

271
   eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
272
   lst = bkp;
273
#endif
274

275
   tmpl = eina_inlist_last(lst);
276
   fail_if(tmpl == lst);
277
   tmpl = eina_inlist_first(tmpl);
278
   fail_if(tmpl != lst);
279

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);
283

284
   lst = eina_inlist_promote(lst, EINA_INLIST_GET(tmp));
285
   fail_if(lst != EINA_INLIST_GET(tmp));
286

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);
293

294
   while (lst)
295
      lst = eina_inlist_remove(lst, lst);
296

297
}
298
EFL_END_TEST
299

300
typedef struct _Eina_Test_Inlist_Sorted Eina_Test_Inlist_Sorted;
301
struct _Eina_Test_Inlist_Sorted
302
{
303
   EINA_INLIST;
304

305
   int value;
306
};
307

308
static int
309
_eina_test_inlist_cmp(const void *d1, const void *d2)
310
{
311
   const Eina_Test_Inlist_Sorted *t1 = d1;
312
   const Eina_Test_Inlist_Sorted *t2 = d2;
313

314
   return t1->value - t2->value;
315
}
316

317
static int
318
_eina_test_inlist_cmp2(const void *d1, const void *d2)
319
{
320
   const Eina_Test_Inlist_Sorted *t1 = d1;
321
   const Eina_Test_Inlist_Sorted *t2 = d2;
322

323
   return t2->value - t1->value;
324
}
325

326
#define _eina_test_inlist_check(LIST) \
327
{ \
328
   const Eina_Test_Inlist_Sorted *_t; \
329
   int _i = 0; \
330
 \
331
   EINA_INLIST_FOREACH(LIST, _t) \
332
     { \
333
        ck_assert_int_eq(_t->value, values[_i]); \
334
        _i++; \
335
     } \
336
}
337

338
#define _eina_test_inlist_check_reverse(LIST) \
339
{ \
340
   const Eina_Test_Inlist_Sorted *_t; \
341
   int _i = EINA_C_ARRAY_LENGTH(values) - 1; \
342
 \
343
   EINA_INLIST_FOREACH(LIST, _t) \
344
     { \
345
        ck_assert_int_eq(_t->value, values[_i]); \
346
        _i--; \
347
     } \
348
}
349

350
static int values_unsorted[] =
351
{
352
   3,
353
   10,
354
   2,
355
   1,
356
   8,
357
   4,
358
   4,
359
   5,
360
   7,
361
   9,
362
   6,
363
};
364

365
static int values[] =
366
{
367
   1,
368
   2,
369
   3,
370
   4,
371
   4,
372
   5,
373
   6,
374
   7,
375
   8,
376
   9,
377
   10,
378
};
379

380

381
static void
382
_eina_test_inlist_check_insert(const Eina_Inlist *list)
383
{
384
   const Eina_Test_Inlist_Sorted *t;
385
   int last_value = 0;
386

387
   EINA_INLIST_FOREACH(list, t)
388
     {
389
        ck_assert_int_ge(t->value, last_value);
390
        last_value = t->value;
391
     }
392
}
393

394
static void
395
_eina_test_inlist_check_insert_reverse(const Eina_Inlist *list)
396
{
397
   const Eina_Test_Inlist_Sorted *t;
398
   int last_value = 0;
399

400
   EINA_INLIST_FOREACH(list, t)
401
     {
402
        if (last_value)
403
          ck_assert_int_le(t->value, last_value);
404
        last_value = t->value;
405
     }
406
}
407
 
408
EFL_START_TEST(eina_inlist_sorted)
409
{
410
   Eina_Test_Inlist_Sorted *t, tmp[EINA_C_ARRAY_LENGTH(values_unsorted)];
411
   Eina_Inlist *list = NULL;
412
   Eina_Inlist *sorted = NULL;
413
   unsigned int i;
414

415
   for (i = 0; i < EINA_C_ARRAY_LENGTH(values_unsorted); ++i)
416
     {
417
        tmp[i].value = values_unsorted[i];
418

419
        list = eina_inlist_prepend(list, EINA_INLIST_GET(&tmp[i]));
420
     }
421

422
   list = eina_inlist_sort(list, _eina_test_inlist_cmp);
423

424
   _eina_test_inlist_check(list);
425

426
   i = EINA_C_ARRAY_LENGTH(values_unsorted) - 1;
427
   EINA_INLIST_FOREACH(list, t)
428
     {
429
        t->value = values_unsorted[i];
430
        i--;
431
     }
432

433
   while (list)
434
     {
435
        Eina_Inlist *p = list;
436

437
        list = eina_inlist_remove(list, list);
438

439
        sorted = eina_inlist_sorted_insert(sorted, p, _eina_test_inlist_cmp);
440
        _eina_test_inlist_check_insert(sorted);
441
     }
442
   _eina_test_inlist_check(sorted);
443
}
444
EFL_END_TEST
445

446
EFL_START_TEST(eina_inlist_sorted_state)
447
{
448
   Eina_Test_Inlist_Sorted tmp[EINA_C_ARRAY_LENGTH(values_unsorted)];
449
   Eina_Inlist_Sorted_State *state;
450
   Eina_Inlist *list = NULL;
451
   unsigned int i;
452

453
   state = eina_inlist_sorted_state_new();
454
   fail_if(!state);
455

456
   for (i = 0; i < EINA_C_ARRAY_LENGTH(values_unsorted); ++i)
457
     {
458
        tmp[i].value = values_unsorted[i];
459

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);
462
     }
463
   eina_inlist_sorted_state_free(state);
464
   _eina_test_inlist_check(list);
465
}
466
EFL_END_TEST
467
 
468
EFL_START_TEST(eina_inlist_sorted2)
469
{
470
   Eina_Test_Inlist_Sorted *t, tmp[EINA_C_ARRAY_LENGTH(values_unsorted)];
471
   Eina_Inlist *list = NULL;
472
   Eina_Inlist *sorted = NULL;
473
   unsigned int i;
474

475
   for (i = 0; i < EINA_C_ARRAY_LENGTH(values_unsorted); ++i)
476
     {
477
        tmp[i].value = values_unsorted[i];
478

479
        list = eina_inlist_prepend(list, EINA_INLIST_GET(&tmp[i]));
480
     }
481

482
   list = eina_inlist_sort(list, _eina_test_inlist_cmp2);
483

484
   _eina_test_inlist_check_reverse(list);
485

486
   i = EINA_C_ARRAY_LENGTH(values_unsorted) - 1;
487
   EINA_INLIST_FOREACH(list, t)
488
     {
489
        t->value = values_unsorted[i];
490
        i--;
491
     }
492

493
   while (list)
494
     {
495
        Eina_Inlist *p = list;
496

497
        list = eina_inlist_remove(list, list);
498

499
        sorted = eina_inlist_sorted_insert(sorted, p, _eina_test_inlist_cmp2);
500
        _eina_test_inlist_check_insert_reverse(sorted);
501
     }
502
   _eina_test_inlist_check_reverse(sorted);
503
}
504
EFL_END_TEST
505

506
EFL_START_TEST(eina_inlist_sorted_state2)
507
{
508
   Eina_Test_Inlist_Sorted tmp[EINA_C_ARRAY_LENGTH(values_unsorted)];
509
   Eina_Inlist_Sorted_State *state;
510
   Eina_Inlist *list = NULL;
511
   unsigned int i;
512

513
   state = eina_inlist_sorted_state_new();
514
   fail_if(!state);
515

516
   for (i = 0; i < EINA_C_ARRAY_LENGTH(values_unsorted); ++i)
517
     {
518
        tmp[i].value = values_unsorted[i];
519

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);
522
     }
523
   eina_inlist_sorted_state_free(state);
524
   _eina_test_inlist_check_reverse(list);
525
}
526
EFL_END_TEST
527

528
void
529
eina_test_inlist(TCase *tc)
530
{
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);
536
}
537

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.