test/list_tests.c

Sat, 14 Jan 2012 13:07:18 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 14 Jan 2012 13:07:18 +0100
changeset 21
d599fefc7620
parent 19
cdd7a3173249
child 22
76cdd8209f1f
permissions
-rw-r--r--

merge

9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * tests of list implementation
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 */
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 #include <stdio.h>
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 #include <stdlib.h>
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 #include "ucx/list.h"
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 #include "ucx/dlist.h"
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 struct test1_data {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 int values[3];
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 int i;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 };
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
16 int int_cmp(void* e1, void *e2, void *data) {
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
17 if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1;
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
19 int *i1 = (int*)e1, *i2 = (int*)e2;
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
20 int r = (*i1) - (*i2);
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
21 return (r < 0) ? -1 : (r == 0 ? 0 : 1);
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
22 }
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
23
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
24 int dlist_tests_foreach(void *v, void *custom) {
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 UcxDlist *dl = (UcxDlist*)v;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 struct test1_data *tdata = (struct test1_data*)custom;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 tdata->values[tdata->i] = *(int*)dl->data;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 tdata->i++;
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
30
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
31 return 0;
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
34 int dlist_tests() {
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 int r = 0;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 int v[8];
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 UcxDlist *dl = NULL;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 // build list 0,1,2,3,4,5,6,7
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
39 printf(" Test ucx_dlist_append\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
40 fflush(stdout);
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 for(int i=0;i<8;i++) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 v[i] = i;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 dl = ucx_dlist_append(dl, &v[i]);
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
46 printf(" Test ucx_dlist_get\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
47 fflush(stdout);
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 for(int i=0;i<8;i++) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 UcxDlist *elm = ucx_dlist_get(dl, i);
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 if(elm == NULL) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
52 r--;
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 if(elm->data == NULL) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
56 r--;
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 int *data = (int*)elm->data;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 if(*data != i) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
61 r--;
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
65 printf(" Test ucx_dlist_free\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
66 fflush(stdout);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
67 ucx_dlist_free(dl);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
68
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 dl = NULL;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 // build list 4,0,4
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
71 printf(" Test ucx_dlist_prepend\n");
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 dl = ucx_dlist_prepend(dl, &v[0]);
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
73 dl = ucx_dlist_prepend(dl, &v[4]);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
74 dl = ucx_dlist_append(dl, &v[4]);
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 struct test1_data tdata;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 tdata.i = 0;
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
78 ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 fprintf(stderr, "prepend/append test failed\n");
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
82 fprintf(stderr, "content: [%d, %d, %d]\n",
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
83 tdata.values[0], tdata.values[1], tdata.values[2]);
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
84 r--;
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
87 printf(" Test ucx_dlist_equals\n");
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
88 UcxDlist *dl2 = NULL;
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
89 dl2 = ucx_dlist_append(dl2, &v[4]);
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
90 dl2 = ucx_dlist_append(dl2, &v[0]);
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
91 dl2 = ucx_dlist_append(dl2, &v[4]);
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
92 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
93 fprintf(stderr, "ucx_dlist_equals failed (false negative)\n");
19
cdd7a3173249 Removed linked list from tests (assume that they are correct if the dlist tests are correct)
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
94 r--;
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
95 }
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
96 dl2->next->data = NULL;
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
97 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
98 fprintf(stderr, "ucx_dlist_equals failed (false positive)\n");
19
cdd7a3173249 Removed linked list from tests (assume that they are correct if the dlist tests are correct)
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
99 r--;
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
100 }
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
101 dl2->next->data = &(tdata.values[1]);
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
102 if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
103 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n");
19
cdd7a3173249 Removed linked list from tests (assume that they are correct if the dlist tests are correct)
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
104 r--;
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
105 }
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
106 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
107 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n");
19
cdd7a3173249 Removed linked list from tests (assume that they are correct if the dlist tests are correct)
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
108 r--;
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
109 }
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
110 ucx_dlist_free(dl2);
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
111
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
112 printf(" Test ucx_dlist_clone\n");
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
113 dl2 = ucx_dlist_clone(dl, NULL, NULL);
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
114 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
115 fprintf(stderr, "ucx_dlist_clone (without copy) failed\n");
19
cdd7a3173249 Removed linked list from tests (assume that they are correct if the dlist tests are correct)
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
116 r--;
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
117 }
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
118 ucx_dlist_free(dl2);
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
119
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
120 printf(" TODO: test clone with copy\n");
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
121
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 return r;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 }
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
124

mercurial