summaryrefslogtreecommitdiff
path: root/src/tests/eet/eet_test_common.c
diff options
context:
space:
mode:
authorVincent Torri <vincent dot torri at gmail dot com>2016-02-04 14:29:22 +0100
committerTom Hacohen <tom@stosb.com>2016-02-16 12:41:06 +0000
commit8cc995cca849033e00d0f51dff0a0d6cba46b717 (patch)
tree4b518399a60c60bd2309d8bcb2dac35de941ccb6 /src/tests/eet/eet_test_common.c
parent885a1d2f7db673d1103f180cc517f4874067bd55 (diff)
Test rework #6: Eet
This one is big: I had to split the huge eet_suite.c into separate test files and it needs more review
Diffstat (limited to 'src/tests/eet/eet_test_common.c')
-rw-r--r--src/tests/eet/eet_test_common.c649
1 files changed, 649 insertions, 0 deletions
diff --git a/src/tests/eet/eet_test_common.c b/src/tests/eet/eet_test_common.c
new file mode 100644
index 0000000000..dba9ab19da
--- /dev/null
+++ b/src/tests/eet/eet_test_common.c
@@ -0,0 +1,649 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#include <string.h>
6
7#include <Eina.h>
8#include <Eet.h>
9
10#include <check.h>
11
12#include "eet_test_common.h"
13
14int i42 = 42;
15int i7 = 7;
16Eina_Bool _dump_call = EINA_FALSE;
17
18void
19append_string(void *data,
20 const char *str)
21{
22 char **string = data;
23 int length;
24
25 if (!data)
26 return;
27
28 length = *string ? strlen(*string) : 0;
29 *string = realloc(*string, strlen(str) + length + 1);
30
31 fail_unless(*string); // Fail test case if realloc fails.
32
33 memcpy((*string) + length, str, strlen(str) + 1);
34}
35
36void
37append_strbuf_string(void *data, const char *string)
38{
39 Eina_Strbuf *strbuf = data;
40 eina_strbuf_append(strbuf, string);
41}
42
43Eina_Bool
44func(EINA_UNUSED const Eina_Hash *hash,
45 const void *key,
46 void *data,
47 void *fdata)
48{
49 int *res = fdata;
50
51 if (strcmp(key, EET_TEST_KEY1) != 0
52 && strcmp(key, EET_TEST_KEY2) != 0)
53 *res = 1;
54
55 if (eet_test_ex_check(data, 2, _dump_call))
56 *res = 1;
57
58 return EINA_TRUE;
59} /* func */
60
61Eina_Bool
62func7(EINA_UNUSED const Eina_Hash *hash,
63 EINA_UNUSED const void *key,
64 void *data,
65 void *fdata)
66{
67 int *res = fdata;
68 int *val;
69
70 val = data;
71 if (!val)
72 *res = 1;
73
74 if (*val != 7)
75 *res = 1;
76
77 return EINA_TRUE;
78} /* func7 */
79
80static char *
81_eet_str_direct_alloc(const char *str)
82{
83 return (char *)str;
84} /* _eet_str_direct_alloc */
85
86static void
87_eet_str_direct_free(const char *str)
88{
89 /* FIXME: Use attribute unused */
90 (void)str;
91} /* _eet_str_direct_free */
92
93static void
94_eet_eina_hash_foreach(void *hash,
95 Eina_Hash_Foreach cb,
96 void *fdata)
97{
98 if (hash)
99 eina_hash_foreach(hash, cb, fdata);
100} /* _eet_eina_hash_foreach */
101
102/* Internal wrapper for eina_hash */
103static Eina_Hash *
104_eet_eina_hash_add(Eina_Hash *hash,
105 const char *key,
106 const void *data)
107{
108 if (!hash)
109 hash = eina_hash_string_superfast_new(NULL);
110
111 if (!hash)
112 return NULL;
113
114 eina_hash_add(hash, key, data);
115 return hash;
116} /* _eet_eina_hash_add */
117
118static void
119_eet_eina_hash_free(Eina_Hash *hash)
120{
121 if (hash)
122 eina_hash_free(hash);
123} /* _eet_eina_hash_free */
124
125void
126eet_test_setup_eddc(Eet_Data_Descriptor_Class *eddc)
127{
128 eddc->version = EET_DATA_DESCRIPTOR_CLASS_VERSION;
129 eddc->func.mem_alloc = NULL;
130 eddc->func.mem_free = NULL;
131 eddc->func.str_alloc = NULL;
132 eddc->func.str_free = NULL;
133 eddc->func.list_next = (void *)eina_list_next;
134 eddc->func.list_append = (void *)eina_list_append;
135 eddc->func.list_data = (void *)eina_list_data_get;
136 eddc->func.list_free = (void *)eina_list_free;
137 eddc->func.hash_foreach = (void *)_eet_eina_hash_foreach;
138 eddc->func.hash_add = (void *)_eet_eina_hash_add;
139 eddc->func.hash_free = (void *)_eet_eina_hash_free;
140 eddc->func.str_direct_alloc = (void *)_eet_str_direct_alloc;
141 eddc->func.str_direct_free = (void *)_eet_str_direct_free;
142 eddc->func.array_alloc = NULL;
143 eddc->func.array_free = NULL;
144}
145
146void
147eet_build_basic_descriptor(Eet_Data_Descriptor *edd)
148{
149 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
150 Eet_Test_Basic_Type,
151 "c",
152 c,
153 EET_T_CHAR);
154 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
155 Eet_Test_Basic_Type,
156 "s",
157 s,
158 EET_T_SHORT);
159 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
160 Eet_Test_Basic_Type,
161 "i",
162 i,
163 EET_T_INT);
164 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
165 Eet_Test_Basic_Type,
166 "l",
167 l,
168 EET_T_LONG_LONG);
169 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
170 Eet_Test_Basic_Type,
171 "str",
172 str,
173 EET_T_STRING);
174 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
175 Eet_Test_Basic_Type,
176 "istr",
177 istr,
178 EET_T_INLINED_STRING);
179 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
180 Eet_Test_Basic_Type,
181 "f1",
182 f1,
183 EET_T_FLOAT);
184 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
185 Eet_Test_Basic_Type,
186 "f2",
187 f2,
188 EET_T_FLOAT);
189 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
190 Eet_Test_Basic_Type,
191 "d",
192 d,
193 EET_T_DOUBLE);
194 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
195 Eet_Test_Basic_Type,
196 "uc",
197 uc,
198 EET_T_UCHAR);
199 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
200 Eet_Test_Basic_Type,
201 "us",
202 us,
203 EET_T_USHORT);
204 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
205 Eet_Test_Basic_Type,
206 "ui",
207 ui,
208 EET_T_UINT);
209 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
210 Eet_Test_Basic_Type,
211 "ul",
212 ul,
213 EET_T_ULONG_LONG);
214 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
215 Eet_Test_Basic_Type,
216 "vp",
217 vp,
218 EET_T_VALUE);
219
220 EET_DATA_DESCRIPTOR_ADD_SUB(edd, Eet_Test_Basic_Type, "empty", empty, edd);
221 EET_DATA_DESCRIPTOR_ADD_SUB(edd, Eet_Test_Basic_Type, "with", with, edd);
222}
223
224void
225eet_test_basic_check(Eet_Test_Basic_Type *result,
226 int i,
227 Eina_Bool dumper)
228{
229 int test = -1;
230 float tmp;
231
232 fail_if(result->c != EET_TEST_CHAR);
233 fail_if(result->s != EET_TEST_SHORT);
234 fail_if(result->i != EET_TEST_INT + i);
235 fail_if(result->l != (long long)EET_TEST_LONG_LONG);
236 fail_if(strcmp(result->str, EET_TEST_STRING) != 0);
237 fail_if(strcmp(result->istr, EET_TEST_STRING) != 0);
238 fail_if(result->uc != EET_TEST_CHAR);
239 fail_if(result->us != EET_TEST_SHORT);
240 fail_if(result->ui != EET_TEST_INT);
241 fail_if(result->ul != EET_TEST_LONG_LONG);
242 if (!dumper)
243 {
244 fail_if(result->vp == NULL);
245 eina_value_get(result->vp, &test);
246 fail_if(test != EET_TEST_INT + i);
247 }
248 else
249 {
250 fail_if(result->vp != NULL);
251 }
252
253 tmp = (result->f1 + EET_TEST_FLOAT);
254 if (tmp < 0)
255 tmp = -tmp;
256
257 fail_if(tmp > 0.005);
258
259 tmp = (result->f2 - EET_TEST_FLOAT4);
260 if (tmp < 0)
261 tmp = -tmp;
262
263 fail_if(tmp > 0.005);
264
265 tmp = (result->d + EET_TEST_DOUBLE);
266 if (tmp < 0)
267 tmp = -tmp;
268
269 fail_if(tmp > 0.00005);
270
271 fail_if(result->empty != NULL);
272 if (i == 0)
273 {
274 Eet_Test_Basic_Type *tmp2;
275
276 tmp2 = result->with;
277 fail_if(tmp2 == NULL);
278
279 fail_if(tmp2->c != EET_TEST_CHAR);
280 fail_if(tmp2->s != EET_TEST_SHORT);
281 fail_if(tmp2->i != EET_TEST_INT + i + 1);
282 fail_if(tmp2->l != (long long)EET_TEST_LONG_LONG);
283 fail_if(strcmp(tmp2->str, EET_TEST_STRING) != 0);
284 fail_if(strcmp(tmp2->istr, EET_TEST_STRING) != 0);
285 fail_if(tmp2->uc != EET_TEST_CHAR);
286 fail_if(tmp2->us != EET_TEST_SHORT);
287 fail_if(tmp2->ui != EET_TEST_INT);
288 fail_if(tmp2->ul != EET_TEST_LONG_LONG);
289 fail_if(tmp2->vp != NULL);
290 }
291 else
292 fail_if(result->with != NULL);
293}
294
295void
296eet_build_ex_descriptor(Eet_Data_Descriptor *edd, Eina_Bool stream)
297{
298 Eet_Data_Descriptor_Class eddc;
299 Eet_Test_Ex_Type etbt;
300 Eet_Data_Descriptor *eddb;
301
302 if (stream)
303 {
304 eet_eina_stream_data_descriptor_class_set(&eddc,
305 sizeof (Eet_Data_Descriptor_Class),
306 "Eet_Test_Basic_Type",
307 sizeof(Eet_Test_Basic_Type));
308 eddb = eet_data_descriptor_stream_new(&eddc);
309 }
310 else
311 {
312 eet_eina_file_data_descriptor_class_set(&eddc,
313 sizeof (Eet_Data_Descriptor_Class),
314 "Eet_Test_Basic_Type",
315 sizeof(Eet_Test_Basic_Type));
316 eddb = eet_data_descriptor_file_new(&eddc);
317 }
318 fail_if(!eddb);
319
320 eet_build_basic_descriptor(eddb);
321
322 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
323 Eet_Test_Ex_Type,
324 "c",
325 c,
326 EET_T_CHAR);
327 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
328 Eet_Test_Ex_Type,
329 "s",
330 s,
331 EET_T_SHORT);
332 EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "i", i, EET_T_INT);
333 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
334 Eet_Test_Ex_Type,
335 "l",
336 l,
337 EET_T_LONG_LONG);
338 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
339 Eet_Test_Ex_Type,
340 "str",
341 str,
342 EET_T_STRING);
343 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
344 Eet_Test_Ex_Type,
345 "istr",
346 istr,
347 EET_T_INLINED_STRING);
348 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
349 Eet_Test_Ex_Type,
350 "f1",
351 f1,
352 EET_T_FLOAT);
353 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
354 Eet_Test_Ex_Type,
355 "f2",
356 f2,
357 EET_T_FLOAT);
358 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
359 Eet_Test_Ex_Type,
360 "f3",
361 f3,
362 EET_T_FLOAT);
363 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
364 Eet_Test_Ex_Type,
365 "f4",
366 f4,
367 EET_T_FLOAT);
368 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
369 Eet_Test_Ex_Type,
370 "d1",
371 d1,
372 EET_T_DOUBLE);
373 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
374 Eet_Test_Ex_Type,
375 "d2",
376 d2,
377 EET_T_DOUBLE);
378 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
379 Eet_Test_Ex_Type,
380 "d3",
381 d3,
382 EET_T_DOUBLE);
383 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
384 Eet_Test_Ex_Type,
385 "d4",
386 d4,
387 EET_T_DOUBLE);
388 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
389 Eet_Test_Ex_Type,
390 "uc",
391 uc,
392 EET_T_UCHAR);
393 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
394 Eet_Test_Ex_Type,
395 "us",
396 us,
397 EET_T_USHORT);
398 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
399 Eet_Test_Ex_Type,
400 "ui",
401 ui,
402 EET_T_UINT);
403 EET_DATA_DESCRIPTOR_ADD_BASIC(edd,
404 Eet_Test_Ex_Type,
405 "ul",
406 ul,
407 EET_T_ULONG_LONG);
408 EET_DATA_DESCRIPTOR_ADD_ARRAY(edd,
409 Eet_Test_Ex_Type,
410 "sarray1",
411 sarray1,
412 eddb);
413 EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY(edd,
414 Eet_Test_Ex_Type,
415 "varray2",
416 varray2,
417 eddb);
418 eet_data_descriptor_element_add(edd,
419 "varray1",
420 EET_T_INT,
421 EET_G_VAR_ARRAY,
422 (char *)(&(etbt.varray1)) - (char *)(&(etbt)),
423 (char *)(&(etbt.varray1_count)) -
424 (char *)(&(etbt)),
425 /* 0, */ NULL,
426 NULL);
427 eet_data_descriptor_element_add(edd, "sarray2", EET_T_INT, EET_G_ARRAY,
428 (char *)(&(etbt.sarray2)) - (char *)(&(etbt)),
429 /* 0, */ sizeof(etbt.sarray2) /
430 sizeof(etbt.sarray2[0]), NULL, NULL);
431 eet_data_descriptor_element_add(edd, "charray", EET_T_STRING, EET_G_ARRAY,
432 (char *)(&(etbt.charray)) - (char *)(&(etbt)),
433 /* 0, */ sizeof(etbt.charray) /
434 sizeof(etbt.charray[0]), NULL, NULL);
435 EET_DATA_DESCRIPTOR_ADD_LIST(edd, Eet_Test_Ex_Type, "list", list, edd);
436 EET_DATA_DESCRIPTOR_ADD_HASH(edd, Eet_Test_Ex_Type, "hash", hash, edd);
437 eet_data_descriptor_element_add(edd, "ilist", EET_T_INT, EET_G_LIST,
438 (char *)(&(etbt.ilist)) - (char *)(&(etbt)),
439 0, /* 0, */ NULL, NULL);
440 eet_data_descriptor_element_add(edd, "ihash", EET_T_INT, EET_G_HASH,
441 (char *)(&(etbt.ihash)) - (char *)(&(etbt)),
442 0, /* 0, */ NULL, NULL);
443 eet_data_descriptor_element_add(edd, "slist", EET_T_STRING, EET_G_LIST,
444 (char *)(&(etbt.slist)) - (char *)(&(etbt)),
445 0, /* 0, */ NULL, NULL);
446 eet_data_descriptor_element_add(edd, "shash", EET_T_STRING, EET_G_HASH,
447 (char *)(&(etbt.shash)) - (char *)(&(etbt)),
448 0, /* 0, */ NULL, NULL);
449}
450
451Eet_Test_Ex_Type *
452eet_test_ex_set(Eet_Test_Ex_Type *res,
453 int offset)
454{
455 unsigned int i;
456
457 if (!res)
458 res = malloc(sizeof(Eet_Test_Ex_Type));
459
460 if (!res)
461 return NULL;
462
463 res->c = EET_TEST_CHAR + offset;
464 res->s = EET_TEST_SHORT + offset;
465 res->i = EET_TEST_INT + offset;
466 res->l = EET_TEST_LONG_LONG + offset;
467 res->str = EET_TEST_STRING;
468 res->istr = EET_TEST_STRING;
469 res->f1 = EET_TEST_FLOAT + offset;
470 res->f2 = -(EET_TEST_FLOAT2 + offset);
471 res->f3 = EET_TEST_FLOAT3 + offset;
472 res->f4 = EET_TEST_FLOAT2 + offset;
473 res->d1 = EET_TEST_DOUBLE + offset;
474 res->d2 = -(EET_TEST_DOUBLE2 + offset);
475 res->d3 = EET_TEST_DOUBLE3 + offset;
476 res->d4 = EET_TEST_DOUBLE2 + offset;
477 res->list = NULL;
478 res->hash = NULL;
479 res->ilist = NULL;
480 res->ihash = NULL;
481 res->slist = NULL;
482 res->shash = NULL;
483 for (i = 0; i < sizeof(res->charray) / sizeof(res->charray[0]); ++i)
484 res->charray[i] = NULL;
485
486 res->varray2 = malloc(sizeof (Eet_Test_Basic_Type) * 10);
487 res->varray1 = malloc(sizeof (int) * 5);
488 fail_if(!res->varray1 || !res->varray2);
489 for (i = 0; i < 10; ++i)
490 {
491 eet_test_basic_set(res->sarray1 + i, i);
492 eet_test_basic_set(res->varray2 + i, i);
493 }
494 res->varray2_count = 10;
495 for (i = 0; i < 5; ++i)
496 {
497 res->sarray2[i] = i * 42 + 1;
498 res->varray1[i] = i * 42 + 1;
499 }
500 res->varray1_count = 5;
501
502 res->uc = EET_TEST_CHAR + offset;
503 res->us = EET_TEST_SHORT + offset;
504 res->ui = EET_TEST_INT + offset;
505 res->ul = EET_TEST_LONG_LONG + offset;
506
507 return res;
508}
509
510int
511eet_test_ex_check(Eet_Test_Ex_Type *stuff,
512 int offset,
513 Eina_Bool dumper)
514{
515 double tmp;
516 unsigned int i;
517
518 if (!stuff)
519 return 1;
520
521 if (stuff->c != EET_TEST_CHAR + offset)
522 return 1;
523
524 if (stuff->s != EET_TEST_SHORT + offset)
525 return 1;
526
527 if (stuff->i != EET_TEST_INT + offset)
528 return 1;
529
530 if (stuff->l != EET_TEST_LONG_LONG + offset)
531 return 1;
532
533 if (strcmp(stuff->str, EET_TEST_STRING) != 0)
534 return 1;
535
536 if (strcmp(stuff->istr, EET_TEST_STRING) != 0)
537 return 1;
538
539 tmp = stuff->f1 - (EET_TEST_FLOAT + offset);
540 if (tmp < 0)
541 tmp = -tmp;
542
543 if (tmp > 0.005)
544 return 1;
545
546 tmp = stuff->d1 - (EET_TEST_DOUBLE + offset);
547 if (tmp < 0)
548 tmp = -tmp;
549
550 if (tmp > 0.00005)
551 return 1;
552
553 if (stuff->f2 != -(EET_TEST_FLOAT2 + offset))
554 return 1;
555
556 if (stuff->d2 != -(EET_TEST_DOUBLE2 + offset))
557 return 1;
558
559 if (stuff->f3 != EET_TEST_FLOAT3 + offset)
560 return 1;
561
562 if (stuff->d3 != EET_TEST_DOUBLE3 + offset)
563 return 1;
564
565 if (stuff->f4 != EET_TEST_FLOAT2 + offset)
566 return 1;
567
568 if (stuff->d4 != EET_TEST_DOUBLE2 + offset)
569 return 1;
570
571 if (stuff->uc != EET_TEST_CHAR + offset)
572 return 1;
573
574 if (stuff->us != EET_TEST_SHORT + offset)
575 return 1;
576
577 if (stuff->ui != (unsigned int)EET_TEST_INT + offset)
578 return 1;
579
580 if (stuff->ul != EET_TEST_LONG_LONG + offset)
581 return 1;
582
583 if (stuff->varray1_count != 5)
584 return 1;
585
586 if (stuff->varray2_count != 10)
587 return 1;
588
589 for (i = 0; i < 5; ++i)
590 if (stuff->sarray2[i] != i * 42 + 1 && stuff->varray1[i] != i * 42 + 1)
591 return 1;
592
593 for (i = 0; i < 10; ++i)
594 {
595 eet_test_basic_check(stuff->sarray1 + i, i, dumper);
596 eet_test_basic_check(stuff->varray2 + i, i, dumper);
597 }
598
599 return 0;
600}
601
602void
603eet_test_basic_set(Eet_Test_Basic_Type *res,
604 int i)
605{
606 res->c = EET_TEST_CHAR;
607 res->s = EET_TEST_SHORT;
608 res->i = EET_TEST_INT + i;
609 res->l = EET_TEST_LONG_LONG;
610 res->str = EET_TEST_STRING;
611 res->istr = EET_TEST_STRING;
612 res->f1 = -EET_TEST_FLOAT;
613 res->d = -EET_TEST_DOUBLE;
614 res->f2 = EET_TEST_FLOAT4;
615 res->uc = EET_TEST_CHAR;
616 res->us = EET_TEST_SHORT;
617 res->ui = EET_TEST_INT;
618 res->ul = EET_TEST_LONG_LONG;
619 res->empty = NULL;
620 res->with = NULL;
621 res->vp = eina_value_new(EINA_VALUE_TYPE_INT);
622 eina_value_set(res->vp, EET_TEST_INT + i);
623
624 if (i == 0)
625 {
626 Eet_Test_Basic_Type *tmp;
627
628 tmp = malloc(sizeof (Eet_Test_Basic_Type));
629 fail_if(!tmp);
630
631 res->with = tmp;
632 tmp->c = EET_TEST_CHAR;
633 tmp->s = EET_TEST_SHORT;
634 tmp->i = EET_TEST_INT + i + 1;
635 tmp->l = EET_TEST_LONG_LONG;
636 tmp->str = EET_TEST_STRING;
637 tmp->istr = EET_TEST_STRING;
638 tmp->f1 = -EET_TEST_FLOAT;
639 tmp->d = -EET_TEST_DOUBLE;
640 tmp->f2 = EET_TEST_FLOAT4;
641 tmp->uc = EET_TEST_CHAR;
642 tmp->us = EET_TEST_SHORT;
643 tmp->ui = EET_TEST_INT;
644 tmp->ul = EET_TEST_LONG_LONG;
645 tmp->empty = NULL;
646 tmp->with = NULL;
647 tmp->vp = NULL;
648 }
649}