Sun, 15 Jan 2012 14:12:34 +0100
added ucx_dlist_remove and tests + fixed makefile error
9 | 1 | /* |
2 | * tests of list implementation | |
3 | */ | |
4 | ||
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | ||
8 | #include "ucx/list.h" | |
9 | #include "ucx/dlist.h" | |
10 | ||
11 | struct test1_data { | |
12 | int values[3]; | |
13 | int i; | |
14 | }; | |
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 | 25 | UcxDlist *dl = (UcxDlist*)v; |
26 | struct test1_data *tdata = (struct test1_data*)custom; | |
27 | ||
28 | tdata->values[tdata->i] = *(int*)dl->data; | |
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 | 32 | } |
33 | ||
11 | 34 | int dlist_tests() { |
9 | 35 | int r = 0; |
36 | int v[8]; | |
37 | UcxDlist *dl = NULL; | |
38 | // build list 0,1,2,3,4,5,6,7 | |
11 | 39 | printf(" Test ucx_dlist_append\n"); |
40 | fflush(stdout); | |
9 | 41 | for(int i=0;i<8;i++) { |
42 | v[i] = i; | |
43 | dl = ucx_dlist_append(dl, &v[i]); | |
44 | } | |
45 | ||
11 | 46 | printf(" Test ucx_dlist_get\n"); |
47 | fflush(stdout); | |
9 | 48 | for(int i=0;i<8;i++) { |
49 | UcxDlist *elm = ucx_dlist_get(dl, i); | |
50 | if(elm == NULL) { | |
51 | fprintf(stderr, "ucx_dlist_get failed: element is NULL\n"); | |
11 | 52 | r--; |
9 | 53 | } |
54 | if(elm->data == NULL) { | |
55 | fprintf(stderr, "ucx_dlist_get failed: data is NULL\n"); | |
11 | 56 | r--; |
9 | 57 | } |
58 | int *data = (int*)elm->data; | |
59 | if(*data != i) { | |
60 | fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); | |
11 | 61 | r--; |
9 | 62 | } |
63 | } | |
64 | ||
11 | 65 | printf(" Test ucx_dlist_free\n"); |
66 | fflush(stdout); | |
67 | ucx_dlist_free(dl); | |
68 | ||
9 | 69 | dl = NULL; |
70 | // build list 4,0,4 | |
11 | 71 | printf(" Test ucx_dlist_prepend\n"); |
9 | 72 | dl = ucx_dlist_prepend(dl, &v[0]); |
11 | 73 | dl = ucx_dlist_prepend(dl, &v[4]); |
74 | dl = ucx_dlist_append(dl, &v[4]); | |
9 | 75 | |
76 | struct test1_data tdata; | |
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 | 79 | |
80 | if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { | |
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 | 84 | r--; |
9 | 85 | } |
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 | |
22
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
122 | ucx_dlist_free(dl); |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
123 | |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
124 | dl = NULL; |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
125 | printf(" Test ucx_dlist_remove\n"); |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
126 | dl = ucx_dlist_append(dl, &v[4]); |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
127 | dl = ucx_dlist_append(dl, &v[0]); |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
128 | dl = ucx_dlist_append(dl, &v[3]); |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
129 | dl = ucx_dlist_remove(dl, dl->next); |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
130 | if (ucx_dlist_size(dl) == 2) { |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
131 | if ((*((int*)(dl->data)) != 4) || (*((int*)(dl->next->data)) != 3)) { |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
132 | fprintf(stderr, "ucx_dlist_remove failed (wrong data)\n"); |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
133 | r--; |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
134 | } |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
135 | } else { |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
136 | fprintf(stderr, "ucx_dlist_remove failed (wrong size)\n"); |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
137 | r--; |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
138 | } |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
139 | dl = ucx_dlist_remove(dl, dl); |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
140 | if (ucx_dlist_size(dl) == 1) { |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
141 | if ((*((int*)(dl->data)) != 3)) { |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
142 | fprintf(stderr, "ucx_dlist_remove first failed (wrong data)\n"); |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
143 | r--; |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
144 | } |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
145 | } else { |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
146 | fprintf(stderr, "ucx_dlist_remove first failed (wrong size)\n"); |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
147 | r--; |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
148 | } |
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
149 | |
9 | 150 | return r; |
151 | } | |
11 | 152 |