forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_order.c
373 lines (284 loc) · 11.8 KB
/
test_order.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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <tests_internal.h>
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");
KeySet * ks2 = ksNew (0, KS_END);
ksCopy (ks2, ks);
succeed_if (ksGetSize (ks2) == 0, "size not correct after copy");
ksClear (ks2); // useless, just test for double free
ksCopy (ks2, ks);
succeed_if (ksGetSize (ks2) == 0, "could not append 3 more keys");
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");
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 (17, 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) == 17, "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");
}
static void test_ksDuplicate (void)
{
printf ("Test bug duplicate\n");
KeySet * ks = ksNew (0, KS_END);
succeed_if (ksAppendKey (ks, keyNew ("system:/duplicate", KEY_VALUE, "abc", KEY_END)) == 1, "could not append key");
succeed_if (!strcmp (keyValue (ksLookupByName (ks, "system:/duplicate", 0)), "abc"), "wrong value for inserted key");
succeed_if (ksAppendKey (ks, keyNew ("system:/duplicate", KEY_VALUE, "xyz", KEY_END)) == 1, "could not append duplicate key");
succeed_if (!strcmp (keyValue (ksLookupByName (ks, "system:/duplicate", 0)), "xyz"), "wrong value for inserted key");
ksDel (ks);
}
static void test_ksHole (void)
{
printf ("Test holes in keysets\n");
KeySet * ks = ksNew (0, KS_END);
succeed_if (ksAppendKey (ks, keyNew ("system:/sw/new", KEY_VALUE, "abc", KEY_END)) == 1, "could not append key");
succeed_if (ksAppendKey (ks, keyNew ("system:/sw/new/sub", KEY_VALUE, "xyz", KEY_END)) == 2, "could not append key");
succeed_if (ksAppendKey (ks, keyNew ("system:/sw/new/mis/sub", KEY_VALUE, "xyz", KEY_END)) == 3,
"could not append key which makes a hole");
ksDel (ks);
}
#define size 5
int fac (int i)
{
if (i == 0)
return 1;
else
return i * fac (i - 1);
}
/* Buggy, does not really yield all permutations */
static void per (int k, Key ** pool, Key ** result)
{
int i;
int cursize = size - 1;
for (i = 0; i < size - 1; ++i)
{
// printf ("%d -- ", k);
int selected = k % (cursize);
k /= cursize + 1;
cursize--;
// copy the selected to the result
result[i] = pool[selected];
// remove the selected from the pool
memmove (pool + selected, // destination
pool + (selected + 1), // source
(size - selected - 1) * sizeof (struct Key *));
}
result[size - 1] = 0;
}
static void test_append (void)
{
printf ("Test if keyset is sorted after appending\n");
Key * key;
Key * n;
Key *s1, *s2, *s3;
KeySet * ks;
ks = ksNew (0, KS_END);
succeed_if (ksAppendKey (ks, key = keyNew ("system:/sw/new", KEY_VALUE, "abc", KEY_END)) == 1, "could not append key");
succeed_if (ksGetSize (ks) == 1, "wrong size");
succeed_if (ks->array[0] == key, "key not on position 0");
succeed_if (ks->array[1] == 0, "array not null terminated");
succeed_if (ksAppendKey (ks, n = keyNew ("system:/sw/new", KEY_VALUE, "xyz1", KEY_END)) == 1, "could not append key");
succeed_if (ksGetSize (ks) == 1, "wrong size");
succeed_if (ks->array[0] == n, "n not on position 0");
succeed_if (ks->array[0] != key, "key is on position 0");
succeed_if (ks->array[1] == 0, "array not null terminated");
ksDel (ks);
ks = ksNew (0, KS_END);
succeed_if (ksAppendKey (ks, key = keyNew ("system:/sw/new", KEY_VALUE, "abc", KEY_END)) == 1, "could not append key");
succeed_if (ksGetSize (ks) == 1, "wrong size");
succeed_if (ks->array[0] == key, "key not on position 0");
succeed_if (ks->array[1] == 0, "array not null terminated");
succeed_if (ksAppendKey (ks, n = keyNew ("system:/sw/new/sub1", KEY_VALUE, "xyz1", KEY_END)) == 2, "could not append key");
succeed_if (ksGetSize (ks) == 2, "wrong size");
succeed_if (ks->array[0] == key, "key not on position 0");
succeed_if (ks->array[1] == n, "new key not on position 1");
succeed_if (ks->array[2] == 0, "array not null terminated");
ksDel (ks);
ks = ksNew (0, KS_END);
succeed_if (ksAppendKey (ks, n = keyNew ("system:/sw/new/sub1", KEY_VALUE, "xyz1", KEY_END)) == 1, "could not append key");
succeed_if (ksGetSize (ks) == 1, "wrong size");
succeed_if (ks->array[0] == n, "key not on position 0");
succeed_if (ks->array[1] == 0, "array not null terminated");
succeed_if (ksAppendKey (ks, key = keyNew ("system:/sw/new", KEY_VALUE, "abc", KEY_END)) == 2, "could not append key");
succeed_if (ksGetSize (ks) == 2, "wrong size");
succeed_if (ks->array[0] == key, "key not on position 0");
succeed_if (ks->array[1] == n, "key not on position 1");
succeed_if (ks->array[2] == 0, "array not null terminated");
ksDel (ks);
key = keyNew ("system:/sw/new", KEY_VALUE, "abc", KEY_END);
s1 = keyNew ("system:/sw/new/sub1", KEY_VALUE, "xyz1", KEY_END);
s2 = keyNew ("system:/sw/new/sub2", KEY_VALUE, "xyz2", KEY_END);
s3 = keyNew ("system:/sw/new/sub3", KEY_VALUE, "xyz3", KEY_END);
keyIncRef (key);
keyIncRef (s1);
keyIncRef (s2);
keyIncRef (s3);
Key * solution[size] = { key, s1, s2, s3, 0 };
Key * solutioncopy[size];
Key * next[size];
int i, j;
for (i = 0; i < fac (size - 1); ++i)
{
memcpy (solutioncopy, solution, size * sizeof (struct Key *));
per (i, solutioncopy, next);
ks = ksNew (0, KS_END);
for (j = 0; j < size - 1; ++j)
{
// printf ("%p ", (void*)next[j]);
// printf ("%s ", keyName(next[j]));
succeed_if (ksAppendKey (ks, next[j]) == j + 1, "could not append key");
}
// printf ("\nsolution is:\n");
succeed_if (!memcmp (ks->array, solution, size), "solution is not correct");
ksDel (ks);
}
ks = ksNew (0, KS_END);
succeed_if (ksAppendKey (ks, key) == 1, "could not append key");
succeed_if (ksGetSize (ks) == 1, "wrong size");
succeed_if (ks->array[0] == key, "key not on position 0");
succeed_if (ks->array[1] == 0, "array not null terminated");
succeed_if (ksAppendKey (ks, s1) == 2, "could not append key");
succeed_if (ksGetSize (ks) == 2, "wrong size");
succeed_if (ks->array[0] == key, "key not on position 0");
succeed_if (ks->array[1] == s1, "key not on position 1");
succeed_if (ks->array[2] == 0, "array not null terminated");
succeed_if (ksAppendKey (ks, s2) == 3, "could not append key");
succeed_if (ksGetSize (ks) == 3, "wrong size");
succeed_if (ks->array[0] == key, "key not on position 0");
succeed_if (ks->array[1] == s1, "key not on position 1");
succeed_if (ks->array[2] == s2, "key not on position 2");
succeed_if (ks->array[3] == 0, "array not null terminated");
succeed_if (ksAppendKey (ks, s3) == 4, "could not append key");
succeed_if (ksGetSize (ks) == 4, "wrong size");
succeed_if (ks->array[0] == key, "key not on position 0");
succeed_if (ks->array[1] == s1, "key not on position 1");
succeed_if (ks->array[2] == s2, "key not on position 2");
succeed_if (ks->array[3] == s3, "key not on position 3");
succeed_if (ks->array[4] == 0, "array not null terminated");
succeed_if (!memcmp (ks->array, solution, size), "solution is not correct");
ksDel (ks);
ks = ksNew (0, KS_END);
succeed_if (ksAppendKey (ks, s3) == 1, "could not append key");
succeed_if (ksGetSize (ks) == 1, "wrong size");
succeed_if (ks->array[0] == s3, "key not on position 0");
succeed_if (ks->array[1] == 0, "array not null terminated");
succeed_if (ksAppendKey (ks, key) == 2, "could not append key");
succeed_if (ksGetSize (ks) == 2, "wrong size");
succeed_if (ks->array[0] == key, "key not on position 0");
succeed_if (ks->array[1] == s3, "key not on position 1");
succeed_if (ks->array[2] == 0, "array not null terminated");
succeed_if (ksAppendKey (ks, s1) == 3, "could not append key");
succeed_if (ksGetSize (ks) == 3, "wrong size");
succeed_if (ks->array[0] == key, "key not on position 0");
succeed_if (ks->array[1] == s1, "key not on position 1");
succeed_if (ks->array[2] == s3, "key not on position 2");
succeed_if (ks->array[3] == 0, "array not null terminated");
succeed_if (ksAppendKey (ks, s2) == 4, "could not append key");
succeed_if (ksGetSize (ks) == 4, "wrong size");
succeed_if (ks->array[0] == key, "key not on position 0");
succeed_if (ks->array[1] == s1, "key not on position 1");
succeed_if (ks->array[2] == s2, "key not on position 2");
succeed_if (ks->array[3] == s3, "key not on position 3");
succeed_if (ks->array[4] == 0, "array not null terminated");
succeed_if (!memcmp (ks->array, solution, size), "solution is not correct");
/*
Key *it;
ksRewind(ks);
while ((it = ksNext(ks)) != 0)
{
printf ("%s\n", keyName(it));
}
*/
ksDel (ks);
keyDecRef (key);
keyDel (key);
keyDecRef (s1);
keyDel (s1);
keyDecRef (s2);
keyDel (s2);
keyDecRef (s3);
keyDel (s3);
}
static void test_equal (void)
{
Key * k1 = keyNew ("/", KEY_END);
Key * k2 = keyNew ("/", KEY_END);
succeed_if (keyCmp (0, 0) == 0, "null pointers should be same");
succeed_if (keyCmp (k1, k2) == 0, "should be same");
keySetName (k1, "");
keySetName (k2, "");
succeed_if (keyCmp (k1, k2) == 0, "should be same");
keySetName (k1, "user:/");
keySetName (k2, "user:/");
succeed_if (keyCmp (k1, k2) == 0, "should be same");
keySetName (k1, "system:/");
keySetName (k2, "system:/");
succeed_if (keyCmp (k1, k2) == 0, "should be same");
keySetName (k1, "user:/a");
keySetName (k2, "user:/a");
succeed_if (keyCmp (k1, k2) == 0, "should be same");
keyDel (k1);
keyDel (k2);
}
static void test_cmp (void)
{
printf ("Compare two keys\n");
Key * k1 = keyNew ("/", KEY_END);
Key * k2 = keyNew ("/", KEY_END);
succeed_if (keyCmp (0, 0) == 0, "null keys comparision");
keySetName (k1, "user:/a");
keySetName (k2, "user:/a");
succeed_if (keyCmp (k1, k1) == 0, "compare the same key");
succeed_if (keyCmp (k1, k2) == 0, "compare the same key");
succeed_if (keyCmp (k2, k1) == 0, "compare the same key");
keySetName (k2, "user:/b");
succeed_if (keyCmp (k1, k2) < 0, "compare key with different names");
succeed_if (keyCmp (k2, k1) > 0, "compare key with different names");
keyDel (k1);
keyDel (k2);
}
int main (int argc, char ** argv)
{
printf ("KEYSET ORDERING TESTS\n");
printf ("==========================\n\n");
init (argc, argv);
test_ksNew ();
test_ksDuplicate ();
test_ksHole ();
test_append ();
test_equal ();
test_cmp ();
printf ("\n%s RESULTS: %d test(s) done. %d error(s).\n", argv[0], nbTest, nbError);
return nbError;
}