Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <../../src/libs/elektra/backend.c>
10 : #include <../../src/libs/elektra/trie.c>
11 : #include <tests_internal.h>
12 :
13 :
14 168 : Trie * test_insert (Trie * trie, char * name, char * value)
15 : {
16 168 : Backend * backend = elektraCalloc (sizeof (Backend));
17 168 : backend->mountpoint = keyNew (name, KEY_VALUE, value, KEY_END);
18 168 : backend->refcounter = 1;
19 168 : keyIncRef (backend->mountpoint);
20 168 : return trieInsert (trie, name, backend);
21 : }
22 :
23 :
24 2 : static void test_minimaltrie (void)
25 : {
26 2 : printf ("Test minimal trie\n");
27 :
28 2 : Trie * trie = test_insert (0, "", "");
29 2 : Key * s = keyNew ("", KEY_END);
30 2 : Key * mp = keyNew ("", KEY_VALUE, "", KEY_END);
31 :
32 2 : succeed_if (trieLookup (trie, s), "trie should not be null");
33 2 : compare_key (trieLookup (trie, s)->mountpoint, mp);
34 :
35 2 : keySetName (s, "user");
36 2 : compare_key (trieLookup (trie, s)->mountpoint, mp);
37 :
38 2 : keySetName (s, "system");
39 2 : compare_key (trieLookup (trie, s)->mountpoint, mp);
40 :
41 2 : keySetName (s, "user/below");
42 2 : compare_key (trieLookup (trie, s)->mountpoint, mp);
43 :
44 2 : keySetName (s, "system/below");
45 2 : compare_key (trieLookup (trie, s)->mountpoint, mp);
46 :
47 2 : trieClose (trie, 0);
48 2 : keyDel (s);
49 2 : keyDel (mp);
50 2 : }
51 :
52 0 : KeySet * simple_config (void)
53 : {
54 0 : return ksNew (5, keyNew ("system/elektra/mountpoints", KEY_END), keyNew ("system/elektra/mountpoints/simple", KEY_END),
55 : keyNew ("system/elektra/mountpoints/simple/mountpoint", KEY_VALUE, "user/tests/simple", KEY_END), KS_END);
56 : }
57 :
58 2 : static void test_simple (void)
59 : {
60 2 : printf ("Test simple trie\n");
61 :
62 2 : Trie * trie = test_insert (0, "user/tests/simple", "simple");
63 :
64 2 : exit_if_fail (trie, "trie was not build up successfully");
65 :
66 2 : Key * searchKey = keyNew ("user", KEY_END);
67 2 : Backend * backend = trieLookup (trie, searchKey);
68 2 : succeed_if (!backend, "there should be no backend");
69 :
70 :
71 2 : Key * mp = keyNew ("user/tests/simple", KEY_VALUE, "simple", KEY_END);
72 2 : keySetName (searchKey, "user/tests/simple");
73 2 : backend = trieLookup (trie, searchKey);
74 2 : succeed_if (backend, "there should be a backend");
75 2 : if (backend) compare_key (backend->mountpoint, mp);
76 :
77 2 : keySetName (searchKey, "user/tests/simple/below");
78 2 : Backend * b2 = trieLookup (trie, searchKey);
79 2 : succeed_if (b2, "there should be a backend");
80 2 : succeed_if (backend == b2, "should be same backend");
81 2 : if (b2) compare_key (b2->mountpoint, mp);
82 :
83 :
84 2 : keySetName (searchKey, "user/tests/simple/deep/below");
85 2 : b2 = trieLookup (trie, searchKey);
86 2 : succeed_if (b2, "there should be a backend");
87 2 : succeed_if (backend == b2, "should be same backend");
88 2 : if (b2) compare_key (b2->mountpoint, mp);
89 :
90 2 : trieClose (trie, 0);
91 2 : keyDel (mp);
92 2 : keyDel (searchKey);
93 2 : }
94 :
95 92 : static void collect_mountpoints (Trie * trie, KeySet * mountpoints)
96 : {
97 : int i;
98 23644 : for (i = 0; i < KDB_MAX_UCHAR; ++i)
99 : {
100 23552 : if (trie->value[i]) ksAppendKey (mountpoints, ((Backend *) trie->value[i])->mountpoint);
101 23552 : if (trie->children[i]) collect_mountpoints (trie->children[i], mountpoints);
102 : }
103 92 : if (trie->empty_value)
104 : {
105 48 : ksAppendKey (mountpoints, ((Backend *) trie->empty_value)->mountpoint);
106 : }
107 92 : }
108 :
109 2 : static void test_iterate (void)
110 : {
111 2 : printf ("Test iterate trie\n");
112 :
113 2 : Trie * trie = test_insert (0, "user/tests/hosts", "hosts");
114 2 : trie = test_insert (trie, "user/tests/hosts/below", "below");
115 :
116 2 : exit_if_fail (trie, "trie was not build up successfully");
117 :
118 2 : Key * searchKey = keyNew ("user", KEY_END);
119 2 : Backend * backend = trieLookup (trie, searchKey);
120 2 : succeed_if (!backend, "there should be no backend");
121 :
122 :
123 2 : Key * mp = keyNew ("user/tests/hosts", KEY_VALUE, "hosts", KEY_END);
124 2 : keySetName (searchKey, "user/tests/hosts");
125 2 : backend = trieLookup (trie, searchKey);
126 2 : succeed_if (backend, "there should be a backend");
127 2 : if (backend) compare_key (backend->mountpoint, mp);
128 : // printf ("backend: %p\n", (void*)backend);
129 :
130 :
131 2 : keySetName (searchKey, "user/tests/hosts/other/below");
132 2 : Backend * b2 = trieLookup (trie, searchKey);
133 2 : succeed_if (b2, "there should be a backend");
134 2 : succeed_if (backend == b2, "should be same backend");
135 2 : if (b2) compare_key (b2->mountpoint, mp);
136 : // printf ("b2: %p\n", (void*)b2);
137 :
138 :
139 2 : keySetName (searchKey, "user/tests/hosts/other/deep/below");
140 2 : b2 = trieLookup (trie, searchKey);
141 2 : succeed_if (b2, "there should be a backend");
142 2 : succeed_if (backend == b2, "should be same backend");
143 2 : if (b2) compare_key (b2->mountpoint, mp);
144 :
145 :
146 2 : Key * mp2 = keyNew ("user/tests/hosts/below", KEY_VALUE, "below", KEY_END);
147 2 : keySetName (searchKey, "user/tests/hosts/below");
148 2 : Backend * b3 = trieLookup (trie, searchKey);
149 2 : succeed_if (b3, "there should be a backend");
150 2 : succeed_if (backend != b3, "should be different backend");
151 2 : if (b3) compare_key (b3->mountpoint, mp2);
152 2 : backend = b3;
153 : // printf ("b3: %p\n", (void*)b3);
154 :
155 :
156 2 : keySetName (searchKey, "user/tests/hosts/below/other/deep/below");
157 2 : b3 = trieLookup (trie, searchKey);
158 2 : succeed_if (b3, "there should be a backend");
159 2 : succeed_if (backend == b3, "should be same backend");
160 2 : if (b3) compare_key (b3->mountpoint, mp2);
161 :
162 2 : KeySet * mps = ksNew (0, KS_END);
163 2 : collect_mountpoints (trie, mps);
164 : // output_keyset(mps);
165 : // output_trie(trie);
166 2 : succeed_if (ksGetSize (mps) == 2, "not both mountpoints collected");
167 2 : compare_key (ksHead (mps), mp);
168 2 : compare_key (ksTail (mps), mp2);
169 2 : ksDel (mps);
170 :
171 2 : trieClose (trie, 0);
172 :
173 2 : keyDel (mp);
174 2 : keyDel (mp2);
175 2 : keyDel (searchKey);
176 2 : }
177 :
178 2 : static void test_reviterate (void)
179 : {
180 2 : printf ("Test reviterate trie\n");
181 :
182 2 : Trie * trie = test_insert (0, "user/tests/hosts/below", "below");
183 2 : trie = test_insert (trie, "user/tests/hosts", "hosts");
184 :
185 2 : exit_if_fail (trie, "trie was not build up successfully");
186 :
187 2 : Key * searchKey = keyNew ("user", KEY_END);
188 2 : Backend * backend = trieLookup (trie, searchKey);
189 2 : succeed_if (!backend, "there should be no backend");
190 :
191 :
192 2 : Key * mp = keyNew ("user/tests/hosts", KEY_VALUE, "hosts", KEY_END);
193 2 : keySetName (searchKey, "user/tests/hosts");
194 2 : backend = trieLookup (trie, searchKey);
195 2 : succeed_if (backend, "there should be a backend");
196 2 : if (backend) compare_key (backend->mountpoint, mp);
197 : // printf ("backend: %p\n", (void*)backend);
198 :
199 :
200 2 : keySetName (searchKey, "user/tests/hosts/other/below");
201 2 : Backend * b2 = trieLookup (trie, searchKey);
202 2 : succeed_if (b2, "there should be a backend");
203 2 : succeed_if (backend == b2, "should be same backend");
204 2 : if (b2) compare_key (b2->mountpoint, mp);
205 : // printf ("b2: %p\n", (void*)b2);
206 :
207 :
208 2 : keySetName (searchKey, "user/tests/hosts/other/deep/below");
209 2 : b2 = trieLookup (trie, searchKey);
210 2 : succeed_if (b2, "there should be a backend");
211 2 : succeed_if (backend == b2, "should be same backend");
212 2 : if (b2) compare_key (b2->mountpoint, mp);
213 :
214 :
215 2 : Key * mp2 = keyNew ("user/tests/hosts/below", KEY_VALUE, "below", KEY_END);
216 2 : keySetName (searchKey, "user/tests/hosts/below");
217 2 : Backend * b3 = trieLookup (trie, searchKey);
218 2 : succeed_if (b3, "there should be a backend");
219 2 : succeed_if (backend != b3, "should be different backend");
220 2 : if (b3) compare_key (b3->mountpoint, mp2);
221 2 : backend = b3;
222 : // printf ("b3: %p\n", (void*)b3);
223 :
224 :
225 2 : keySetName (searchKey, "user/tests/hosts/below/other/deep/below");
226 2 : b2 = trieLookup (trie, searchKey);
227 2 : succeed_if (b2, "there should be a backend");
228 2 : succeed_if (backend == b2, "should be same backend");
229 2 : if (b2) compare_key (b2->mountpoint, mp2);
230 :
231 2 : KeySet * mps = ksNew (0, KS_END);
232 2 : collect_mountpoints (trie, mps);
233 2 : succeed_if (ksGetSize (mps) == 2, "not both mountpoints collected");
234 2 : compare_key (ksHead (mps), mp);
235 2 : compare_key (ksTail (mps), mp2);
236 2 : ksDel (mps);
237 :
238 2 : trieClose (trie, 0);
239 :
240 2 : keyDel (mp);
241 2 : keyDel (mp2);
242 2 : keyDel (searchKey);
243 2 : }
244 :
245 0 : KeySet * moreiterate_config (void)
246 : {
247 0 : return ksNew (50, keyNew ("system/elektra/mountpoints", KEY_END), keyNew ("system/elektra/mountpoints/user", KEY_END),
248 : keyNew ("system/elektra/mountpoints/user/mountpoint", KEY_VALUE, "user", KEY_END),
249 : keyNew ("system/elektra/mountpoints/tests", KEY_END),
250 : keyNew ("system/elektra/mountpoints/tests/mountpoint", KEY_VALUE, "user/tests", KEY_END),
251 : keyNew ("system/elektra/mountpoints/hosts", KEY_END),
252 : keyNew ("system/elektra/mountpoints/hosts/mountpoint", KEY_VALUE, "user/tests/hosts", KEY_END),
253 : keyNew ("system/elektra/mountpoints/below", KEY_END),
254 : keyNew ("system/elektra/mountpoints/below/mountpoint", KEY_VALUE, "user/tests/hosts/below", KEY_END),
255 : keyNew ("system/elektra/mountpoints/system", KEY_END),
256 : keyNew ("system/elektra/mountpoints/system/mountpoint", KEY_VALUE, "system", KEY_END),
257 : keyNew ("system/elektra/mountpoints/systests", KEY_END),
258 : keyNew ("system/elektra/mountpoints/systests/mountpoint", KEY_VALUE, "system/tests", KEY_END),
259 : keyNew ("system/elektra/mountpoints/syshosts", KEY_END),
260 : keyNew ("system/elektra/mountpoints/syshosts/mountpoint", KEY_VALUE, "system/tests/hosts", KEY_END),
261 : keyNew ("system/elektra/mountpoints/sysbelow", KEY_END),
262 : keyNew ("system/elektra/mountpoints/sysbelow/mountpoint", KEY_VALUE, "system/tests/hosts/below", KEY_END), KS_END);
263 : }
264 :
265 12 : KeySet * set_mountpoints (void)
266 : {
267 12 : return ksNew (10, keyNew ("user", KEY_VALUE, "user", KEY_END), keyNew ("user/tests", KEY_VALUE, "tests", KEY_END),
268 : keyNew ("user/tests/hosts", KEY_VALUE, "hosts", KEY_END),
269 : keyNew ("user/tests/hosts/below", KEY_VALUE, "below", KEY_END), keyNew ("system", KEY_VALUE, "system", KEY_END),
270 : keyNew ("system/tests", KEY_VALUE, "systests", KEY_END),
271 : keyNew ("system/tests/hosts", KEY_VALUE, "syshosts", KEY_END),
272 : keyNew ("system/tests/hosts/below", KEY_VALUE, "sysbelow", KEY_END), KS_END);
273 : }
274 :
275 2 : static void test_moreiterate (void)
276 : {
277 2 : printf ("Test moreiterate trie\n");
278 :
279 2 : Trie * trie = test_insert (0, "user", "user");
280 2 : trie = test_insert (trie, "user/tests", "tests");
281 2 : trie = test_insert (trie, "user/tests/hosts", "hosts");
282 2 : trie = test_insert (trie, "user/tests/hosts/below", "below");
283 2 : trie = test_insert (trie, "system", "system");
284 2 : trie = test_insert (trie, "system/tests", "systests");
285 2 : trie = test_insert (trie, "system/tests/hosts", "syshosts");
286 2 : trie = test_insert (trie, "system/tests/hosts/below", "sysbelow");
287 :
288 2 : KeySet * mps = set_mountpoints ();
289 :
290 2 : exit_if_fail (trie, "trie was not build up successfully");
291 :
292 2 : Key * searchKey = keyNew (0);
293 :
294 2 : keySetName (searchKey, "user");
295 2 : Backend * backend = trieLookup (trie, searchKey);
296 2 : succeed_if (backend, "there should be a backend");
297 2 : compare_key (backend->mountpoint, ksLookupByName (mps, "user", 0));
298 : // printf ("backend: %p\n", (void*)backend);
299 :
300 :
301 2 : keySetName (searchKey, "user/tests/hosts/other/below");
302 2 : Backend * b2 = trieLookup (trie, searchKey);
303 2 : succeed_if (b2, "there should be a backend");
304 2 : compare_key (b2->mountpoint, ksLookupByName (mps, "user/tests/hosts", 0));
305 : // printf ("b2: %p\n", (void*)b2);
306 :
307 :
308 2 : keySetName (searchKey, "user/tests/hosts/other/deep/below");
309 2 : b2 = trieLookup (trie, searchKey);
310 2 : succeed_if (b2, "there should be a backend");
311 2 : compare_key (b2->mountpoint, ksLookupByName (mps, "user/tests/hosts", 0));
312 :
313 :
314 2 : keySetName (searchKey, "user/tests/hosts/below");
315 2 : Backend * b3 = trieLookup (trie, searchKey);
316 2 : succeed_if (b3, "there should be a backend");
317 2 : if (b3) compare_key (b3->mountpoint, ksLookupByName (mps, "user/tests/hosts/below", 0));
318 : // printf ("b3: %p\n", (void*)b3);
319 :
320 :
321 2 : keySetName (searchKey, "user/tests/hosts/below/other/deep/below");
322 2 : backend = trieLookup (trie, searchKey);
323 2 : succeed_if (backend, "there should be a backend");
324 2 : if (backend) compare_key (backend->mountpoint, ksLookupByName (mps, "user/tests/hosts/below", 0));
325 :
326 2 : keySetName (searchKey, "system");
327 2 : backend = trieLookup (trie, searchKey);
328 2 : succeed_if (backend, "there should be a backend");
329 2 : if (backend) compare_key (backend->mountpoint, ksLookupByName (mps, "system", 0));
330 : // printf ("backend: %p\n", (void*)backend);
331 :
332 :
333 2 : keySetName (searchKey, "system/tests/hosts/other/below");
334 2 : b2 = trieLookup (trie, searchKey);
335 2 : succeed_if (b2, "there should be a backend");
336 2 : if (b2) compare_key (b2->mountpoint, ksLookupByName (mps, "system/tests/hosts", 0));
337 : // printf ("b2: %p\n", (void*)b2);
338 :
339 :
340 2 : keySetName (searchKey, "system/tests/hosts/other/deep/below");
341 2 : b2 = trieLookup (trie, searchKey);
342 2 : succeed_if (b2, "there should be a backend");
343 2 : if (b2) compare_key (b2->mountpoint, ksLookupByName (mps, "system/tests/hosts", 0));
344 :
345 :
346 2 : keySetName (searchKey, "system/tests/hosts/below");
347 2 : b3 = trieLookup (trie, searchKey);
348 2 : succeed_if (b3, "there should be a backend");
349 2 : if (b3) compare_key (b3->mountpoint, ksLookupByName (mps, "system/tests/hosts/below", 0));
350 : // printf ("b3: %p\n", (void*)b3);
351 :
352 :
353 2 : keySetName (searchKey, "system/tests/hosts/below/other/deep/below");
354 2 : b2 = trieLookup (trie, searchKey);
355 2 : succeed_if (b2, "there should be a backend");
356 2 : if (b2) compare_key (b2->mountpoint, ksLookupByName (mps, "system/tests/hosts/below", 0));
357 :
358 2 : KeySet * mps_cmp = ksNew (0, KS_END);
359 2 : collect_mountpoints (trie, mps_cmp);
360 2 : succeed_if (ksGetSize (mps_cmp) == 8, "size should be 8");
361 2 : compare_keyset (mps, mps_cmp);
362 :
363 2 : ksDel (mps_cmp);
364 2 : ksDel (mps);
365 :
366 2 : trieClose (trie, 0);
367 2 : keyDel (searchKey);
368 2 : }
369 :
370 2 : static void test_revmoreiterate (void)
371 : {
372 2 : printf ("Test revmoreiterate trie\n");
373 :
374 12 : for (int i = 0; i < 5; ++i)
375 : {
376 :
377 10 : Trie * trie = 0;
378 10 : switch (i)
379 : {
380 : case 0:
381 2 : trie = test_insert (trie, "user/tests", "tests");
382 2 : trie = test_insert (trie, "user/tests/hosts", "hosts");
383 2 : trie = test_insert (trie, "user/tests/hosts/below", "below");
384 2 : trie = test_insert (trie, "system/tests", "systests");
385 2 : trie = test_insert (trie, "system/tests/hosts", "syshosts");
386 2 : trie = test_insert (trie, "system/tests/hosts/below", "sysbelow");
387 2 : trie = test_insert (trie, "system", "system");
388 2 : trie = test_insert (trie, "user", "user");
389 2 : break;
390 : case 1:
391 2 : trie = test_insert (trie, "system/tests/hosts", "syshosts");
392 2 : trie = test_insert (trie, "system", "system");
393 2 : trie = test_insert (trie, "user/tests", "tests");
394 2 : trie = test_insert (trie, "user/tests/hosts", "hosts");
395 2 : trie = test_insert (trie, "user/tests/hosts/below", "below");
396 2 : trie = test_insert (trie, "system/tests", "systests");
397 2 : trie = test_insert (trie, "user", "user");
398 2 : trie = test_insert (trie, "system/tests/hosts/below", "sysbelow");
399 2 : break;
400 : case 2:
401 2 : trie = test_insert (trie, "system/tests/hosts/below", "sysbelow");
402 2 : trie = test_insert (trie, "system/tests/hosts", "syshosts");
403 2 : trie = test_insert (trie, "user/tests/hosts/below", "below");
404 2 : trie = test_insert (trie, "user/tests/hosts", "hosts");
405 2 : trie = test_insert (trie, "user/tests", "tests");
406 2 : trie = test_insert (trie, "user", "user");
407 2 : trie = test_insert (trie, "system/tests", "systests");
408 2 : trie = test_insert (trie, "system", "system");
409 2 : break;
410 : case 3:
411 2 : trie = test_insert (trie, "user/tests/hosts/below", "below");
412 2 : trie = test_insert (trie, "user/tests/hosts", "hosts");
413 2 : trie = test_insert (trie, "user/tests", "tests");
414 2 : trie = test_insert (trie, "user", "user");
415 2 : trie = test_insert (trie, "system/tests/hosts/below", "sysbelow");
416 2 : trie = test_insert (trie, "system/tests/hosts", "syshosts");
417 2 : trie = test_insert (trie, "system/tests", "systests");
418 2 : trie = test_insert (trie, "system", "system");
419 2 : break;
420 : case 4:
421 2 : trie = test_insert (trie, "system/tests/hosts/below", "sysbelow");
422 2 : trie = test_insert (trie, "system/tests/hosts", "syshosts");
423 2 : trie = test_insert (trie, "system/tests", "systests");
424 2 : trie = test_insert (trie, "system", "system");
425 2 : trie = test_insert (trie, "user/tests/hosts/below", "below");
426 2 : trie = test_insert (trie, "user/tests/hosts", "hosts");
427 2 : trie = test_insert (trie, "user/tests", "tests");
428 2 : trie = test_insert (trie, "user", "user");
429 2 : break;
430 : }
431 :
432 10 : KeySet * mps = set_mountpoints ();
433 :
434 10 : exit_if_fail (trie, "trie was not build up successfully");
435 :
436 10 : Key * searchKey = keyNew (0);
437 :
438 10 : keySetName (searchKey, "user");
439 10 : Backend * backend = trieLookup (trie, searchKey);
440 10 : succeed_if (backend, "there should be a backend");
441 10 : if (backend) compare_key (backend->mountpoint, ksLookupByName (mps, "user", 0));
442 : // printf ("backend: %p\n", (void*)backend);
443 :
444 :
445 10 : keySetName (searchKey, "user/tests/hosts/other/below");
446 10 : Backend * b2 = trieLookup (trie, searchKey);
447 10 : succeed_if (b2, "there should be a backend");
448 10 : if (b2) compare_key (b2->mountpoint, ksLookupByName (mps, "user/tests/hosts", 0));
449 : // printf ("b2: %p\n", (void*)b2);
450 :
451 :
452 10 : keySetName (searchKey, "user/tests/hosts/other/deep/below");
453 10 : b2 = trieLookup (trie, searchKey);
454 10 : succeed_if (b2, "there should be a backend");
455 10 : if (b2) compare_key (b2->mountpoint, ksLookupByName (mps, "user/tests/hosts", 0));
456 :
457 :
458 10 : keySetName (searchKey, "user/tests/hosts/below");
459 10 : Backend * b3 = trieLookup (trie, searchKey);
460 10 : succeed_if (b3, "there should be a backend");
461 10 : if (b3) compare_key (b3->mountpoint, ksLookupByName (mps, "user/tests/hosts/below", 0));
462 : // printf ("b3: %p\n", (void*)b3);
463 :
464 :
465 10 : keySetName (searchKey, "user/tests/hosts/below/other/deep/below");
466 10 : backend = trieLookup (trie, searchKey);
467 10 : succeed_if (backend, "there should be a backend");
468 10 : if (backend) compare_key (backend->mountpoint, ksLookupByName (mps, "user/tests/hosts/below", 0));
469 :
470 10 : keySetName (searchKey, "system");
471 10 : backend = trieLookup (trie, searchKey);
472 10 : succeed_if (backend, "there should be a backend");
473 10 : if (backend) compare_key (backend->mountpoint, ksLookupByName (mps, "system", 0));
474 : // printf ("backend: %p\n", (void*)backend);
475 :
476 :
477 10 : keySetName (searchKey, "system/tests/hosts/other/below");
478 10 : b2 = trieLookup (trie, searchKey);
479 10 : succeed_if (b2, "there should be a backend");
480 10 : if (b2) compare_key (b2->mountpoint, ksLookupByName (mps, "system/tests/hosts", 0));
481 : // printf ("b2: %p\n", (void*)b2);
482 :
483 :
484 10 : keySetName (searchKey, "system/tests/hosts/other/deep/below");
485 10 : b2 = trieLookup (trie, searchKey);
486 10 : succeed_if (b2, "there should be a backend");
487 10 : if (b2) compare_key (b2->mountpoint, ksLookupByName (mps, "system/tests/hosts", 0));
488 :
489 :
490 10 : keySetName (searchKey, "system/tests/hosts/below");
491 10 : b3 = trieLookup (trie, searchKey);
492 10 : succeed_if (b3, "there should be a backend");
493 10 : if (b3) compare_key (b3->mountpoint, ksLookupByName (mps, "system/tests/hosts/below", 0));
494 : // printf ("b3: %p\n", (void*)b3);
495 :
496 :
497 10 : keySetName (searchKey, "system/tests/hosts/below/other/deep/below");
498 10 : b2 = trieLookup (trie, searchKey);
499 10 : succeed_if (b2, "there should be a backend");
500 10 : if (b2) compare_key (b2->mountpoint, ksLookupByName (mps, "system/tests/hosts/below", 0));
501 :
502 : /*
503 : printf ("---------\n");
504 : output_trie(trie);
505 : */
506 :
507 10 : KeySet * mps_cmp = ksNew (0, KS_END);
508 10 : collect_mountpoints (trie, mps_cmp);
509 10 : succeed_if (ksGetSize (mps_cmp) == 8, "size should be 8");
510 10 : compare_keyset (mps, mps_cmp);
511 :
512 10 : ksDel (mps_cmp);
513 10 : ksDel (mps);
514 :
515 10 : trieClose (trie, 0);
516 10 : keyDel (searchKey);
517 :
518 : } // end for
519 2 : }
520 :
521 :
522 2 : static void test_umlauts (void)
523 : {
524 2 : printf ("Test umlauts trie\n");
525 :
526 2 : Trie * trie = test_insert (0, "user/umlauts/test", "slash");
527 2 : trie = test_insert (trie, "user/umlauts#test", "hash");
528 2 : trie = test_insert (trie, "user/umlauts test", "space");
529 2 : trie = test_insert (trie, "user/umlauts\200test", "umlauts");
530 :
531 2 : exit_if_fail (trie, "trie was not build up successfully");
532 :
533 2 : Key * searchKey = keyNew ("user", KEY_END);
534 2 : Backend * backend = trieLookup (trie, searchKey);
535 2 : succeed_if (!backend, "there should be no backend");
536 :
537 :
538 2 : Key * mp = keyNew ("user/umlauts/test", KEY_VALUE, "slash", KEY_END);
539 2 : keySetName (searchKey, "user/umlauts/test");
540 2 : backend = trieLookup (trie, searchKey);
541 2 : succeed_if (backend, "there should be a backend");
542 2 : if (backend) compare_key (backend->mountpoint, mp);
543 :
544 :
545 2 : keySetName (searchKey, "user/umlauts#test");
546 2 : keySetName (mp, "user/umlauts#test");
547 2 : keySetString (mp, "hash");
548 2 : Backend * b2 = trieLookup (trie, searchKey);
549 2 : succeed_if (b2, "there should be a backend");
550 2 : succeed_if (backend != b2, "should be other backend");
551 2 : if (b2) compare_key (b2->mountpoint, mp);
552 :
553 :
554 2 : keySetName (searchKey, "user/umlauts test");
555 2 : keySetName (mp, "user/umlauts test");
556 2 : keySetString (mp, "space");
557 2 : b2 = trieLookup (trie, searchKey);
558 2 : succeed_if (b2, "there should be a backend");
559 2 : succeed_if (backend != b2, "should be other backend");
560 2 : if (b2) compare_key (b2->mountpoint, mp);
561 :
562 2 : keySetName (searchKey, "user/umlauts\200test");
563 2 : keySetName (mp, "user/umlauts\200test");
564 2 : keySetString (mp, "umlauts");
565 2 : b2 = trieLookup (trie, searchKey);
566 2 : succeed_if (b2, "there should be a backend");
567 2 : succeed_if (backend != b2, "should be other backend");
568 2 : if (b2) compare_key (b2->mountpoint, mp);
569 :
570 : // output_trie(trie);
571 :
572 2 : trieClose (trie, 0);
573 2 : keyDel (mp);
574 2 : keyDel (searchKey);
575 2 : }
576 :
577 2 : static void test_endings (void)
578 : {
579 2 : printf ("Test endings trie\n");
580 :
581 10 : for (int i = 0; i < 4; ++i)
582 : {
583 :
584 8 : Trie * trie = 0;
585 8 : switch (i)
586 : {
587 : case 0:
588 2 : trie = test_insert (trie, "user/endings/", "slash");
589 2 : trie = test_insert (trie, "user/endings#", "hash");
590 2 : trie = test_insert (trie, "user/endings ", "space");
591 2 : trie = test_insert (trie, "user/endings\200", "endings");
592 2 : break;
593 : case 1:
594 2 : trie = test_insert (trie, "user/endings#", "hash");
595 2 : trie = test_insert (trie, "user/endings ", "space");
596 2 : trie = test_insert (trie, "user/endings\200", "endings");
597 2 : trie = test_insert (trie, "user/endings/", "slash");
598 2 : break;
599 : case 2:
600 2 : trie = test_insert (trie, "user/endings ", "space");
601 2 : trie = test_insert (trie, "user/endings\200", "endings");
602 2 : trie = test_insert (trie, "user/endings/", "slash");
603 2 : trie = test_insert (trie, "user/endings#", "hash");
604 2 : break;
605 : case 3:
606 2 : trie = test_insert (trie, "user/endings\200", "endings");
607 2 : trie = test_insert (trie, "user/endings ", "space");
608 2 : trie = test_insert (trie, "user/endings#", "hash");
609 2 : trie = test_insert (trie, "user/endings/", "slash");
610 2 : break;
611 : }
612 :
613 8 : exit_if_fail (trie, "trie was not build up successfully");
614 :
615 8 : Key * searchKey = keyNew ("user", KEY_END);
616 8 : Backend * backend = trieLookup (trie, searchKey);
617 8 : succeed_if (!backend, "there should be no backend");
618 :
619 :
620 8 : Key * mp = keyNew ("user/endings", KEY_VALUE, "slash", KEY_END);
621 8 : keySetName (searchKey, "user/endings");
622 8 : backend = trieLookup (trie, searchKey);
623 8 : succeed_if (backend, "there should be a backend");
624 8 : if (backend) compare_key (backend->mountpoint, mp);
625 :
626 :
627 8 : keySetName (searchKey, "user/endings#");
628 8 : keySetName (mp, "user/endings#");
629 8 : keySetString (mp, "hash");
630 8 : Backend * b2 = trieLookup (trie, searchKey);
631 8 : succeed_if (b2, "there should be a backend");
632 8 : succeed_if (backend != b2, "should be other backend");
633 8 : if (b2) compare_key (b2->mountpoint, mp);
634 :
635 :
636 8 : keySetName (searchKey, "user/endings/_");
637 8 : keySetName (mp, "user/endings");
638 8 : keySetString (mp, "slash");
639 8 : b2 = trieLookup (trie, searchKey);
640 8 : succeed_if (b2, "there should be a backend");
641 8 : succeed_if (backend == b2, "should be the same backend");
642 8 : if (b2) compare_key (b2->mountpoint, mp);
643 :
644 :
645 8 : keySetName (searchKey, "user/endings/X");
646 8 : keySetName (mp, "user/endings");
647 8 : keySetString (mp, "slash");
648 8 : b2 = trieLookup (trie, searchKey);
649 8 : succeed_if (b2, "there should be a backend");
650 8 : succeed_if (backend == b2, "should be the same backend");
651 8 : if (b2) compare_key (b2->mountpoint, mp);
652 :
653 :
654 8 : keySetName (searchKey, "user/endings_");
655 8 : b2 = trieLookup (trie, searchKey);
656 8 : succeed_if (!b2, "there should be no backend");
657 :
658 :
659 8 : keySetName (searchKey, "user/endingsX");
660 8 : b2 = trieLookup (trie, searchKey);
661 8 : succeed_if (!b2, "there should be no backend");
662 :
663 :
664 8 : keySetName (searchKey, "user/endings!");
665 8 : b2 = trieLookup (trie, searchKey);
666 8 : succeed_if (!b2, "there should be no backend");
667 :
668 :
669 8 : keySetName (searchKey, "user/endings ");
670 8 : keySetName (mp, "user/endings ");
671 8 : keySetString (mp, "space");
672 8 : b2 = trieLookup (trie, searchKey);
673 8 : succeed_if (b2, "there should be a backend");
674 8 : succeed_if (backend != b2, "should be other backend");
675 8 : if (b2) compare_key (b2->mountpoint, mp);
676 :
677 8 : keySetName (searchKey, "user/endings\200");
678 8 : keySetName (mp, "user/endings\200");
679 8 : keySetString (mp, "endings");
680 8 : b2 = trieLookup (trie, searchKey);
681 8 : succeed_if (b2, "there should be a backend");
682 8 : succeed_if (backend != b2, "should be other backend");
683 8 : if (b2) compare_key (b2->mountpoint, mp);
684 :
685 : // output_trie(trie);
686 :
687 8 : trieClose (trie, 0);
688 8 : keyDel (mp);
689 8 : keyDel (searchKey);
690 : }
691 2 : }
692 :
693 2 : static void test_root (void)
694 : {
695 2 : printf ("Test trie with root\n");
696 :
697 2 : Trie * trie = 0;
698 2 : trie = test_insert (trie, "", "root");
699 2 : trie = test_insert (trie, "user/tests/simple", "simple");
700 :
701 2 : exit_if_fail (trie, "trie was not build up successfully");
702 :
703 2 : Key * searchKey = keyNew ("user", KEY_END);
704 2 : Key * rmp = keyNew ("", KEY_VALUE, "root", KEY_END);
705 2 : Backend * backend = trieLookup (trie, searchKey);
706 2 : succeed_if (backend, "there should be the root backend");
707 2 : if (backend) compare_key (backend->mountpoint, rmp);
708 :
709 :
710 2 : Key * mp = keyNew ("user/tests/simple", KEY_VALUE, "simple", KEY_END);
711 2 : keySetName (searchKey, "user/tests/simple");
712 2 : backend = trieLookup (trie, searchKey);
713 2 : succeed_if (backend, "there should be a backend");
714 2 : if (backend) compare_key (backend->mountpoint, mp);
715 :
716 :
717 2 : keySetName (searchKey, "user/tests/simple/below");
718 2 : Backend * b2 = trieLookup (trie, searchKey);
719 2 : succeed_if (b2, "there should be a backend");
720 2 : succeed_if (backend == b2, "should be same backend");
721 2 : if (b2) compare_key (b2->mountpoint, mp);
722 :
723 :
724 2 : keySetName (searchKey, "user/tests/simple/deep/below");
725 2 : b2 = trieLookup (trie, searchKey);
726 2 : succeed_if (b2, "there should be a backend");
727 2 : succeed_if (backend == b2, "should be same backend");
728 2 : if (b2) compare_key (b2->mountpoint, mp);
729 :
730 : // output_trie(trie);
731 :
732 2 : trieClose (trie, 0);
733 2 : keyDel (mp);
734 2 : keyDel (rmp);
735 2 : keyDel (searchKey);
736 2 : }
737 :
738 2 : static void test_double (void)
739 : {
740 2 : printf ("Test double insertion\n");
741 :
742 2 : Trie * trie = test_insert (0, "", "root");
743 2 : succeed_if (trie, "could not insert into trie");
744 :
745 2 : Trie * t1 = test_insert (trie, "user/tests/simple", "t1");
746 2 : succeed_if (t1, "could not insert into trie");
747 2 : succeed_if (t1 == trie, "should be the same");
748 :
749 : // output_trie (trie);
750 :
751 2 : Trie * t2 = test_insert (trie, "user/tests/simple", "t2");
752 2 : succeed_if (t2, "could not insert into trie");
753 2 : succeed_if (t2 == trie, "should be not the same");
754 :
755 : // output_trie (trie);
756 :
757 : /* ... gets lost
758 :
759 : Trie *t3 = test_insert (trie, "user/tests/simple", "t3");
760 : succeed_if (t3, "could not insert into trie");
761 : succeed_if (t3 == trie, "should be not the same");
762 :
763 : // output_trie (trie);
764 :
765 : */
766 :
767 2 : trieClose (trie, 0);
768 2 : }
769 :
770 2 : static void test_emptyvalues (void)
771 : {
772 2 : printf ("Test empty values in trie\n");
773 :
774 2 : Trie * trie = 0;
775 2 : trie = test_insert (trie, "user/umlauts/b/", "b");
776 2 : trie = test_insert (trie, "user/umlauts/a/", "a");
777 2 : trie = test_insert (trie, "user/umlauts/", "/");
778 2 : trie = test_insert (trie, "user/umlauts/c/", "c");
779 2 : trie = test_insert (trie, "user/", "u");
780 :
781 2 : exit_if_fail (trie, "trie was not build up successfully");
782 :
783 : // output_trie(trie);
784 :
785 2 : trieClose (trie, 0);
786 2 : }
787 :
788 :
789 2 : int main (int argc, char ** argv)
790 : {
791 2 : printf ("TRIE TESTS\n");
792 2 : printf ("==================\n\n");
793 :
794 2 : init (argc, argv);
795 :
796 2 : test_minimaltrie ();
797 2 : test_simple ();
798 2 : test_iterate ();
799 2 : test_reviterate ();
800 2 : test_moreiterate ();
801 2 : test_revmoreiterate ();
802 2 : test_umlauts ();
803 2 : test_endings ();
804 2 : test_root ();
805 2 : test_double ();
806 2 : test_emptyvalues ();
807 :
808 2 : printf ("\ntest_trie RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
809 :
810 2 : return nbError;
811 : }
|