Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <tests.hpp>
10 :
11 : #include <memory>
12 :
13 : #include <algorithm>
14 : #include <vector>
15 :
16 2 : KeySet fun (size_t alloc, ...)
17 : {
18 : va_list vl;
19 :
20 2 : va_start (vl, alloc);
21 4 : KeySet ks (VaAlloc (alloc), vl);
22 2 : va_end (vl);
23 2 : return ks;
24 : }
25 :
26 20 : TEST (ks, new)
27 : {
28 : // would fail to compile with: error: call to ‘kdb::KeySet::KeySet’ declared with attribute error: wrong usage of API
29 : // or error: call to deleted constructor of 'kdb::KeySet'
30 : // KeySet(Key("user", KEY_END), KS_END);
31 :
32 4 : KeySet ks1;
33 :
34 4 : KeySet ks2 (5, ckdb::keyNew ("user/key2", KEY_END), KS_END);
35 :
36 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
37 10 : KS_END);
38 : // ks3.toStream(stdout, 0);
39 :
40 4 : Key k1 ("user/key4/1", KEY_END);
41 4 : Key k2 ("user/key4/2", KEY_END);
42 4 : Key k3 ("user/key4/3", KEY_VALUE, "value", KEY_END);
43 : KeySet ks4 (5,
44 : *k1, // k1 will lose its key and pass it to keyset
45 8 : *k2, *k3, KS_END);
46 : // ks4.toStream(stdout, 0);
47 :
48 4 : Key k4 ("user/key5/1", KEY_END);
49 4 : Key k5 ("user/key5/2", KEY_END);
50 4 : Key k6 ("user/key5/3", KEY_VALUE, "value", KEY_END);
51 8 : KeySet ks5 (5, k4.dup (), k5.dup (), k6.dup (), KS_END);
52 : // ks5.toStream(stdout, 0);
53 : // k4, k5, k6 can still be used
54 :
55 8 : KeySet ks6 = fun (5, k4.dup (), k5.dup (), k6.dup (), KS_END);
56 2 : }
57 :
58 :
59 20 : TEST (ks, dup)
60 : {
61 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
62 10 : KS_END);
63 16 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
64 16 : succeed_if (ks3.lookup ("user/key3/2"), "could not find key");
65 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
66 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
67 6 : succeed_if (ks3.size () == 3, "size not correct");
68 :
69 6 : KeySet ks4 (ks3.dup ());
70 16 : succeed_if (ks4.lookup ("user/key3/1"), "could not find key");
71 16 : succeed_if (ks4.lookup ("user/key3/2"), "could not find key");
72 16 : succeed_if (ks4.lookup ("user/key3/3"), "could not find key");
73 20 : succeed_if (ks4.lookup ("user/key3/3").getString () == "value", "value not correct");
74 6 : succeed_if (ks4.size () == 3, "size not correct");
75 :
76 : // ks3.toStream(stdout, 0);
77 : // ks4.toStream(stdout, 0);
78 : }
79 :
80 20 : TEST (ks, copy)
81 : {
82 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
83 10 : KS_END);
84 :
85 4 : KeySet ks4 (ks3);
86 6 : succeed_if (ks3.size () == 3, "size not correct");
87 6 : succeed_if (ks4.size () == 3, "size not correct");
88 :
89 4 : KeySet ks5;
90 2 : ks5.copy (ks4);
91 6 : succeed_if (ks4.size () == 3, "size not correct");
92 6 : succeed_if (ks5.size () == 3, "size not correct");
93 :
94 2 : ks5.clear ();
95 6 : succeed_if (ks5.size () == 0, "size not correct");
96 :
97 :
98 : // ks3.toStream(stdout, 0);
99 : // ks4.toStream(stdout, 0);
100 : }
101 :
102 20 : TEST (ks, iterate)
103 : {
104 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
105 10 : KS_END);
106 :
107 2 : ks3.rewind ();
108 :
109 4 : Key k1 = ks3.next ();
110 10 : succeed_if (k1.getName () == "user/key3/1", "wrong keyname");
111 10 : succeed_if (k1 == ks3.head (), "first key not head key");
112 4 : Key k2 = ks3.next ();
113 10 : succeed_if (k2.getName () == "user/key3/2", "wrong keyname");
114 4 : Key k3 = ks3.next ();
115 10 : succeed_if (k3.getName () == "user/key3/3", "wrong keyname");
116 10 : succeed_if (k3.getString () == "value", "wrong value");
117 10 : succeed_if (k3 == ks3.tail (), "last key not tail key");
118 10 : succeed_if (!ks3.next (), "no more key");
119 10 : succeed_if (!ks3.next (), "no more key");
120 10 : succeed_if (!ks3.next (), "no more key");
121 10 : succeed_if (!ks3.next (), "no more key");
122 :
123 4 : Key null = static_cast<ckdb::Key *> (nullptr);
124 6 : succeed_if (!null, "null key");
125 :
126 : ks3.rewind ();
127 14 : for (ssize_t i = 0; i < ks3.size (); i++)
128 : {
129 12 : Key k = ks3.next ();
130 6 : char str[] = "user/key3/X";
131 :
132 6 : str[10] = i + '1';
133 30 : succeed_if (k.getName () == str, "wrong keyname");
134 : }
135 :
136 2 : ks3.rewind ();
137 2 : Key n;
138 : int j = 0;
139 32 : while ((n = ks3.next ()))
140 : {
141 6 : char str[] = "user/key3/X";
142 :
143 6 : str[10] = j + '1';
144 30 : succeed_if (n.getName () == str, "wrong keyname");
145 6 : j++;
146 : }
147 :
148 2 : j = 0;
149 : ks3.rewind ();
150 32 : while ((n = ks3.next ()) == true)
151 : {
152 6 : char str[] = "user/key3/X";
153 :
154 6 : str[10] = j + '1';
155 30 : succeed_if (n.getName () == str, "wrong keyname");
156 6 : j++;
157 : }
158 :
159 2 : j = 0;
160 2 : ks3.rewind ();
161 34 : for (Key k; (k = ks3.next ());)
162 : {
163 6 : char str[] = "user/key3/X";
164 :
165 6 : str[10] = j + '1';
166 30 : succeed_if (k.getName () == str, "wrong keyname");
167 6 : j++;
168 : }
169 :
170 2 : j = 0;
171 2 : ks3.rewind ();
172 26 : for (Key k = ks3.next (); k; (k = ks3.next ()))
173 : {
174 6 : char str[] = "user/key3/X";
175 :
176 6 : str[10] = j + '1';
177 30 : succeed_if (k.getName () == str, "wrong keyname");
178 6 : j++;
179 : }
180 : }
181 :
182 20 : TEST (ks, cursor)
183 : {
184 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
185 10 : KS_END);
186 2 : cursor_t cursorTest = ks3.getCursor ();
187 :
188 : ks3.rewind ();
189 14 : for (ssize_t i = 0; i < ks3.size (); i++)
190 : {
191 12 : Key k = ks3.next ();
192 6 : if (i == 0) cursorTest = ks3.getCursor ();
193 : }
194 :
195 2 : ks3.setCursor (cursorTest);
196 4 : Key k1 = ks3.current ();
197 10 : succeed_if (k1.getName () == "user/key3/1", "wrong keyname");
198 10 : succeed_if (k1 == ks3.head (), "first key not head key");
199 : }
200 :
201 20 : TEST (ks, pop)
202 : {
203 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
204 10 : KS_END);
205 :
206 2 : ks3.rewind ();
207 :
208 4 : Key k3 = ks3.pop ();
209 10 : succeed_if (k3.getName () == "user/key3/3", "wrong keyname");
210 10 : succeed_if (k3.getString () == "value", "wrong value");
211 4 : Key k2 = ks3.pop ();
212 10 : succeed_if (k2.getName () == "user/key3/2", "wrong keyname");
213 4 : Key k1 = ks3.pop ();
214 10 : succeed_if (k1.getName () == "user/key3/1", "wrong keyname");
215 4 : Key k0 = ks3.pop ();
216 6 : succeed_if (!k0, "Out of Range, no more key");
217 :
218 12 : KeySet ks4 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
219 10 : KS_END);
220 :
221 2 : ks4.rewind ();
222 6 : for (int i = ks4.size () - 1; i > 0; i--)
223 : {
224 8 : Key k = ks4.pop ();
225 4 : char str[] = "user/key3/X";
226 :
227 4 : str[10] = i + '1';
228 20 : succeed_if (k.getName () == str, str);
229 : }
230 : }
231 :
232 20 : TEST (ks, lookup)
233 : {
234 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
235 10 : KS_END);
236 :
237 12 : Key k1 = ks3.lookup ("user/key3/1");
238 4 : succeed_if (k1, "did not find key");
239 10 : succeed_if (k1.getName () == "user/key3/1", "wrong keyname");
240 :
241 12 : Key k2 = ks3.lookup ("user/key3/2");
242 4 : succeed_if (k2, "did not find key");
243 10 : succeed_if (k2.getName () == "user/key3/2", "wrong keyname");
244 :
245 12 : Key k3 = ks3.lookup ("user/key3/3");
246 4 : succeed_if (k3, "did not find key");
247 10 : succeed_if (k3.getName () == "user/key3/3", "wrong keyname");
248 10 : succeed_if (k3.getString () == "value", "wrong value");
249 :
250 12 : Key k4 = ks3.lookup ("user/key3/4");
251 6 : succeed_if (!k4, "Key does not exist");
252 : }
253 :
254 20 : TEST (ks, append)
255 : {
256 4 : KeySet ks1;
257 :
258 4 : KeySet ks2 (5, ckdb::keyNew ("user/key2", KEY_END), KS_END);
259 2 : ks1.append (ks2);
260 :
261 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
262 10 : KS_END);
263 2 : ks2.append (ks3);
264 2 : ks1.append (ks3);
265 2 : ks3.append (ks2);
266 :
267 4 : Key k1 ("user/key4/1", KEY_END);
268 4 : Key k2 ("user/key4/2", KEY_END);
269 4 : Key k3 ("user/key4/3", KEY_VALUE, "value", KEY_END);
270 2 : ks1.append (k1);
271 2 : ks1.append (k2);
272 2 : ks1.append (k3);
273 2 : ks2.append (k1);
274 2 : ks2.append (k2);
275 2 : ks2.append (k3);
276 2 : ks3.append (k1);
277 2 : ks3.append (k2);
278 2 : ks3.append (k3);
279 :
280 12 : KeySet ks4 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
281 10 : KS_END);
282 :
283 4 : KeySet ks5;
284 6 : std::vector<Key> v (3);
285 10 : ks5.append (v[1] = Key ("user/s/2", KEY_END));
286 8 : ks5.append (v[0] = Key ("user/s/1", KEY_END));
287 10 : ks5.append (v[2] = Key ("user/s/3", KEY_END));
288 :
289 : ks5.rewind ();
290 14 : for (ssize_t i = 0; i < ks5.size (); ++i)
291 : {
292 42 : succeed_if (*ks5.next () == *v[i], "wrong order");
293 : }
294 :
295 : // ks1.toStream();
296 : // ks2.toStream();
297 : // ks3.toStream();
298 : }
299 :
300 20 : TEST (ks, permutations)
301 : {
302 4 : vector<Key> solution;
303 6 : solution.push_back (Key ("user/s/1", KEY_END));
304 6 : solution.push_back (Key ("user/s/2", KEY_END));
305 6 : solution.push_back (Key ("user/s/3", KEY_END));
306 6 : solution.push_back (Key ("user/s/3/s", KEY_END));
307 6 : solution.push_back (Key ("user/s/3-3", KEY_END));
308 :
309 4 : vector<Key> permutation (solution);
310 :
311 240 : do
312 : {
313 480 : KeySet ks;
314 480 : ks.append (permutation[0]);
315 480 : ks.append (permutation[1]);
316 480 : ks.append (permutation[2]);
317 480 : ks.append (permutation[3]);
318 480 : ks.append (permutation[4]);
319 : ks.rewind ();
320 2640 : for (ssize_t i = 0; i < ks.size (); ++i)
321 : {
322 8400 : succeed_if (*ks.next () == *solution[i], "wrong order ");
323 : }
324 480 : } while (next_permutation (permutation.begin (), permutation.end ()));
325 :
326 6 : solution.push_back (Key ("user/s/x", KEY_END));
327 4 : permutation.push_back (solution[4]); // need a copy of same key, otherwise name is not the same string
328 4 : sort (permutation.begin (), permutation.end ());
329 :
330 720 : do
331 : {
332 1440 : KeySet ks;
333 1440 : ks.append (permutation[0]);
334 1440 : ks.append (permutation[1]);
335 1440 : ks.append (permutation[2]);
336 1440 : ks.append (permutation[3]);
337 1440 : ks.append (permutation[4]);
338 1440 : ks.append (permutation[5]);
339 : ks.rewind ();
340 7920 : for (ssize_t i = 0; i < ks.size (); ++i)
341 : {
342 25200 : succeed_if (*ks.next () == *solution[i], "wrong order");
343 : }
344 1440 : } while (next_permutation (permutation.begin (), permutation.end ()));
345 :
346 6 : solution.push_back (Key ("user/x/y", KEY_END));
347 4 : permutation.push_back (solution[5]);
348 4 : sort (permutation.begin (), permutation.end ());
349 :
350 5040 : do
351 : {
352 10080 : KeySet ks;
353 10080 : ks.append (permutation[0]);
354 10080 : ks.append (permutation[1]);
355 10080 : ks.append (permutation[2]);
356 10080 : ks.append (permutation[3]);
357 10080 : ks.append (permutation[4]);
358 10080 : ks.append (permutation[5]);
359 10080 : ks.append (permutation[6]);
360 : ks.rewind ();
361 65520 : for (ssize_t i = 0; i < ks.size (); ++i)
362 : {
363 : // note: raw pointers check the identity! It needs to be the same reference
364 211680 : succeed_if (*ks.next () == *solution[i], "wrong order");
365 : }
366 10080 : } while (next_permutation (permutation.begin (), permutation.end ()));
367 :
368 6 : solution.push_back (Key ("user/x/y/z", KEY_END));
369 4 : permutation.push_back (solution[5]);
370 4 : sort (permutation.begin (), permutation.end ());
371 :
372 20160 : do
373 : {
374 40320 : KeySet ks;
375 40320 : ks.append (permutation[0]);
376 40320 : ks.append (permutation[1]);
377 40320 : ks.append (permutation[2]);
378 40320 : ks.append (permutation[3]);
379 40320 : ks.append (permutation[4]);
380 40320 : ks.append (permutation[5]);
381 40320 : ks.append (permutation[6]);
382 40320 : ks.append (permutation[7]);
383 : ks.rewind ();
384 262080 : for (ssize_t i = 0; i < ks.size (); ++i)
385 : {
386 846720 : succeed_if (*ks.next () == *solution[i], "wrong order");
387 : }
388 40320 : } while (next_permutation (permutation.begin (), permutation.end ()));
389 : }
390 :
391 20 : TEST (ks, appendOwner)
392 : {
393 4 : KeySet ks;
394 6 : std::vector<Key> v (3);
395 10 : ks.append (v[1] = Key ("user/s/1", KEY_OWNER, "markus", KEY_END));
396 8 : ks.append (v[0] = Key ("user/s/1", KEY_END));
397 10 : ks.append (v[2] = Key ("user/s/1", KEY_OWNER, "max", KEY_END));
398 :
399 : ks.rewind ();
400 14 : for (ssize_t i = 0; i < ks.size (); ++i)
401 : {
402 42 : succeed_if (*ks.next () == *v[i], "wrong order");
403 : }
404 : }
405 :
406 20 : TEST (ks, permutateOwner)
407 : {
408 4 : vector<Key> solution;
409 6 : solution.push_back (Key ("user/s", KEY_END));
410 6 : solution.push_back (Key ("user/s", KEY_OWNER, "albert", KEY_END));
411 6 : solution.push_back (Key ("user/s", KEY_OWNER, "barbara", KEY_END));
412 :
413 4 : vector<Key> permutation (solution);
414 :
415 12 : do
416 : {
417 24 : KeySet ks;
418 24 : ks.append (permutation[0]);
419 24 : ks.append (permutation[1]);
420 24 : ks.append (permutation[2]);
421 : ks.rewind ();
422 84 : for (ssize_t i = 0; i < ks.size (); ++i)
423 : {
424 252 : succeed_if (*ks.next () == *solution[i], "wrong order");
425 : }
426 24 : } while (next_permutation (permutation.begin (), permutation.end ()));
427 :
428 6 : solution.push_back (Key ("user/s", KEY_OWNER, "markus", KEY_END));
429 4 : permutation.push_back (solution[3]); // need a copy of same key, otherwise name is not the same string
430 4 : sort (permutation.begin (), permutation.end ());
431 :
432 48 : do
433 : {
434 96 : KeySet ks;
435 96 : ks.append (permutation[0]);
436 96 : ks.append (permutation[1]);
437 96 : ks.append (permutation[2]);
438 96 : ks.append (permutation[3]);
439 : ks.rewind ();
440 432 : for (ssize_t i = 0; i < ks.size (); ++i)
441 : {
442 1344 : succeed_if (*ks.next () == *solution[i], "wrong order");
443 : }
444 96 : } while (next_permutation (permutation.begin (), permutation.end ()));
445 :
446 6 : solution.push_back (Key ("user/s", KEY_OWNER, "max", KEY_END));
447 4 : permutation.push_back (solution[4]);
448 4 : sort (permutation.begin (), permutation.end ());
449 :
450 240 : do
451 : {
452 480 : KeySet ks;
453 480 : ks.append (permutation[0]);
454 480 : ks.append (permutation[1]);
455 480 : ks.append (permutation[2]);
456 480 : ks.append (permutation[3]);
457 480 : ks.append (permutation[4]);
458 : ks.rewind ();
459 2640 : for (ssize_t i = 0; i < ks.size (); ++i)
460 : {
461 8400 : succeed_if (*ks.next () == *solution[i], "wrong order");
462 : }
463 480 : } while (next_permutation (permutation.begin (), permutation.end ()));
464 :
465 6 : solution.push_back (Key ("user/s", KEY_OWNER, "patrick", KEY_END));
466 4 : permutation.push_back (solution[5]);
467 4 : sort (permutation.begin (), permutation.end ());
468 :
469 1440 : do
470 : {
471 2880 : KeySet ks;
472 2880 : ks.append (permutation[0]);
473 2880 : ks.append (permutation[1]);
474 2880 : ks.append (permutation[2]);
475 2880 : ks.append (permutation[3]);
476 2880 : ks.append (permutation[4]);
477 2880 : ks.append (permutation[5]);
478 : ks.rewind ();
479 18720 : for (ssize_t i = 0; i < ks.size (); ++i)
480 : {
481 60480 : succeed_if (*ks.next () == *solution[i], "wrong order");
482 : }
483 2880 : } while (next_permutation (permutation.begin (), permutation.end ()));
484 : }
485 :
486 20 : TEST (ks, comparision)
487 : {
488 4 : KeySet ks0 (5, KS_END);
489 4 : KeySet ks00 (5, KS_END);
490 12 : KeySet ks1 (5, *Key ("user/a", KEY_END), *Key ("user/b", KEY_END), KS_END);
491 12 : KeySet ks11 (5, *Key ("user/a", KEY_END), *Key ("user/b", KEY_END), KS_END);
492 12 : KeySet ks2 (5, *Key ("user/a", KEY_END), *Key ("user/bb", KEY_END), KS_END);
493 12 : KeySet ks3 (5, *Key ("user/aa", KEY_END), *Key ("user/b", KEY_END), KS_END);
494 12 : KeySet ks4 (5, *Key ("user/aa", KEY_END), *Key ("user/bb", KEY_END), KS_END);
495 :
496 4 : EXPECT_EQ (ks0, ks0);
497 4 : EXPECT_EQ (ks0, ks00);
498 4 : EXPECT_EQ (ks00, ks0);
499 4 : EXPECT_EQ (ks00, ks00);
500 :
501 4 : EXPECT_EQ (ks1, ks1);
502 4 : EXPECT_EQ (ks1, ks11);
503 4 : EXPECT_EQ (ks11, ks1);
504 4 : EXPECT_EQ (ks11, ks11);
505 :
506 4 : EXPECT_EQ (ks1, ks1);
507 4 : EXPECT_NE (ks1, ks0);
508 4 : EXPECT_NE (ks1, ks2);
509 4 : EXPECT_NE (ks1, ks3);
510 4 : EXPECT_NE (ks1, ks4);
511 :
512 4 : EXPECT_EQ (ks2, ks2);
513 4 : EXPECT_NE (ks2, ks0);
514 4 : EXPECT_NE (ks2, ks1);
515 4 : EXPECT_NE (ks2, ks3);
516 4 : EXPECT_NE (ks2, ks4);
517 :
518 4 : EXPECT_EQ (ks3, ks3);
519 4 : EXPECT_NE (ks3, ks0);
520 4 : EXPECT_NE (ks3, ks1);
521 4 : EXPECT_NE (ks3, ks2);
522 4 : EXPECT_NE (ks3, ks4);
523 :
524 4 : EXPECT_EQ (ks4, ks4);
525 4 : EXPECT_NE (ks4, ks0);
526 4 : EXPECT_NE (ks4, ks1);
527 4 : EXPECT_NE (ks4, ks2);
528 4 : EXPECT_NE (ks4, ks3);
529 :
530 :
531 4 : EXPECT_EQ (ks1, ks1);
532 4 : EXPECT_NE (ks0, ks1);
533 4 : EXPECT_NE (ks2, ks1);
534 4 : EXPECT_NE (ks3, ks1);
535 4 : EXPECT_NE (ks4, ks1);
536 :
537 4 : EXPECT_EQ (ks2, ks2);
538 4 : EXPECT_NE (ks0, ks2);
539 4 : EXPECT_NE (ks1, ks2);
540 4 : EXPECT_NE (ks3, ks2);
541 4 : EXPECT_NE (ks4, ks2);
542 :
543 4 : EXPECT_EQ (ks3, ks3);
544 4 : EXPECT_NE (ks0, ks3);
545 4 : EXPECT_NE (ks1, ks3);
546 4 : EXPECT_NE (ks2, ks3);
547 4 : EXPECT_NE (ks4, ks3);
548 :
549 4 : EXPECT_EQ (ks4, ks4);
550 4 : EXPECT_NE (ks0, ks4);
551 4 : EXPECT_NE (ks1, ks4);
552 4 : EXPECT_NE (ks2, ks4);
553 4 : EXPECT_NE (ks3, ks4);
554 2 : }
555 :
556 2 : void call (KeySet ks3)
557 : {
558 14 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
559 16 : succeed_if (ks3.lookup ("user/key3/2"), "could not find key");
560 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
561 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
562 6 : succeed_if (ks3.size () == 3, "size not correct");
563 : }
564 :
565 2 : void refcall (KeySet & ks3)
566 : {
567 16 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
568 16 : succeed_if (ks3.lookup ("user/key3/2"), "could not find key");
569 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
570 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
571 6 : succeed_if (ks3.size () == 3, "size not correct");
572 : }
573 :
574 20 : TEST (ks, call)
575 : {
576 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
577 10 : KS_END);
578 16 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
579 16 : succeed_if (ks3.lookup ("user/key3/2"), "could not find key");
580 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
581 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
582 6 : succeed_if (ks3.size () == 3, "size not correct");
583 :
584 4 : call (ks3);
585 16 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
586 16 : succeed_if (ks3.lookup ("user/key3/2"), "could not find key");
587 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
588 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
589 6 : succeed_if (ks3.size () == 3, "size not correct");
590 :
591 2 : refcall (ks3);
592 16 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
593 16 : succeed_if (ks3.lookup ("user/key3/2"), "could not find key");
594 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
595 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
596 6 : succeed_if (ks3.size () == 3, "size not correct");
597 : }
598 :
599 :
600 2 : void ccall (KeySet ks3)
601 : {
602 16 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
603 16 : succeed_if (ks3.lookup ("user/key3/2", KDB_O_POP), "could not find key");
604 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
605 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
606 6 : succeed_if (ks3.size () == 2, "size not correct");
607 :
608 20 : ks3.lookup ("user/key3/1").setString ("will change");
609 6 : ks3.append (Key ("user/key3/ccall", KEY_END));
610 : }
611 :
612 2 : void refccall (KeySet & ks3)
613 : {
614 16 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
615 16 : succeed_if (ks3.lookup ("user/key3/2", KDB_O_POP), "could not find key");
616 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
617 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
618 6 : succeed_if (ks3.size () == 2, "size not correct");
619 :
620 6 : ks3.append (Key ("user/key3/refccall", KEY_END));
621 20 : ks3.lookup ("user/key3/1").setString ("will change again");
622 : }
623 :
624 20 : TEST (ks, call2)
625 : {
626 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
627 10 : KS_END);
628 16 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
629 16 : succeed_if (ks3.lookup ("user/key3/2"), "could not find key");
630 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
631 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
632 6 : succeed_if (ks3.size () == 3, "size not correct");
633 :
634 4 : ccall (ks3);
635 16 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
636 20 : succeed_if (ks3.lookup ("user/key3/1").getString () == "will change", "value did not change");
637 16 : succeed_if (ks3.lookup ("user/key3/2"), "could not find key");
638 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
639 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
640 6 : succeed_if (ks3.size () == 3, "size not correct");
641 18 : succeed_if (!ks3.lookup ("user/key3/ccall"), "key should not be there");
642 :
643 2 : refccall (ks3);
644 16 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
645 20 : succeed_if (ks3.lookup ("user/key3/1").getString () == "will change again", "value did not change");
646 18 : succeed_if (!ks3.lookup ("user/key3/2"), "could not find key");
647 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
648 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
649 16 : succeed_if (ks3.lookup ("user/key3/refccall"), "could not find key");
650 6 : succeed_if (ks3.size () == 3, "size not correct");
651 : }
652 :
653 :
654 2 : void rcopycall (KeySet ks)
655 : {
656 : // do something with keyset
657 : // (wont be one hierarchy higher)
658 6 : ks.append (Key ("user/yyy", KEY_END));
659 2 : }
660 :
661 2 : void rrefcall (KeySet & ks)
662 : {
663 : // do something with keyset
664 6 : ks.append (Key ("user/xxx", KEY_END));
665 2 : }
666 :
667 : /*Calling conventions: user need to free the keyset */
668 2 : void rcall (KeySet ks)
669 : {
670 : // do something with ks
671 :
672 2 : rrefcall (ks);
673 16 : succeed_if (ks.lookup ("user/xxx"), "could not find key");
674 :
675 :
676 4 : rcopycall (ks);
677 16 : succeed_if (ks.lookup ("user/xxx"), "could not find key");
678 18 : succeed_if (!ks.lookup ("user/yyy"), "could not find key");
679 :
680 : // don't destroy ks
681 : ks.release ();
682 : }
683 :
684 20 : TEST (ks, release)
685 : {
686 4 : KeySet ks1;
687 :
688 2 : ckdb::KeySet * ks = ks1.release ();
689 2 : ckdb::ksDel (ks);
690 :
691 4 : KeySet ks2 (5, ckdb::keyNew ("user/key2", KEY_END), KS_END);
692 :
693 2 : ks = ks2.release ();
694 2 : ckdb::ksDel (ks);
695 :
696 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
697 10 : KS_END);
698 :
699 2 : ks = ks3.release ();
700 2 : ckdb::ksDel (ks);
701 :
702 2 : ks = ckdb::ksNew (5, ckdb::keyNew ("user/abc", KEY_END), KS_END);
703 4 : rcall (ks);
704 6 : succeed_if (ckdb::ksLookupByName (ks, "user/xxx", 0) != nullptr, "could not find key");
705 2 : ckdb::ksDel (ks);
706 : }
707 :
708 20 : TEST (ks, lookupPop)
709 : {
710 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
711 10 : KS_END);
712 :
713 6 : succeed_if (ks3.size () == 3, "size not correct");
714 :
715 12 : Key k3 = ks3.lookup ("user/key3/3", KDB_O_POP);
716 10 : succeed_if (k3.getName () == "user/key3/3", "wrong keyname");
717 10 : succeed_if (k3.getString () == "value", "wrong value");
718 6 : succeed_if (ks3.size () == 2, "size not correct");
719 :
720 12 : Key k1 = ks3.lookup ("user/key3/1", KDB_O_POP);
721 10 : succeed_if (k1.getName () == "user/key3/1", "wrong keyname");
722 6 : succeed_if (ks3.size () == 1, "size not correct");
723 :
724 12 : Key k2 = ks3.lookup ("user/key3/2", KDB_O_POP);
725 10 : succeed_if (k2.getName () == "user/key3/2", "wrong keyname");
726 6 : succeed_if (ks3.size () == 0, "size not correct");
727 :
728 12 : Key k0 = ks3.lookup ("user/key3/2", KDB_O_POP);
729 6 : succeed_if (!k0, "Out of Range, no more key");
730 6 : succeed_if (ks3.size () == 0, "size not correct");
731 :
732 12 : Key kn = ks3.lookup ("user/key3/n", KDB_O_POP);
733 6 : succeed_if (!kn, "key was never in set");
734 6 : succeed_if (ks3.size () == 0, "size not correct");
735 :
736 12 : KeySet ks4 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
737 10 : KS_END);
738 :
739 14 : for (ssize_t i = ks4.size (); i > 0; i--)
740 : {
741 6 : char str[] = "user/key3/X";
742 :
743 6 : str[10] = i + '0';
744 18 : succeed_if (ks4.size () == i, "size not correct");
745 36 : Key k = ks4.lookup (str, KDB_O_POP);
746 12 : succeed_if (k, "there should be a key");
747 :
748 30 : succeed_if (k.getName () == str, str);
749 18 : succeed_if (ks4.size () == i - 1, "size not correct");
750 : }
751 :
752 12 : KeySet ks5 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
753 10 : KS_END);
754 :
755 14 : for (ssize_t i = ks5.size (); i > 0; i--)
756 : {
757 6 : char str[] = "user/key3/X";
758 :
759 6 : str[10] = i + '0';
760 12 : Key searchKey (str, KEY_END);
761 18 : succeed_if (ks5.size () == i, "size not correct");
762 :
763 12 : Key k = ks5.lookup (searchKey, KDB_O_POP);
764 12 : succeed_if (k, "there should be a key");
765 :
766 30 : succeed_if (k.getName () == str, str);
767 18 : succeed_if (ks5.size () == i - 1, "size not correct");
768 : }
769 : }
770 :
771 20 : TEST (ks, duplicate)
772 : {
773 12 : KeySet ks3 (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END), *Key ("user/key3/3", KEY_VALUE, "value", KEY_END),
774 10 : KS_END);
775 16 : succeed_if (ks3.lookup ("user/key3/1"), "could not find key");
776 16 : succeed_if (ks3.lookup ("user/key3/2"), "could not find key");
777 16 : succeed_if (ks3.lookup ("user/key3/3"), "could not find key");
778 20 : succeed_if (ks3.lookup ("user/key3/3").getString () == "value", "value not correct");
779 6 : succeed_if (ks3.size () == 3, "size not correct");
780 :
781 4 : KeySet ks4;
782 2 : ks4 = ks3;
783 16 : succeed_if (ks4.lookup ("user/key3/1"), "could not find key");
784 16 : succeed_if (ks4.lookup ("user/key3/2"), "could not find key");
785 16 : succeed_if (ks4.lookup ("user/key3/3"), "could not find key");
786 20 : succeed_if (ks4.lookup ("user/key3/3").getString () == "value", "value not correct");
787 6 : succeed_if (ks4.size () == 3, "size not correct");
788 :
789 : // ks3.toStream(stdout, 0);
790 : // ks4.toStream(stdout, 0);
791 : }
792 :
793 2 : KeySet fill_vaargs (size_t size, ...)
794 : {
795 : va_list ap;
796 2 : va_start (ap, size);
797 4 : KeySet ks (VaAlloc (size), ap);
798 2 : va_end (ap);
799 2 : return ks;
800 : }
801 :
802 8 : struct C
803 : {
804 : KeySet ks;
805 : };
806 :
807 20 : TEST (ks, move)
808 : {
809 :
810 10 : std::unique_ptr<KeySet> u1 (new KeySet (5, *Key ("user/key3/1", KEY_END), *Key ("user/key3/2", KEY_END),
811 12 : *Key ("user/key3/3", KEY_VALUE, "value", KEY_END), KS_END));
812 6 : std::unique_ptr<KeySet> u2 (std::move (u1));
813 6 : std::unique_ptr<KeySet> u3 = std::move (u1);
814 :
815 8 : std::unique_ptr<C> c1 (new C);
816 6 : std::unique_ptr<C> c2 (std::move (c1));
817 6 : std::unique_ptr<C> c3 = std::move (c1);
818 4 : c3->ks = *u3;
819 2 : }
820 :
821 20 : TEST (ks, vaargs)
822 : {
823 12 : KeySet ks = fill_vaargs (20, *Key ("user/a", KEY_END), *Key ("user/b", KEY_END), KS_END);
824 16 : succeed_if (ks.lookup ("user/a"), "could not find key");
825 16 : succeed_if (ks.lookup ("user/b"), "could not find key");
826 6 : }
|