source: branches/samhain-2_2-branch/src/cutest_zAVLTree.c

Last change on this file was 17, checked in by katerina, 19 years ago

Optimized version of tiger algorithm, and basic ingredients for unit testing (part 1)

File size: 7.3 KB
Line 
1
2#include "config_xor.h"
3
4#include <string.h>
5#include "CuTest.h"
6#include "zAVLTree.h"
7
8struct ztest {
9 char name[32];
10};
11
12static zAVLTree * ztest_tree = NULL;
13
14static zAVLKey ztest_key (void const * arg)
15{
16 const struct ztest * sa = (const struct ztest *) arg;
17 return (zAVLKey) sa->name;
18}
19
20static void free_node (void * inptr)
21{
22 struct ztest * ptr = (struct ztest *) inptr;
23 ptr->name[0] = '\0';
24}
25
26void Test_zAVLTree(CuTest *tc) {
27 zAVLCursor cursor;
28
29 int result;
30 int counter = 0;
31
32 struct ztest z1 = { "abc" };
33 struct ztest z2 = { "aac" };
34 struct ztest z3 = { "aaa1" };
35 struct ztest z4 = { "aaa3" };
36 struct ztest z5 = { "aaa2" };
37 struct ztest z6 = { "aaa6" };
38 struct ztest z7 = { "aaa5" };
39 struct ztest z8 = { "aaa4" };
40
41 struct ztest * ptr;
42
43 ptr = zAVLFirst(&cursor, ztest_tree);
44 CuAssertTrue(tc, NULL == ptr);
45
46 ztest_tree = zAVLAllocTree (ztest_key);
47 CuAssertPtrNotNull(tc, ztest_tree);
48
49 do {
50
51 ++counter;
52
53 result = zAVLInsert(ztest_tree, &z1);
54 CuAssertTrue(tc, 0 == result);
55 result = zAVLInsert(ztest_tree, &z2);
56 CuAssertTrue(tc, 0 == result);
57 result = zAVLInsert(ztest_tree, &z3);
58 CuAssertTrue(tc, 0 == result);
59 result = zAVLInsert(ztest_tree, &z4);
60 CuAssertTrue(tc, 0 == result);
61 result = zAVLInsert(ztest_tree, &z5);
62 CuAssertTrue(tc, 0 == result);
63 result = zAVLInsert(ztest_tree, &z6);
64 CuAssertTrue(tc, 0 == result);
65 result = zAVLInsert(ztest_tree, &z7);
66 CuAssertTrue(tc, 0 == result);
67 result = zAVLInsert(ztest_tree, &z8);
68 CuAssertTrue(tc, 0 == result);
69
70 ptr = zAVLFirst(&cursor, ztest_tree);
71 CuAssertStrEquals(tc, ptr->name, z3.name);
72 ptr = zAVLNext(&cursor);
73 CuAssertStrEquals(tc, ptr->name, z5.name);
74 ptr = zAVLNext(&cursor);
75 CuAssertStrEquals(tc, ptr->name, z4.name);
76 ptr = zAVLNext(&cursor);
77 CuAssertStrEquals(tc, ptr->name, z8.name);
78 ptr = zAVLNext(&cursor);
79 CuAssertStrEquals(tc, ptr->name, z7.name);
80 ptr = zAVLNext(&cursor);
81 CuAssertStrEquals(tc, ptr->name, z6.name);
82 ptr = zAVLNext(&cursor);
83 CuAssertStrEquals(tc, ptr->name, z2.name);
84 ptr = zAVLNext(&cursor);
85 CuAssertStrEquals(tc, ptr->name, z1.name);
86 ptr = zAVLNext(&cursor);
87 CuAssertTrue(tc, NULL == ptr);
88
89 result = zAVLInsert(ztest_tree, &z8);
90 CuAssertTrue(tc, 0 != result);
91 result = zAVLInsert(ztest_tree, &z7);
92 CuAssertTrue(tc, 0 != result);
93 result = zAVLInsert(ztest_tree, &z6);
94 CuAssertTrue(tc, 0 != result);
95 result = zAVLInsert(ztest_tree, &z5);
96 CuAssertTrue(tc, 0 != result);
97
98 ptr = zAVLSearch(ztest_tree, z1.name);
99 CuAssertStrEquals(tc, ptr->name, z1.name);
100 ptr = zAVLSearch(ztest_tree, z2.name);
101 CuAssertStrEquals(tc, ptr->name, z2.name);
102 ptr = zAVLSearch(ztest_tree, z3.name);
103 CuAssertStrEquals(tc, ptr->name, z3.name);
104 ptr = zAVLSearch(ztest_tree, z4.name);
105 CuAssertStrEquals(tc, ptr->name, z4.name);
106
107 ptr = zAVLFirst(&cursor, ztest_tree);
108 CuAssertStrEquals(tc, ptr->name, z3.name);
109 ptr = zAVLNext(&cursor);
110 CuAssertStrEquals(tc, ptr->name, z5.name);
111 ptr = zAVLNext(&cursor);
112 CuAssertStrEquals(tc, ptr->name, z4.name);
113 ptr = zAVLNext(&cursor);
114 CuAssertStrEquals(tc, ptr->name, z8.name);
115 ptr = zAVLNext(&cursor);
116 CuAssertStrEquals(tc, ptr->name, z7.name);
117 ptr = zAVLNext(&cursor);
118 CuAssertStrEquals(tc, ptr->name, z6.name);
119 ptr = zAVLNext(&cursor);
120 CuAssertStrEquals(tc, ptr->name, z2.name);
121 ptr = zAVLNext(&cursor);
122 CuAssertStrEquals(tc, ptr->name, z1.name);
123 ptr = zAVLNext(&cursor);
124 CuAssertTrue(tc, NULL == ptr);
125
126
127 ptr = zAVLSearch(ztest_tree, z5.name);
128 CuAssertStrEquals(tc, ptr->name, z5.name);
129 ptr = zAVLSearch(ztest_tree, z6.name);
130 CuAssertStrEquals(tc, ptr->name, z6.name);
131 ptr = zAVLSearch(ztest_tree, z7.name);
132 CuAssertStrEquals(tc, ptr->name, z7.name);
133 ptr = zAVLSearch(ztest_tree, z8.name);
134 CuAssertStrEquals(tc, ptr->name, z8.name);
135 ptr = zAVLSearch(ztest_tree, "foobar");
136 CuAssertTrue(tc, NULL == ptr);
137
138 result = zAVLDelete(ztest_tree, z8.name);
139 CuAssertTrue(tc, 0 == result);
140 ptr = zAVLSearch(ztest_tree, z8.name);
141 CuAssertTrue(tc, NULL == ptr);
142
143 result = zAVLDelete(ztest_tree, z3.name);
144 CuAssertTrue(tc, 0 == result);
145 ptr = zAVLSearch(ztest_tree, z3.name);
146 CuAssertTrue(tc, NULL == ptr);
147
148 result = zAVLDelete(ztest_tree, z1.name);
149 CuAssertTrue(tc, 0 == result);
150 ptr = zAVLSearch(ztest_tree, z1.name);
151 CuAssertTrue(tc, NULL == ptr);
152
153 result = zAVLInsert(ztest_tree, &z1);
154 CuAssertTrue(tc, 0 == result);
155 result = zAVLInsert(ztest_tree, &z8);
156 CuAssertTrue(tc, 0 == result);
157 result = zAVLInsert(ztest_tree, &z3);
158 CuAssertTrue(tc, 0 == result);
159
160 ptr = zAVLFirst(&cursor, ztest_tree);
161 CuAssertStrEquals(tc, ptr->name, z3.name);
162 ptr = zAVLNext(&cursor);
163 CuAssertStrEquals(tc, ptr->name, z5.name);
164 ptr = zAVLNext(&cursor);
165 CuAssertStrEquals(tc, ptr->name, z4.name);
166 ptr = zAVLNext(&cursor);
167 CuAssertStrEquals(tc, ptr->name, z8.name);
168 ptr = zAVLNext(&cursor);
169 CuAssertStrEquals(tc, ptr->name, z7.name);
170 ptr = zAVLNext(&cursor);
171 CuAssertStrEquals(tc, ptr->name, z6.name);
172 ptr = zAVLNext(&cursor);
173 CuAssertStrEquals(tc, ptr->name, z2.name);
174 ptr = zAVLNext(&cursor);
175 CuAssertStrEquals(tc, ptr->name, z1.name);
176 ptr = zAVLNext(&cursor);
177 CuAssertTrue(tc, NULL == ptr);
178
179 result = zAVLDelete(ztest_tree, z1.name);
180 CuAssertTrue(tc, 0 == result);
181 ptr = zAVLSearch(ztest_tree, z1.name);
182 CuAssertTrue(tc, NULL == ptr);
183
184 result = zAVLDelete(ztest_tree, z2.name);
185 CuAssertTrue(tc, 0 == result);
186 ptr = zAVLSearch(ztest_tree, z2.name);
187 CuAssertTrue(tc, NULL == ptr);
188
189 result = zAVLDelete(ztest_tree, z3.name);
190 CuAssertTrue(tc, 0 == result);
191 ptr = zAVLSearch(ztest_tree, z3.name);
192 CuAssertTrue(tc, NULL == ptr);
193
194 result = zAVLDelete(ztest_tree, z4.name);
195 CuAssertTrue(tc, 0 == result);
196 ptr = zAVLSearch(ztest_tree, z4.name);
197 CuAssertTrue(tc, NULL == ptr);
198
199 result = zAVLDelete(ztest_tree, z5.name);
200 CuAssertTrue(tc, 0 == result);
201 ptr = zAVLSearch(ztest_tree, z5.name);
202 CuAssertTrue(tc, NULL == ptr);
203
204 result = zAVLDelete(ztest_tree, z6.name);
205 CuAssertTrue(tc, 0 == result);
206 ptr = zAVLSearch(ztest_tree, z6.name);
207 CuAssertTrue(tc, NULL == ptr);
208
209 result = zAVLDelete(ztest_tree, z7.name);
210 CuAssertTrue(tc, 0 == result);
211 ptr = zAVLSearch(ztest_tree, z7.name);
212 CuAssertTrue(tc, NULL == ptr);
213
214 result = zAVLDelete(ztest_tree, z8.name);
215 CuAssertTrue(tc, 0 == result);
216 ptr = zAVLSearch(ztest_tree, z8.name);
217 CuAssertTrue(tc, NULL == ptr);
218
219} while (counter < 100);
220
221 result = zAVLInsert(ztest_tree, &z1);
222 CuAssertTrue(tc, 0 == result);
223 result = zAVLInsert(ztest_tree, &z2);
224 CuAssertTrue(tc, 0 == result);
225 result = zAVLInsert(ztest_tree, &z3);
226 CuAssertTrue(tc, 0 == result);
227 result = zAVLInsert(ztest_tree, &z4);
228 CuAssertTrue(tc, 0 == result);
229 result = zAVLInsert(ztest_tree, &z5);
230 CuAssertTrue(tc, 0 == result);
231 result = zAVLInsert(ztest_tree, &z6);
232 CuAssertTrue(tc, 0 == result);
233 result = zAVLInsert(ztest_tree, &z7);
234 CuAssertTrue(tc, 0 == result);
235 result = zAVLInsert(ztest_tree, &z8);
236 CuAssertTrue(tc, 0 == result);
237
238 zAVLFreeTree (ztest_tree, free_node);
239 CuAssertTrue (tc, z1.name[0] == '\0');
240 CuAssertTrue (tc, z2.name[0] == '\0');
241 CuAssertTrue (tc, z3.name[0] == '\0');
242 CuAssertTrue (tc, z4.name[0] == '\0');
243 CuAssertTrue (tc, z5.name[0] == '\0');
244 CuAssertTrue (tc, z6.name[0] == '\0');
245 CuAssertTrue (tc, z7.name[0] == '\0');
246 CuAssertTrue (tc, z8.name[0] == '\0');
247}
Note: See TracBrowser for help on using the repository browser.