forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestabi_ks.c
3019 lines (2507 loc) · 110 KB
/
testabi_ks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <kdbprivate.h> // for ksBelow
#include <tests.h>
#define NUMBER_OF_NAMESPACES 5
char * namespaces[] = { "spec:/", "proc:/", "dir:/", "user:/", "system:/", 0 };
static void test_ksNew (void)
{
KeySet * ks = 0;
KeySet * keys = ksNew (15, KS_END);
KeySet * config;
printf ("Test ks creation\n");
exit_if_fail ((ks = ksNew (0, KS_END)) != 0, "could not create new keyset");
succeed_if (ksAppendKey (ks, keyNew ("user:/a", KEY_END)) == 1, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/b", KEY_END)) == 2, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/c", KEY_END)) == 3, "could not append a key");
succeed_if (ksGetSize (ks) == 3, "size not correct after 3 keys");
KeySet * ks2 = ksNew (0, KS_END);
ksCopy (ks2, ks);
compare_keyset (ks, ks2);
succeed_if (ksAppendKey (ks, keyNew ("user:/d", KEY_END)) == 4, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/e", KEY_END)) == 5, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/f", KEY_END)) == 6, "could not append a key");
succeed_if (ksGetSize (ks) == 6, "could not append 3 more keys");
ksCopy (ks2, ks);
compare_keyset (ks, ks2);
ksClear (ks2); // useless, just test for double free
ksCopy (ks2, ks);
compare_keyset (ks, ks2);
succeed_if (ksDel (ks) == 0, "could not delete keyset");
succeed_if (ksGetSize (keys) == 0, "could not append 3 more keys");
// succeed_if(ksGetAlloc(keys) == 15, "allocation size wrong");
succeed_if (ksDel (keys) == 0, "could not delete keyset");
config = ksNew (100, keyNew ("user:/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key3", KEY_VALUE, "value3", KEY_END), KS_END);
succeed_if (ksGetSize (config) == 3, "could not append 3 keys in keyNew");
// this behaviour might change, do not build on it,
// and there is no compatible way to get the alloc info
// succeed_if(ksGetAlloc(config) == 100, "allocation size wrong");
keyDel (ksPop (config));
// succeed_if(ksGetAlloc(config) == 49, "allocation size wrong");
keyDel (ksPop (config));
// succeed_if(ksGetAlloc(config) == 24, "allocation size wrong");
keyDel (ksPop (config));
// succeed_if(ksGetAlloc(config) == 15, "allocation size wrong");
succeed_if (ksDel (config) == 0, "could not delete keyset");
config = ksNew (10, keyNew ("user:/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key3", KEY_VALUE, "value1", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key4", KEY_VALUE, "value3", KEY_END), KS_END);
succeed_if (ksGetSize (config) == 4, "could not append 5 keys in keyNew");
// succeed_if(ksGetAlloc(config) == 15, "allocation size wrong");
ksAppendKey (config, keyNew ("user:/sw/app/fixedConfiguration/key6", KEY_VALUE, "value4", KEY_END));
ksClear (ks2);
ksCopy (ks2, config);
compare_keyset (config, ks2);
succeed_if (ksDel (config) == 0, "could not delete keyset");
succeed_if (ksDel (ks2) == 0, "could not delete keyset");
KeySet * ks_c = ksNew (5, keyNew ("user:/valid/key1", KEY_END), keyNew ("user:/valid/key2", KEY_END),
keyNew ("system:/valid/key1", KEY_END), keyNew ("system:/valid/key2", KEY_END), KS_END);
succeed_if (ksCurrent (ks_c) == 0, "should be rewinded");
ksDel (ks_c);
succeed_if (ksDel (0) == -1, "No error on NULL pointer");
}
static void test_ksEmpty (void)
{
printf ("Test empty keysets\n");
KeySet * ks;
KeySet * ks2;
Key * current;
ks = ksNew (0, KS_END);
succeed_if (ksGetSize (ks) == 0, "size not correct");
succeed_if (ksPop (ks) == 0, "pop empty keyset");
succeed_if (ksGetSize (ks) == 0, "size not correct");
ksDel (ks);
ks = ksNew (1, current = keyNew ("user:/test", KEY_END), KS_END);
succeed_if (ksGetSize (ks) == 1, "size not correct");
succeed_if (ksPop (ks) == current, "pop empty keyset");
succeed_if (ksGetSize (ks) == 0, "size not correct");
succeed_if (ksPop (ks) == 0, "pop empty keyset");
succeed_if (ksGetSize (ks) == 0, "size not correct");
keyDel (current);
ksDel (ks);
ks = ksNew (0, KS_END);
ks2 = ksNew (0, KS_END);
succeed_if (ksAppend (ks, ks2) == 0, "could not append empty keyset");
succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
succeed_if (ksPop (ks) == 0, "could not pop empty keyset");
succeed_if (ksPop (ks2) == 0, "could not pop empty keyset2");
ksDel (ks);
ksDel (ks2);
ks = ksNew (1, current = keyNew ("user:/test", KEY_END), KS_END);
ks2 = ksNew (0, KS_END);
succeed_if (ksGetSize (ks) == 1, "empty keyset does not have correct size");
succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
succeed_if (ksAppend (ks, ks2) == 1, "could not append empty keyset");
succeed_if (ksGetSize (ks) == 1, "empty keyset does not have correct size");
succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
succeed_if (ksPop (ks) == current, "could not pop keyset");
succeed_if (ksPop (ks2) == 0, "could not pop empty keyset2");
succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
succeed_if (ksAppend (ks, ks2) == 0, "could not append empty keyset");
succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
keyDel (current);
ksDel (ks);
ksDel (ks2);
ks = ksNew (0, KS_END);
ks2 = ksNew (1, current = keyNew ("user:/test", KEY_END), KS_END);
succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
succeed_if (ksGetSize (ks2) == 1, "empty keyset does not have correct size");
succeed_if (ksAppend (ks, ks2) == 1, "could not append empty keyset");
succeed_if (ksGetSize (ks) == 1, "empty keyset does not have correct size");
succeed_if (ksGetSize (ks2) == 1, "empty keyset does not have correct size");
succeed_if (ksPop (ks) == current, "could not pop keyset");
succeed_if (ksPop (ks2) == current, "could not pop empty keyset2");
succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
succeed_if (ksAppend (ks, ks2) == 0, "could not append empty keyset");
succeed_if (ksGetSize (ks) == 0, "empty keyset does not have correct size");
succeed_if (ksGetSize (ks2) == 0, "empty keyset does not have correct size");
keyDel (current); // only one keyDel, because ksPop decrements counter
ksDel (ks);
ksDel (ks2);
}
#define NR_KEYSETS 10
static void test_ksReference (void)
{
KeySet * ks = 0;
KeySet * ks1;
Key *k1, *k2;
KeySet * kss[NR_KEYSETS];
int i;
printf ("Test reference of key\n");
ks = ksNew (0, KS_END);
k1 = keyNew ("user:/aname", KEY_END);
succeed_if (ksAtCursor (0, 0) == 0, "Not NULL on NULL KeySet");
succeed_if (ksAtCursor (0, ksGetSize (0) - 1) == 0, "Not NULL on NULL KeySet");
succeed_if (ksAtCursor (ks, 0) == 0, "Not NULL on empty KeySet");
succeed_if (ksAtCursor (ks, ksGetSize (ks) - 1) == 0, "Not NULL on empty KeySet");
succeed_if (keyGetRef (k1) == 0, "reference counter of new key");
succeed_if (ksAppendKey (ks, k1) == 1, "size should be one");
succeed_if (keyGetRef (k1) == 1, "reference counter of inserted key");
succeed_if (ksGetSize (ks) == 1, "wrong size, should stay after inserting duplication");
succeed_if (ksAtCursor (ks, 0) == k1, "head wrong");
succeed_if (ksAtCursor (ks, ksGetSize (ks) - 1) == k1, "tail wrong");
k2 = keyDup (k1, KEY_CP_ALL);
keySetString (k2, "newvalue");
succeed_if (keyGetRef (k2) == 0, "reference counter not resetted");
succeed_if (ksAppendKey (ks, k2) == 1, "size should stay at 1");
// k1 should be freed by now and instead k2 in the keyset
succeed_if (ksGetSize (ks) == 1, "wrong size, should stay after inserting duplication");
succeed_if_same_string (keyValue (ksAtCursor (ks, 0)), "newvalue");
ksDel (ks);
ks = ksNew (5, keyNew ("user:/key", KEY_END), keyNew ("system:/key", KEY_END), KS_END);
k1 = ksLookupByName (ks, "system:/key", 0);
k2 = ksLookupByName (ks, "user:/key", 0);
succeed_if (keyGetRef (k1) == 1, "reference counter of new inserted key");
succeed_if (keyGetRef (k2) == 1, "reference counter of new inserted key");
succeed_if (ksAtCursor (ks, 0) == k2, "head wrong");
succeed_if (ksAtCursor (ks, ksGetSize (ks) - 1) == k1, "tail wrong");
ksDel (ks);
ks = ksNew (5, keyNew ("user:/key", KEY_END), keyNew ("system:/key", KEY_END), KS_END);
k1 = ksLookupByName (ks, "system:/key", 0);
k2 = ksLookupByName (ks, "user:/key", 0);
succeed_if (keyGetRef (k1) == 1, "reference counter of new inserted key");
succeed_if (keyGetRef (k2) == 1, "reference counter of new inserted key");
ks1 = ksDup (ks);
succeed_if (ksAtCursor (ks1, 0) == k2, "head in dup wrong");
succeed_if (ksAtCursor (ks1, ksGetSize (ks1) - 1) == k1, "tail in dup wrong");
// COW - key references stay the same
succeed_if (keyGetRef (k1) == 1, "reference counter after duplication of keyset");
succeed_if (keyGetRef (k2) == 1, "reference counter after ksdup");
k1 = ksPop (ks);
succeed_if (keyGetRef (k1) == 1, "reference counter after pop");
keyDel (k1);
succeed_if (keyGetRef (k1) == 1, "reference counter");
succeed_if (keyGetRef (k2) == 2, "reference counter should not be influenced");
ksDel (ks);
succeed_if (keyGetRef (k1) == 1, "reference counter, delete from first keyset");
succeed_if (keyGetRef (k2) == 1, "reference counter, delete from first keyset");
ksDel (ks1); // k1 and k2 deleted
ks1 = ksNew (0, KS_END);
k1 = keyNew ("user:/k1", KEY_END);
succeed_if (keyGetRef (k1) == 0, "reference counter of new inserted key");
succeed_if (ksAppendKey (ks1, k1) == 1, "appending did not work");
succeed_if (ksGetSize (ks1) == 1, "size did not match");
succeed_if (keyGetRef (k1) == 1, "reference counter of new inserted key");
succeed_if (ksAppendKey (ks1, k1) == 1, "appending the very same key");
succeed_if (ksGetSize (ks1) == 1, "size did not match");
succeed_if (keyGetRef (k1) == 1, "reference counter of new inserted key should stay the same");
k1 = ksPop (ks1);
succeed_if (keyGetRef (k1) == 0, "reference counter of new inserted key");
succeed_if (keyDel (k1) == 0, "keyDel did not work");
succeed_if (ksDel (ks1) == 0, "could not delete key");
kss[0] = ksNew (5, k1 = keyNew ("user:/key", KEY_END), k2 = keyNew ("system:/key", KEY_END), KS_END);
for (i = 1; i < NR_KEYSETS; i++)
{
succeed_if (keyGetRef (k1) == i, "reference counter");
succeed_if (keyGetRef (k2) == 1, "reference counter");
kss[i] = ksDup (kss[i - 1]);
// COW - key references stay the same after a ksDup
succeed_if (keyGetRef (k2) == 1, "reference counter");
succeed_if_same_string (keyName (ksPop (kss[i - 1])), "system:/key");
succeed_if (keyGetRef (k2) == 1, "reference counter");
succeed_if (keyDel (k2) == 1, "delete key");
succeed_if (keyGetRef (k2) == 1, "reference counter");
}
succeed_if (keyGetRef (k1) == NR_KEYSETS, "reference counter");
succeed_if (keyGetRef (k2) == 1, "reference counter");
for (i = 0; i < NR_KEYSETS; i++)
{
succeed_if (keyGetRef (k1) == NR_KEYSETS - i, "reference counter");
ksDel (kss[i]);
}
}
static void test_ksDup (void)
{
KeySet * ks = 0;
KeySet * other = 0;
printf ("Test ks duplication\n");
succeed_if (ksDup (0) == 0, "No error on NULL pointer");
exit_if_fail ((ks = ksNew (0, KS_END)) != 0, "could not create new keyset");
other = ksDup (ks);
succeed_if (other, "other creation failed");
succeed_if (ksGetSize (ks) == 0, "ks has keys");
succeed_if (ksGetSize (other) == 0, "other has keys");
ksDel (other);
ksDel (ks);
exit_if_fail ((ks = ksNew (1, keyNew ("user:/anything", KEY_END), KS_END)) != 0, "could not create new keyset");
other = ksDup (ks);
succeed_if (other, "other creation failed");
succeed_if (ksGetSize (ks) == 1, "ks has no keys");
succeed_if (ksGetSize (other) == 1, "other has no keys");
ksDel (other);
ksDel (ks);
exit_if_fail ((ks = ksNew (1, keyNew ("system:/some", KEY_END), KS_END)) != 0, "could not create new keyset");
succeed_if (ksAppendKey (ks, keyNew ("user:/test1", KEY_END)) == 2, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test2", KEY_END)) == 3, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test3", KEY_END)) == 4, "could not append a key");
other = ksDup (ks);
succeed_if (other, "other creation failed");
succeed_if (ksGetSize (ks) == 4, "ks has no keys");
succeed_if (ksGetSize (other) == 4, "other has no keys");
ksDel (other);
ksDel (ks);
exit_if_fail ((ks = ksNew (1, keyNew ("user:/any123", KEY_END), KS_END)) != 0, "could not create new keyset");
succeed_if (ksAppendKey (ks, keyNew ("user:/test1", KEY_END)) == 2, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test2", KEY_END)) == 3, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test3", KEY_END)) == 4, "could not append a key");
other = ksDup (ks);
succeed_if (other, "other creation failed");
keyDel (ksPop (other));
succeed_if (ksGetSize (ks) == 4, "ks has no keys");
succeed_if (ksGetSize (other) == 3, "other has no keys");
ksDel (other);
ksDel (ks);
exit_if_fail ((ks = ksNew (1, keyNew ("system:/test", KEY_END), KS_END)) != 0, "could not create new keyset");
succeed_if (ksAppendKey (ks, keyNew ("user:/test1", KEY_END)) == 2, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test2", KEY_END)) == 3, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test3", KEY_END)) == 4, "could not append a key");
other = ksDup (ks);
succeed_if (other, "other creation failed");
keyDel (ksPop (other));
succeed_if (ksAppendKey (ks, keyNew ("user:/test4", KEY_END)) == 5, "could not append a key");
succeed_if (ksGetSize (ks) == 5, "ks has no keys");
succeed_if (ksGetSize (other) == 3, "other has no keys");
ksDel (other);
ksDel (ks);
}
static void test_ksCopy (void)
{
KeySet * ks = 0;
KeySet * other = 0;
printf ("Test ks copy\n");
exit_if_fail ((ks = ksNew (1, keyNew ("user:/testro", KEY_END), KS_END)) != 0, "could not create new keyset");
succeed_if (ksAppendKey (ks, keyNew ("user:/test1", KEY_END)) == 2, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test2", KEY_END)) == 3, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test3", KEY_END)) == 4, "could not append a key");
succeed_if (ksCopy (0, ks) == -1, "No error on NULL pointer");
succeed_if (ksCopy (ks, 0) == 0, "Could not delete ks with ksCopy");
succeed_if (ksGetSize (ks) == 0, "ks has keys after deleting with ksCopy");
ksDel (ks);
other = ksNew (0, KS_END);
exit_if_fail ((ks = ksNew (0, KS_END)) != 0, "could not create new keyset");
succeed_if (ksCopy (other, ks) == 1, "Copy failed");
succeed_if (other, "other creation failed");
succeed_if (ksGetSize (ks) == 0, "ks has keys");
succeed_if (ksGetSize (other) == 0, "other has keys");
ksDel (other);
ksDel (ks);
other = ksNew (0, KS_END);
exit_if_fail ((ks = ksNew (1, keyNew ("user:/test3", KEY_END), KS_END)) != 0, "could not create new keyset");
succeed_if (ksCopy (other, ks) == 1, "Copy failed");
succeed_if (other, "other creation failed");
succeed_if (ksGetSize (ks) == 1, "ks has no keys");
succeed_if (ksGetSize (other) == 1, "other has no keys");
ksDel (other);
ksDel (ks);
other = ksNew (0, KS_END);
exit_if_fail ((ks = ksNew (1, keyNew ("user:/testro", KEY_END), KS_END)) != 0, "could not create new keyset");
succeed_if (ksAppendKey (ks, keyNew ("user:/test1", KEY_END)) == 2, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test2", KEY_END)) == 3, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test3", KEY_END)) == 4, "could not append a key");
succeed_if (ksCopy (other, ks) == 1, "Copy failed");
succeed_if (other, "other creation failed");
succeed_if (ksGetSize (ks) == 4, "ks has no keys");
succeed_if (ksGetSize (other) == 4, "other has no keys");
ksDel (other);
ksDel (ks);
other = ksNew (0, KS_END);
exit_if_fail ((ks = ksNew (1, keyNew ("system:/test", KEY_END), KS_END)) != 0, "could not create new keyset");
succeed_if (ksAppendKey (ks, keyNew ("user:/test1", KEY_END)) == 2, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test2", KEY_END)) == 3, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test3", KEY_END)) == 4, "could not append a key");
succeed_if (ksCopy (other, ks) == 1, "Copy failed");
succeed_if (other, "other creation failed");
keyDel (ksPop (other));
succeed_if (ksGetSize (ks) == 4, "ks has no keys");
succeed_if (ksGetSize (other) == 3, "other has no keys");
ksDel (other);
ksDel (ks);
other = ksNew (0, KS_END);
exit_if_fail ((ks = ksNew (1, keyNew ("user:/mykeys", KEY_END), KS_END)) != 0, "could not create new keyset");
succeed_if (ksAppendKey (ks, keyNew ("user:/test1", KEY_END)) == 2, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test2", KEY_END)) == 3, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/test3", KEY_END)) == 4, "could not append a key");
succeed_if (ksCopy (other, ks) == 1, "Copy failed");
succeed_if (other, "other creation failed");
keyDel (ksPop (other));
succeed_if (ksAppendKey (ks, keyNew ("user:/test", KEY_END)) == 5, "could not append a key");
succeed_if (ksGetSize (ks) == 5, "ks has no keys");
succeed_if (ksGetSize (other) == 3, "other has no keys");
ksDel (other);
ksDel (ks);
other = ksNew (0, KS_END);
exit_if_fail ((ks = ksNew (1, keyNew ("user:/a/b/c", KEY_END), KS_END)) != 0, "could not create new keyset");
succeed_if (ksAppendKey (ks, keyNew ("user:/a/test", KEY_END)) == 2, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/a/b/test", KEY_END)) == 3, "could not append a key");
succeed_if (ksAppendKey (ks, keyNew ("user:/a/b/ctest", KEY_END)) == 4, "could not append a key");
succeed_if (ksCopy (other, ks) == 1, "Copy failed");
succeed_if (other, "other creation failed");
keyDel (ksPop (other));
succeed_if (ksAppendKey (ks, keyNew ("user:/test", KEY_END)) == 5, "could not append a key");
succeed_if (ksGetSize (ks) == 5, "ks has no keys");
succeed_if (ksGetSize (other) == 3, "other has no keys");
succeed_if (ksCopy (ks, 0) == 0, "Clear failed");
succeed_if (ksGetSize (ks) == 0, "ks has keys");
succeed_if (ksCopy (other, 0) == 0, "Clear failed");
succeed_if (ksGetSize (other) == 0, "other has keys");
ksDel (other);
ksDel (ks);
ks = ksNew (0, KS_END);
ksAppendKey (ks, keyNew ("user:/abc", KEY_META, "def", "egh", KEY_END));
other = ksNew (0, KS_END);
ksCopy (other, ks);
compare_keyset (ks, other);
ksDel (other);
ksDel (ks);
}
static void test_ksIterate (void)
{
KeySet * ks = ksNew (0, KS_END);
KeySet * other = ksNew (0, KS_END);
Key * key;
int i;
char name[] = "user:/n";
printf ("Test keyset iterate\n");
succeed_if (ksNext (0) == 0, "No NULL pointer on NULL pointer keyset");
succeed_if (ksCurrent (0) == 0, "No NULL pointer on NULL pointer keyset");
succeed_if (ksRewind (0) == -1, "No error on NULL pointer");
succeed_if (ksCurrent (ks) == 0, "No NULL pointer on empty keyset");
succeed_if (ksNext (ks) == 0, "No NULL pointer on empty keyset");
succeed_if (ksRewind (ks) == 0, "Cannot rewind empty keyset");
ksAppendKey (ks, keyNew ("user:/1", KEY_END));
ksAppendKey (ks, keyNew ("user:/2", KEY_END));
ksAppendKey (ks, keyNew ("user:/3", KEY_END));
ksAppendKey (ks, keyNew ("user:/4", KEY_END));
ksAppendKey (ks, keyNew ("user:/5", KEY_END));
succeed_if (ksGetSize (ks) == 5, "could not append 5 keys");
succeed_if (ksRewind (ks) == 0, "Could not rewind keyset");
succeed_if (ksRewind (ks) == 0, "Could not rewind keyset twice");
succeed_if (ksGetCursor (ks) == -1, "Internal cursor after rewinding is set");
succeed_if (ksNext (ks) != 0, "Could not get first key");
succeed_if_same_string (keyName (ksCurrent (ks)), "user:/1");
succeed_if (ksNext (ks) != 0, "Could not get second key");
succeed_if_same_string (keyName (ksCurrent (ks)), "user:/2");
succeed_if (ksNext (ks) != 0, "Could not get third key");
succeed_if_same_string (keyName (ksCurrent (ks)), "user:/3");
succeed_if (ksNext (ks) != 0, "Could not get fourth key");
succeed_if_same_string (keyName (ksCurrent (ks)), "user:/4");
succeed_if (ksNext (ks) != 0, "Could not get fifth key");
succeed_if_same_string (keyName (ksCurrent (ks)), "user:/5");
succeed_if (ksNext (ks) == 0, "Could not iterate over last");
succeed_if (ksCurrent (ks) == 0, "This is not the beyond last key");
succeed_if (ksNext (ks) == 0, "Could not iterate over last (again)");
succeed_if (ksCurrent (ks) == 0, "This is not the beyond last key (again)");
key = ksPop (ks);
succeed_if_same_string (keyName (key), "user:/5");
succeed_if (keyDel (key) == 0, "could not del popped key");
succeed_if (ksAppend (other, ks) == 4, "could not append keys");
for (i = 4; i >= 1; i--)
{
key = ksPop (other);
succeed_if (key != 0, "got null pointer key");
name[6] = '0' + i;
succeed_if_same_string (keyName (key), name);
keyDel (key);
}
succeed_if (ksAppendKey (other, keyNew ("user:/3", KEY_END)) == 1, "could not append one key");
key = ksPop (other);
succeed_if (key != 0, "got null pointer key");
succeed_if_same_string (keyName (key), "user:/3");
succeed_if (keyDel (key) == 0, "could not del popped key");
ksDel (other);
ksDel (ks);
ks = ksNew (10, keyNew ("user:/0", KEY_END), keyNew ("user:/1", KEY_END), keyNew ("user:/2", KEY_END), keyNew ("user:/3", KEY_END),
KS_END);
other = ksNew (10, keyNew ("user:/4", KEY_END), keyNew ("user:/5", KEY_END), keyNew ("user:/6", KEY_END),
keyNew ("user:/7", KEY_END), KS_END);
succeed_if (ksAppend (ks, other) == 8, "could not append keys");
for (i = 7; i >= 0; i--)
{
key = ksPop (ks);
succeed_if (key != 0, "got null pointer key");
name[6] = '0' + i;
succeed_if_same_string (keyName (key), name);
keyDel (key);
}
ksDel (ks);
ksDel (other);
}
static void test_ksCursor (void)
{
KeySet * ks = ksNew (0, KS_END);
Key * key;
elektraCursor cursor;
Key * cur;
int i;
char name[] = "user:/n";
printf ("Test keyset cursor\n");
ksAppendKey (ks, cur = keyNew ("user:/1", KEY_END));
succeed_if (ksCurrent (ks) == cur, "cursor not set after append key");
ksAppendKey (ks, cur = keyNew ("user:/2", KEY_END));
succeed_if (ksCurrent (ks) == cur, "cursor not set after append key");
ksAppendKey (ks, cur = keyNew ("user:/3", KEY_END));
succeed_if (ksCurrent (ks) == cur, "cursor not set after append key");
cursor = ksGetCursor (ks);
succeed_if_same_string (keyName (ksAtCursor (ks, cursor)), "user:/3");
ksAppendKey (ks, cur = keyNew ("user:/4", KEY_END));
succeed_if (ksCurrent (ks) == cur, "cursor not set after append key");
ksAppendKey (ks, cur = keyNew ("user:/5", KEY_END));
succeed_if (ksCurrent (ks) == cur, "cursor not set after append key");
succeed_if (ksGetSize (ks) == 5, "could not append 5 keys");
succeed_if_same_string (keyName (ksAtCursor (ks, cursor)), "user:/3");
ksSetCursor (ks, cursor);
succeed_if (cursor == ksGetCursor (ks), "cursor not set to 3");
succeed_if_same_string (keyName (ksAtCursor (ks, cursor)), "user:/3");
ksSetCursor (ks, cursor);
succeed_if (cursor == ksGetCursor (ks), "cursor not set to 3 (again)");
cursor = ksGetCursor (ks);
key = ksPop (ks);
succeed_if (cursor == ksGetCursor (ks), "cursor should stay the same");
succeed_if_same_string (keyName (key), "user:/5");
succeed_if (keyDel (key) == 0, "could not del popped key");
ksRewind (ks);
for (i = 0; i < 5; i++)
{
ksNext (ks);
if (i == 1)
{
cursor = ksGetCursor (ks);
name[6] = '0' + i;
}
}
ksSetCursor (ks, cursor);
ksCurrent (ks);
ksDel (ks);
ks = ksNew (10, keyNew ("user:/0", KEY_END), keyNew ("user:/1", KEY_END), keyNew ("user:/2", KEY_END), keyNew ("user:/3", KEY_END),
KS_END);
ksRewind (ks);
for (i = 0; i < 4; i++)
{
ksNext (ks);
if (i == 1)
{
cursor = ksGetCursor (ks);
name[6] = '0' + i;
}
}
ksSetCursor (ks, cursor);
key = ksCurrent (ks);
succeed_if_same_string (keyName (key), name);
ksDel (ks);
ks = ksNew (10, keyNew ("user:/0", KEY_END), keyNew ("user:/1", KEY_END), keyNew ("user:/2", KEY_END), keyNew ("user:/3", KEY_END),
KS_END);
ksRewind (ks);
for (i = 0; i < 4; i++)
{
ksNext (ks);
cursor = ksGetCursor (ks);
name[6] = '0' + i;
succeed_if_same_string (keyName (ksAtCursor (ks, cursor)), name);
}
succeed_if_same_string (keyName (ksAtCursor (ks, 0)), "user:/0");
succeed_if_same_string (keyName (ksAtCursor (ks, 1)), "user:/1");
succeed_if_same_string (keyName (ksAtCursor (ks, 2)), "user:/2");
succeed_if_same_string (keyName (ksAtCursor (ks, 3)), "user:/3");
succeed_if (ksAtCursor (ks, -1) == 0, "bounds check not correct");
succeed_if (ksAtCursor (ks, 4) == 0, "bounds check not correct");
ksDel (ks);
}
static void test_ksAtCursor (void)
{
KeySet * ks;
Key * current;
Key * testKeys[5];
ks = ksNew (0, KS_END);
testKeys[0] = keyNew ("user:/test1", KEY_END);
testKeys[1] = keyNew ("user:/test2", KEY_END);
testKeys[2] = keyNew ("user:/test3", KEY_END);
testKeys[3] = keyNew ("user:/test4", KEY_END);
testKeys[4] = keyNew ("user:/test5", KEY_END);
for (size_t index = 0; index < 5; index++)
{
ksAppendKey (ks, testKeys[index]);
}
ksRewind (ks);
elektraCursor cursor;
/* test whether the correct key is returned */
for (size_t index = 0; index < 5; index++)
{
current = testKeys[index];
ksNext (ks);
cursor = ksGetCursor (ks);
Key * other = ksAtCursor (ks, cursor);
succeed_if_same_string (keyName (current), keyName (other));
}
succeed_if (ksAtCursor (ks, 5) == 0, "Not NULL on invalid cursor position");
/* test whether the correct key is returned even if
* the internal cursor is positioned somewhere else */
ksRewind (ks);
ksNext (ks);
cursor = ksGetCursor (ks);
ksNext (ks);
ksNext (ks);
current = ksAtCursor (ks, cursor);
succeed_if_same_string (keyName (current), "user:/test1");
/* test whether the internal cursor is modified */
ksRewind (ks);
ksNext (ks);
cursor = ksGetCursor (ks);
ksNext (ks);
current = ksAtCursor (ks, cursor);
succeed_if_same_string (keyName (current), "user:/test1");
current = ksNext (ks);
succeed_if_same_string (keyName (current), "user:/test3");
/* test postconditions */
succeed_if (!ksAtCursor (0, cursor), "did not return NULL on NULL keyset");
succeed_if (!ksAtCursor (ks, -1), "did not return NULL on negative cursor");
succeed_if (!ksAtCursor (ks, 10), "did not return NULL on invalid cursor");
ksDel (ks);
}
static void test_ksSort (void)
{
KeySet * ks;
Key *key, *k1, *k2;
int i;
printf ("Test ks sort\n");
ks = ksNew (0, KS_END);
ksAppendKey (ks, keyNew ("user:/bname", KEY_END));
ksAppendKey (ks, keyNew ("user:/aname", KEY_END));
ksAppendKey (ks, keyNew ("user:/cname", KEY_END));
ksRewind (ks);
key = ksNext (ks);
succeed_if_same_string (keyName (key), "user:/aname");
key = ksNext (ks);
succeed_if_same_string (keyName (key), "user:/bname");
key = ksNext (ks);
succeed_if_same_string (keyName (key), "user:/cname");
ksDel (ks);
ks = ksNew (0, KS_END);
ksAppendKey (ks, keyNew ("user:/a", KEY_END));
ksAppendKey (ks, keyNew ("user:/e", KEY_END));
ksAppendKey (ks, keyNew ("user:/b1", KEY_END));
ksAppendKey (ks, keyNew ("user:/h2", KEY_END));
ksAppendKey (ks, keyNew ("user:/b2", KEY_END));
ksAppendKey (ks, keyNew ("user:/d", KEY_END));
ksAppendKey (ks, keyNew ("user:/a", KEY_END));
ksAppendKey (ks, keyNew ("user:/g", KEY_END));
ksAppendKey (ks, keyNew ("user:/g", KEY_END));
ksAppendKey (ks, keyNew ("user:/c2", KEY_END));
ksAppendKey (ks, keyNew ("user:/c1", KEY_END));
ksAppendKey (ks, keyNew ("user:/g", KEY_END));
ksAppendKey (ks, keyNew ("user:/h1", KEY_END));
ksAppendKey (ks, keyNew ("user:/f", KEY_END));
ksRewind (ks);
for (i = 0; (key = ksNext (ks)) != 0; i++)
{
switch (i)
{
case 0:
succeed_if_same_string (keyName (key), "user:/a");
break;
case 1:
succeed_if_same_string (keyName (key), "user:/b1");
break;
case 2:
succeed_if_same_string (keyName (key), "user:/b2");
break;
case 3:
succeed_if_same_string (keyName (key), "user:/c1");
break;
case 4:
succeed_if_same_string (keyName (key), "user:/c2");
break;
case 5:
succeed_if_same_string (keyName (key), "user:/d");
break;
case 6:
succeed_if_same_string (keyName (key), "user:/e");
break;
case 7:
succeed_if_same_string (keyName (key), "user:/f");
break;
case 8:
succeed_if_same_string (keyName (key), "user:/g");
break;
case 9:
succeed_if_same_string (keyName (key), "user:/h1");
break;
case 10:
succeed_if_same_string (keyName (key), "user:/h2");
break;
default:
succeed_if (0, "should not reach");
break;
}
}
ksDel (ks);
ks = ksNew (0, KS_END);
k1 = keyNew ("user:/xname", KEY_END);
ksAppendKey (ks, k1);
k2 = keyDup (k1, KEY_CP_ALL);
succeed_if (keyGetRef (k2) == 0, "reference counter not resetted");
ksAppendKey (ks, k2);
succeed_if (keyGetRef (k2) == 1, "reference counter not incremented after insertion");
ksRewind (ks);
ksNext (ks);
ksDel (ks);
ks = ksNew (0, KS_END);
k1 = keyNew ("user:/yname", KEY_END);
k2 = keyDup (k1, KEY_CP_ALL);
ksAppendKey (ks, k2);
ksAppendKey (ks, k1);
ksRewind (ks);
ksNext (ks);
ksDel (ks);
ks = ksNew (0, KS_END);
ksAppendKey (ks, keyNew ("user:/a", KEY_END));
ksAppendKey (ks, keyNew ("user:/e", KEY_END));
ksAppendKey (ks, keyNew ("user:/b", KEY_END));
ksAppendKey (ks, keyNew ("user:/b", KEY_END));
ksAppendKey (ks, keyNew ("user:/d", KEY_END));
ksAppendKey (ks, keyNew ("user:/c", KEY_END));
ksAppendKey (ks, keyNew ("user:/c", KEY_END));
ksAppendKey (ks, keyNew ("user:/g", KEY_END));
ksAppendKey (ks, keyNew ("user:/h", KEY_END));
ksAppendKey (ks, keyNew ("user:/h", KEY_END));
ksAppendKey (ks, keyNew ("user:/f", KEY_END));
ksRewind (ks);
for (i = 0; (key = ksNext (ks)) != 0; i++)
{
switch (i)
{
case 0:
succeed_if_same_string (keyName (key), "user:/a");
break;
case 1:
succeed_if_same_string (keyName (key), "user:/b");
break;
case 2:
succeed_if_same_string (keyName (key), "user:/c");
break;
case 3:
succeed_if_same_string (keyName (key), "user:/d");
break;
case 4:
succeed_if_same_string (keyName (key), "user:/e");
break;
case 5:
succeed_if_same_string (keyName (key), "user:/f");
break;
case 6:
succeed_if_same_string (keyName (key), "user:/g");
break;
case 7:
succeed_if_same_string (keyName (key), "user:/h");
break;
default:
succeed_if (0, "should not reach");
break;
}
}
ksDel (ks);
ks = ksNew (0, KS_END);
ksAppendKey (ks, keyNew ("user:/a", KEY_END));
ksAppendKey (ks, keyNew ("user:/e", KEY_END));
ksAppendKey (ks, keyNew ("user:/b/a", KEY_END));
ksAppendKey (ks, keyNew ("user:/b", KEY_END));
ksAppendKey (ks, keyNew ("user:/d", KEY_END));
ksAppendKey (ks, keyNew ("user:/c", KEY_END));
ksAppendKey (ks, keyNew ("user:/c/a", KEY_END));
ksAppendKey (ks, keyNew ("user:/g", KEY_END));
ksAppendKey (ks, keyNew ("user:/h/a", KEY_END));
ksAppendKey (ks, keyNew ("user:/h", KEY_END));
ksAppendKey (ks, keyNew ("user:/f", KEY_END));
ksRewind (ks);
// output_keyset(ks,0);
for (i = 0; (key = ksNext (ks)) != 0; i++)
{
switch (i)
{
case 10:
succeed_if_same_string (keyName (key), "user:/h/a");
break;
case 9:
succeed_if_same_string (keyName (key), "user:/h");
break;
case 8:
succeed_if_same_string (keyName (key), "user:/g");
break;
case 7:
succeed_if_same_string (keyName (key), "user:/f");
break;
case 6:
succeed_if_same_string (keyName (key), "user:/e");
break;
case 5:
succeed_if_same_string (keyName (key), "user:/d");
break;
case 4:
succeed_if_same_string (keyName (key), "user:/c/a");
break;
case 3:
succeed_if_same_string (keyName (key), "user:/c");
break;
case 2:
succeed_if_same_string (keyName (key), "user:/b/a");
break;
case 1:
succeed_if_same_string (keyName (key), "user:/b");
break;
case 0:
succeed_if_same_string (keyName (key), "user:/a");
break;
default:
succeed_if (0, "should not reach");
break;
}
}
ksDel (ks);
ks = ksNew (0, KS_END);
ksAppendKey (ks, keyNew ("user:/dir1/key1", KEY_END));
ksAppendKey (ks, keyNew ("user:/dir1/key2", KEY_END));
ksAppendKey (ks, keyNew ("user:/dir1/key3", KEY_END));
ksAppendKey (ks, keyNew ("user:/dir2", KEY_END));
ksAppendKey (ks, keyNew ("user:/dir2/key1", KEY_END));
ksAppendKey (ks, keyNew ("user:/dir3/key1", KEY_END));
ksAppendKey (ks, keyNew ("user:/dir3", KEY_END));
ksAppendKey (ks, keyNew ("user:/dir3/key2", KEY_END));
ksAppendKey (ks, keyNew ("user:/dir4", KEY_END));
ksAppendKey (ks, keyNew ("user:/dir5/key1", KEY_END));
ksAppendKey (ks, keyNew ("user:/dir6/key1", KEY_END));
ksRewind (ks);
// output_keyset(ks,0);
for (i = 0; (key = ksNext (ks)) != 0; i++)
{
switch (i)
{
case 9:
succeed_if_same_string (keyName (key), "user:/dir5/key1");
break;
case 4:
succeed_if_same_string (keyName (key), "user:/dir2/key1");
break;
case 3:
succeed_if_same_string (keyName (key), "user:/dir2");
break;
case 2:
succeed_if_same_string (keyName (key), "user:/dir1/key3");
break;
case 0:
succeed_if_same_string (keyName (key), "user:/dir1/key1");
break;
case 1:
succeed_if_same_string (keyName (key), "user:/dir1/key2");
break;
case 5:
succeed_if_same_string (keyName (key), "user:/dir3");
break;
case 6:
succeed_if_same_string (keyName (key), "user:/dir3/key1");
break;
case 7:
succeed_if_same_string (keyName (key), "user:/dir3/key2");
break;
case 8:
succeed_if_same_string (keyName (key), "user:/dir4");
break;
case 10:
succeed_if_same_string (keyName (key), "user:/dir6/key1");
break;
default:
succeed_if (0, "should not reach");
break;
}
}
ksDel (ks);
}
static void ksUnsort (KeySet * ks)
{
KeySet * randks = ksNew (0, KS_END); /*This is the final randomized keyset*/
KeySet * tempks = ksNew (0, KS_END); /*Temporary storage for keys not chosen to be inserted*/
while (ksGetSize (ks) > 0)
{
ksRewind (ks);
size_t size = ksGetSize (ks);
/* printf ("iterating %d\n", size); */
Key * cur;
while ((cur = ksPop (ks)) != 0)
{
/* printf ("\titerating %s\n", keyName(cur)); */
if (!(rand () % size))
ksAppendKey (randks, cur);
else
ksAppendKey (tempks, cur);
}
ksAppend (ks, tempks);
ksCopy (tempks, 0);
}
ksCopy (ks, randks);
ksDel (randks);
ksDel (tempks);