Git init
[pkgs/e/elektra.git] / tests / test_ks.c
1 /***************************************************************************
2  *          test_ks.c  -  KeySet struct test suite
3  *                  -------------------
4  *  begin                : Thu Dez 12 2006
5  *  copyright            : (C) 2006 by Markus Raab
6  *  email                : sizon5@gmail.com
7  ****************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the BSD License (revised).                      *
13  *                                                                         *
14  ***************************************************************************/
15
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19
20 #include <stdio.h>
21 #ifdef HAVE_STDLIB_H
22 #include <stdlib.h>
23 #endif
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #endif
27
28 #include <tests.h>
29
30 void test_ksNew()
31 {
32         KeySet *ks=0;
33         KeySet * keys = ksNew (15,0);
34         KeySet * config;
35
36         printf("Test ks creation\n");
37         exit_if_fail((ks=ksNew(0)) != 0, "could not create new keyset");
38
39         succeed_if (ksAppendKey(ks,keyNew(0)) == 1, "could not append a key");
40         succeed_if (ksAppendKey(ks,keyNew(0)) == 2, "could not append a key");
41         succeed_if (ksAppendKey(ks,keyNew(0)) == 3, "could not append a key");
42         succeed_if(ksGetSize(ks) == 3, "size not correct after 3 keys");
43
44         KeySet *ks2=ksNew (0);
45         ksCopy (ks2, ks);
46         compare_keyset (ks, ks2, 0, 0);
47
48         succeed_if (ksAppendKey(ks,keyNew(0)) == 4, "could not append a key");
49         succeed_if (ksAppendKey(ks,keyNew(0)) == 5, "could not append a key");
50         succeed_if (ksAppendKey(ks,keyNew(0)) == 6, "could not append a key");
51         succeed_if(ksGetSize(ks) == 6, "could not append 3 more keys");
52
53         ksCopy (ks2, ks);
54         compare_keyset (ks, ks2, 0, 0);
55
56         ksClear (ks2); // useless, just test for double free
57         ksCopy (ks2, ks);
58         compare_keyset (ks, ks2, 0, 0);
59
60         succeed_if(ksDel(ks) == 0, "could not delete keyset");
61
62
63         succeed_if(ksGetSize(keys) == 0, "could not append 3 more keys");
64         succeed_if(ksGetAlloc(keys) == 16, "allocation size wrong");
65         succeed_if(ksDel(keys) == 0, "could not delete keyset");
66
67         config = ksNew (100,
68                 keyNew ("user/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", 0),
69                 keyNew ("user/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", 0),
70                 keyNew ("user/sw/app/fixedConfiguration/key3", KEY_VALUE, "value3", 0),0);
71         succeed_if(ksGetSize(config) == 3, "could not append 3 keys in keyNew");
72         succeed_if(ksGetAlloc(config) == 100, "allocation size wrong");
73         keyDel (ksPop (config));
74         succeed_if(ksGetAlloc(config) == 50, "allocation size wrong");
75         keyDel (ksPop (config));
76         succeed_if(ksGetAlloc(config) == 25, "allocation size wrong");
77         keyDel (ksPop (config));
78         succeed_if(ksGetAlloc(config) == 16, "allocation size wrong");
79         succeed_if(ksDel(config) == 0, "could not delete keyset");
80
81         config = ksNew (10,
82                 keyNew ("user/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", 0),
83                 keyNew ("user/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", 0),
84                 keyNew ("user/sw/app/fixedConfiguration/key3", KEY_VALUE, "value1", 0),
85                 keyNew ("user/sw/app/fixedConfiguration/key4", KEY_VALUE, "value3", 0),0);
86
87         succeed_if(ksGetSize(config) == 4, "could not append 5 keys in keyNew");
88         succeed_if(ksGetAlloc(config) == 16, "allocation size wrong");
89         ksAppendKey(config, keyNew ("user/sw/app/fixedConfiguration/key6", KEY_VALUE, "value4", 0));
90
91         ksClear (ks2);
92         ksCopy (ks2, config);
93         compare_keyset (config, ks2, 0, 0);
94
95         succeed_if(ksDel(config) == 0, "could not delete keyset");
96         succeed_if(ksDel(ks2) == 0, "could not delete keyset");
97 }
98
99 void test_ksEmpty()
100 {
101         printf ("Test empty keysets\n");
102         KeySet *ks;
103         KeySet *ks2;
104         Key *current;
105
106         ks = ksNew(0);
107         succeed_if (ksGetSize (ks) == 0, "size not correct");
108         succeed_if (ksPop (ks) == 0, "pop empty keyset");
109         succeed_if (ksGetSize (ks) == 0, "size not correct");
110         ksDel (ks);
111
112         ks = ksNew (1, current=keyNew("user/test", KEY_END), KS_END);
113         succeed_if (ksGetSize (ks) == 1, "size not correct");
114         succeed_if (ksPop (ks) == current, "pop empty keyset");
115         succeed_if (ksGetSize (ks) == 0, "size not correct");
116         succeed_if (ksPop (ks) == 0, "pop empty keyset");
117         succeed_if (ksGetSize (ks) == 0, "size not correct");
118         keyDel (current);
119         ksDel (ks);
120
121         ks = ksNew (0);
122         ks2 = ksNew (0);
123         succeed_if (ksAppend (ks, ks2) == 0, "could not append empty keyset");
124         succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
125         succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
126         succeed_if (ksPop(ks) == 0, "could not pop empty keyset");
127         succeed_if (ksPop(ks2) == 0, "could not pop empty keyset2");
128         ksDel (ks);
129         ksDel (ks2);
130
131         ks = ksNew (1, current=keyNew("user/test", KEY_END), KS_END);
132         ks2 = ksNew (0);
133         succeed_if (ksGetSize (ks) == 1, "empty keyset does not have correct size");
134         succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
135         succeed_if (ksAppend (ks, ks2) == 1, "could not append empty keyset");
136         succeed_if (ksGetSize (ks) == 1, "empty keyset does not have correct size");
137         succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
138         succeed_if (ksPop(ks) == current, "could not pop keyset");
139         succeed_if (ksPop(ks2) == 0, "could not pop empty keyset2");
140         succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
141         succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
142         succeed_if (ksAppend (ks, ks2) == 0, "could not append empty keyset");
143         succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
144         succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
145         keyDel (current);
146         ksDel (ks);
147         ksDel (ks2);
148
149
150         ks = ksNew (0);
151         ks2 = ksNew (1, current=keyNew("user/test", KEY_END), KS_END);
152         succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
153         succeed_if (ksGetSize (ks2) == 1, "empty keyset does not have correct size");
154         succeed_if (ksAppend (ks, ks2) == 1, "could not append empty keyset");
155         succeed_if (ksGetSize (ks) == 1, "empty keyset does not have correct size");
156         succeed_if (ksGetSize (ks2) == 1, "empty keyset does not have correct size");
157         succeed_if (ksPop(ks) == current, "could not pop keyset");
158         succeed_if (ksPop(ks2) == current, "could not pop empty keyset2");
159         succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
160         succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
161         succeed_if (ksAppend (ks, ks2) == 0, "could not append empty keyset");
162         succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
163         succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
164         keyDel (current); // only one keyDel, because ksPop decrements counter
165         ksDel (ks);
166         ksDel (ks2);
167 }
168
169 #define NR_KEYSETS 10
170
171 void test_ksReference()
172 {
173         KeySet *ks=0;
174         KeySet *ks1, *ks2;
175         Key *k1, *k2;
176         KeySet *kss[NR_KEYSETS];
177         int i;
178
179         printf("Test reference of key\n");
180
181         ks=ksNew(0);
182         k1 = keyNew("user/aname", KEY_END);
183         succeed_if (keyGetRef(k1) == 0, "reference counter of new key");
184         ksAppendKey(ks,k1);
185         succeed_if (keyGetRef(k1) == 1, "reference counter of inserted key");
186
187         k2 = keyDup (k1);
188
189         succeed_if (keyGetRef(k2) == 0, "reference counter not resetted");
190         ksAppendKey(ks,k2);
191         succeed_if (keyGetRef(k2) == 1, "reference counter not incremented");
192         ksSort (ks);
193
194         ksDel (ks);
195
196         ks=ksNew(5,
197                 keyNew ("user/key", KEY_END),
198                 keyNew ("system/key", KEY_END),
199                 KS_END);
200
201         k1=ksLookupByName(ks, "user/key", 0);
202         k2=ksLookupByName(ks, "system/key", 0);
203         succeed_if (keyGetRef(k1) == 1, "reference counter of new inserted key");
204         succeed_if (keyGetRef(k2) == 1, "reference counter of new inserted key");
205
206         ksDel (ks);
207
208         ks=ksNew(5,
209                 keyNew ("user/key", KEY_END),
210                 keyNew ("system/key", KEY_END),
211                 KS_END);
212
213         k1=ksLookupByName(ks, "user/key", 0);
214         k2=ksLookupByName(ks, "system/key", 0);
215         succeed_if (keyGetRef(k1) == 1, "reference counter of new inserted key");
216         succeed_if (keyGetRef(k2) == 1, "reference counter of new inserted key");
217         ks1=ksDup (ks);
218
219         succeed_if (keyGetRef(k1) == 2, "reference counter after duplication of keyset");
220         succeed_if (keyGetRef(k2) == 2, "reference counter after ksdup");
221         k1=ksPop (ks);
222         succeed_if (keyGetRef(k1) == 1, "reference counter after pop");
223         keyDel (k1);
224         succeed_if (keyGetRef(k1) == 1, "reference counter");
225         succeed_if (keyGetRef(k2) == 2, "reference counter should not be influenced");
226
227         ksDel (ks);
228         succeed_if (keyGetRef(k1) == 1, "reference counter, delete from first keyset");
229         succeed_if (keyGetRef(k2) == 1, "reference counter, delete from first keyset");
230         ksDel (ks1); // k1 and k2 deleted
231
232         ks1=ksNew(0);
233         ks2=ksNew(0);
234         k1=keyNew(0);
235         succeed_if (keyGetRef(k1) == 0, "reference counter of new inserted key");
236         ksAppendKey(ks1, k1);
237         succeed_if (keyGetRef(k1) == 1, "reference counter of new inserted key");
238         ksAppendKey(ks2, k1);
239         succeed_if (keyGetRef(k1) == 2, "reference counter of new inserted key");
240
241         k1=ksPop (ks1);
242         succeed_if (keyGetRef(k1) == 1, "reference counter of new inserted key");
243         succeed_if (keyDel (k1) == 1, "keyDel did not work");
244         succeed_if (keyGetRef(k1) == 1, "reference counter of new inserted key");
245
246         succeed_if (ksDel (ks1) == 0, "could not delete key");
247         succeed_if (ksDel (ks2) == 0, "could not delete key");
248
249
250         kss[0]=ksNew(5,
251                 k1=keyNew ("user/key", KEY_END),
252                 k2=keyNew ("system/key", KEY_END),
253                 KS_END);
254         for (i=1; i< NR_KEYSETS; i++)
255         {
256                 succeed_if (keyGetRef(k1) == i, "reference counter");
257                 succeed_if (keyGetRef(k2) == 1, "reference counter");
258                 kss[i] = ksDup (kss[i-1]);
259                 succeed_if (keyGetRef(k2) == 2, "reference counter");
260                 ksPop (kss[i-1]);
261                 succeed_if (keyGetRef(k2) == 1, "reference counter");
262                 succeed_if (keyDel(k2) == 1, "delete key");
263                 succeed_if (keyGetRef(k2) == 1, "reference counter");
264                 
265         }
266         succeed_if (keyGetRef(k1) == NR_KEYSETS, "reference counter");
267         succeed_if (keyGetRef(k2) == 1, "reference counter");
268
269         for (i=0; i< NR_KEYSETS; i++)
270         {
271                 succeed_if (keyGetRef(k1) == NR_KEYSETS-i, "reference counter");
272                 ksDel (kss[i]);
273         }
274 }
275
276 void test_ksResize()
277 {
278         int i;
279         KeySet *ks=0;
280         KeySet *copy = ksNew (0);
281
282         printf("Test resize of keyset\n");
283         exit_if_fail((ks=ksNew(0)) != 0, "could not create new keyset");
284         for (i=0; i< 100; i++)
285         {
286                 ksAppendKey(ks, keyNew (KEY_END));
287                 if (i >= 63) { succeed_if(ksGetAlloc(ks) == 128, "allocation size wrong"); }
288                 else if (i >= 31) { succeed_if(ksGetAlloc(ks) == 64, "allocation size wrong"); }
289                 else if (i >= 15) { succeed_if(ksGetAlloc(ks) == 32, "allocation size wrong"); }
290                 else if (i >= 0) { succeed_if(ksGetAlloc(ks) == 16, "allocation size wrong"); }
291         }
292         succeed_if(ksGetSize(ks) == 100, "could not append 100 keys");
293         succeed_if(ksGetAlloc(ks) == 128, "allocation size wrong");
294         for (i=100; i >= 0; i--)
295         {
296                 keyDel (ksPop(ks));
297                 if (i >= 64) { succeed_if(ksGetAlloc(ks) == 128, "allocation size wrong"); }
298                 else if (i >= 32) { succeed_if(ksGetAlloc(ks) == 64, "allocation size wrong"); }
299                 else if (i >= 16) { succeed_if(ksGetAlloc(ks) == 32, "allocation size wrong"); }
300                 else if (i >= 0) { succeed_if(ksGetAlloc(ks) == 16, "allocation size wrong"); }
301         }
302         succeed_if(ksGetSize(ks) == 0, "could not pop 100 keys");
303         succeed_if(ksGetAlloc(ks) == 16, "allocation size wrong");
304         ksDel (ks);
305         
306         exit_if_fail((ks=ksNew(0)) != 0, "could not create new keyset");
307         ksResize (ks, 101);
308         succeed_if(ksGetAlloc(ks) == 101, "allocation size wrong");
309         for (i=0; i< 100; i++)
310         {
311                 ksAppendKey(ks, keyNew (KEY_END));
312         }
313         succeed_if(ksGetSize(ks) == 100, "could not append 100 keys");
314         succeed_if(ksGetAlloc(ks) == 101, "allocation size wrong");
315         ksDel (ks);
316
317         ks =
318 #include "keyset.c"
319
320         succeed_if(ksGetSize(ks) == 102, "Problem loading keyset with 102 keys");
321         succeed_if(ksGetAlloc(ks) == 128, "alloc size wrong");
322
323         ksCopy (copy, ks);
324         compare_keyset(copy, ks, 0, 0);
325
326         ksClear (copy); // useless, just test for double free
327         ksCopy (copy, ks);
328         compare_keyset(copy, ks, 0, 0);
329
330         ksDel (copy);
331         ksDel (ks);
332 }
333
334 void test_ksDup()
335 {
336         KeySet *ks=0;
337         KeySet *other=0;
338
339         printf("Test ks duplication\n");
340
341         exit_if_fail((ks=ksNew(0)) != 0, "could not create new keyset");
342         other=ksDup(ks);
343         succeed_if(other, "other creation failed");
344         succeed_if(ksGetSize(ks) == 0, "ks has keys");
345         succeed_if(ksGetSize(other) == 0, "other has keys");
346         ksDel (other);
347         ksDel (ks);
348
349         exit_if_fail((ks=ksNew(1, keyNew(0), 0)) != 0, "could not create new keyset");
350         other=ksDup(ks);
351         succeed_if(other, "other creation failed");
352         succeed_if(ksGetSize(ks) == 1, "ks has no keys");
353         succeed_if(ksGetSize(other) == 1, "other has no keys");
354         ksDel (other);
355         ksDel (ks);
356
357         exit_if_fail((ks=ksNew(1, keyNew(0), 0)) != 0, "could not create new keyset");
358         succeed_if (ksAppendKey(ks,keyNew("user/test", KEY_END)) == 2, "could not append a key");
359         succeed_if (ksAppendKey(ks,keyNew(0)) == 3, "could not append a key");
360         succeed_if (ksAppendKey(ks,keyNew(0)) == 4, "could not append a key");
361         other=ksDup(ks);
362         succeed_if(other, "other creation failed");
363         succeed_if(ksGetSize(ks) == 4, "ks has no keys");
364         succeed_if(ksGetSize(other) == 4, "other has no keys");
365         ksDel (other);
366         ksDel (ks);
367         
368         exit_if_fail((ks=ksNew(1, keyNew(0), 0)) != 0, "could not create new keyset");
369         succeed_if (ksAppendKey(ks,keyNew("user/test", KEY_END)) == 2, "could not append a key");
370         succeed_if (ksAppendKey(ks,keyNew(0)) == 3, "could not append a key");
371         succeed_if (ksAppendKey(ks,keyNew(0)) == 4, "could not append a key");
372         other=ksDup(ks);
373         succeed_if(other, "other creation failed");
374         keyDel (ksPop (other));
375         succeed_if(ksGetSize(ks) == 4, "ks has no keys");
376         succeed_if(ksGetSize(other) == 3, "other has no keys");
377         ksDel (other);
378         ksDel (ks);
379         
380         exit_if_fail((ks=ksNew(1, keyNew(0), 0)) != 0, "could not create new keyset");
381         succeed_if (ksAppendKey(ks,keyNew("user/test", KEY_END)) == 2, "could not append a key");
382         succeed_if (ksAppendKey(ks,keyNew(0)) == 3, "could not append a key");
383         succeed_if (ksAppendKey(ks,keyNew(0)) == 4, "could not append a key");
384         other=ksDup(ks);
385         succeed_if(other, "other creation failed");
386         keyDel(ksPop (other));
387         succeed_if (ksAppendKey(ks,keyNew(0)) == 5, "could not append a key");
388         succeed_if(ksGetSize(ks) == 5, "ks has no keys");
389         succeed_if(ksGetSize(other) == 3, "other has no keys");
390         ksDel (other);
391         ksDel (ks);
392 }
393
394 void test_ksCopy()
395 {
396         KeySet *ks=0;
397         KeySet *other=0;
398
399         printf("Test ks copy\n");
400
401         other = ksNew(0);
402         exit_if_fail((ks=ksNew(0)) != 0, "could not create new keyset");
403         succeed_if(ksCopy(other,ks)==1, "Copy failed");
404         succeed_if(other, "other creation failed");
405         succeed_if(ksGetSize(ks) == 0, "ks has keys");
406         succeed_if(ksGetSize(other) == 0, "other has keys");
407         ksDel (other);
408         ksDel (ks);
409
410         other = ksNew(0);
411         exit_if_fail((ks=ksNew(1, keyNew(0), 0)) != 0, "could not create new keyset");
412         succeed_if(ksCopy(other,ks)==1, "Copy failed");
413         succeed_if(other, "other creation failed");
414         succeed_if(ksGetSize(ks) == 1, "ks has no keys");
415         succeed_if(ksGetSize(other) == 1, "other has no keys");
416         ksDel (other);
417         ksDel (ks);
418
419         other = ksNew(0);
420         exit_if_fail((ks=ksNew(1, keyNew(0), 0)) != 0, "could not create new keyset");
421         succeed_if (ksAppendKey(ks,keyNew("user/test", KEY_END)) == 2, "could not append a key");
422         succeed_if (ksAppendKey(ks,keyNew(0)) == 3, "could not append a key");
423         succeed_if (ksAppendKey(ks,keyNew(0)) == 4, "could not append a key");
424         succeed_if(ksCopy(other,ks)==1, "Copy failed");
425         succeed_if(other, "other creation failed");
426         succeed_if(ksGetSize(ks) == 4, "ks has no keys");
427         succeed_if(ksGetSize(other) == 4, "other has no keys");
428         ksDel (other);
429         ksDel (ks);
430
431         other = ksNew(0);
432         exit_if_fail((ks=ksNew(1, keyNew(0), 0)) != 0, "could not create new keyset");
433         succeed_if (ksAppendKey(ks,keyNew("user/test", KEY_END)) == 2, "could not append a key");
434         succeed_if (ksAppendKey(ks,keyNew(0)) == 3, "could not append a key");
435         succeed_if (ksAppendKey(ks,keyNew(0)) == 4, "could not append a key");
436         succeed_if(ksCopy(other,ks)==1, "Copy failed");
437         succeed_if(other, "other creation failed");
438         keyDel (ksPop (other));
439         succeed_if(ksGetSize(ks) == 4, "ks has no keys");
440         succeed_if(ksGetSize(other) == 3, "other has no keys");
441         ksDel (other);
442         ksDel (ks);
443
444         other = ksNew(0);
445         exit_if_fail((ks=ksNew(1, keyNew(0), 0)) != 0, "could not create new keyset");
446         succeed_if (ksAppendKey(ks,keyNew("user/test", KEY_END)) == 2, "could not append a key");
447         succeed_if (ksAppendKey(ks,keyNew(0)) == 3, "could not append a key");
448         succeed_if (ksAppendKey(ks,keyNew(0)) == 4, "could not append a key");
449         succeed_if(ksCopy(other,ks)==1, "Copy failed");
450         succeed_if(other, "other creation failed");
451         keyDel(ksPop (other));
452         succeed_if (ksAppendKey(ks,keyNew(0)) == 5, "could not append a key");
453         succeed_if(ksGetSize(ks) == 5, "ks has no keys");
454         succeed_if(ksGetSize(other) == 3, "other has no keys");
455         ksDel (other);
456         ksDel (ks);
457
458         other = ksNew(0);
459         exit_if_fail((ks=ksNew(1, keyNew(0), 0)) != 0, "could not create new keyset");
460         succeed_if (ksAppendKey(ks,keyNew("user/test", KEY_END)) == 2, "could not append a key");
461         succeed_if (ksAppendKey(ks,keyNew(0)) == 3, "could not append a key");
462         succeed_if (ksAppendKey(ks,keyNew(0)) == 4, "could not append a key");
463         succeed_if(ksCopy(other,ks)==1, "Copy failed");
464         succeed_if(other, "other creation failed");
465         keyDel(ksPop (other));
466         succeed_if (ksAppendKey(ks,keyNew(0)) == 5, "could not append a key");
467         succeed_if(ksGetSize(ks) == 5, "ks has no keys");
468         succeed_if(ksGetSize(other) == 3, "other has no keys");
469
470         succeed_if(ksCopy(ks,0)==0, "Clear failed");
471         succeed_if(ksGetSize(ks) == 0, "ks has keys");
472
473         succeed_if(ksCopy(other,0)==0, "Clear failed");
474         succeed_if(ksGetSize(other) == 0, "other has keys");
475         ksDel (other);
476         ksDel (ks);
477 }
478
479 void test_ksIterate()
480 {
481         KeySet *ks=ksNew(0);
482         KeySet *other=ksNew(0);
483         Key * key;
484         int i;
485         char name [] = "user/n";
486         char output [] = "2 key not on its place";
487
488         printf("Test keyset iterate\n");
489         ksAppendKey(ks,keyNew("user/1", KEY_END));
490         ksAppendKey(ks,keyNew("user/2", KEY_END));
491         ksAppendKey(ks,keyNew("user/3", KEY_END));
492         ksAppendKey(ks,keyNew("user/4", KEY_END));
493         ksAppendKey(ks,keyNew("user/5", KEY_END));
494         succeed_if(ksGetSize(ks) == 5, "could not append 5 keys");
495
496         key = ksPop(ks);
497         succeed_if(strcmp(keyName(key), "user/5") == 0, "1 key not on first place");
498         succeed_if (keyDel (key) == 0, "could not del popped key");
499
500         succeed_if(ksAppend(other,ks) == 4, "could not append keys");
501
502         for (i=4; i>=1; i--)
503         {
504                 key = ksPop(other);
505                 succeed_if (key != 0, "got null pointer key");
506                 name[5] = '0' + i;
507                 output[0] = '0' + i;
508                 succeed_if(strcmp(keyName(key), name) == 0, output);
509                 keyDel (key);
510         }
511         
512         succeed_if (ksAppendKey(other, keyNew ("user/3", KEY_END)) == 1, "could not append one key");
513         key = ksPop(other);
514         succeed_if (key != 0, "got null pointer key");
515         succeed_if(strcmp(keyName(key), "user/3") == 0, "only key to pop not found");
516         succeed_if (keyDel (key) == 0, "could not del popped key");
517         ksDel(other);
518         ksDel(ks);
519
520         ks = ksNew(10,
521                 keyNew("user/0", KEY_END),
522                 keyNew("user/1", KEY_END),
523                 keyNew("user/2", KEY_END),
524                 keyNew("user/3", KEY_END),
525                 0);
526         
527         other = ksNew(10,
528                 keyNew("user/4", KEY_END),
529                 keyNew("user/5", KEY_END),
530                 keyNew("user/6", KEY_END),
531                 keyNew("user/7", KEY_END),
532                 0);
533
534         succeed_if(ksAppend(ks,other) == 8, "could not append keys");
535
536         for (i=7; i >= 0; i--)
537         {
538                 key = ksPop(ks);
539                 succeed_if (key != 0, "got null pointer key");
540                 name[5] = '0' + i;
541                 output[0] = '0' + i;
542                 succeed_if(strcmp(keyName(key), name) == 0, output);
543                 keyDel (key);
544         }
545         ksDel (ks);
546         ksDel (other);
547 }
548
549 void test_ksCursor()
550 {
551         KeySet *ks=ksNew(0);
552         Key * key;
553         cursor_t cursor;
554         int i;
555         char name [] = "user/n";
556         char output [] = "n key not on its place";
557
558         printf("Test keyset cursor\n");
559
560         ksAppendKey(ks,keyNew("user/1", KEY_END));
561         ksAppendKey(ks,keyNew("user/2", KEY_END));
562         ksAppendKey(ks,keyNew("user/3", KEY_END));
563         cursor = ksGetCursor (ks);
564         ksAppendKey(ks,keyNew("user/4", KEY_END));
565         ksAppendKey(ks,keyNew("user/5", KEY_END));
566         succeed_if(ksGetSize(ks) == 5, "could not append 5 keys");
567
568         succeed_if (cursor == ksGetCursor(ks), "cursor after appending");
569         ksSetCursor (ks, cursor);
570         succeed_if (cursor == ksGetCursor(ks), "cursor set to 3");
571
572         cursor = ksGetCursor (ks);
573         key = ksPop(ks);
574         succeed_if (cursor == ksGetCursor(ks), "cursor should stay the same");
575         succeed_if(strcmp(keyName(key), "user/5") == 0, "1 key not on first place");
576         succeed_if (keyDel (key) == 0, "could not del popped key");
577
578         ksRewind (ks);
579         for (i=0; i<5; i++)
580         {
581                 key = ksNext(ks);
582                 if (i==1)
583                 {
584                         cursor = ksGetCursor (ks);
585                         name[5] = '0' + i;
586                         output[0] = '0' + i;
587                 }
588         }
589         ksSetCursor(ks, cursor);
590         key = ksCurrent (ks);
591
592         ksDel(ks);
593
594         ks = ksNew(10,
595                 keyNew("user/0", KEY_END),
596                 keyNew("user/1", KEY_END),
597                 keyNew("user/2", KEY_END),
598                 keyNew("user/3", KEY_END),
599                 0);
600
601         ksRewind (ks);
602         for (i=0; i < 4; i++)
603         {
604                 key = ksNext(ks);
605                 if (i==1)
606                 {
607                         cursor = ksGetCursor (ks);
608                         name[5] = '0' + i;
609                         output[0] = '0' + i;
610                 }
611         }
612
613         ksSetCursor(ks, cursor);
614         key = ksCurrent (ks);
615         succeed_if(!strcmp (keyName(key), name), output);
616
617         ksDel (ks);
618 }
619
620 void test_ksSort()
621 {
622         KeySet  *ks;
623         Key     *key, *k1, *k2;
624         int     i;
625
626         printf("Test ks sort\n");
627
628         printf ("Sort with no keyNeedRemove set\n");
629         ks=ksNew(0);
630         ksAppendKey(ks, keyNew("user/bname", KEY_END));
631         ksAppendKey(ks, keyNew("user/aname", KEY_END));
632         ksAppendKey(ks, keyNew("user/cname", KEY_END));
633         
634         ksSort (ks);
635         ksRewind(ks);
636         key = ksNext(ks);
637         succeed_if (strcmp (keyName (key), "user/aname") == 0, "a should be 1.");
638         
639         key = ksNext(ks);
640         succeed_if (strcmp (keyName (key), "user/bname") == 0, "b should be 2.");
641         
642         key = ksNext(ks);
643         succeed_if (strcmp (keyName (key), "user/cname") == 0, "c should be 3.");
644         ksDel (ks);
645         
646         ks=ksNew(0);
647         ksAppendKey(ks, keyNew("user/a", KEY_END));
648         ksAppendKey(ks, keyNew("user/e", KEY_END));
649         ksAppendKey(ks, keyNew("user/b", KEY_END));
650         ksAppendKey(ks, keyNew("user/b", KEY_END));
651         ksAppendKey(ks, keyNew("user/d", KEY_END));
652         ksAppendKey(ks, keyNew("user/c", KEY_END));
653         ksAppendKey(ks, keyNew("user/c", KEY_END));
654         ksAppendKey(ks, keyNew("user/g", KEY_END));
655         ksAppendKey(ks, keyNew("user/h", KEY_END));
656         ksAppendKey(ks, keyNew("user/h", KEY_END));
657         ksAppendKey(ks, keyNew("user/f", KEY_END));
658
659         ksSort (ks);
660         ksRewind (ks);
661         for (i=0; (key=ksNext(ks)) != 0; i++)
662         {
663                 switch (i)
664                 {
665                 case 0: succeed_if (strcmp (keyName (key), "user/a") == 0, "wrong name found.");
666                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
667                         break;
668                 case 1: succeed_if (strcmp (keyName (key), "user/b") == 0, "wrong name found.");
669                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
670                         break;
671                 case 2: succeed_if (strcmp (keyName (key), "user/b") == 0, "wrong name found.");
672                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
673                         break;
674                 case 3: succeed_if (strcmp (keyName (key), "user/c") == 0, "wrong name found.");
675                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
676                         break;
677                 case 4: succeed_if (strcmp (keyName (key), "user/c") == 0, "wrong name found.");
678                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
679                         break;
680                 case 5: succeed_if (strcmp (keyName (key), "user/d") == 0, "wrong name found.");
681                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
682                         break;
683                 case 6: succeed_if (strcmp (keyName (key), "user/e") == 0, "wrong name found.");
684                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
685                         break;
686                 case 7: succeed_if (strcmp (keyName (key), "user/f") == 0, "wrong name found.");
687                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
688                         break;
689                 case 8: succeed_if (strcmp (keyName (key), "user/g") == 0, "wrong name found.");
690                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
691                         break;
692                 case 9: succeed_if (strcmp (keyName (key), "user/h") == 0, "wrong name found.");
693                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
694                         break;
695                 case 10:succeed_if (strcmp (keyName (key), "user/h") == 0, "wrong name found.");
696                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
697                         break;
698                 default:succeed_if (0, "should not reach");
699                         break;
700                 }
701         }
702         ksDel (ks);
703
704         printf ("Sort with some keyNeedRemove set\n");
705         ks=ksNew(0);
706         k1 = keyNew("user/aname", KEY_END);
707         ksAppendKey(ks,k1);
708
709         k2 = keyDup (k1);
710         keyRemove(k2);
711
712         succeed_if (keyGetRef(k2) == 0, "reference counter not resetted");
713         ksAppendKey(ks,k2);
714         succeed_if (keyGetRef(k2) == 1, "reference counter not incremented");
715         ksSort (ks);
716
717         ksRewind (ks);
718         key = ksNext(ks);
719         // printf ("%d\n", keyNeedRemove (key));
720         succeed_if (keyNeedRemove (key) == 1, "Removed key should be on first position");
721         ksDel(ks);
722         
723         ks=ksNew(0);
724         k1 = keyNew("user/aname", KEY_END);
725         k2 = keyDup (k1);
726         keyRemove(k2);
727         ksAppendKey(ks,k2);
728         ksAppendKey(ks,k1);
729         ksSort (ks);
730
731         ksRewind (ks);
732         key = ksNext(ks);
733         succeed_if (keyNeedRemove (key) == 1, "Removed key should be on first position");
734         ksDel(ks);
735
736         ks=ksNew(0);
737         ksAppendKey(ks, keyNew("user/a", KEY_REMOVE, KEY_END));
738         ksAppendKey(ks, keyNew("user/e", KEY_END));
739         ksAppendKey(ks, keyNew("user/b", KEY_REMOVE, KEY_END));
740         ksAppendKey(ks, keyNew("user/b", KEY_END));
741         ksAppendKey(ks, keyNew("user/d", KEY_REMOVE, KEY_END));
742         ksAppendKey(ks, keyNew("user/c", KEY_END));
743         ksAppendKey(ks, keyNew("user/c", KEY_REMOVE, KEY_END));
744         ksAppendKey(ks, keyNew("user/g", KEY_REMOVE, KEY_END));
745         ksAppendKey(ks, keyNew("user/h", KEY_REMOVE, KEY_END));
746         ksAppendKey(ks, keyNew("user/h", KEY_END));
747         ksAppendKey(ks, keyNew("user/f", KEY_REMOVE, KEY_END));
748
749         ksSort (ks);
750         ksRewind (ks);
751         // output_keyset(ks,0);
752         for (i=0; (key=ksNext(ks)) != 0; i++)
753         {
754                 switch (i)
755                 {
756                 case 0: succeed_if (strcmp (keyName (key), "user/h") == 0, "wrong name found.");
757                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
758                         break;
759                 case 1: succeed_if (strcmp (keyName (key), "user/g") == 0, "wrong name found.");
760                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
761                         break;
762                 case 2: succeed_if (strcmp (keyName (key), "user/f") == 0, "wrong name found.");
763                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
764                         break;
765                 case 3: succeed_if (strcmp (keyName (key), "user/d") == 0, "wrong name found.");
766                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
767                         break;
768                 case 4: succeed_if (strcmp (keyName (key), "user/c") == 0, "wrong name found.");
769                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
770                         break;
771                 case 5: succeed_if (strcmp (keyName (key), "user/b") == 0, "wrong name found.");
772                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
773                         break;
774                 case 6: succeed_if (strcmp (keyName (key), "user/a") == 0, "wrong name found.");
775                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
776                         break;
777                 case 7: succeed_if (strcmp (keyName (key), "user/b") == 0, "wrong name found.");
778                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
779                         break;
780                 case 8: succeed_if (strcmp (keyName (key), "user/c") == 0, "wrong name found.");
781                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
782                         break;
783                 case 9: succeed_if (strcmp (keyName (key), "user/e") == 0, "wrong name found.");
784                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
785                         break;
786                 case 10:succeed_if (strcmp (keyName (key), "user/h") == 0, "wrong name found.");
787                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
788                         break;
789                 default:succeed_if (0, "should not reach");
790                         break;
791                 }
792         }
793         ksDel (ks);
794
795         
796         printf ("Sort with all keyNeedRemove set\n");
797         ks=ksNew(0);
798         ksAppendKey(ks, keyNew("user/a", KEY_REMOVE, KEY_END));
799         ksAppendKey(ks, keyNew("user/e", KEY_REMOVE, KEY_END));
800         ksAppendKey(ks, keyNew("user/b/a", KEY_REMOVE, KEY_END));
801         ksAppendKey(ks, keyNew("user/b", KEY_REMOVE, KEY_END));
802         ksAppendKey(ks, keyNew("user/d", KEY_REMOVE, KEY_END));
803         ksAppendKey(ks, keyNew("user/c", KEY_REMOVE, KEY_END));
804         ksAppendKey(ks, keyNew("user/c/a", KEY_REMOVE, KEY_END));
805         ksAppendKey(ks, keyNew("user/g", KEY_REMOVE, KEY_END));
806         ksAppendKey(ks, keyNew("user/h/a", KEY_REMOVE, KEY_END));
807         ksAppendKey(ks, keyNew("user/h", KEY_REMOVE, KEY_END));
808         ksAppendKey(ks, keyNew("user/f", KEY_REMOVE, KEY_END));
809
810         ksSort(ks);
811         ksRewind (ks);
812         // output_keyset(ks,0);
813         for (i=0; (key=ksNext(ks)) != 0; i++)
814         {
815                 switch (i)
816                 {
817                 case 0: succeed_if (strcmp (keyName (key), "user/h/a") == 0, "wrong name found.");
818                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
819                         break;
820                 case 1: succeed_if (strcmp (keyName (key), "user/h") == 0, "wrong name found.");
821                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
822                         break;
823                 case 2: succeed_if (strcmp (keyName (key), "user/g") == 0, "wrong name found.");
824                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
825                         break;
826                 case 3: succeed_if (strcmp (keyName (key), "user/f") == 0, "wrong name found.");
827                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
828                         break;
829                 case 4: succeed_if (strcmp (keyName (key), "user/e") == 0, "wrong name found.");
830                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
831                         break;
832                 case 5: succeed_if (strcmp (keyName (key), "user/d") == 0, "wrong name found.");
833                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
834                         break;
835                 case 6: succeed_if (strcmp (keyName (key), "user/c/a") == 0, "wrong name found.");
836                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
837                         break;
838                 case 7: succeed_if (strcmp (keyName (key), "user/c") == 0, "wrong name found.");
839                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
840                         break;
841                 case 8: succeed_if (strcmp (keyName (key), "user/b/a") == 0, "wrong name found.");
842                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
843                         break;
844                 case 9: succeed_if (strcmp (keyName (key), "user/b") == 0, "wrong name found.");
845                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
846                         break;
847                 case 10:succeed_if (strcmp (keyName (key), "user/a") == 0, "wrong name found.");
848                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
849                         break;
850                 default:succeed_if (0, "should not reach");
851                         break;
852                 }
853         }
854         ksDel (ks);
855         
856         printf ("Sort with mixed keyNeedRemove set and subdirs\n");
857         ks=ksNew(0);
858         ksAppendKey(ks, keyNew("user/dir1/key1", KEY_REMOVE, KEY_END));
859         ksAppendKey(ks, keyNew("user/dir1/key2", KEY_END));
860         ksAppendKey(ks, keyNew("user/dir1/key3", KEY_REMOVE, KEY_END));
861         ksAppendKey(ks, keyNew("user/dir2",      KEY_REMOVE, KEY_END));
862         ksAppendKey(ks, keyNew("user/dir2/key1", KEY_REMOVE, KEY_END));
863         ksAppendKey(ks, keyNew("user/dir3/key1", KEY_END));
864         ksAppendKey(ks, keyNew("user/dir3",      KEY_END));
865         ksAppendKey(ks, keyNew("user/dir3/key2", KEY_END));
866         ksAppendKey(ks, keyNew("user/dir4",      KEY_END));
867         ksAppendKey(ks, keyNew("user/dir5/key1", KEY_REMOVE, KEY_END));
868         ksAppendKey(ks, keyNew("user/dir6/key1", KEY_END));
869
870         ksSort(ks);
871         ksRewind (ks);
872         // output_keyset(ks,0);
873         for (i=0; (key=ksNext(ks)) != 0; i++)
874         {
875                 switch (i)
876                 {
877                 case 0: succeed_if (strcmp (keyName (key), "user/dir5/key1") == 0, "wrong name found.");
878                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
879                         break;
880                 case 1: succeed_if (strcmp (keyName (key), "user/dir2/key1") == 0, "wrong name found.");
881                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
882                         break;
883                 case 2: succeed_if (strcmp (keyName (key), "user/dir2") == 0, "wrong name found.");
884                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
885                         break;
886                 case 3: succeed_if (strcmp (keyName (key), "user/dir1/key3") == 0, "wrong name found.");
887                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
888                         break;
889                 case 4: succeed_if (strcmp (keyName (key), "user/dir1/key1") == 0, "wrong name found.");
890                         succeed_if (keyNeedRemove (key) == 1, "wrong removed key information");
891                         break;
892                 case 5: succeed_if (strcmp (keyName (key), "user/dir1/key2") == 0, "wrong name found.");
893                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
894                         break;
895                 case 6: succeed_if (strcmp (keyName (key), "user/dir3") == 0, "wrong name found.");
896                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
897                         break;
898                 case 7: succeed_if (strcmp (keyName (key), "user/dir3/key1") == 0, "wrong name found.");
899                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
900                         break;
901                 case 8: succeed_if (strcmp (keyName (key), "user/dir3/key2") == 0, "wrong name found.");
902                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
903                         break;
904                 case 9: succeed_if (strcmp (keyName (key), "user/dir4") == 0, "wrong name found.");
905                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
906                         break;
907                 case 10:succeed_if (strcmp (keyName (key), "user/dir6/key1") == 0, "wrong name found.");
908                         succeed_if (keyNeedRemove (key) == 0, "wrong removed key information");
909                         break;
910                 default:succeed_if (0, "should not reach");
911                         break;
912                 }
913         }
914         ksDel (ks);
915 }
916
917 void ksUnsort (KeySet *ks)
918 {
919         Key *cur;
920         size_t size = 0;
921         KeySet *randks=ksNew(0); /*This is the final randomized keyset*/
922         KeySet *tempks=ksNew(0); /*Temporary storage for keys not chosen to be inserted*/
923
924         while (ksGetSize(ks) > 0)
925         {
926                 ksRewind(ks);
927                 size = ksGetSize(ks);
928                 /* printf ("iterating %d\n", size); */
929                 while ((cur=ksPop(ks)) != 0)
930                 {
931                         /* printf ("\titerating %s\n", keyName(cur)); */
932                         if (!(rand()%size)) ksAppendKey(randks, cur);
933                         else ksAppendKey(tempks,cur);
934                 }
935                 ksAppend(ks, tempks);
936                 ksCopy(tempks,0);
937         }
938
939         ksCopy (ks, randks);
940
941         ksDel (randks);
942         ksDel (tempks);
943 }
944
945 void test_ksLookup()
946 {
947         printf ("Test lookup\n");
948
949         int i,j;
950         Key *cur=0;
951         Key *found=0;
952         Key *k[1000];
953         KeySet *ks = ksNew (30,
954                 k[0]=keyNew ("user/rem3", KEY_REMOVE, KEY_DIR, KEY_END),
955                 k[1]=keyNew ("user/rem2", KEY_REMOVE, KEY_DIR, KEY_END),
956                 k[2]=keyNew ("user/rem1/key2", KEY_REMOVE, KEY_END),
957                 k[3]=keyNew ("user/rem1/key1", KEY_REMOVE, KEY_END),
958                 k[4]=keyNew ("user/rem1", KEY_REMOVE, KEY_DIR, KEY_END),
959                 k[5]=keyNew ("user/dir1", KEY_DIR, KEY_END),
960                 k[6]=keyNew ("user/dir1/key1", KEY_VALUE, "value1", KEY_END),
961                 k[7]=keyNew ("user/dir1/key2", KEY_VALUE, "value2", KEY_END),
962                 k[8]=keyNew ("user/dir1/key3", KEY_VALUE, "value3", KEY_END),
963                 k[9]=keyNew ("user/dir1/key4", KEY_VALUE, "value4", KEY_END),
964                 k[10]=keyNew ("user/dir1/.inactive1", KEY_COMMENT, "key is inactive", KEY_END),
965                 k[11]=keyNew ("user/dir1/.inactive2", KEY_COMMENT, "additional information", KEY_END),
966                 k[12]=keyNew ("user:max/dir2", KEY_DIR, KEY_END),
967                 k[13]=keyNew ("user:max/dir2/key1", KEY_VALUE, "value1", KEY_END),
968                 k[14]=keyNew ("user/dir2/key2", KEY_VALUE, "value2", KEY_END),
969                 k[15]=keyNew ("user/dir2/key3", KEY_VALUE, "value3", KEY_END),
970                 k[16]=keyNew ("user:hugo/dir2/key4", KEY_VALUE, "value4", KEY_END),
971                 k[17]=keyNew ("user/dir3", KEY_DIR, KEY_END),
972                 k[18]=keyNew ("user/dir3/key1", KEY_VALUE, "value1", KEY_END),
973                 k[19]=keyNew ("user:sb/dir3/.inactive1", KEY_COMMENT, "key is inactive", KEY_END),
974                 k[20]=keyNew ("user/dir3/.inactive2", KEY_COMMENT, "a users comment", KEY_END),
975                 k[21]=keyNew ("user/dir4", KEY_DIR, KEY_END),
976                 k[22]=keyNew ("user/dir5", KEY_DIR, KEY_END),
977                 KS_END);
978
979         /* key 5 */
980         k[23] = keyNew ("user/DiR1", KEY_END);
981         /* key 6 */
982         k[24] = keyNew ("user/DiR1/KEY1", KEY_END);
983         k[25] = keyNew ("user:wrongowner/DiR1/KEY1", KEY_END);
984         k[26] = keyNew ("user:nop/DiR1/KEY1", KEY_END);
985         k[27] = keyNew ("user:wrongowner/dir1/key1", KEY_END);
986         k[28] = keyNew ("user:nop/dir1/key1", KEY_END);
987         /*key 13 */
988         k[29] = keyNew ("user:wrongowner/dir2/key1", KEY_END);
989         k[30] = keyNew ("user/dir2/key1", KEY_END);
990         k[31] = keyNew ("user:max/dir2/key1", KEY_END);
991
992         srand(23);
993
994         succeed_if (ksLookup(0, k[23], 0) == 0, "null pointer");
995         succeed_if (ksLookup(ks, 0, 0) == 0, "null pointer");
996
997         for (i=0; i<100; i++)
998         {
999                 ksUnsort(ks);
1000                 for (j=0; j<5; j++)
1001                         succeed_if (ksLookup(ks, k[j], 0)==0, "found removed key");
1002                 for (j=5; j<23;j++)
1003                         succeed_if (ksLookup(ks, k[j], 0)==k[j], "did not found key");
1004                 succeed_if (ksLookup(ks, k[23], KDB_O_NOCASE) == k[5], "did not found key");
1005                 succeed_if (ksLookup(ks, k[23], 0) == 0, "found wrong key");
1006                 succeed_if (ksLookup(ks, k[24], KDB_O_NOCASE) == k[6], "did not found key");
1007                 succeed_if (ksLookup(ks, k[24], 0) == 0, "found wrong key");
1008                 succeed_if (ksLookup(ks, k[25], KDB_O_NOCASE) == k[6], "did not found key");
1009                 succeed_if (ksLookup(ks, k[25], KDB_O_WITHOWNER|KDB_O_NOCASE) == 0, "found wrong key");
1010                 succeed_if (ksLookup(ks, k[28], 0) == k[6], "did not found key");
1011                 succeed_if (ksLookup(ks, k[28], KDB_O_WITHOWNER) == 0, "found wrong key");
1012                 succeed_if (ksLookup(ks, k[31], KDB_O_WITHOWNER) == k[13], "did not found key");
1013                 /* Empty lines to add more tests:
1014                 succeed_if (ksLookup(ks, k[], ) == k[], "did not found key");
1015                 succeed_if (ksLookup(ks, k[], ) == 0, "found wrong key");
1016                 */
1017         }
1018
1019         ksDel (ks);
1020         for (i=23; i<32;i++) keyDel (k[i]);
1021 }
1022
1023 void test_ksLookupByName()
1024 {
1025         printf ("Test lookup by name\n");
1026
1027         int i,j;
1028         Key *cur=0;
1029         Key *found=0;
1030         char *name[1000];
1031         Key *k[1000];
1032         KeySet *ks = ksNew (30,
1033                 k[0]=keyNew (name[0] = "user/rem3", KEY_REMOVE, KEY_DIR, KEY_END),
1034                 k[1]=keyNew (name[1] = "user/rem2", KEY_REMOVE, KEY_DIR, KEY_END),
1035                 k[2]=keyNew (name[2] = "user/rem1/key2", KEY_REMOVE, KEY_END),
1036                 k[3]=keyNew (name[3] = "user/rem1/key1", KEY_REMOVE, KEY_END),
1037                 k[4]=keyNew (name[4] = "user/rem1", KEY_REMOVE, KEY_DIR, KEY_END),
1038                 k[5]=keyNew (name[5] = "user/dir1", KEY_DIR, KEY_END),
1039                 k[6]=keyNew (name[6] = "user/dir1/key1", KEY_VALUE, "value1", KEY_END),
1040                 k[7]=keyNew (name[7] = "user/dir1/key2", KEY_VALUE, "value2", KEY_END),
1041                 k[8]=keyNew (name[8] = "user/dir1/key3", KEY_VALUE, "value3", KEY_END),
1042                 k[9]=keyNew (name[9] = "user/dir1/key4", KEY_VALUE, "value4", KEY_END),
1043                 k[10]=keyNew (name[10] ="user/dir1/.inactive1", KEY_COMMENT, "key is inactive", KEY_END),
1044                 k[11]=keyNew (name[11] ="user/dir1/.inactive2", KEY_COMMENT, "additional information", KEY_END),
1045                 k[12]=keyNew (name[12] ="user:max/dir2", KEY_DIR, KEY_END),
1046                 k[13]=keyNew (name[13] ="user:max/dir2/key1", KEY_VALUE, "value1", KEY_END),
1047                 k[14]=keyNew (name[14] ="user/dir2/key2", KEY_VALUE, "value2", KEY_END),
1048                 k[15]=keyNew (name[15] ="user/dir2/key3", KEY_VALUE, "value3", KEY_END),
1049                 k[16]=keyNew (name[16] ="user:hugo/dir2/key4", KEY_VALUE, "value4", KEY_END),
1050                 k[17]=keyNew (name[17] ="user/dir3", KEY_DIR, KEY_END),
1051                 k[18]=keyNew (name[18] ="user/dir3/key1", KEY_VALUE, "value1", KEY_END),
1052                 k[19]=keyNew (name[19] ="user:sb/dir3/.inactive1", KEY_COMMENT, "key is inactive", KEY_END),
1053                 k[20]=keyNew (name[20] ="user/dir3/.inactive2", KEY_COMMENT, "a users comment", KEY_END),
1054                 k[21]=keyNew (name[21] ="user/dir4", KEY_DIR, KEY_END),
1055                 k[22]=keyNew (name[22] ="user/dir5", KEY_DIR, KEY_END),
1056                 KS_END);
1057
1058         name[23] = "user/DiR1";
1059         name[24] = "user/DiR1/KEY1";
1060         name[25] = "user:wrongowner/DiR1/KEY1";
1061         name[26] = "user:nop/DiR1/KEY1";
1062         name[27] = "user:wrongowner/dir1/key1";
1063         name[28] = "user:nop/dir1/key1";
1064         name[29] = "user:wrongowner/dir2/key1";
1065         name[30] = "user/dir2/key1";
1066         name[31] = "user:max/dir2/key1";
1067
1068         srand(23);
1069
1070         succeed_if (ksLookupByName(0, name[23], 0) == 0, "null pointer");
1071         succeed_if (ksLookup(ks, 0, 0) == 0, "null pointer");
1072
1073         for (i=0; i<100; i++)
1074         {
1075                 ksUnsort(ks);
1076                 for (j=0; j<5; j++)
1077                         succeed_if (ksLookupByName(ks, name[j], 0)==0, "found removed key");
1078                 for (j=5; j<23;j++)
1079                         succeed_if (ksLookupByName(ks, name[j], 0)==k[j], "did not found key");
1080                 succeed_if (ksLookupByName(ks, name[23], KDB_O_NOCASE) == k[5], "did not found key");
1081                 succeed_if (ksLookupByName(ks, name[23], 0) == 0, "found wrong key");
1082                 succeed_if (ksLookupByName(ks, name[24], KDB_O_NOCASE) == k[6], "did not found key");
1083                 succeed_if (ksLookupByName(ks, name[24], 0) == 0, "found wrong key");
1084                 succeed_if (ksLookupByName(ks, name[25], KDB_O_NOCASE) == k[6], "did not found key");
1085                 succeed_if (ksLookupByName(ks, name[25], KDB_O_WITHOWNER|KDB_O_NOCASE) == 0, "found wrong key");
1086                 succeed_if (ksLookupByName(ks, name[28], 0) == k[6], "did not found key");
1087                 succeed_if (ksLookupByName(ks, name[28], KDB_O_WITHOWNER) == 0, "found wrong key");
1088                 succeed_if (ksLookupByName(ks, name[31], KDB_O_WITHOWNER) == k[13], "did not found key");
1089                 /* Empty lines to add more tests:
1090                 succeed_if (ksLookupByName(ks, name[], ) == name[], "did not found key");
1091                 succeed_if (ksLookupByName(ks, name[], ) == 0, "found wrong key");
1092                 */
1093         }
1094
1095         ksDel (ks);
1096 }
1097
1098
1099 void test_ksLookupName()
1100 {
1101         Key * found;
1102         KeySet *ks= ksNew(0);
1103         
1104         printf ("Test lookup functions\n");
1105         succeed_if (ksNeedSort (ks) == 1, "sort state not correct");
1106
1107         ksAppendKey(ks, keyNew("user/domain/key",  KEY_VALUE, "domainvalue",
1108                 KEY_OWNER, "markus", KEY_END));
1109         ksAppendKey(ks, keyNew("user/single/key",  KEY_VALUE, "singlevalue", KEY_END));
1110         ksAppendKey(ks, keyNew("user/named/key",   KEY_VALUE, "myvalue", KEY_END));
1111         ksAppendKey(ks, keyNew("system/named/key", KEY_VALUE, "syskey",  KEY_END));
1112         ksAppendKey(ks, keyNew("system/sysonly/key", KEY_VALUE, "sysonlykey",  KEY_END));
1113         ksAppendKey(ks, keyNew("user/named/bin", KEY_TYPE, KEY_TYPE_BINARY, KEY_SIZE, 10,
1114                 KEY_VALUE, "binary\1\2data", KEY_END));
1115         ksAppendKey(ks, keyNew("system/named/bin", KEY_TYPE, KEY_TYPE_BINARY, KEY_SIZE, 10,
1116                 KEY_VALUE, "sys\1bin\2", KEY_END));
1117         ksAppendKey(ks, keyNew("system/named/key", KEY_TYPE, KEY_TYPE_BINARY, KEY_SIZE, 10,
1118                 KEY_VALUE, "syskey", KEY_END));
1119         succeed_if (ksGetSize(ks) == 8, "could not append all keys");
1120
1121         succeed_if (ksNeedSort (ks) == 1, "sort state not correct");
1122
1123         // a positive testcase
1124         found = ksLookupByName (ks, "user/named/key", 0);
1125         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1126         succeed_if (ksNeedSort (ks) == 0, "sort state not correct");
1127         ksAppendKey(ks, keyNew("user/single/key",  KEY_VALUE, "singlevalue", KEY_END));
1128         succeed_if (ksNeedSort (ks) == 1, "sort state not correct");
1129
1130         succeed_if (found != 0, "did not found correct name");
1131         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1132         succeed_if (strcmp (keyName(found), "user/named/key") == 0, "name not correct in found key");
1133         succeed_if (strcmp (keyValue(found), "myvalue") == 0, "not correct value in found key");
1134         
1135         // here you can't find the keys
1136         succeed_if (ksLookupByName (ks, "named/key", 0) == 0, "not valid keyname");
1137         succeed_if (ksNeedSort (ks) == 0, "sort state not correct");
1138         succeed_if (ksLookupByName (ks, "u/named/key", 0) == 0, "not valid keyname");
1139         succeed_if (ksLookupByName (ks, "usea/named/key", 0) == 0, "not valid keyname");
1140         succeed_if (ksLookupByName (ks, " user/named/key", 0) == 0, "found key with bad prefix");
1141
1142         succeed_if (ksLookupByName (ks, "user/named/Key", 0) == 0, "found wrong case key");
1143         succeed_if (ksLookupByName (ks, "User/Named/key", 0) == 0, "found wrong case key");
1144         succeed_if (ksLookupByName (ks, "User/named/key", 0) == 0, "found wrong case key");
1145         succeed_if (ksLookupByName (ks, "user/NAMED/key", 0) == 0, "found wrong case key");
1146         succeed_if (ksLookupByName (ks, "USER/NAMED/KEY", 0) == 0, "found wrong case key");
1147         
1148         succeed_if (ksLookupByName (ks, "user/named/keys", 0) == 0, "wrong postfix");
1149         succeed_if (ksLookupByName (ks, "user/named/key_", 0) == 0, "wrong postfix");
1150
1151         succeed_if (ksLookupByName (ks, "user/named/k/ey", 0) == 0, "seperation that should be");
1152         succeed_if (ksLookupByName (ks, "user/na/med/key", 0) == 0, "seperation that should be");
1153
1154         succeed_if (ksLookupByName (ks, "system/domain/key", 0) == 0, "found key in wrong domain");
1155         
1156         //now try to find them, and compare value
1157         found = ksLookupByName (ks, "user/domain/key", 0);
1158         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1159         succeed_if (found != 0, "did not found correct name");
1160         succeed_if (strcmp (keyName(found), "user/domain/key") == 0, "name not correct in found key");
1161         succeed_if (strcmp (keyValue(found), "domainvalue") == 0, "not correct value in found key");
1162         
1163         found = ksLookupByName (ks, "user/single/key", 0);
1164         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1165         succeed_if (found != 0, "did not found correct name");
1166         succeed_if (strcmp (keyName(found), "user/single/key") == 0, "name not correct in found key");
1167         succeed_if (strcmp (keyValue(found), "singlevalue") == 0, "not correct value in found key");
1168         
1169         found = ksLookupByName (ks, "system/named/key", 0);
1170         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1171         succeed_if (found != 0, "did not found correct name");
1172         succeed_if (strcmp (keyName(found), "system/named/key") == 0, "name not correct in found key");
1173         succeed_if (strcmp (keyValue(found), "syskey") == 0, "not correct value in found key");
1174         
1175         found = ksLookupByName (ks, "user/named/bin", 0);
1176         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1177         succeed_if (found != 0, "did not found correct name");
1178         succeed_if (strcmp (keyName(found), "user/named/bin") == 0, "name not correct in found key");
1179         succeed_if (strncmp (keyValue(found), "binary\1\2data",10) == 0, "not correct value in found key");
1180
1181         found = ksLookupByName (ks, "user/named/key", 0);
1182         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1183         succeed_if (found != 0, "could not find same key again");
1184         succeed_if (strcmp (keyName(found), "user/named/key") == 0, "name not correct in found key");
1185         succeed_if (strcmp (keyValue(found), "myvalue") == 0, "not correct value in found key");
1186
1187         printf ("Test nocase lookup functions\n");
1188         found = ksLookupByName (ks, "user/named/key", KDB_O_NOCASE);
1189         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1190         succeed_if (found != 0, "could not find same key again, nocase");
1191         succeed_if (strcmp (keyName(found), "user/named/key") == 0, "name not correct in found key");
1192         succeed_if (strcmp (keyValue(found), "myvalue") == 0, "not correct value in found key");
1193
1194         found = ksLookupByName (ks, "user/NameD/KeY", KDB_O_NOCASE);
1195         succeed_if (found != 0, "could not find same key again, nocase used");
1196         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1197         succeed_if (strcmp (keyName(found), "user/named/key") == 0, "name not correct in found key");
1198         succeed_if (strcmp (keyValue(found), "myvalue") == 0, "not correct value in found key");
1199
1200         found = ksLookupByName (ks, "user/NameD/KEY", KDB_O_NOCASE);
1201         succeed_if (found != 0, "could not find same key again, nocase used");
1202         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1203         succeed_if (strcmp (keyName(found), "user/named/key") == 0, "name not correct in found key");
1204         succeed_if (strcmp (keyValue(found), "myvalue") == 0, "not correct value in found key");
1205         
1206         printf ("Test cascading lookup functions\n");
1207         found = ksLookupByName (ks, "/named/key", 0);
1208         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1209         succeed_if (found != 0, "cascading search failed");
1210         succeed_if (strcmp (keyName(found), "user/named/key") == 0, "name not correct in found key");
1211         succeed_if (strcmp (keyValue(found), "myvalue") == 0, "not correct value in found key");
1212
1213         found = ksLookupByName (ks, "/single/Key", KDB_O_NOCASE);
1214         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1215         succeed_if (found != 0, "could not find same key again, nocase used");
1216         succeed_if (strcmp (keyName(found), "user/single/key") == 0, "name not correct in found key");
1217         succeed_if (strcmp (keyValue(found), "singlevalue") == 0, "not correct value in found key");
1218
1219         found = ksLookupByName (ks, "/sysonly/key", 0);
1220         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1221         succeed_if (found != 0, "could not find same key again, nocase used");
1222         succeed_if (strcmp (keyName(found), "system/sysonly/key") == 0, "name not correct in found key");
1223         succeed_if (strcmp (keyValue(found), "sysonlykey") == 0, "not correct value in found key");
1224
1225         succeed_if (ksLookupByName (ks, "/named/", 0) == 0, "found part of key with cascading");
1226         succeed_if (ksLookupByName (ks, "/named/keyd", 0) == 0, "found part of key with cascading, bad postfix");
1227         
1228         printf ("Test domain lookup functions\n");
1229         found = ksLookupByName (ks, "user:markus/domain/key", KDB_O_WITHOWNER);
1230         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1231         succeed_if (found != 0, "could not find domain key");
1232         succeed_if (strcmp (keyValue(found), "domainvalue") == 0, "not correct value in domain key");
1233         succeed_if (ksLookupByName (ks, "user:hugo/domain/key", KDB_O_WITHOWNER) == 0, "found key in wrong domain");
1234         succeed_if (ksLookupByName (ks, "user:y/domain/key", KDB_O_WITHOWNER) == 0, "found key in wrong domain");
1235         succeed_if (ksLookupByName (ks, "user:markuss/domain/key", KDB_O_WITHOWNER) == 0, "found key in wrong domain");
1236         succeed_if (ksLookupByName (ks, "user:marku/domain/key", KDB_O_WITHOWNER) == 0, "found key in wrong domain");
1237
1238         ksDel(ks);
1239         
1240 }
1241
1242 void test_ksLookupNameRemove()
1243 {
1244         Key * found;
1245         KeySet *ks=ksNew(0);
1246         printf ("Test lookup functions with removed keys\n");
1247         ksAppendKey(ks, keyNew("user/a", KEY_REMOVE, KEY_END));
1248         ksAppendKey(ks, keyNew("user/e", KEY_END));
1249         ksAppendKey(ks, keyNew("user/b", KEY_REMOVE, KEY_END));
1250         ksAppendKey(ks, keyNew("user/b", KEY_END));
1251         ksAppendKey(ks, keyNew("user/d", KEY_REMOVE, KEY_END));
1252         ksAppendKey(ks, keyNew("user/c", KEY_END));
1253         ksAppendKey(ks, keyNew("user/c", KEY_REMOVE, KEY_END));
1254         ksAppendKey(ks, keyNew("user/g", KEY_REMOVE, KEY_END));
1255         ksAppendKey(ks, keyNew("user/h", KEY_REMOVE, KEY_END));
1256         ksAppendKey(ks, keyNew("user/h", KEY_END));
1257         ksAppendKey(ks, keyNew("user/f", KEY_REMOVE, KEY_END));
1258         
1259         found = ksLookupByName (ks, "user/e", 0);
1260         succeed_if (found != 0, "could not find same key again, nocase used");
1261         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1262         succeed_if (strcmp (keyName(found), "user/e") == 0, "name not correct in found key");
1263
1264         found = ksLookupByName (ks, "user/b", 0);
1265         succeed_if (found != 0, "could not find same key again, nocase used");
1266         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1267         succeed_if (strcmp (keyName(found), "user/b") == 0, "name not correct in found key");
1268
1269         found = ksLookupByName (ks, "user/c", 0);
1270         succeed_if (found != 0, "could not find same key again, nocase used");
1271         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1272         succeed_if (strcmp (keyName(found), "user/c") == 0, "name not correct in found key");
1273
1274         found = ksLookupByName (ks, "user/h", 0);
1275         succeed_if (found != 0, "could not find same key again, nocase used");
1276         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1277         succeed_if (strcmp (keyName(found), "user/h") == 0, "name not correct in found key");
1278
1279         succeed_if (ksLookupByName (ks, "user/nonexists", 0) == 0, "found nonexists key");
1280         succeed_if (ksLookupByName (ks, "user/g", 0) == 0, "found removed key");
1281         succeed_if (ksLookupByName (ks, "user/f", 0) == 0, "found removed key");
1282
1283         ksDel (ks);
1284 }
1285
1286 void test_ksLookupNameAll()
1287 {
1288         Key * found;
1289         cursor_t cursor;
1290         KeySet *ks= ksNew(0);
1291         printf ("Test lookup functions with KDB_O_NOALL\n");
1292         ksAppendKey(ks, keyNew("user/a", KEY_END));
1293         ksAppendKey(ks, keyNew("user/b", KEY_END));
1294         ksAppendKey(ks, keyNew("user/c", KEY_END));
1295         ksAppendKey(ks, keyNew("user/d", KEY_END));
1296         ksAppendKey(ks, keyNew("user/e", KEY_END));
1297         ksAppendKey(ks, keyNew("user/00", KEY_DIR, KEY_END));
1298         ksAppendKey(ks, keyNew("user/00/a", KEY_END));
1299         ksAppendKey(ks, keyNew("user/00/b", KEY_END));
1300         ksAppendKey(ks, keyNew("user/00/c", KEY_END));
1301         ksAppendKey(ks, keyNew("user/01", KEY_DIR, KEY_END));
1302         ksAppendKey(ks, keyNew("user/01/a", KEY_END));
1303         ksAppendKey(ks, keyNew("user/01/b", KEY_END));
1304         ksAppendKey(ks, keyNew("user/01/c", KEY_END));
1305         ksAppendKey(ks, keyNew("user/01/d", KEY_END));
1306         ksAppendKey(ks, keyNew("user/02", KEY_DIR, KEY_END));
1307         ksAppendKey(ks, keyNew("user/02/a", KEY_END));
1308         ksAppendKey(ks, keyNew("user/02/b", KEY_END));
1309         ksAppendKey(ks, keyNew("user/02/c", KEY_END));
1310         ksAppendKey(ks, keyNew("user/02/d", KEY_END));
1311         ksAppendKey(ks, keyNew("user/02/e", KEY_END));
1312         ksAppendKey(ks, keyNew("user/02/f", KEY_END));
1313
1314         found = ksLookupByName (ks, "user/e", KDB_O_NOALL);
1315         succeed_if (found != 0, "could not find key");
1316         succeed_if (strcmp (keyName(found), "user/e") == 0, "name not correct in found key");
1317
1318         found = ksLookupByName (ks, "user/e", KDB_O_NOALL);
1319         succeed_if (found == 0, "should not find");
1320
1321         ksRewind(ks);
1322         found = ksLookupByName (ks, "user/a", KDB_O_NOALL);
1323         succeed_if (found != 0, "could not find key");
1324         succeed_if (strcmp (keyName(found), "user/a") == 0, "name not correct in found key");
1325
1326         found = ksLookupByName (ks, "user/e", KDB_O_NOALL);
1327         succeed_if (found != 0, "could not find key");
1328         succeed_if (strcmp (keyName(found), "user/e") == 0, "name not correct in found key");
1329
1330         found = ksLookupByName (ks, "user/00", KDB_O_NOALL);
1331         succeed_if (found != 0, "could not find key");
1332         succeed_if (keyIsDir (found) == 1, "should be dir");
1333         succeed_if (strcmp (keyName(found), "user/00") == 0, "name not correct in found key");
1334
1335         found = ksLookupByName (ks, "user/01", KDB_O_NOALL);
1336         succeed_if (found != 0, "could not find key");
1337         succeed_if (keyIsDir (found) == 1, "should be dir");
1338         succeed_if (strcmp (keyName(found), "user/01") == 0, "name not correct in found key");
1339
1340         found = ksNext (ks);
1341         succeed_if (found != 0, "could not get next key");
1342         succeed_if (strcmp (keyName(found), "user/01/a") == 0, "name not correct in next key");
1343
1344         found = ksLookupByName (ks, "user/02", KDB_O_NOALL);
1345         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1346         succeed_if (found != 0, "could not find key");
1347         succeed_if (keyIsDir (found) == 1, "should be dir");
1348         succeed_if (strcmp (keyName(found), "user/02") == 0, "name not correct in found key");
1349
1350         cursor = ksGetCursor (ks);
1351         found = ksLookupByName (ks, "user/01", KDB_O_NOALL);
1352         succeed_if (ksGetCursor(ks) == cursor, "cursor should not change");
1353         succeed_if (found == 0, "should not find");
1354
1355         ksRewind(ks);
1356         found = ksLookupByName (ks, "user/a", KDB_O_NOALL | KDB_O_NOCASE);
1357         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1358         succeed_if (found != 0, "could not find key");
1359         succeed_if (strcmp (keyName(found), "user/a") == 0, "name not correct in found key");
1360
1361         found = ksLookupByName (ks, "user/E", KDB_O_NOALL | KDB_O_NOCASE);
1362         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1363         succeed_if (found != 0, "could not find key");
1364         succeed_if (strcmp (keyName(found), "user/e") == 0, "name not correct in found key");
1365
1366         found = ksLookupByName (ks, "user/00", KDB_O_NOALL | KDB_O_NOCASE);
1367         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1368         succeed_if (found != 0, "could not find key");
1369         succeed_if (keyIsDir (found) == 1, "should be dir");
1370         succeed_if (strcmp (keyName(found), "user/00") == 0, "name not correct in found key");
1371
1372         found = ksLookupByName (ks, "user/01", KDB_O_NOALL | KDB_O_NOCASE);
1373         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1374         succeed_if (found != 0, "could not find key");
1375         succeed_if (keyIsDir (found) == 1, "should be dir");
1376         succeed_if (strcmp (keyName(found), "user/01") == 0, "name not correct in found key");
1377
1378         found = ksNext (ks);
1379         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1380         succeed_if (found != 0, "could not get next key");
1381         succeed_if (strcmp (keyName(found), "user/01/a") == 0, "name not correct in next key");
1382
1383         found = ksLookupByName (ks, "user/02", KDB_O_NOALL | KDB_O_NOCASE);
1384         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1385         succeed_if (found != 0, "could not find key");
1386         succeed_if (keyIsDir (found) == 1, "should be dir");
1387         succeed_if (strcmp (keyName(found), "user/02") == 0, "name not correct in found key");
1388
1389         found = ksLookupByName (ks, "user/02/F", KDB_O_NOALL | KDB_O_NOCASE);
1390         succeed_if (ksCurrent(ks) == found, "current not set correctly");
1391         succeed_if (found != 0, "could not find key");
1392         succeed_if (strcmp (keyName(found), "user/02/f") == 0, "name not correct in found key");
1393
1394         cursor = ksGetCursor (ks);
1395         found = ksLookupByName (ks, "user/a", KDB_O_NOALL | KDB_O_NOCASE);
1396         succeed_if (ksGetCursor(ks) == cursor, "cursor should stay as is if not found");
1397         succeed_if (found == 0, "should not find");
1398
1399         ksRewind(ks);
1400         found = ksLookupByName (ks, "user/a", KDB_O_NOALL | KDB_O_WITHOWNER | KDB_O_NOCASE);
1401         succeed_if (found != 0, "could not find key");
1402         succeed_if (strcmp (keyName(found), "user/a") == 0, "name not correct in found key");
1403
1404         found = ksLookupByName (ks, "user/E", KDB_O_NOALL | KDB_O_WITHOWNER | KDB_O_NOCASE);
1405         succeed_if (found != 0, "could not find key");
1406         succeed_if (strcmp (keyName(found), "user/e") == 0, "name not correct in found key");
1407
1408         found = ksLookupByName (ks, "user/00", KDB_O_NOALL | KDB_O_WITHOWNER | KDB_O_NOCASE);
1409         succeed_if (found != 0, "could not find key");
1410         succeed_if (keyIsDir (found) == 1, "should be dir");
1411         succeed_if (strcmp (keyName(found), "user/00") == 0, "name not correct in found key");
1412
1413         found = ksLookupByName (ks, "user/01", KDB_O_NOALL | KDB_O_WITHOWNER | KDB_O_NOCASE);
1414         succeed_if (found != 0, "could not find key");
1415         succeed_if (keyIsDir (found) == 1, "should be dir");
1416         succeed_if (strcmp (keyName(found), "user/01") == 0, "name not correct in found key");
1417
1418         found = ksNext (ks);
1419         succeed_if (found != 0, "could not get next key");
1420         succeed_if (strcmp (keyName(found), "user/01/a") == 0, "name not correct in next key");
1421
1422         found = ksLookupByName (ks, "user/02", KDB_O_NOALL | KDB_O_WITHOWNER | KDB_O_NOCASE);
1423         succeed_if (found != 0, "could not find key");
1424         succeed_if (keyIsDir (found) == 1, "should be dir");
1425         succeed_if (strcmp (keyName(found), "user/02") == 0, "name not correct in found key");
1426
1427         found = ksLookupByName (ks, "user:notvalid/02/F", KDB_O_NOALL | KDB_O_WITHOWNER | KDB_O_NOCASE);
1428         succeed_if (found == 0, "should not find key");
1429
1430         ksDel (ks);
1431 }
1432
1433 /*
1434 void test_ksLookupValue()
1435 {
1436         KeySet *ks = ksNew(0);
1437         Key *found;
1438         printf ("test lookups for values\n");
1439         
1440         ksAppendKey(ks, keyNew("user/1",  KEY_VALUE, "singlevalue", KEY_END));
1441         ksAppendKey(ks, keyNew("user/2",   KEY_VALUE, "myvalue", KEY_END));
1442         ksAppendKey(ks, keyNew("user/3", KEY_VALUE, "syskey",  KEY_END));
1443         
1444         ksAppendKey(ks, keyNew("user/b1", KEY_TYPE, KEY_TYPE_BINARY, KEY_SIZE, 10,
1445                 KEY_VALUE, "binary\1\2data", KEY_END));
1446         ksAppendKey(ks, keyNew("user/b2", KEY_TYPE, KEY_TYPE_BINARY, KEY_SIZE, 10,
1447                 KEY_VALUE, "sys\1bin\2", KEY_END));
1448         ksAppendKey(ks, keyNew("user/b3", KEY_TYPE, KEY_TYPE_BINARY, KEY_SIZE, 10,
1449                 KEY_VALUE, "sy\1\33\12\32skey", KEY_END));
1450         succeed_if(ksGetSize(ks) == 6, "could not append keys");
1451
1452         succeed_if (ksLookupByString (ks, "value", 0) == 0, "found part of value");
1453         succeed_if (ksLookupByString (ks, " singlevalue", 0) == 0, "prefix of value");
1454         succeed_if (ksLookupByString (ks, "/singlevalue", 0) == 0, "prefix of value");
1455         succeed_if (ksLookupByString (ks, "singlevalue ", 0) == 0, "postfix of value");
1456
1457         found = ksLookupByString(ks, "singlevalue", 0);
1458         succeed_if (found != 0, "could not find value");
1459         succeed_if (strcmp (keyName(found), "user/1") == 0, "not correct key found");
1460
1461         found = ksLookupByString(ks, "singlevalue", 0);
1462         succeed_if (found != 0, "could not find value again");
1463         succeed_if (strcmp (keyName(found), "user/1") == 0, "not correct key found");
1464         
1465         found = ksLookupByString(ks, "myvalue", 0);
1466         succeed_if (found != 0, "could not find value");
1467         succeed_if (strcmp (keyName(found), "user/2") == 0, "not correct key found");
1468
1469         found = ksLookupByString(ks, "syskey", 0);
1470         succeed_if (found != 0, "could not find value");
1471         succeed_if (strcmp (keyName(found), "user/3") == 0, "not correct key found");
1472
1473         ksRewind(ks);
1474         found = ksLookupByString(ks, "singlevalue", KDB_O_NOALL);
1475         succeed_if (found != 0, "could not find value");
1476         succeed_if (strcmp (keyName(found), "user/1") == 0, "not correct key found");
1477
1478         found = ksLookupByString(ks, "singlevalue", 0);
1479         succeed_if (found != 0, "could not find value again");
1480         succeed_if (strcmp (keyName(found), "user/1") == 0, "not correct key found");
1481
1482         ksRewind(ks);
1483         found = ksLookupByString(ks, "myvalue", KDB_O_NOALL);
1484         succeed_if (found != 0, "could not find value");
1485         succeed_if (strcmp (keyName(found), "user/2") == 0, "not correct key found");
1486
1487         found = ksLookupByString(ks, "syskey", 0);
1488         succeed_if (found != 0, "could not find value");
1489         succeed_if (strcmp (keyName(found), "user/3") == 0, "not correct key found");
1490
1491         // TODO: BUG found = ksLookupByString(ks, "singlevalue", KDB_O_NOALL);
1492         // succeed_if (found == 0, "could find value");
1493
1494         // found = ksLookupByString(ks, "singlevalue", KDB_O_NOALL);
1495         // succeed_if (found == 0, "found value again");
1496
1497         ksRewind(ks);
1498         found = ksLookupByString(ks, "myvalue", KDB_O_NOALL);
1499         succeed_if (found != 0, "could not find value");
1500         succeed_if (strcmp (keyName(found), "user/2") == 0, "not correct key found");
1501
1502         found = ksLookupByString(ks, "syskey", 0);
1503         succeed_if (found != 0, "could not find value");
1504         succeed_if (strcmp (keyName(found), "user/3") == 0, "not correct key found");
1505
1506         found = ksLookupByString(ks, "syskey", KDB_O_NOALL);
1507         succeed_if (found != 0, "could not find value");
1508         succeed_if (strcmp (keyName(found), "user/3") == 0, "not correct key found");
1509
1510         found = ksLookupByString(ks, "syskey", KDB_O_NOALL);
1511         succeed_if (found == 0, "found value");
1512
1513         ksDel(ks);
1514 }
1515 */
1516
1517 //copied out from example       
1518 void test_ksExample()
1519 {
1520         KeySet *ks=ksNew(0);
1521         Key * key;
1522
1523         ksAppendKey(ks,keyNew(0));       // an empty key
1524                 
1525         ksAppendKey(ks,keyNew("user/sw",              // the name of the key
1526                 KEY_END));                      // no more args
1527                 
1528         ksAppendKey(ks,keyNew("user/tmp/ex1",
1529                 KEY_VALUE,"some data",          // set a string value
1530                 KEY_END));                      // end of args
1531                 
1532         ksAppendKey(ks,keyNew("user/tmp/ex2",
1533                 KEY_VALUE,"some data",          // with a simple value
1534                 KEY_MODE,0777,                  // permissions
1535                 KEY_END));                      // end of args
1536                 
1537         ksAppendKey(ks,keyNew("user/tmp/ex4",
1538                 KEY_TYPE,KEY_TYPE_BINARY,       // key type
1539                 KEY_SIZE,7,                     // assume binary length 7
1540                 KEY_VALUE,"some data",          // value that will be truncated in 7 bytes
1541                 KEY_COMMENT,"value is truncated",
1542                 KEY_OWNER,"root",               // owner (not uid) is root
1543                 KEY_UID,0,                      // root uid
1544                 KEY_END));                      // end of args
1545
1546         ksAppendKey(ks,keyNew("user/tmp/ex5",
1547                 KEY_TYPE, KEY_TYPE_BINARY,      // binary value
1548                 KEY_SIZE,7,
1549                 KEY_VALUE,"some data",          // value that will be truncated in 7 bytes
1550                 KEY_COMMENT,"value is truncated",
1551                 KEY_OWNER,"root",              // owner (not uid) is root
1552                 KEY_UID,0,                      // root uid
1553                 KEY_END));                      // end of args
1554         
1555         ksRewind(ks);
1556         // end of example
1557
1558         key=ksNext(ks);
1559         succeed_if(key != NULL, "keyNew: Unable to create a new empty key");
1560         
1561         key=ksNext(ks);
1562         succeed_if(key != NULL, "keyNew: Unable to create a key with name");
1563         succeed_if(strcmp(keyName(key), "user/sw") == 0, "keyNew: Key's name setted incorrectly");
1564
1565         ksDel(ks);
1566 }
1567
1568 #define MAX_SIZE 200
1569 void test_ksCommonParentName()
1570 {
1571         char ret [MAX_SIZE+1];
1572         KeySet *ks = ksNew (10,
1573                 keyNew("system/sw/xorg/Monitors/Monitor1/vrefresh",0),
1574                 keyNew("system/sw/xorg/Monitors/Monitor1/hrefresh",0),
1575                 keyNew("system/sw/xorg/Monitors/Monitor2/vrefresh",0),
1576                 keyNew("system/sw/xorg/Monitors/Monitor2/hrefresh",0),
1577                 keyNew("system/sw/xorg/Devices/Device1/driver",0),
1578                 keyNew("system/sw/xorg/Devices/Device1/mode",0),KS_END);
1579
1580         printf ("Test common parentname\n");
1581         ksSort (ks);
1582
1583         succeed_if (ksGetCommonParentName(ks, ret, MAX_SIZE) > 0, "could not find correct parentname");
1584         succeed_if (strcmp (ret, "system/sw/xorg") == 0, "parentname not correct");
1585         ksDel (ks);
1586
1587         ks = ksNew (10,
1588                 keyNew("system",0),
1589                 keyNew("user",0),KS_END);
1590         succeed_if (ksGetCommonParentName(ks, ret, MAX_SIZE) == 0, "could find correct parentname");
1591         succeed_if (strcmp (ret, "") == 0, "parentname not empty");
1592         ksDel (ks);
1593
1594         ks = ksNew (10,
1595                 keyNew("system/some/thing",0),
1596                 keyNew("system/other/thing",0), KS_END);
1597         succeed_if (ksGetCommonParentName(ks, ret, MAX_SIZE) == 7, "could find correct parentname");
1598         succeed_if (strcmp (ret, "system") == 0, "parentname not empty");
1599         ksDel (ks);
1600
1601         ks = ksNew (10,
1602                 keyNew("system/here/in/deep/goes/ok/thing",0),
1603                 keyNew("system/here/in/deep/goes/ok/other/thing",0),
1604                 KS_END);
1605         succeed_if (ksGetCommonParentName(ks, ret, MAX_SIZE) > 0, "could find correct parentname");
1606         succeed_if (strcmp (ret, "system/here/in/deep/goes/ok") == 0, "parentname not empty");
1607         ksDel (ks);
1608
1609         ks = ksNew (10,
1610                 keyNew("system/here/in/deep/goes/ok/thing",0),
1611                 keyNew("system/here/in/deep/goes/ok/other/thing",0),
1612                 keyNew("user/unique/thing",0),KS_END);
1613         succeed_if (ksGetCommonParentName(ks, ret, MAX_SIZE) == 0, "could find correct parentname");
1614         succeed_if (strcmp (ret, "") == 0, "parentname not empty");
1615         ksDel (ks);
1616
1617         ks = ksNew (10,
1618                 keyNew("user/unique/thing",0),KS_END);
1619         succeed_if (ksGetCommonParentName(ks, ret, MAX_SIZE) > 0, "could find correct parentname");
1620         succeed_if (strcmp (ret, "user/unique/thing") == 0, "parentname not empty");
1621         ksDel (ks);
1622 }
1623
1624 void test_ksAppend()
1625 {
1626         int i;
1627
1628         printf ("Test appending keys\n");
1629
1630         KeySet *returned =
1631 #include "keyset.c"
1632         KeySet *testDirectBelow =
1633 #include "dbelow.c"
1634         KeySet *testReturned =
1635 #include "others.c"
1636         Key *parentKey[2];
1637         parentKey[0] = keyNew ("user/test/keyset", KEY_END);
1638         parentKey[1] = keyNew ("user/test/keyset/dir1", KEY_END);
1639         Key *current;
1640
1641         /* A real world example out in kdb.c */
1642         for (i=0; i<2; i++)
1643         {
1644                 KeySet *tmp = ksNew(ksGetSize(returned), KS_END);
1645                 KeySet *keys = ksNew (0);
1646
1647                 /* add all keys direct below parentKey */
1648                 ksRewind (returned);
1649                 while ((current = ksPop(returned)) != 0)
1650                 {
1651                         if (keyIsDirectBelow(parentKey[i], current))
1652                         {
1653                                 ksAppendKey(keys, current);
1654                         } else {
1655                                 ksAppendKey(tmp, current);
1656                         }
1657                 }
1658                 ksAppend (returned, tmp);
1659
1660                 /*
1661                 ksOutput (tmp, stdout, KDB_O_HEADER);
1662                 ksOutput (returned, stdout, KDB_O_HEADER);
1663                 printf (" ----- keys -------\n");
1664                 ksOutput (keys, stdout, KDB_O_HEADER);
1665                 */
1666
1667                 if (!i)
1668                 {
1669                         compare_keyset (returned, testReturned, 0, 0);
1670                         compare_keyset (keys, testDirectBelow, 0, 0);
1671
1672                         succeed_if (ksGetSize (tmp) == 84, "size not correct");
1673                         succeed_if (ksGetSize (returned) == 84, "size not correct");
1674                         succeed_if (ksGetSize (keys) == 18, "size not correct");
1675
1676                         succeed_if (ksGetAlloc (tmp) == 102, "alloc not correct");
1677                         succeed_if (ksGetAlloc (returned) == 128, "alloc not correct");
1678                         succeed_if (ksGetAlloc (keys) == 32, "alloc not correct");
1679                 }
1680
1681                 ksAppend (returned, keys); /* add the keys back */
1682
1683                 ksDel (tmp);
1684                 ksDel (keys);
1685
1686         }
1687
1688         keyDel (parentKey[0]);
1689         keyDel (parentKey[1]);
1690
1691         ksDel (testReturned);
1692         ksDel (testDirectBelow);
1693         ksDel (returned);
1694 }
1695
1696
1697
1698 /**A functional mode to keys.
1699  *
1700  * Instead of writing your own loop you can write
1701  * a function working with a key and pass it to
1702  * this method.
1703  *
1704  * The function will be executed for all keys in
1705  * the keyset.
1706  *
1707  * @param ks the keyset to work with
1708  * @param func the function to execute on every key of the keyset
1709  * @return the sum of all return values
1710  * @return -1 if any function returned -1, the execution will stop there, but
1711  *      ksCurrent() will tell you where it stopped.
1712  * @see ksFilter()
1713  */
1714 int ksForEach (KeySet *ks, int (*func) (Key *k))
1715 {
1716         int rc = 0;
1717         int ret = 0;
1718         Key *current;
1719
1720         cursor_t cursor = ksGetCursor (ks);
1721         ksRewind (ks);
1722         while ((current = ksNext (ks)) != 0)
1723         {
1724                 rc = func (current);
1725                 if (rc == -1) return -1;
1726                 ret += rc;
1727         }
1728         ksSetCursor(ks, cursor);
1729         return ret;
1730 }
1731
1732
1733 /**Filter a keyset.
1734  *
1735  * filter is executed for every key in the keyset result. When it returns 0,
1736  * the key will be dropped, when it returns 1 it will be ksAppendKey()ed to result,
1737  * when it returns -1 the processing will be stopped. You can use ksCurrent()
1738  * on input to see where the problem was. Because of this input is not const,
1739  * apart from ksCurrent() the input will not be changed. The keys that have
1740  * been in result before will stay untouched.
1741  *
1742  * @param result is the keyset where keys are added.
1743  * @param input is the keyset the filter works on.
1744  * @param filter is the function to execute on every key of the keyset to decide if
1745  *      it should be ksAppendKey()ed to the result.
1746  * @return the number of keys added on success
1747  * @return 0 when nothing was done
1748  * @return -1 when filter returned an error (-1), ksCurrent() of input will
1749  *      be the problematic key.
1750  * @see ksForEach()
1751  **/
1752 int ksFilter (KeySet *result, KeySet *input, int (*filter) (Key *k))
1753 {
1754         int rc = 0;
1755         int ret = 0;
1756         Key *current;
1757
1758         cursor_t cursor = ksGetCursor (input);
1759         ksRewind (input);
1760         while ((current = ksNext (input)) != 0)
1761         {
1762                 rc = filter (current);
1763                 if (rc == -1) return -1;
1764                 else if (rc != 0)
1765                 {
1766                         ++ ret;
1767                         ksAppendKey(result, keyDup (current));
1768                 }
1769         }
1770         ksSetCursor(input, cursor);
1771         return ret;
1772 }
1773
1774
1775 Key *global_a;
1776
1777 int add_string (Key *check) { return keySetString (check, "string"); }
1778 int add_comment (Key *check) { return keySetComment (check, "comment"); }
1779 int has_a (Key *check) { return keyName(check)[5]=='a'; }
1780 int below_a (Key *check) { return keyIsBelow(global_a, check); }
1781 int direct_below_a (Key *check) { return keyIsDirectBelow(global_a, check); }
1782
1783 int sum_helper (Key *check) { return atoi(keyValue(check)); }
1784 int below_30 (Key *check) { return atoi(keyValue(check))<30; }
1785 int find_80 (Key *check) { int n=atoi(keyValue(check)); return n>70?-1:1; }
1786
1787 void test_ksFunctional()
1788 {
1789         Key *found;
1790         Key *current;
1791         KeySet *out;
1792         KeySet *ks = ksNew (64,
1793                 keyNew ("user/a/1", KEY_END),
1794                 keyNew ("user/a/2", KEY_END),
1795                 keyNew ("user/a/b/1", KEY_END),
1796                 keyNew ("user/a/b/2", KEY_END),
1797                 keyNew ("user/ab/2", KEY_END),
1798                 keyNew ("user/b/1", KEY_END),
1799                 keyNew ("user/b/2", KEY_END),
1800                 KS_END);
1801         global_a = keyNew ("user/a", KEY_END);
1802
1803         printf ("Test functional style\n");
1804
1805         ksForEach (ks, add_string);
1806         ksForEach (ks, add_comment);
1807
1808         ksRewind (ks);
1809         while ((current = ksNext(ks)) != 0)
1810         {
1811                 succeed_if (strcmp (keyValue (current), "string") == 0, "for each did not add string");
1812                 succeed_if (strcmp (keyComment (current), "comment") == 0, "for each did not add comment");
1813         }
1814
1815         out = ksNew (0);
1816         succeed_if (ksGetSize(ks) == 7, "initial size wrong");
1817         succeed_if (ksGetSize(out) == 0, "initial size wrong");
1818         ksFilter (out, ks, has_a);
1819         succeed_if (ksGetSize(out) == 5, "has_a cutted more then the user/b");
1820         ksDel (out);
1821
1822         out = ksNew (0);
1823         ksFilter (out, ks, below_a);
1824         succeed_if (ksGetSize(out) == 4, "below_a cutted more then the user/ab/2");
1825         ksDel (out);
1826
1827         out = ksNew (0);
1828         ksFilter (out, ks, direct_below_a);
1829         succeed_if (ksGetSize(out) == 2, "direct_below_a cutted more then the user/a/b/*");
1830         ksDel (out);
1831
1832         ksDel (ks);
1833         keyDel (global_a); global_a = 0;
1834
1835         KeySet *values = ksNew (64,
1836                 keyNew ("user/a", KEY_VALUE, "40", KEY_END),
1837                 keyNew ("user/b", KEY_VALUE, "20", KEY_END),
1838                 keyNew ("user/c", KEY_VALUE, "80", KEY_END),
1839                 keyNew ("user/d", KEY_VALUE, "24", KEY_END),
1840                 keyNew ("user/e", KEY_VALUE, "32", KEY_END),
1841                 keyNew ("user/f", KEY_VALUE, "12", KEY_END),
1842                 keyNew ("user/g", KEY_VALUE, "43", KEY_END),
1843                 KS_END);
1844
1845         succeed_if (ksForEach (values, sum_helper) == 251, "could not sum up");
1846
1847         KeySet *values_below_30 = ksNew (0);
1848         ksFilter (values_below_30, values, below_30);
1849         succeed_if (ksGetSize (values_below_30) == 3, "could not filter out everything above 30");
1850         succeed_if (ksForEach (values_below_30, sum_helper) == 56, "could not sum up");
1851
1852         succeed_if (ksForEach (values, find_80) == -1, "did not find 80");
1853         found = ksCurrent (values);
1854         succeed_if (ksLookupByName (values, "user/c", 0) == found, "did not find 80");
1855         /*succeed_if (ksLookupByString (values, "80", 0) == found, "lookup by value did not find 80");*/
1856         ksDel (values);
1857         ksDel (values_below_30);
1858 }
1859
1860 void test_ksLookupPop()
1861 {
1862         printf ("Test ksLookup with KDB_O_POP\n");
1863
1864         Key * found;
1865         Key *a, *b, *c;
1866         KeySet *small =ksNew (5,
1867                         a=keyNew ("user/a", KEY_END),
1868                         b=keyNew ("user/b", KEY_END),
1869                         c=keyNew ("user/c", KEY_END), KS_END);
1870
1871         ksRewind (small);
1872         ksNext(small);
1873         succeed_if (ksCurrent(small) == a, "current not set correctly");
1874
1875         succeed_if (ksGetSize(small) == 3, "could not append all keys");
1876         found = ksLookupByName (small, "user/a", KDB_O_POP);
1877         succeed_if (found == a, "not correct key");
1878         succeed_if (strcmp (keyName(found), "user/a") == 0, "name not correct in found key");
1879         succeed_if (ksCurrent(small) == 0, "current not set correctly");
1880         succeed_if (keyDel (found) == 0, "could not del popped key");
1881
1882         ksNext(small);
1883         ksNext(small);
1884         succeed_if (ksCurrent(small) == c, "current not set correctly");
1885
1886         succeed_if (ksGetSize(small) == 2, "could not append all keys");
1887         found = ksLookupByName (small, "user/b", KDB_O_POP);
1888         succeed_if (found == b, "not correct key");
1889         succeed_if (strcmp (keyName(found), "user/b") == 0, "name not correct in found key");
1890         succeed_if (ksCurrent(small) == c, "current not set correctly");
1891         succeed_if (keyDel (found) == 0, "could not del popped key");
1892
1893         succeed_if (ksGetSize(small) == 1, "could not append all keys");
1894         found = ksLookupByName (small, "user/b", KDB_O_POP);
1895         succeed_if (found == 0, "found something, but shouldnt");
1896         succeed_if (ksCurrent(small) == c, "current not set correctly");
1897
1898         succeed_if (ksGetSize(small) == 1, "could not append all keys");
1899         found = ksLookupByName (small, "user/c", KDB_O_POP);
1900         succeed_if (found == c, "not correct key");
1901         succeed_if (strcmp (keyName(found), "user/c") == 0, "name not correct in found key");
1902         succeed_if (ksCurrent(small) == 0, "current not set correctly");
1903         succeed_if (keyDel (found) == 0, "could not del popped key");
1904
1905         succeed_if (ksGetSize(small) == 0, "could not append all keys");
1906         found = ksLookupByName (small, "user/d", KDB_O_POP);
1907         succeed_if (found == 0, "found something, but shouldnt");
1908         succeed_if (ksCurrent(small) == 0, "current not set correctly");
1909
1910         ksDel (small);
1911
1912         KeySet *ks= ksNew(0);
1913
1914         succeed_if (ksNeedSort (ks) == 1, "sort state not correct");
1915
1916         ksAppendKey(ks, keyNew("user/domain/key",  KEY_VALUE, "domainvalue",
1917                 KEY_OWNER, "markus", KEY_END));
1918         ksAppendKey(ks, keyNew("user/single/key",  KEY_VALUE, "singlevalue", KEY_END));
1919         ksAppendKey(ks, keyNew("user/named/key",   KEY_VALUE, "myvalue", KEY_END));
1920         ksAppendKey(ks, keyNew("system/named/key", KEY_VALUE, "syskey",  KEY_END));
1921         ksAppendKey(ks, keyNew("system/sysonly/key", KEY_VALUE, "sysonlykey",  KEY_END));
1922         ksAppendKey(ks, keyNew("user/named/bin", KEY_TYPE, KEY_TYPE_BINARY, KEY_SIZE, 10,
1923                 KEY_VALUE, "binary\1\2data", KEY_END));
1924         ksAppendKey(ks, keyNew("system/named/bin", KEY_TYPE, KEY_TYPE_BINARY, KEY_SIZE, 10,
1925                 KEY_VALUE, "sys\1bin\2", KEY_END));
1926         ksAppendKey(ks, keyNew("system/named/key", KEY_TYPE, KEY_TYPE_BINARY, KEY_SIZE, 10,
1927                 KEY_VALUE, "syskey", KEY_END));
1928         succeed_if (ksGetSize(ks) == 8, "could not append all keys");
1929
1930         succeed_if (ksNeedSort (ks) == 1, "sort state not correct");
1931
1932         // a positive testcase
1933         found = ksLookupByName (ks, "user/named/key", KDB_O_POP);
1934         succeed_if (ksGetSize(ks) == 7, "did not pop key");
1935         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
1936         succeed_if (ksNeedSort (ks) == 0, "sort state not correct");
1937         ksAppendKey(ks, keyNew("user/single/key",  KEY_VALUE, "singlevalue", KEY_END));
1938         succeed_if (ksGetSize(ks) == 8, "did not pop key");
1939         succeed_if (ksNeedSort (ks) == 1, "sort state not correct");
1940         succeed_if (found != 0, "did not found correct name");
1941         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
1942         succeed_if (strcmp (keyName(found), "user/named/key") == 0, "name not correct in found key");
1943         succeed_if (strcmp (keyValue(found), "myvalue") == 0, "not correct value in found key");
1944         succeed_if (keyDel (found) == 0, "could not del popped key");
1945
1946         // here you can't find the keys
1947         succeed_if (ksLookupByName (ks, "named/key", KDB_O_POP) == 0, "not valid keyname");
1948         succeed_if (ksNeedSort (ks) == 0, "sort state not correct");
1949         succeed_if (ksLookupByName (ks, "u/named/key", KDB_O_POP) == 0, "not valid keyname");
1950         succeed_if (ksLookupByName (ks, "usea/named/key", KDB_O_POP) == 0, "not valid keyname");
1951         succeed_if (ksLookupByName (ks, " user/named/key", KDB_O_POP) == 0, "found key with bad prefix");
1952
1953         succeed_if (ksLookupByName (ks, "user/named/Key", KDB_O_POP) == 0, "found wrong case key");
1954         succeed_if (ksLookupByName (ks, "User/Named/key", KDB_O_POP) == 0, "found wrong case key");
1955         succeed_if (ksLookupByName (ks, "User/named/key", KDB_O_POP) == 0, "found wrong case key");
1956         succeed_if (ksLookupByName (ks, "user/NAMED/key", KDB_O_POP) == 0, "found wrong case key");
1957         succeed_if (ksLookupByName (ks, "USER/NAMED/KEY", KDB_O_POP) == 0, "found wrong case key");
1958         
1959         succeed_if (ksLookupByName (ks, "user/named/keys", KDB_O_POP) == 0, "wrong postfix");
1960         succeed_if (ksLookupByName (ks, "user/named/key_", KDB_O_POP) == 0, "wrong postfix");
1961
1962         succeed_if (ksLookupByName (ks, "user/named/k/ey", KDB_O_POP) == 0, "seperation that should be");
1963         succeed_if (ksLookupByName (ks, "user/na/med/key", KDB_O_POP) == 0, "seperation that should be");
1964
1965         succeed_if (ksLookupByName (ks, "system/domain/key", KDB_O_POP) == 0, "found key in wrong domain");
1966         
1967         //now try to find them, and compare value
1968         found = ksLookupByName (ks, "user/domain/key", KDB_O_POP);
1969         succeed_if (ksGetSize(ks) == 7, "did not pop key");
1970         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
1971         succeed_if (found != 0, "did not found correct name");
1972         succeed_if (strcmp (keyName(found), "user/domain/key") == 0, "name not correct in found key");
1973         succeed_if (strcmp (keyValue(found), "domainvalue") == 0, "not correct value in found key");
1974         succeed_if (keyDel (found) == 0, "could not del popped key");
1975
1976         found = ksLookupByName (ks, "user/single/key", KDB_O_POP);
1977         succeed_if (ksGetSize(ks) == 6, "did not pop key");
1978         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
1979         succeed_if (found != 0, "did not found correct name");
1980         succeed_if (strcmp (keyName(found), "user/single/key") == 0, "name not correct in found key");
1981         succeed_if (strcmp (keyValue(found), "singlevalue") == 0, "not correct value in found key");
1982         succeed_if (keyDel (found) == 0, "could not del popped key");
1983
1984         found = ksLookupByName (ks, "system/named/key", KDB_O_POP);
1985         succeed_if (ksGetSize(ks) == 5, "did not pop key");
1986         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
1987         succeed_if (found != 0, "did not found correct name");
1988         succeed_if (strcmp (keyName(found), "system/named/key") == 0, "name not correct in found key");
1989         succeed_if (strcmp (keyValue(found), "syskey") == 0, "not correct value in found key");
1990         succeed_if (keyDel (found) == 0, "could not del popped key");
1991
1992         found = ksLookupByName (ks, "user/named/bin", KDB_O_POP);
1993         succeed_if (ksGetSize(ks) == 4, "pop key");
1994         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
1995         succeed_if (found != 0, "did not found correct name");
1996         succeed_if (strcmp (keyName(found), "user/named/bin") == 0, "name not correct in found key");
1997         succeed_if (strncmp (keyValue(found), "binary\1\2data",10) == 0, "not correct value in found key");
1998         succeed_if (keyDel (found) == 0, "could not del popped key");
1999
2000         found = ksLookupByName (ks, "user/named/key", KDB_O_POP);
2001         succeed_if (ksGetSize(ks) == 4, "did not pop key");
2002         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
2003         succeed_if (found == 0, "could find same key again");
2004
2005         printf ("Test nocase lookup functions\n");
2006         found = ksLookupByName (ks, "user/named/key", KDB_O_NOCASE | KDB_O_POP);
2007         succeed_if (ksGetSize(ks) == 4, "key");
2008         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
2009         succeed_if (found == 0, "could find same key again, nocase");
2010
2011         ksAppendKey(ks, keyNew("user/named/key",   KEY_VALUE, "myvalue", KEY_END));
2012         found = ksLookupByName (ks, "user/NameD/KeY", KDB_O_NOCASE | KDB_O_POP);
2013         succeed_if (ksGetSize(ks) == 4, "did not pop key");
2014         succeed_if (found != 0, "could not find same key again, nocase used");
2015         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
2016         succeed_if (strcmp (keyName(found), "user/named/key") == 0, "name not correct in found key");
2017         succeed_if (strcmp (keyValue(found), "myvalue") == 0, "not correct value in found key");
2018         succeed_if (keyDel (found) == 0, "could not del popped key");
2019
2020         ksAppendKey(ks, keyNew("user/named/key",   KEY_VALUE, "myvalue", KEY_END));
2021         found = ksLookupByName (ks, "user/NameD/KEY", KDB_O_NOCASE | KDB_O_POP);
2022         succeed_if (ksGetSize(ks) == 4, "did not pop key");
2023         succeed_if (found != 0, "could not find same key again, nocase used");
2024         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
2025         succeed_if (strcmp (keyName(found), "user/named/key") == 0, "name not correct in found key");
2026         succeed_if (strcmp (keyValue(found), "myvalue") == 0, "not correct value in found key");
2027         succeed_if (keyDel (found) == 0, "could not del popped key");
2028
2029         ksAppendKey(ks, keyNew("user/named/key",   KEY_VALUE, "myvalue", KEY_END));
2030         printf ("Test cascading lookup functions\n");
2031         found = ksLookupByName (ks, "/named/key", KDB_O_POP);
2032         succeed_if (ksGetSize(ks) == 4, "did not pop key");
2033         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
2034         succeed_if (found != 0, "cascading search failed");
2035         succeed_if (strcmp (keyName(found), "user/named/key") == 0, "name not correct in found key");
2036         succeed_if (strcmp (keyValue(found), "myvalue") == 0, "not correct value in found key");
2037         succeed_if (keyDel (found) == 0, "could not del popped key");
2038
2039         found = ksLookupByName (ks, "/single/Key", KDB_O_NOCASE | KDB_O_POP);
2040         succeed_if (ksGetSize(ks) == 3, "did not pop key");
2041         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
2042         succeed_if (found != 0, "could not find same key again, nocase used");
2043         succeed_if (strcmp (keyName(found), "user/single/key") == 0, "name not correct in found key");
2044         succeed_if (strcmp (keyValue(found), "singlevalue") == 0, "not correct value in found key");
2045         succeed_if (keyDel (found) == 0, "could not del popped key");
2046
2047         found = ksLookupByName (ks, "/sysonly/key", KDB_O_POP);
2048         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
2049         succeed_if (ksGetSize(ks) == 2, "did not pop key");
2050         succeed_if (found != 0, "could not find same key again, nocase used");
2051         succeed_if (strcmp (keyName(found), "system/sysonly/key") == 0, "name not correct in found key");
2052         succeed_if (strcmp (keyValue(found), "sysonlykey") == 0, "not correct value in found key");
2053         succeed_if (keyDel (found) == 0, "could not del popped key");
2054
2055         succeed_if (ksLookupByName (ks, "/named/", KDB_O_POP) == 0, "found part of key with cascading");
2056         succeed_if (ksLookupByName (ks, "/named/keyd", KDB_O_POP) == 0, "found part of key with cascading, bad postfix");
2057
2058         ksAppendKey(ks, keyNew("user:markus/domain/key",   KEY_VALUE, "domainvalue", KEY_END));
2059         printf ("Test domain lookup functions\n");
2060         found = ksLookupByName (ks, "user:markus/domain/key", KDB_O_WITHOWNER | KDB_O_POP);
2061         succeed_if (ksCurrent(ks) == 0, "current not set correctly");
2062         succeed_if (ksGetSize(ks) == 2, "did not pop key");
2063         succeed_if (found != 0, "could not find domain key");
2064         succeed_if (strcmp (keyValue(found), "domainvalue") == 0, "not correct value in domain key");
2065         succeed_if (ksLookupByName (ks, "user:hugo/domain/key", KDB_O_WITHOWNER | KDB_O_POP) == 0, "found key in wrong domain");
2066         succeed_if (ksLookupByName (ks, "user:y/domain/key", KDB_O_WITHOWNER | KDB_O_POP) == 0, "found key in wrong domain");
2067         succeed_if (ksLookupByName (ks, "user:markuss/domain/key", KDB_O_WITHOWNER | KDB_O_POP) == 0, "found key in wrong domain");
2068         succeed_if (ksLookupByName (ks, "user:marku/domain/key", KDB_O_WITHOWNER | KDB_O_POP) == 0, "found key in wrong domain");
2069         succeed_if (ksGetSize(ks) == 2, "did not pop key");
2070         succeed_if (keyDel (found) == 0, "could not del popped key");
2071
2072         ksDel(ks);
2073         
2074 }
2075
2076 int main()
2077 {
2078         printf("KEYSET       TESTS\n");
2079         printf("==================\n\n");
2080
2081         init ();
2082
2083         test_ksNew();
2084         test_ksEmpty();
2085         test_ksReference();
2086         test_ksDup();
2087         test_ksCopy();
2088         test_ksResize();
2089         test_ksIterate();
2090         test_ksCursor();
2091         test_ksSort();
2092         test_ksLookup();
2093         test_ksLookupByName();
2094         test_ksLookupName();
2095         test_ksLookupNameRemove();
2096         test_ksLookupNameAll();
2097         /*test_ksLookupValue();*/
2098         test_ksExample();
2099         test_ksCommonParentName();
2100         test_ksAppend();
2101         test_ksFunctional();
2102         test_ksLookupPop();
2103
2104         printf("\ntest_ks RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
2105
2106         return nbError;
2107 }
2108