Debian Codegraph @

collection/collection_ut.c

raw symbols SCIP
1 /*
2 COLLECTION LIBRARY
3
4 Collection unit test.
5
6 Copyright (C) Dmitri Pal <[email protected]> 2009
7
8 Collection Library is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Collection Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with Collection Library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 #define TRACE_HOME
27 #include "trace.h"
28 #include "collection.h"
29 #include "collection_tools.h"
30
31 typedef int (*test_fn)(void);
32
33 int verbose = 0;
34
35 #define COLOUT(foo) \
36 do { \
37 if (verbose) foo; \
38 } while(0)
39
40 static int dup_test(void)
41 {
42 int error = EOK;
43 struct collection_item *dup = NULL;
44 struct collection_item *item = NULL;
45 int i;
46 const char *results[] = { "value5",
47 "value5",
48 "value0",
49 "value1",
50 "value2",
51 "value3",
52 "value4",
53 "value5" };
54
55 TRACE_FLOW_ENTRY();
56
57 COLOUT(printf("\n\nDUP TEST!!!.\n\n\n"));
58 COLOUT(printf("Creating DUP collection.\n"));
59
60 if ((error = col_create_collection(&dup, "dup", 0)) ||
61 (error = col_add_str_property(dup, NULL, "key", "value0", 0)) ||
62 (error = col_add_str_property(dup, NULL, "key", "value1", 0)) ||
63 (error = col_add_str_property(dup, NULL, "key", "value2", 0)) ||
64 (error = col_add_str_property(dup, NULL, "foo", "bar", 0)) ||
65 (error = col_add_str_property(dup, NULL, "key", "value3", 0)) ||
66 (error = col_add_str_property(dup, NULL, "key", "value4", 0)) ||
67 (error = col_add_str_property(dup, NULL, "key", "value5", 0))) {
68 printf("Failed to add property. Error %d\n", error);
69 col_destroy_collection(dup);
70 return error;
71 }
72
73 COLOUT(col_debug_collection(dup, COL_TRAVERSE_DEFAULT));
74
75 if (!col_get_dup_item(NULL, NULL, "key", COL_TYPE_ANY, 10000, 0, &item) ||
76 !col_get_dup_item(dup, NULL, NULL, COL_TYPE_ANY, 10000, 0, &item) ||
77 !col_get_dup_item(dup, NULL, "key", COL_TYPE_ANY, 10000, 1, &item) ||
78 !col_get_dup_item(dup, NULL, "key", COL_TYPE_STRING, 0, 1, NULL)) {
79 /* Expected error but did not get it */
80 printf("Expected error but got success.\n");
81 col_destroy_collection(dup);
82 return EINVAL;
83 }
84
85 for (i = 0; i < 6; i++) {
86 error = col_get_dup_item(dup, NULL, "key",
87 COL_TYPE_STRING, i, 1, &item);
88 if (error) {
89 printf("Search returned error %d.\n", error);
90 col_destroy_collection(dup);
91 return error;
92 }
93
94 if (!item) {
95 printf("Item should be found - but in reality is NULL.\n");
96 col_destroy_collection(dup);
97 return EINVAL;
98 }
99
100 if (strcmp((char *)col_get_item_data(item), results[i+2]) != 0) {
101 printf("Expected %s got %s.\n", results[i+2],
102 (char *)col_get_item_data(item));
103 col_destroy_collection(dup);
104 return EINVAL;
105 }
106 }
107
108 col_destroy_collection(dup);
109
110 TRACE_FLOW_EXIT();
111
112 COLOUT(printf("\n\nEND OF DUP TEST!!!.\n\n\n"));
113
114 return EOK;
115 }
116
117
118 static int ref_collection_test(void)
119 {
120 struct collection_item *peer = NULL;
121 struct collection_item *socket = NULL;
122 struct collection_item *socket2 = NULL;
123 char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
124
125 int error = EOK;
126
127 TRACE_FLOW_STRING("ref_collection_test", "Entry.");
128
129 COLOUT(printf("\n\nREF TEST!!!.\n\n\n"));
130 COLOUT(printf("Creating PEER collection.\n"));
131
132 if ((error = col_create_collection(&peer, "peer", 0)) ||
133 (error = col_add_str_property(peer, NULL, "hostname", "peerhost.mytest.com", 0)) ||
134 /* Expect trailing zero to be truncated */
135 (error = col_add_str_property(peer, NULL, "IPv4", "10.10.10.10", 12)) ||
136 (error = col_add_str_property(peer, NULL, "IPv6", "bla:bla:bla:bla:bla:bla", 0))) {
137 printf("Failed to add property. Error %d\n", error);
138 col_destroy_collection(peer);
139 return error;
140 }
141
142 COLOUT(printf("Creating SOCKET collection.\n"));
143
144 if ((error = col_create_collection(&socket, "socket", 0)) ||
145 (error = col_add_int_property(socket, NULL, "id", 1)) ||
146 (error = col_add_long_property(socket, NULL, "packets", 100000000L)) ||
147 (error = col_add_binary_property(socket, NULL, "stack", binary_dump, sizeof(binary_dump)))) {
148 col_destroy_collection(peer);
149 col_destroy_collection(socket);
150 printf("Failed to add property. Error %d\n", error);
151 return error;
152 }
153
154 COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
155 COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
156
157 COLOUT(printf("Adding PEER collection to SOCKET collection as a reference named PEER\n"));
158
159 /* Embed peer host into the socket2 as reference */
160 error = col_add_collection_to_collection(socket, NULL, "peer", peer, COL_ADD_MODE_REFERENCE);
161 if (error) {
162 col_destroy_collection(peer);
163 col_destroy_collection(socket);
164 printf("Failed to add collection to collection. Error %d\n", error);
165 return error;
166 }
167
168 COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
169 COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
170
171 COLOUT(printf("About to destroy PEER\n"));
172 col_destroy_collection(peer);
173 COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
174
175 COLOUT(printf("About to extract PEER\n"));
176 error = col_get_collection_reference(socket, &peer, "peer");
177 if (error) {
178 col_destroy_collection(socket);
179 printf("Failed to extract collection. Error %d\n", error);
180 return error;
181 }
182
183 COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
184 COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
185 col_destroy_collection(peer);
186
187 COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
188
189 error = col_get_collection_reference(socket, &socket2, NULL);
190 if (error) {
191 col_destroy_collection(socket);
192 printf("Failed to extract collection. Error %d\n", error);
193 return error;
194 }
195
196 COLOUT(col_debug_collection(socket2, COL_TRAVERSE_DEFAULT));
197 col_destroy_collection(socket);
198 COLOUT(col_debug_collection(socket2, COL_TRAVERSE_DEFAULT));
199 col_destroy_collection(socket2);
200
201 TRACE_FLOW_NUMBER("ref_collection_test. Returning", error);
202
203 COLOUT(printf("\n\nEND OF REF TEST!!!.\n\n\n"));
204
205 return error;
206
207 }
208
209
210 static int single_collection_test(void)
211 {
212 struct collection_item *handle = NULL;
213 int error = EOK;
214
215 TRACE_FLOW_STRING("single_collection_test", "Entry.");
216
217 if ((error = col_create_collection(&handle, "string_test", 0)) ||
218 (error = col_add_str_property(handle, NULL, "property_1", "some data", 0)) ||
219 (error = col_add_str_property(handle, NULL, "property_2", "some other data", 2)) ||
220 (error = col_add_str_property(handle, NULL, "property_3", "more data", 7))) {
221 printf("Failed to add property. Error %d\n", error);
222 col_destroy_collection(handle);
223 return error;
224 }
225
226 error = col_add_str_property(handle, NULL, "property 1!", "some data", 0);
227 if (error) {
228 COLOUT(printf("Expected error adding bad property to collection %d\n", error));
229 }
230 else {
231 printf("Expected error but got success\n");
232 return -1;
233 }
234
235 error = col_add_double_property(handle, NULL, "double", 0.253545);
236 if (error) {
237 printf("Failed to add double property. Error %d\n", error);
238 col_destroy_collection(handle);
239 return error;
240 }
241
242 error = col_update_double_property(handle, "double", COL_TRAVERSE_DEFAULT, 1.999999);
243 if (error) {
244 printf("Failed to update double property. Error %d\n", error);
245 col_destroy_collection(handle);
246 return error;
247 }
248
249 COLOUT(printf("Created collection\n"));
250
251 /* Traverse collection */
252 if (verbose) {
253 error = col_debug_collection(handle, COL_TRAVERSE_DEFAULT);
254 if (error) {
255 printf("Error debugging collection %d\n", error);
256 return error;
257 }
258 error = col_print_collection(handle);
259 if (error) {
260 printf("Error printing collection %d\n", error);
261 return error;
262 }
263 }
264
265 col_destroy_collection(handle);
266
267 TRACE_FLOW_NUMBER("single_collection_test. Error: ", error);
268 return error;
269 }
270
271 static int add_collection_test(void)
272 {
273 struct collection_item *peer = NULL;
274 struct collection_item *socket = NULL;
275 char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
276
277 int error = EOK;
278
279 TRACE_FLOW_STRING("add_collection_test", "Entry.");
280
281 COLOUT(printf("\n\nADD TEST!!!.\n\n\n"));
282 COLOUT(printf("Creating PEER collection.\n"));
283
284 if ((error = col_create_collection(&peer, "peer", 0)) ||
285 (error = col_add_str_property(peer, NULL, "hostname", "peerhost.mytest.com", 0)) ||
286 /* Expect trailing zero to be truncated */
287 (error = col_add_str_property(peer, NULL, "IPv4", "10.10.10.10", 12)) ||
288 (error = col_add_str_property(peer, NULL, "IPv6", "bla:bla:bla:bla:bla:bla", 0))) {
289 printf("Failed to add property. Error %d", error);
290 col_destroy_collection(peer);
291 return error;
292 }
293
294 COLOUT(printf("Creating SOCKET collection.\n"));
295
296 if ((error = col_create_collection(&socket, "socket", 0)) ||
297 (error = col_add_int_property(socket, NULL, "id", 1)) ||
298 (error = col_add_long_property(socket, NULL, "packets", 100000000L)) ||
299 (error = col_add_binary_property(socket, NULL, "stack", binary_dump, sizeof(binary_dump)))) {
300 col_destroy_collection(peer);
301 col_destroy_collection(socket);
302 printf("Failed to add property. Error %d\n", error);
303 return error;
304 }
305
306 COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
307 COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
308
309 COLOUT(printf("Adding PEER collection to SOCKET collection as a reference named PEER\n"));
310
311 /* Embed peer host into the socket2 as reference */
312 error = col_add_collection_to_collection(socket, NULL, "peer", peer, COL_ADD_MODE_REFERENCE);
313 if (error) {
314 col_destroy_collection(peer);
315 col_destroy_collection(socket);
316 printf("Failed to create collection. Error %d\n", error);
317 return error;
318 }
319
320 COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
321 COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
322 col_destroy_collection(peer);
323 COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
324 col_destroy_collection(socket);
325 TRACE_FLOW_NUMBER("add_collection_test. Returning", error);
326 return error;
327 }
328
329 static int copy_cb(struct collection_item *item,
330 void *ext_data,
331 int *skip)
332 {
333 COLOUT(printf("INSIDE Copy Callback\n"));
334 COLOUT(col_debug_item(item));
335 COLOUT(printf("Passed in data: %s\n", (char *) ext_data));
336 if (strcmp(col_get_item_property(item, NULL), "id") == 0) *skip = 1;
337 return EOK;
338 }
339
340
341 static int mixed_collection_test(void)
342 {
343 struct collection_item *peer;
344 struct collection_item *socket1;
345 struct collection_item *socket2;
346 struct collection_item *socket3;
347 struct collection_item *event;
348 struct collection_item *host;
349 char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
350 int found = 0;
351 unsigned int class = 0;
352 char foo[] = "foo";
353
354 int error = EOK;
355
356 TRACE_FLOW_STRING("mixed_collection_test", "Entry.");
357
358 COLOUT(printf("\n\nMIXED TEST!!!.\n\n\n"));
359 COLOUT(printf("Creating PEER collection.\n"));
360
361 if ((error = col_create_collection(&peer, "peer", 0)) ||
362 (error = col_add_str_property(peer, NULL, "hostname", "peerhost.mytest.com", 0)) ||
363 /* Expect trailing zero to be truncated */
364 (error = col_add_str_property(peer, NULL, "IPv4", "10.10.10.10", 12)) ||
365 (error = col_add_str_property(peer, NULL, "IPv6", "bla:bla:bla:bla:bla:bla", 0))) {
366 printf("Failed to add property. Error %d", error);
367 col_destroy_collection(peer);
368 return error;
369 }
370
371 COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
372
373 COLOUT(printf("Creating HOST collection.\n"));
374
375 if ((error = col_create_collection(&host, "host", 0)) ||
376 (error = col_add_str_property(host, NULL, "hostname", "myhost.mytest.com", 0)) ||
377 (error = col_add_str_property(host, NULL, "IPv4", "20.20.20.20", 13)) ||
378 (error = col_add_str_property(host, NULL, "IPv6", "bla:bla:bla:bla:bla:bla", 0)) ||
379 (error = col_add_double_property(host, NULL, "double", 0.253545))) {
380 printf("Failed to add property. Error %d", error);
381 col_destroy_collection(peer);
382 col_destroy_collection(host);
383 return error;
384 }
385
386 COLOUT(col_debug_collection(host, COL_TRAVERSE_DEFAULT));
387
388 COLOUT(printf("Creating SOCKET1 collection.\n"));
389
390 if ((error = col_create_collection(&socket1, "socket1", 0)) ||
391 (error = col_add_int_property(socket1, NULL, "id", 1)) ||
392 (error = col_add_long_property(socket1, NULL, "packets", 100000000L)) ||
393 (error = col_add_binary_property(socket1, NULL, "stack", binary_dump, sizeof(binary_dump)))) {
394 col_destroy_collection(peer);
395 col_destroy_collection(host);
396 col_destroy_collection(socket1);
397 printf("Failed to add property. Error %d\n", error);
398 return error;
399 }
400
401 COLOUT(col_debug_collection(socket1, COL_TRAVERSE_DEFAULT));
402 COLOUT(printf("Creating a copy of SOCKET1 collection named SOCKET2.\n"));
403
404 error = col_copy_collection(&socket2, socket1, "socket2", COL_COPY_NORMAL);
405 if (error) {
406 col_destroy_collection(peer);
407 col_destroy_collection(host);
408 col_destroy_collection(socket1);
409 printf("Failed to copy collection. Error %d\n", error);
410 return error;
411 }
412
413 COLOUT(col_debug_collection(socket2, COL_TRAVERSE_DEFAULT));
414 COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
415
416 error = col_copy_collection_with_cb(&socket3, socket1, "socket3",
417 COL_COPY_FLATDOT, copy_cb, (void *)foo);
418 if (error) {
419 col_destroy_collection(peer);
420 col_destroy_collection(host);
421 col_destroy_collection(socket1);
422 col_destroy_collection(socket2);
423 printf("Failed to copy collection. Error %d\n", error);
424 return error;
425 }
426
427 COLOUT(col_debug_collection(socket3, COL_TRAVERSE_DEFAULT));
428 col_destroy_collection(socket3);
429
430 COLOUT(printf("Adding PEER collection to SOCKET2 collection as a reference named PEER2\n"));
431
432 /* Embed peer host into the socket2 as reference */
433 error = col_add_collection_to_collection(socket2, NULL, "peer2", peer, COL_ADD_MODE_REFERENCE);
434 if (error) {
435 col_destroy_collection(peer);
436 col_destroy_collection(host);
437 col_destroy_collection(socket1);
438 col_destroy_collection(socket2);
439 printf("Failed to create collection. Error %d\n", error);
440 return error;
441 }
442
443 COLOUT(col_debug_collection(socket2, COL_TRAVERSE_DEFAULT));
444
445 COLOUT(printf("Creating an EVENT collection.\n"));
446
447 /* Construct event */
448 error = col_create_collection(&event, "event", 0);
449 if (error) {
450 col_destroy_collection(peer);
451 col_destroy_collection(host);
452 col_destroy_collection(socket1);
453 col_destroy_collection(socket2);
454 printf("Failed to create collection. Error %d\n", error);
455 return error;
456 }
457
458 COLOUT(col_debug_collection(event, COL_TRAVERSE_DEFAULT));
459
460 COLOUT(printf("Adding HOST to EVENT.\n"));
461
462 /* Add host to event */
463 error = col_add_collection_to_collection(event, NULL, NULL, host, COL_ADD_MODE_REFERENCE);
464 if (error) {
465 col_destroy_collection(peer);
466 col_destroy_collection(host);
467 col_destroy_collection(socket1);
468 col_destroy_collection(socket2);
469 col_destroy_collection(event);
470 printf("Failed to add collections. Error %d\n", error);
471 return error;
472 }
473
474 COLOUT(col_debug_collection(event, COL_TRAVERSE_DEFAULT));
475
476 COLOUT(printf("Embed SOCKET1 into EVENT.\n"));
477 /* Donate socket1 to event */
478 /* Socket1 should not be used after this */
479 error = col_add_collection_to_collection(event, NULL, NULL, socket1, COL_ADD_MODE_EMBED);
480 if (error) {
481 col_destroy_collection(peer);
482 col_destroy_collection(host);
483 col_destroy_collection(socket1);
484 col_destroy_collection(socket2);
485 col_destroy_collection(event);
486 printf("Failed to add collections. Error %d\n", error);
487 return error;
488 }
489
490 COLOUT(printf("Traverse one level:\n"));
491 COLOUT(col_debug_collection(event, COL_TRAVERSE_ONELEVEL));
492 COLOUT(printf("Traverse ignore subcollections:\n"));
493 COLOUT(col_debug_collection(event, COL_TRAVERSE_IGNORE));
494 COLOUT(printf("Traverse normal:\n"));
495 COLOUT(col_debug_collection(event, COL_TRAVERSE_DEFAULT));
496 COLOUT(col_debug_collection(socket1, COL_TRAVERSE_DEFAULT));
497
498 COLOUT(printf("SOCKET1 MUST NOT BE USED AFTER THIS POINT!!!\n"));
499 socket1 = NULL;
500
501 COLOUT(printf("Add collection PEER as PEER1 to subcollection SOCKET1 of the EVENT.\n"));
502
503 COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
504
505 error = col_add_collection_to_collection(event, "socket1", "peer1", peer, COL_ADD_MODE_CLONE);
506 if (error) {
507 col_destroy_collection(peer);
508 col_destroy_collection(host);
509 /* No socket1 any more :) */
510 col_destroy_collection(socket2);
511 col_destroy_collection(event);
512 printf("Failed to add collections. Error %d\n", error);
513 return error;
514 }
515
516 COLOUT(col_debug_collection(event, COL_TRAVERSE_DEFAULT));
517
518 COLOUT(printf("Add property named TIMEOUT to PEER collection.\n"));
519
520 /* Add new property to the peer collection */
521 error = col_add_int_property(peer, NULL, "timeout", 5);
522 if (error) {
523 col_destroy_collection(peer);
524 col_destroy_collection(host);
525 /* No socket1 any more :) */
526 col_destroy_collection(socket2);
527 col_destroy_collection(event);
528 printf("Failed to add property. Error %d\n", error);
529 return error;
530 }
531
532 COLOUT(col_debug_collection(socket2, COL_TRAVERSE_DEFAULT));
533
534 COLOUT(printf("Add property named DELAY to PEER1 collection.\n"));
535
536 error = col_add_int_property(event, "peer1", "delay", 10);
537 if (error) {
538 col_destroy_collection(peer);
539 col_destroy_collection(host);
540 /* No socket1 any more :) */
541 col_destroy_collection(socket2);
542 col_destroy_collection(event);
543 printf("Failed to add property. Error %d\n", error);
544 return error;
545 }
546
547 COLOUT(col_debug_collection(event, COL_TRAVERSE_DEFAULT));
548 COLOUT(col_debug_collection(host, COL_TRAVERSE_DEFAULT));
549
550 COLOUT(printf("Check if property PEER1.DELAY is in the EVENT collection.\n"));
551
552 /* Check if the property in the collection */
553 found = 0;
554 error = col_is_item_in_collection(event, "peer1!delay", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
555 if (error) {
556 col_destroy_collection(peer);
557 col_destroy_collection(host);
558 /* No socket1 any more :) */
559 col_destroy_collection(socket2);
560 col_destroy_collection(event);
561 printf("Failed to check property. Error %d\n", error);
562 return error;
563 }
564
565 if (found == 1) {
566 COLOUT(printf("Property is found!\n"));
567 }
568 else {
569 COLOUT(printf("Error property is not found!\n"));
570 }
571
572
573 COLOUT(col_print_item(event, "peer1!IPv6"));
574 COLOUT(col_print_item(event, "event!socket1!peer1!IPv6"));
575 COLOUT(col_print_item(event, "event!peer1!IPv6"));
576 COLOUT(col_print_item(event, "speer1!IPv6"));
577 COLOUT(col_print_item(event, "eer1!IPv6"));
578 COLOUT(col_print_item(event, "!peer1!IPv6"));
579 COLOUT(col_print_item(event, "t!peer1!IPv6"));
580
581 /* Traverse collection */
582 if (verbose) {
583 error = col_print_collection2(event);
584 if (error) {
585 col_destroy_collection(peer);
586 col_destroy_collection(host);
587 /* No socket1 any more :) */
588 col_destroy_collection(socket2);
589 col_destroy_collection(event);
590 printf("Error printing collection %d\n", error);
591 return error;
592 }
593 }
594
595 COLOUT(printf("Delete property PEER1!DELAY from the EVENT collection.\n"));
596
597 error = col_delete_property(event, "peer1!delay", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT);
598 if (error) {
599 col_destroy_collection(peer);
600 col_destroy_collection(host);
601 /* No socket1 any more :) */
602 col_destroy_collection(socket2);
603 col_destroy_collection(event);
604 printf("Failed to delete property. Error %d\n", error);
605 return error;
606 }
607
608 COLOUT(printf("Printing EVENT.\n"));
609
610 /* Traverse collection */
611 if (verbose) {
612 error = col_print_collection2(event);
613 if (error) {
614 col_destroy_collection(peer);
615 col_destroy_collection(host);
616 /* No socket1 any more :) */
617 col_destroy_collection(socket2);
618 col_destroy_collection(event);
619 printf("Error printing collection %d\n", error);
620 return error;
621 }
622 }
623
624 COLOUT(printf("Debugging EVENT.\n"));
625 if (verbose) {
626 error = col_debug_collection(event, COL_TRAVERSE_DEFAULT);
627 if (error) {
628 col_destroy_collection(peer);
629 col_destroy_collection(host);
630 /* No socket1 any more :) */
631 col_destroy_collection(socket2);
632 col_destroy_collection(event);
633 printf("Error printing collection %d\n", error);
634 return error;
635 }
636 }
637 COLOUT(printf("Cleanup of the collections PEER, HOST and SOCKET2.\n"));
638
639 /* Destroy a referenced collection */
640 col_destroy_collection(peer);
641 col_destroy_collection(host);
642 col_destroy_collection(socket2);
643
644 COLOUT(printf("Printing EVENT again.\n"));
645
646 /* Traverse collection again - peer should still be there */
647 if (verbose) {
648 error = col_print_collection(event);
649 if (error) {
650 col_destroy_collection(event);
651 printf("Error printing collection %d\n", error);
652 return error;
653 }
654 }
655
656 COLOUT(printf("Debugging EVENT again.\n"));
657
658 if (verbose) {
659 error = col_debug_collection(event, COL_TRAVERSE_DEFAULT);
660 if (error) {
661 col_destroy_collection(event);
662 printf("Error printing collection %d\n", error);
663 return error;
664 }
665 }
666
667 COLOUT(printf("Attempt to add property to a referenced collection.\n"));
668
669 error = col_add_int_property(event, "host", "session", 500);
670 if (error) {
671 col_destroy_collection(event);
672 printf("Error was NOT able to add property to a referenced collection %d.\n", error);
673 return error;
674 }
675
676 COLOUT(printf("Attempt to delete non-existent property.\n"));
677
678 /* Can't delete non exitent property */
679 error = col_delete_property(event, "host.host", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT);
680 if (error == 0) {
681 col_destroy_collection(event);
682 printf("Error was able to delete property that does not exist.\n");
683 return -1;
684 }
685 else COLOUT(printf("Expected error %d\n", error));
686
687 /* Set collection class */
688 error = col_set_collection_class(event, 2);
689 if (error != 0) {
690 col_destroy_collection(event);
691 printf("Error was NOT able to set class.\n");
692 return error;
693 }
694
695 error = col_get_collection_class(event, &class);
696 if (error != 0) {
697 col_destroy_collection(event);
698 printf("Error was NOT able to get class.\n");
699 return error;
700 }
701 else COLOUT(printf("Class = %d\n", class));
702
703 if (col_is_of_class(event, 2)) {
704 COLOUT(printf("Class mathced!\n"));
705 }
706 else {
707 col_destroy_collection(event);
708 printf("Error - bad class.\n");
709 return -1;
710 }
711
712 COLOUT(printf("Done. Cleaning...\n"));
713
714 col_destroy_collection(event);
715
716 COLOUT(printf("Exit.\n"));
717 TRACE_FLOW_NUMBER("add_collection_test. Returning", EOK);
718 return EOK;
719 }
720
721
722 static int iterator_test(void)
723 {
724 struct collection_item *peer = NULL;
725 struct collection_item *initial = NULL;
726
727 struct collection_item *socket1 = NULL;
728 struct collection_item *socket2 = NULL;
729 struct collection_item *socket3 = NULL;
730 struct collection_iterator *iterator = NULL;
731 int error = EOK;
732 struct collection_item *item = NULL;
733 char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
734 int depth = 0;
735 int idepth = 0;
736 int len = 0;
737 int i;
738 uint64_t hash1, hash2;
739 int rwnd = 0;
740
741 COLOUT(printf("\n\n==== ITERATOR TEST ====\n\n"));
742
743 if ((error = col_create_collection(&initial, "strater", 0)) ||
744 (error = col_create_collection(&peer, "peer", 0)) ||
745 (error = col_add_str_property(initial, NULL, "hostname", "peerhost.mytest.com", 0)) ||
746 /* Expect trailing zero to be truncated */
747 (error = col_add_str_property(initial, NULL, "IPv4", "10.10.10.10", 12)) ||
748 (error = col_add_str_property(initial, NULL, "IPv6", "bla:bla:bla:bla:bla:bla", 0)) ||
749 (error = col_add_collection_to_collection(peer, NULL, NULL, initial, COL_ADD_MODE_FLAT))) {
750 printf("Failed to add property. Error %d", error);
751 col_destroy_collection(peer);
752 col_destroy_collection(initial);
753 return error;
754 }
755
756 col_destroy_collection(initial);
757
758 if ((error = col_create_collection(&socket1, "socket", 0)) ||
759 (error = col_add_int_property(socket1, NULL, "id", 1)) ||
760 (error = col_add_long_property(socket1, NULL, "packets", 100000000L)) ||
761 (error = col_add_binary_property(socket1, NULL, "stack", binary_dump, sizeof(binary_dump)))) {
762 col_destroy_collection(peer);
763 col_destroy_collection(socket1);
764 printf("Failed to add property. Error %d\n", error);
765 return error;
766 }
767
768 if ((error = col_create_collection(&socket2, "socket", 0)) ||
769 (error = col_add_int_property(socket2, NULL, "id", 2)) ||
770 (error = col_add_long_property(socket2, NULL, "packets", 200000000L)) ||
771 (error = col_add_binary_property(socket2, NULL, "queue", binary_dump, sizeof(binary_dump)))) {
772 col_destroy_collection(peer);
773 col_destroy_collection(socket1);
774 col_destroy_collection(socket2);
775 printf("Failed to add property. Error %d\n", error);
776 return error;
777 }
778
779 if ((error = col_create_collection(&socket3, "socket", 0))) {
780 col_destroy_collection(peer);
781 col_destroy_collection(socket1);
782 col_destroy_collection(socket2);
783 printf("Failed to add property. Error %d\n", error);
784 return error;
785 }
786
787 error = col_add_collection_to_collection(peer, NULL, "first", socket1, COL_ADD_MODE_REFERENCE);
788 if (error) {
789 col_destroy_collection(peer);
790 col_destroy_collection(socket1);
791 col_destroy_collection(socket2);
792 col_destroy_collection(socket3);
793 printf("Failed to add collection to collection. Error %d\n", error);
794 return error;
795 }
796
797 error = col_add_collection_to_collection(peer, NULL, "second", socket2, COL_ADD_MODE_EMBED);
798 if (error) {
799 col_destroy_collection(peer);
800 col_destroy_collection(socket1);
801 col_destroy_collection(socket2);
802 col_destroy_collection(socket3);
803 printf("Failed to add collection to collection. Error %d\n", error);
804 return error;
805 }
806
807 error = col_add_collection_to_collection(peer, NULL, "third", socket3, COL_ADD_MODE_EMBED);
808 if (error) {
809 col_destroy_collection(peer);
810 col_destroy_collection(socket1);
811 col_destroy_collection(socket3);
812 printf("Failed to add collection to collection. Error %d\n", error);
813 return error;
814 }
815
816 error = col_add_collection_to_collection(peer, NULL, "forth", socket1, COL_ADD_MODE_FLATDOT);
817 if (error) {
818 col_destroy_collection(peer);
819 col_destroy_collection(socket1);
820 printf("Failed to add collection to collection. Error %d\n", error);
821 return error;
822 }
823
824 error = col_add_collection_to_collection(peer, NULL, NULL, socket1, COL_ADD_MODE_FLATDOT);
825 if (error) {
826 col_destroy_collection(peer);
827 col_destroy_collection(socket1);
828 printf("Failed to add collection to collection. Error %d\n", error);
829 return error;
830 }
831
832 col_destroy_collection(socket1);
833
834 /* Bind iterator */
835 error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_DEFAULT);
836 if (error) {
837 printf("Error (bind): %d\n", error);
838 col_destroy_collection(peer);
839 return error;
840 }
841
842 COLOUT(printf("\n\nCollection (traverse default):\n\n"));
843 COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
844
845 COLOUT(printf("\n\nCollection (traverse flat):\n\n"));
846 COLOUT(col_debug_collection(peer, COL_TRAVERSE_FLAT | COL_TRAVERSE_END));
847
848 COLOUT(printf("\n\nIteration (1):\n\n"));
849
850 do {
851
852
853 /* Loop through a collection */
854 error = col_iterate_collection(iterator, &item);
855 if (error) {
856 printf("Error (iterate): %d\n", error);
857 col_unbind_iterator(iterator);
858 col_destroy_collection(peer);
859 return error;
860 }
861
862 /* Are we done ? */
863 if (item == NULL) break;
864
865 depth = 0;
866 col_get_item_depth(iterator, &depth);
867 idepth = 0;
868 col_get_iterator_depth(iterator, &idepth);
869
870
871 COLOUT(printf("%*sProperty (%s), type = %d, data size = %d depth = %d idepth = %d\n",
872 depth * 4, "",
873 col_get_item_property(item, NULL),
874 col_get_item_type(item),
875 col_get_item_length(item),
876 depth,
877 idepth));
878
879 if ((strcmp(col_get_item_property(item, NULL), "id")==0) &&
880 (*((int *)(col_get_item_data(item))) == 1)) {
881 COLOUT(printf("\n\nFound property we need - go up!!!\n\n\n"));
882
883 /* This should work! */
884 error = col_iterate_up(iterator, 1);
885 if (error) {
886 printf("We expected success but got error %d\n", error);
887 col_unbind_iterator(iterator);
888 col_destroy_collection(peer);
889 return error;
890 }
891
892 if ((error = col_modify_str_item(item, "id2", "test", 0)) ||
893 ((verbose) && (error = col_debug_item(item))) ||
894 (error = col_modify_str_item(item, NULL, "test", 2)) ||
895 ((verbose) && (error = col_debug_item(item))) ||
896 (error = col_modify_binary_item(item, NULL, binary_dump, sizeof(binary_dump))) ||
897 ((verbose) && (error = col_debug_item(item))) ||
898 (error = col_modify_bool_item(item, NULL, 1)) ||
899 ((verbose) && (error = col_debug_item(item))) ||
900 (error = col_modify_int_item(item, "int", 1)) ||
901 ((verbose) && (error = col_debug_item(item))) ||
902 (error = col_modify_long_item(item, "long", 1000000000L)) ||
903 ((verbose) && (error = col_debug_item(item))) ||
904 (error = col_modify_ulong_item(item, "ulong", 4000000000UL)) ||
905 ((verbose) && (error = col_debug_item(item))) ||
906 (error = col_modify_unsigned_item(item, "unsigned", 4000000000U)) ||
907 ((verbose) && (error = col_debug_item(item))) ||
908 (error = col_modify_double_item(item, "double", -1.1)) ||
909 ((verbose) && (error = col_debug_item(item)))) {
910 printf("Failed to change property.\n");
911 col_unbind_iterator(iterator);
912 col_destroy_collection(peer);
913 return error;
914 }
915
916 COLOUT(printf("Item name: %s\n", col_get_item_property(item, NULL)));
917 COLOUT(printf("Item hash: %lu\n", (unsigned long int)col_get_item_hash(item)));
918 error = col_modify_item_property(item, "new_name");
919 if (error) {
920 printf("We expected success but got error %d\n", error);
921 col_unbind_iterator(iterator);
922 col_destroy_collection(peer);
923 return error;
924 }
925 len = 0;
926 COLOUT(printf("Item name: %s\n", col_get_item_property(item, &len)));
927 COLOUT(printf("Item hash: %lu\n", (unsigned long int)col_get_item_hash(item)));
928 COLOUT(printf("Item length: %d\n", len));
929
930 len = 0;
931 hash1 = col_make_hash("new_name", 0, &len);
932 COLOUT(printf("String name: %s\n", "new_name"));
933 COLOUT(printf("String hash: %lu\n", (unsigned long int)hash1));
934 COLOUT(printf("String length: %d\n", len));
935
936 len = 0;
937 hash2 = col_make_hash("new_name_suffix", 8, &len);
938 COLOUT(printf("String name: %.*s\n", len, "new_name_suffix"));
939 COLOUT(printf("String hash: %lu\n", (unsigned long int)hash2));
940 COLOUT(printf("String length: %d\n", len));
941 if (hash1 != hash2) {
942 printf("Hash calculation failed\n");
943 col_unbind_iterator(iterator);
944 col_destroy_collection(peer);
945 return EINVAL;
946 }
947
948 hash2 = col_make_hash("new_name", 8, &len);
949 COLOUT(printf("String name: %.*s\n", len, "new_name"));
950 COLOUT(printf("String hash: %lu\n", (unsigned long int)hash2));
951 COLOUT(printf("String length: %d\n", len));
952 if (hash1 != hash2) {
953 printf("Hash calculation failed\n");
954 col_unbind_iterator(iterator);
955 col_destroy_collection(peer);
956 return EINVAL;
957 }
958
959 }
960 }
961 while(1);
962
963 col_unbind_iterator(iterator);
964
965 /* Bind iterator again in flat mode */
966 error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_FLAT);
967 if (error) {
968 printf("Error (bind): %d\n", error);
969 col_destroy_collection(peer);
970 return error;
971 }
972
973 COLOUT(printf("\n\nIteration (2 - flat):\n\n"));
974
975 do {
976
977 /* Loop through a collection */
978 error = col_iterate_collection(iterator, &item);
979 if (error) {
980 printf("Error (iterate): %d\n", error);
981 col_destroy_collection(peer);
982 col_unbind_iterator(iterator);
983 return error;
984 }
985
986 /* Are we done ? */
987 if (item == NULL) break;
988
989 depth = 0;
990 col_get_item_depth(iterator, &depth);
991 COLOUT(printf("%*s", depth * 4, ""));
992 COLOUT(col_debug_item(item));
993
994 if ((strcmp(col_get_item_property(item, NULL), "queue") == 0) &&
995 (rwnd == 0)) {
996 COLOUT(printf("Rewinding iterator...\n"));
997 col_rewind_iterator(iterator);
998 rwnd++;
999 }
1000
1001 }
1002 while(1);
1003
1004 /* Do not forget to unbind iterator - otherwise there will be a leak */
1005 col_unbind_iterator(iterator);
1006
1007 /* Bind iterator again in flat mode */
1008 error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_FLAT | COL_TRAVERSE_END);
1009 if (error) {
1010 printf("Error (bind): %d\n", error);
1011 col_destroy_collection(peer);
1012 return error;
1013 }
1014
1015 COLOUT(printf("\n\nIteration (3 flat with end):\n\n"));
1016
1017 do {
1018
1019 /* Loop through a collection */
1020 error = col_iterate_collection(iterator, &item);
1021 if (error) {
1022 printf("Error (iterate): %d\n", error);
1023 col_destroy_collection(peer);
1024 col_unbind_iterator(iterator);
1025 return error;
1026 }
1027
1028 /* Are we done ? */
1029 if (item == NULL) break;
1030
1031 depth = 0;
1032 col_get_item_depth(iterator, &depth);
1033 COLOUT(printf("%*s", depth * 4, ""));
1034 COLOUT(col_debug_item(item));
1035
1036 }
1037 while(1);
1038
1039 /* Do not forget to unbind iterator - otherwise there will be a leak */
1040 col_unbind_iterator(iterator);
1041
1042 /* Bind iterator again in flat mode */
1043 error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_DEFAULT | COL_TRAVERSE_END);
1044 if (error) {
1045 printf("Error (bind): %d\n", error);
1046 col_destroy_collection(peer);
1047 return error;
1048 }
1049
1050 COLOUT(printf("\n\nIteration (4 default with end):\n\n"));
1051
1052 do {
1053
1054 /* Loop through a collection */
1055 error = col_iterate_collection(iterator, &item);
1056 if (error) {
1057 printf("Error (iterate): %d\n", error);
1058 col_destroy_collection(peer);
1059 col_unbind_iterator(iterator);
1060 return error;
1061 }
1062
1063 /* Are we done ? */
1064 if (item == NULL) break;
1065
1066 depth = 0;
1067 col_get_item_depth(iterator, &depth);
1068 COLOUT(printf("%*s", depth * 4, ""));
1069 COLOUT(col_debug_item(item));
1070
1071 }
1072 while(1);
1073
1074 /* Do not forget to unbind iterator - otherwise there will be a leak */
1075 col_unbind_iterator(iterator);
1076
1077 /* Bind iterator again in flat mode */
1078 error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_SHOWSUB | COL_TRAVERSE_END);
1079 if (error) {
1080 printf("Error (bind): %d\n", error);
1081 col_destroy_collection(peer);
1082 return error;
1083 }
1084
1085
1086 COLOUT(printf("\n\nIteration (5 show headers and references with end):\n\n"));
1087
1088 do {
1089
1090 /* Loop through a collection */
1091 error = col_iterate_collection(iterator, &item);
1092 if (error) {
1093 printf("Error (iterate): %d\n", error);
1094 col_destroy_collection(peer);
1095 col_unbind_iterator(iterator);
1096 return error;
1097 }
1098
1099 /* Are we done ? */
1100 if (item == NULL) break;
1101
1102 depth = 0;
1103 col_get_item_depth(iterator, &depth);
1104 COLOUT(printf("%*s", depth * 4, ""));
1105 COLOUT(col_debug_item(item));
1106
1107 }
1108 while(1);
1109
1110 /* Do not forget to unbind iterator - otherwise there will be a leak */
1111 col_unbind_iterator(iterator);
1112
1113 /* Bind iterator again in flat mode */
1114 error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_SHOWSUB);
1115 if (error) {
1116 printf("Error (bind): %d\n", error);
1117 col_destroy_collection(peer);
1118 return error;
1119 }
1120
1121
1122 COLOUT(printf("\n\nIteration (6 show headers and references no END):\n\n"));
1123
1124 do {
1125
1126 /* Loop through a collection */
1127 error = col_iterate_collection(iterator, &item);
1128 if (error) {
1129 printf("Error (iterate): %d\n", error);
1130 col_destroy_collection(peer);
1131 col_unbind_iterator(iterator);
1132 return error;
1133 }
1134
1135 /* Are we done ? */
1136 if (item == NULL) break;
1137
1138 depth = 0;
1139 col_get_item_depth(iterator, &depth);
1140 COLOUT(printf("%*s", depth * 4, ""));
1141 COLOUT(col_debug_item(item));
1142
1143 }
1144 while(1);
1145
1146 /* Do not forget to unbind iterator - otherwise there will be a leak */
1147 col_unbind_iterator(iterator);
1148
1149 /* Bind iterator again in flat mode */
1150 error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_ONLYSUB);
1151 if (error) {
1152 printf("Error (bind): %d\n", error);
1153 col_destroy_collection(peer);
1154 return error;
1155 }
1156
1157 COLOUT(printf("\n\nIteration (7 show headers only no END):\n\n"));
1158
1159 do {
1160
1161 /* Loop through a collection */
1162 error = col_iterate_collection(iterator, &item);
1163 if (error) {
1164 printf("Error (iterate): %d\n", error);
1165 col_destroy_collection(peer);
1166 col_unbind_iterator(iterator);
1167 return error;
1168 }
1169
1170 /* Are we done ? */
1171 if (item == NULL) break;
1172
1173 depth = 0;
1174 col_get_item_depth(iterator, &depth);
1175 COLOUT(printf("%*s", depth * 4, ""));
1176 COLOUT(col_debug_item(item));
1177
1178 }
1179 while(1);
1180
1181 /* Do not forget to unbind iterator - otherwise there will be a leak */
1182 col_unbind_iterator(iterator);
1183
1184
1185 /* Bind iterator */
1186 error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_DEFAULT);
1187 if (error) {
1188 printf("Error (bind): %d\n", error);
1189 col_destroy_collection(peer);
1190 return error;
1191 }
1192
1193 COLOUT(printf("\n\nIterate up test:\n\n"));
1194
1195 do {
1196
1197 /* Loop through a collection */
1198 error = col_iterate_collection(iterator, &item);
1199 if (error) {
1200 printf("Error (iterate): %d\n", error);
1201 col_unbind_iterator(iterator);
1202 col_destroy_collection(peer);
1203 return error;
1204 }
1205
1206 /* Are we done ? */
1207 if (item == NULL) break;
1208
1209 depth = 0;
1210 col_get_item_depth(iterator, &depth);
1211 idepth = 0;
1212 col_get_iterator_depth(iterator, &idepth);
1213
1214
1215 COLOUT(printf("%*sProperty (%s), type = %d, data size = %d depth = %d idepth = %d\n",
1216 depth * 4, "",
1217 col_get_item_property(item, NULL),
1218 col_get_item_type(item),
1219 col_get_item_length(item),
1220 depth,
1221 idepth));
1222
1223 if (strcmp(col_get_item_property(item, NULL), "queue") == 0) {
1224
1225 COLOUT(printf("\n\nFound property we need - go up!!!\n"));
1226 COLOUT(printf("Expect bail out of collection processing.\n\n"));
1227
1228 /* This should work! */
1229 error = col_iterate_up(iterator, 10);
1230 if (error) {
1231 printf("We expected success but got error %d\n", error);
1232 col_unbind_iterator(iterator);
1233 col_destroy_collection(peer);
1234 return error;
1235 }
1236
1237 }
1238 }
1239 while(1);
1240
1241 col_unbind_iterator(iterator);
1242
1243 /* Bind iterator again in flat mode */
1244 error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_FLAT | COL_TRAVERSE_END);
1245 if (error) {
1246 printf("Error (bind): %d\n", error);
1247 col_destroy_collection(peer);
1248 return error;
1249 }
1250
1251 COLOUT(printf("\n\nCircled looping:\n\n"));
1252
1253 for (i = 0; i < 200; i++) {
1254 /* Loop through a collection */
1255 error = col_iterate_collection(iterator, &item);
1256 if (error) {
1257 printf("Error (iterate): %d\n", error);
1258 col_destroy_collection(peer);
1259 col_unbind_iterator(iterator);
1260 return error;
1261 }
1262
1263 /* Are we done ? */
1264 if (item == NULL) {
1265 COLOUT(printf("Reached end.\n\n"));
1266 }
1267 else {
1268 depth = 0;
1269 col_get_item_depth(iterator, &depth);
1270 COLOUT(printf("%*s", depth * 4, ""));
1271 COLOUT(col_debug_item(item));
1272 }
1273 }
1274
1275 /* Do not forget to unbind iterator - otherwise there will be a leak */
1276 col_unbind_iterator(iterator);
1277
1278 /* Bind iterator again in flat mode */
1279 error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_FLAT | COL_TRAVERSE_END);
1280 if (error) {
1281 printf("Error (bind): %d\n", error);
1282 col_destroy_collection(peer);
1283 return error;
1284 }
1285
1286 COLOUT(printf("\n\nCircled looping with pin:\n\n"));
1287
1288 do {
1289 /* Loop through a collection */
1290 error = col_iterate_collection(iterator, &item);
1291 if (error) {
1292 printf("Error (iterate): %d\n", error);
1293 col_destroy_collection(peer);
1294 col_unbind_iterator(iterator);
1295 return error;
1296 }
1297
1298 if (strcmp(col_get_item_property(item, NULL), "queue") == 0) {
1299 /* Make it a new looping point */
1300 col_pin_iterator(iterator);
1301 COLOUT(printf("Found pin point.\n\n"));
1302 break;
1303 }
1304 /* Are we done ? */
1305 if (item == NULL) {
1306 printf("Unexpected end.\n\n");
1307 col_destroy_collection(peer);
1308 col_unbind_iterator(iterator);
1309 return EINVAL;
1310 }
1311 else {
1312 depth = 0;
1313 col_get_item_depth(iterator, &depth);
1314 COLOUT(printf("%*s", depth * 4, ""));
1315 COLOUT(col_debug_item(item));
1316 }
1317 }
1318 while(1);
1319
1320 /* Second loop around the pin point */
1321 for (i = 0; i < 200; i++) {
1322 /* Loop through a collection */
1323 error = col_iterate_collection(iterator, &item);
1324 if (error) {
1325 printf("Error (iterate): %d\n", error);
1326 col_destroy_collection(peer);
1327 col_unbind_iterator(iterator);
1328 return error;
1329 }
1330
1331 /* Are we done ? */
1332 if (item == NULL) {
1333 COLOUT(printf("Reached end.\n\n"));
1334 }
1335 else {
1336 depth = 0;
1337 col_get_item_depth(iterator, &depth);
1338 COLOUT(printf("%*s", depth * 4, ""));
1339 COLOUT(col_debug_item(item));
1340 }
1341 }
1342
1343 /* Do not forget to unbind iterator - otherwise there will be a leak */
1344 col_unbind_iterator(iterator);
1345
1346
1347 /* Bind iterator again in flat mode */
1348 error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_DEFAULT | COL_TRAVERSE_END);
1349 if (error) {
1350 printf("Error (bind): %d\n", error);
1351 col_destroy_collection(peer);
1352 return error;
1353 }
1354
1355 COLOUT(printf("\n\nCircled looping with pin (default):\n\n"));
1356
1357 do {
1358 /* Loop through a collection */
1359 error = col_iterate_collection(iterator, &item);
1360 if (error) {
1361 printf("Error (iterate): %d\n", error);
1362 col_destroy_collection(peer);
1363 col_unbind_iterator(iterator);
1364 return error;
1365 }
1366
1367 if (strcmp(col_get_item_property(item, NULL), "queue") == 0) {
1368 /* Make it a new looping point */
1369 col_pin_iterator(iterator);
1370 COLOUT(printf("Found pin point.\n\n"));
1371 break;
1372 }
1373 /* Are we done ? */
1374 if (item == NULL) {
1375 printf("Unexpected end.\n\n");
1376 col_destroy_collection(peer);
1377 col_unbind_iterator(iterator);
1378 return EINVAL;
1379 }
1380 else {
1381 depth = 0;
1382 col_get_item_depth(iterator, &depth);
1383 COLOUT(printf("%*s", depth * 4, ""));
1384 COLOUT(col_debug_item(item));
1385 }
1386 }
1387 while(1);
1388
1389 /* Second loop around the pin point */
1390 for (i = 0; i < 200; i++) {
1391 /* Loop through a collection */
1392 error = col_iterate_collection(iterator, &item);
1393 if (error) {
1394 printf("Error (iterate): %d\n", error);
1395 col_destroy_collection(peer);
1396 col_unbind_iterator(iterator);
1397 return error;
1398 }
1399
1400 /* Are we done ? */
1401 if (item == NULL) {
1402 COLOUT(printf("Reached end.\n\n"));
1403 }
1404 else {
1405 depth = 0;
1406 col_get_item_depth(iterator, &depth);
1407 COLOUT(printf("%*s", depth * 4, ""));
1408 COLOUT(col_debug_item(item));
1409 }
1410 }
1411
1412 /* Do not forget to unbind iterator - otherwise there will be a leak */
1413 col_unbind_iterator(iterator);
1414 col_destroy_collection(peer);
1415
1416 return EOK;
1417 }
1418
1419 static int validate_collection(struct collection_item *col,
1420 const char *varray[][2])
1421 {
1422 struct collection_iterator *iterator = NULL;
1423 int error = EOK;
1424 struct collection_item *item = NULL;
1425 int count = -1;
1426
1427 COLOUT(printf("\n\n==== Validating collection ====\n\n"));
1428
1429 /* Bind iterator */
1430 error = col_bind_iterator(&iterator, col, COL_TRAVERSE_DEFAULT);
1431 if (error) {
1432 COLOUT(printf("Error (bind): %d\n", error));
1433 return error;
1434 }
1435
1436 do {
1437
1438 count++;
1439
1440 /* Loop through a collection */
1441 error = col_iterate_collection(iterator, &item);
1442 if (error) {
1443 COLOUT(printf("Error (iterate): %d\n", error));
1444 col_unbind_iterator(iterator);
1445 return error;
1446 }
1447
1448 /* Are we done ? */
1449 if (item == NULL) break;
1450
1451 /* Set count when we skip header */
1452 if (col_get_item_type(item) == COL_TYPE_COLLECTION) {
1453 count = -1;
1454 continue;
1455 }
1456
1457 if (varray[count][0] == NULL) {
1458 /* We have reached the end of the array but not the collection.
1459 * This means the test failed. Return error.
1460 */
1461 COLOUT(printf("Got extra item in the collection: %s=%s\n",
1462 col_get_item_property(item, NULL),
1463 (char *)(col_get_item_data(item))));
1464 col_unbind_iterator(iterator);
1465 return -1;
1466 }
1467
1468 if ((strcmp(col_get_item_property(item, NULL),
1469 varray[count][0]) != 0) ||
1470 (strcmp((char *)(col_get_item_data(item)),
1471 varray[count][1]) != 0 )) {
1472 COLOUT(printf("Expected %s=\"%s\" got %s=\"%s\" \n",
1473 varray[count][0],
1474 varray[count][1],
1475 col_get_item_property(item, NULL),
1476 (char *)(col_get_item_data(item))));
1477 col_unbind_iterator(iterator);
1478 return -1;
1479 }
1480 }
1481 while(1);
1482
1483 col_unbind_iterator(iterator);
1484
1485 if (varray[count][0] != NULL) {
1486 COLOUT(printf("Expected end got %s=\"%s\"\n", varray[count][0],
1487 varray[count][1]));
1488 return -1;
1489 }
1490
1491 COLOUT(printf("\n\n==== Validating collection - OK ====\n\n"));
1492 return EOK;
1493 }
1494
1495
1496 static int insert_extract_test(void)
1497 {
1498 struct collection_item *col;
1499 struct collection_item *col2;
1500 int error = EOK;
1501 struct collection_item *item = NULL;
1502 const char *varray1[][2] = { { "property_-1", "value_-1" },
1503 { "property0", "value0lastdup" },
1504 { "property0_5", "value0_5" },
1505 { "property1", "value1update" },
1506 { "second", "second" },
1507 { "property1_5", "value1_5" },
1508 { "property2", "value2" },
1509 { "property10", "value10" },
1510 { "property10", "value10lastdup" },
1511 { "property_-2", "value-2moved_to_bottom" },
1512 { "property0", "extra_1" },
1513 { "property100", "value100" },
1514 { NULL, NULL } };
1515
1516 const char *varray2[][2] = { { "property_-1_5","value_-1_5" },
1517 { "property1_6", "value_1_6_moved_to_front" },
1518 { "property0", "very_last" },
1519 { "property10", "value10dup" },
1520 { "property_-0_5", "value_-0_5" },
1521 { "property0", "value0firstdupupdate" },
1522 { "property0", "value0" },
1523 { "property0", "before 0_5" },
1524 { "property0", "value0middledup" },
1525 { "property0", "extra_2" },
1526 { "property0", "after_extra2" },
1527 { "property0", "before_extra2" },
1528 { NULL, NULL } };
1529
1530
1531 COLOUT(printf("\n\n==== INSERTION TEST ====\n\n"));
1532
1533 if ((error = col_create_collection(&col, "insertion", 0)) ||
1534 (error = col_insert_str_property(col, NULL, COL_DSP_END,
1535 NULL, 0, COL_INSERT_NOCHECK,
1536 "property1", "value1", 0)) ||
1537 (error = col_insert_str_property(col, NULL, COL_DSP_INDEX,
1538 NULL, 1, COL_INSERT_NOCHECK,
1539 "second", "second", 0)) ||
1540 (error = col_insert_str_property(col, NULL, COL_DSP_END,
1541 NULL, 0, COL_INSERT_NOCHECK,
1542 "property2", "value2", 0)) ||
1543 (error = col_insert_str_property(col, NULL, COL_DSP_FRONT,
1544 NULL, 0, COL_INSERT_NOCHECK,
1545 "property0", "value0", 0)) ||
1546 (error = col_insert_str_property(col, NULL, COL_DSP_BEFORE,
1547 "property0", 0, COL_INSERT_NOCHECK,
1548 "property_-1", "value_-1", 0)) ||
1549 (error = col_insert_str_property(col, NULL, COL_DSP_BEFORE,
1550 "property1", 0, COL_INSERT_NOCHECK,
1551 "property0_5", "value0_5", 0)) ||
1552 (error = col_insert_str_property(col, NULL, COL_DSP_BEFORE,
1553 "property2", 0, COL_INSERT_NOCHECK,
1554 "property1_5", "value1_5", 0)) ||
1555 (error = col_insert_str_property(col, NULL, COL_DSP_AFTER,
1556 "property_-1", 0, COL_INSERT_NOCHECK,
1557 "property_-0_5", "value_-0_5", 0)) ||
1558 (error = col_insert_str_property(col, NULL, COL_DSP_AFTER,
1559 "property1_5", 0, COL_INSERT_NOCHECK,
1560 "property1_6", "value1_6", 0)) ||
1561 (error = col_insert_str_property(col, NULL, COL_DSP_INDEX,
1562 NULL, 10, COL_INSERT_NOCHECK,
1563 "property10", "value10", 0)) ||
1564 (error = col_insert_str_property(col, NULL, COL_DSP_INDEX,
1565 NULL, 0, COL_INSERT_NOCHECK,
1566 "property_-2", "value_-2", 0)) ||
1567 (error = col_insert_str_property(col, NULL, COL_DSP_INDEX,
1568 NULL, 1, COL_INSERT_NOCHECK,
1569 "property_-1_5", "value_-1_5", 0)) ||
1570 (error = col_insert_str_property(col, NULL, COL_DSP_FIRSTDUP,
1571 NULL, 0, COL_INSERT_NOCHECK,
1572 "property0", "value0firstdup", 0)) ||
1573 (error = col_insert_str_property(col, NULL, COL_DSP_LASTDUP,
1574 NULL, 0, COL_INSERT_NOCHECK,
1575 "property0", "value0lastdup", 0)) ||
1576 (error = col_insert_str_property(col, NULL, COL_DSP_NDUP,
1577 NULL, 1, COL_INSERT_NOCHECK,
1578 "property0", "value0middledup", 0)) ||
1579 (error = col_insert_str_property(col, NULL, 0,
1580 NULL, 0, COL_INSERT_DUPOVER ,
1581 "property0",
1582 "value0firstdupupdate", 0)) ||
1583 (error = col_insert_str_property(col, NULL, 0,
1584 NULL, 0, COL_INSERT_DUPOVERT,
1585 "property1", "value1update", 0)) ||
1586 ((error = col_insert_str_property(col, NULL, 0,
1587 NULL, 0, COL_INSERT_DUPERROR,
1588 "property0",
1589 "does not matter", 0)) != EEXIST) ||
1590 (error = col_insert_str_property(col, NULL, COL_DSP_NDUP,
1591 NULL, 5, COL_INSERT_NOCHECK,
1592 "property10", "value10dup", 0)) ||
1593 (error = col_insert_str_property(col, NULL, COL_DSP_LASTDUP,
1594 NULL, 0, COL_INSERT_NOCHECK,
1595 "property10", "value10lastdup", 0)) ||
1596 (error = col_insert_str_property(col, NULL, COL_DSP_END,
1597 NULL, 0, COL_INSERT_DUPMOVET,
1598 "property_-2",
1599 "value-2moved_to_bottom", 0)) ||
1600 (error = col_insert_str_property(col, NULL, COL_DSP_FRONT,
1601 NULL, 0, COL_INSERT_DUPMOVE,
1602 "property1_6",
1603 "value_1_6_moved_to_front", 0)) ||
1604 (error = col_insert_str_property(col, NULL, COL_DSP_END,
1605 NULL, 0, COL_INSERT_NOCHECK,
1606 "property0", "extra_1", 0)) ||
1607 (error = col_insert_str_property(col, NULL, COL_DSP_END,
1608 NULL, 0, COL_INSERT_NOCHECK,
1609 "property0", "extra_2", 0)) ||
1610 (error = col_insert_str_property(col, NULL, COL_DSP_NDUP,
1611 "property0", 10, COL_INSERT_NOCHECK,
1612 "property0", "before 0_5", 0)) ||
1613 (error = col_insert_str_property(col, NULL, COL_DSP_NDUPNS,
1614 "property0", 10, COL_INSERT_NOCHECK,
1615 "property0", "after_extra2", 0)) ||
1616 (error = col_insert_str_property(col, NULL, COL_DSP_NDUPNS,
1617 "property0", 6, COL_INSERT_NOCHECK,
1618 "property0", "before_extra2", 0)) ||
1619 (error = col_insert_str_property(col, NULL, COL_DSP_LASTDUPNS,
1620 "property0", 0, COL_INSERT_NOCHECK,
1621 "property0", "very_last", 0))) {
1622
1623 printf("ERROR in the ITERATION TEST %d\n", error);
1624 col_debug_collection(col, COL_TRAVERSE_DEFAULT);
1625 col_destroy_collection(col);
1626 return error;
1627 }
1628
1629 COLOUT(printf("\n\nCollection:\n\n"));
1630 COLOUT(col_debug_collection(col, COL_TRAVERSE_DEFAULT));
1631
1632
1633 COLOUT(printf("\n\n==== EXTRACTION TEST ====\n\n"));
1634
1635 if ((error = col_create_collection(&col2, "extraction", 0)) ||
1636
1637 /* Extracting "property 1_6" value "value_1_6_moved_to_front" */
1638 (error = col_extract_item(col, NULL, COL_DSP_FRONT,
1639 NULL, 0, 0, &item)) ||
1640
1641 (error = col_insert_item(col2, NULL, item, COL_DSP_FRONT,
1642 NULL, 0, COL_INSERT_NOCHECK)) ||
1643
1644 ((verbose) && (error = col_debug_collection(col2,
1645 COL_TRAVERSE_DEFAULT))) ||
1646
1647 /* Extracting "property_-1_5" value "value_-1_5" */
1648 (error = col_extract_item(col, NULL, COL_DSP_FRONT,
1649 NULL, 0, 0, &item)) ||
1650
1651 /* Putting it in front */
1652 (error = col_insert_item(col2, NULL, item, COL_DSP_FRONT,
1653 NULL, 0, COL_INSERT_NOCHECK)) ||
1654
1655 ((verbose) && (error = col_debug_collection(col2,
1656 COL_TRAVERSE_DEFAULT))) ||
1657
1658 /* Extracting "property0" value "very_last" */
1659 (error = col_extract_item(col, NULL, COL_DSP_END,
1660 NULL, 0, 0, &item)) ||
1661
1662 /* Putting it to the end */
1663 (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1664 NULL, 0, COL_INSERT_NOCHECK)) ||
1665
1666 ((verbose) && (error = col_debug_collection(col2,
1667 COL_TRAVERSE_DEFAULT))) ||
1668
1669 /* Inserting extra property to the end of first collection */
1670 (error = col_insert_str_property(col, NULL, COL_DSP_INDEX,
1671 NULL, 100, COL_INSERT_NOCHECK,
1672 "property100", "value100", 0)) ||
1673
1674 /* This will extract "property10" with value "value10dup" */
1675 (error = col_extract_item(col, NULL, COL_DSP_AFTER,
1676 "property10", 0, COL_TYPE_STRING, &item)) ||
1677
1678 /* Putting it to the end */
1679 (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1680 NULL, 0, COL_INSERT_NOCHECK)) ||
1681
1682 ((verbose) && (error = col_debug_collection(col2,
1683 COL_TRAVERSE_DEFAULT))) ||
1684
1685 /* Extracting "property_-0_5" value "value_-0_5" */
1686 (error = col_extract_item(col, NULL, COL_DSP_BEFORE,
1687 "property0", 0, COL_TYPE_STRING, &item)) ||
1688
1689 /* Putting it to the end */
1690 (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1691 NULL, 0, COL_INSERT_NOCHECK)) ||
1692
1693 /* Printing initial collection to see its state */
1694 ((verbose) && (error = col_debug_collection(col,
1695 COL_TRAVERSE_DEFAULT))) ||
1696
1697 ((verbose) && (error = col_debug_collection(col2,
1698 COL_TRAVERSE_DEFAULT))) ||
1699
1700 /* Going for the second item in the collection which is
1701 * at this moment "property0" with value "value0firstdupupdate"
1702 */
1703 (error = col_extract_item(col, NULL, COL_DSP_INDEX,
1704 NULL, 1, 0, &item)) ||
1705
1706 /* Putting it to the end */
1707 (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1708 NULL, 0, COL_INSERT_NOCHECK)) ||
1709
1710 ((verbose) && (error = col_debug_collection(col2,
1711 COL_TRAVERSE_DEFAULT))) ||
1712
1713 /* Extracting first sequextial duplicate of property0 whic is value
1714 * "value0"
1715 */
1716 (error = col_extract_item(col, NULL, COL_DSP_NDUP,
1717 "property0", 1, 0, &item)) ||
1718
1719 /* Putting it to the end */
1720 (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1721 NULL, 0, COL_INSERT_NOCHECK)) ||
1722
1723 ((verbose) && (error = col_debug_collection(col2,
1724 COL_TRAVERSE_DEFAULT))) ||
1725
1726 /* Extracting last sequential dup of "property0"
1727 * which is at this moment "before 0_5"
1728 */
1729 (error = col_extract_item(col, NULL, COL_DSP_LASTDUP,
1730 "property0", 0, 0, &item)) ||
1731
1732 /* Putting it to the end */
1733 (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1734 NULL, 0, COL_INSERT_NOCHECK)) ||
1735
1736 ((verbose) && (error = col_debug_collection(col2,
1737 COL_TRAVERSE_DEFAULT))) ||
1738
1739 /* Extracting first dup of "property0"
1740 * which is at this moment "value0middledup"
1741 */
1742 (error = col_extract_item(col, NULL, COL_DSP_FIRSTDUP,
1743 "property0", 0, 0, &item)) ||
1744
1745 (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1746 NULL, 0, COL_INSERT_NOCHECK)) ||
1747
1748 ((verbose) && (error = col_debug_collection(col2,
1749 COL_TRAVERSE_DEFAULT))) ||
1750
1751 /* Printing initial collection to see its state */
1752 ((verbose) && (error = col_debug_collection(col,
1753 COL_TRAVERSE_DEFAULT))) ||
1754
1755 /* Should extract extra_2 */
1756 (error = col_extract_item(col, NULL, COL_DSP_NDUPNS,
1757 "property0", 3, 0, &item)) ||
1758
1759 (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1760 NULL, 0, COL_INSERT_NOCHECK)) ||
1761
1762 ((verbose) && (error = col_debug_collection(col2,
1763 COL_TRAVERSE_DEFAULT))) ||
1764
1765 /* Printing initial collection to see its state */
1766 ((verbose) && (error = col_debug_collection(col,
1767 COL_TRAVERSE_DEFAULT))) ||
1768
1769 /* Try extracting something that is out of index, expect ENOENT */
1770 ((error = col_extract_item(col, NULL, COL_DSP_NDUPNS,
1771 "property0", 10, 0, &item)) != ENOENT) ||
1772
1773 /* Should extract after_extra_2 */
1774 (error = col_extract_item(col, NULL, COL_DSP_NDUPNS,
1775 "property0", 3, 0, &item)) ||
1776
1777 (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1778 NULL, 0, COL_INSERT_NOCHECK)) ||
1779
1780 ((verbose) && (error = col_debug_collection(col2,
1781 COL_TRAVERSE_DEFAULT))) ||
1782
1783 /* Printing initial collection to see its state */
1784 ((verbose) && (error = col_debug_collection(col,
1785 COL_TRAVERSE_DEFAULT))) ||
1786
1787 /* Should extract before_extra_2 */
1788 (error = col_extract_item(col, NULL, COL_DSP_LASTDUPNS,
1789 "property0", 0, 0, &item)) ||
1790
1791 (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1792 NULL, 0, COL_INSERT_NOCHECK)) ||
1793
1794 ((verbose) && (error = col_debug_collection(col2,
1795 COL_TRAVERSE_DEFAULT)))) {
1796
1797 COLOUT(printf("ERROR in the EXTRACTION TEST\n"));
1798 COLOUT(printf("Collection 1\n"));
1799 COLOUT(col_debug_collection(col, COL_TRAVERSE_DEFAULT));
1800 COLOUT(printf("Collection 2\n"));
1801 COLOUT(col_debug_collection(col2, COL_TRAVERSE_DEFAULT));
1802 col_destroy_collection(col);
1803 col_destroy_collection(col2);
1804 return error;
1805 }
1806
1807 COLOUT(printf("Collection 1\n"));
1808 COLOUT(col_debug_collection(col, COL_TRAVERSE_DEFAULT));
1809 COLOUT(printf("Collection 2\n"));
1810 COLOUT(col_debug_collection(col2, COL_TRAVERSE_DEFAULT));
1811
1812 error = validate_collection(col, varray1);
1813 if (error) {
1814 COLOUT(printf("Collection 1 validation failed.\n"));
1815 col_destroy_collection(col);
1816 col_destroy_collection(col2);
1817 return error;
1818 }
1819
1820 error = validate_collection(col2, varray2);
1821 if (error) {
1822 COLOUT(printf("Collection 2 validation failed.\n"));
1823 col_destroy_collection(col);
1824 col_destroy_collection(col2);
1825 return error;
1826 }
1827
1828 col_destroy_collection(col2);
1829 col_destroy_collection(col);
1830
1831 return EOK;
1832 }
1833
1834 /* Cleanup collback */
1835 static void cb(const char *property,
1836 int property_len,
1837 int type,
1838 void *data,
1839 int length,
1840 void *ext_data)
1841 {
1842 COLOUT(printf("%s\n", *((const char **)ext_data)));
1843 COLOUT(printf("Property: %s\n", property));
1844 COLOUT(printf("Length: %d\n", property_len));
1845 COLOUT(printf("Type: %d\n", type));
1846 COLOUT(printf("Data len: %d\n", length));
1847 }
1848
1849 static int delete_test(void)
1850 {
1851
1852 struct collection_item *col;
1853 int error = EOK;
1854 const char *str = "Cleanup Callback Test";
1855
1856 COLOUT(printf("\n\n==== DELETION TEST 1====\n\n"));
1857
1858 if ((error = col_create_collection(&col, "test", 0)) ||
1859 (error = col_add_int_property(col, NULL, "tt", 1)) ||
1860 ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1861 (error = col_add_int_property(col, NULL, "test", 1)) ||
1862 ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1863 (error = col_delete_property(col, "test", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT)) ||
1864 ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1865 (error = col_add_int_property(col, NULL, "test", 1)) ||
1866 ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1867 (error = col_delete_property(col, "test", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT)) ||
1868 ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1869 (error = col_add_int_property(col, NULL, "test", 1))) {
1870 printf("Error in delete test %d\n", error);
1871 col_destroy_collection(col);
1872 return error;
1873 }
1874
1875 COLOUT(col_debug_collection(col, COL_TRAVERSE_DEFAULT));
1876 col_destroy_collection(col);
1877
1878 COLOUT(printf("\n\n==== DELETION TEST 1 END ====\n\n"));
1879 COLOUT(printf("\n\n==== DELETION TEST 2====\n\n"));
1880
1881 if ((error = col_create_collection(&col, "test2", 0)) ||
1882 (error = col_add_int_property(col, NULL, "tt", 1)) ||
1883 ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1884 (error = col_add_int_property(col, NULL, "test", 1)) ||
1885 ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1886 (error = col_remove_item(col, NULL, COL_DSP_END, NULL, 0, COL_TYPE_ANY)) ||
1887 ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1888 (error = col_add_int_property(col, NULL, "test", 1)) ||
1889 ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1890 (error = col_remove_item_from_current(col, COL_DSP_AFTER, "tt", 0, COL_TYPE_ANY)) ||
1891 ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1892 (error = col_add_int_property(col, NULL, "test", 1))) {
1893 printf("Error in delete test %d\n", error);
1894 col_destroy_collection(col);
1895 return error;
1896 }
1897
1898 COLOUT(col_debug_collection(col, COL_TRAVERSE_DEFAULT));
1899
1900 COLOUT(printf("\n\n==== DELETION TEST 2 END ====\n\n"));
1901
1902
1903 col_destroy_collection_with_cb(col, cb, (void *)(&str));
1904
1905 return error;
1906 }
1907
1908 /* Search test */
1909 static int search_test(void)
1910 {
1911 struct collection_item *level1 = NULL;
1912 struct collection_item *level2 = NULL;
1913 struct collection_item *level3 = NULL;
1914 struct collection_item *level4 = NULL;
1915 char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
1916 int found = 0;
1917 int error = 0;
1918
1919 COLOUT(printf("\n\n==== SEARCH TEST ====\n\n"));
1920
1921 if ((error = col_create_collection(&level1, "level1", 0)) ||
1922 (error = col_create_collection(&level2, "level2", 0)) ||
1923 (error = col_add_collection_to_collection(level1, NULL, NULL, level2, COL_ADD_MODE_REFERENCE)) ||
1924 (error = col_create_collection(&level3, "level3", 0)) ||
1925 (error = col_add_collection_to_collection(level1, "level2", NULL, level3, COL_ADD_MODE_REFERENCE)) ||
1926 (error = col_create_collection(&level4, "leveL4", 0)) ||
1927 (error = col_add_collection_to_collection(level1, "level3", NULL, level4, COL_ADD_MODE_REFERENCE)) ||
1928 (error = col_add_int_property(level1, "leveL4", "id", 1)) ||
1929 (error = col_add_long_property(level1, "level3", "packets", 100000000L)) ||
1930 (error = col_add_binary_property(level1, "level2", "stack", binary_dump, sizeof(binary_dump)))) {
1931 col_destroy_collection(level1);
1932 col_destroy_collection(level2);
1933 col_destroy_collection(level3);
1934 col_destroy_collection(level4);
1935 printf("Failed to build test. Error %d\n", error);
1936 return error;
1937 }
1938
1939 COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
1940
1941 error = col_is_item_in_collection(level1, "level1!level2!level3!level4!", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
1942 if (!error) {
1943 col_destroy_collection(level1);
1944 col_destroy_collection(level2);
1945 col_destroy_collection(level3);
1946 col_destroy_collection(level4);
1947 printf("Expected error here since the search data is illegal but got success\n");
1948 return EINVAL;
1949 }
1950
1951 found = 0;
1952 error = col_is_item_in_collection(level1, "level1!level2!level3!level4!id", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
1953 if ((error) || (!found)) {
1954 col_destroy_collection(level1);
1955 col_destroy_collection(level2);
1956 col_destroy_collection(level3);
1957 col_destroy_collection(level4);
1958 printf("Failed to find item [level1!level2!level3!level4!id]. Error %d\n", error);
1959 return error ? error : ENOENT;
1960 }
1961 else COLOUT(printf("Expected item is found\n"));
1962
1963
1964 found = 0;
1965 error = col_is_item_in_collection(level1, NULL, COL_TYPE_INTEGER, COL_TRAVERSE_DEFAULT, &found);
1966 if ((error) || (!found)) {
1967 col_destroy_collection(level1);
1968 col_destroy_collection(level2);
1969 col_destroy_collection(level3);
1970 col_destroy_collection(level4);
1971 printf("Failed to find first int item [level1!level2!level3!level4!id]. Error %d\n", error);
1972 return error ? error : ENOENT;
1973 }
1974 else COLOUT(printf("Expected item is found\n"));
1975
1976
1977 found = 0;
1978 error = col_is_item_in_collection(level1, "", COL_TYPE_INTEGER, COL_TRAVERSE_DEFAULT, &found);
1979 if ((error) || (!found)) {
1980 col_destroy_collection(level1);
1981 col_destroy_collection(level2);
1982 col_destroy_collection(level3);
1983 col_destroy_collection(level4);
1984 printf("Failed to find first int item [level1!level2!level3!level4!id]. Error %d\n", error);
1985 return error ? error : ENOENT;
1986 }
1987 else COLOUT(printf("Expected item is found\n"));
1988
1989
1990 found = 0;
1991 error = col_is_item_in_collection(level1, "level3!level4!id", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
1992 if ((error) || (!found)) {
1993 col_destroy_collection(level1);
1994 col_destroy_collection(level2);
1995 col_destroy_collection(level3);
1996 col_destroy_collection(level4);
1997 printf("Failed to find item [level3!level4!id]. Error %d\n", error);
1998 return error ? error : ENOENT;
1999 }
2000 else COLOUT(printf("Expected item is found\n"));
2001
2002 found = 0;
2003 error = col_is_item_in_collection(level1, "level3!packets", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
2004 if ((error) || (!found)) {
2005 col_destroy_collection(level1);
2006 col_destroy_collection(level2);
2007 col_destroy_collection(level3);
2008 col_destroy_collection(level4);
2009 printf("Failed to find item [level3.packets]. Error %d\n", error);
2010 return error ? error : ENOENT;
2011 }
2012 else COLOUT(printf("Expected item is found\n"));
2013
2014 found = 0;
2015 error = col_is_item_in_collection(level1, "level1!level2!stack", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
2016 if ((error) || (!found)) {
2017 col_destroy_collection(level1);
2018 col_destroy_collection(level2);
2019 col_destroy_collection(level3);
2020 col_destroy_collection(level4);
2021 printf("Failed to find item [level1!level2!stack]. Error %d\n", error);
2022 return error ? error : ENOENT;
2023 }
2024 else COLOUT(printf("Expected item is found\n"));
2025
2026 found = 0;
2027 error = col_is_item_in_collection(level1, "level1!level2!level3", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
2028 if ((error) || (!found)) {
2029 col_destroy_collection(level1);
2030 col_destroy_collection(level2);
2031 col_destroy_collection(level3);
2032 col_destroy_collection(level4);
2033 printf("Failed to find item [level1!level2!level3]. Error %d\n", error);
2034 return error ? error : ENOENT;
2035 }
2036 else COLOUT(printf("Expected item is found\n"));
2037
2038 /* Negative tests */
2039 found = 0;
2040 error = col_is_item_in_collection(level1, NULL, 0, COL_TRAVERSE_DEFAULT, &found);
2041 if ((error != ENOENT) || (found)) {
2042 col_destroy_collection(level1);
2043 col_destroy_collection(level2);
2044 col_destroy_collection(level3);
2045 col_destroy_collection(level4);
2046 if (error) {
2047 printf("Unexpected error with NULL & 0 test %d\n", error);
2048 }
2049 else {
2050 printf("Found unexpected item with NULL & 0. Error %d\n", error);
2051 error = EINVAL;
2052 }
2053 return error;
2054 }
2055 else COLOUT(printf("No item is found as expected.\n"));
2056
2057 found = 0;
2058 error = col_is_item_in_collection(level1, "", 0, COL_TRAVERSE_DEFAULT, &found);
2059 if ((error != ENOENT) || (found)) {
2060 col_destroy_collection(level1);
2061 col_destroy_collection(level2);
2062 col_destroy_collection(level3);
2063 col_destroy_collection(level4);
2064 if (error) {
2065 printf("Unexpected error with \"\" & 0 tests %d\n", error);
2066 }
2067 else {
2068 printf("Found unexpected item with \"\" & 0. Error %d\n", error);
2069 error = EINVAL;
2070 }
2071 return error;
2072 }
2073 else COLOUT(printf("No item is found as expected.\n"));
2074
2075 col_destroy_collection(level1);
2076 col_destroy_collection(level2);
2077 col_destroy_collection(level3);
2078 col_destroy_collection(level4);
2079
2080 COLOUT(printf("\n\n==== SEARCH TEST END ====\n\n"));
2081
2082 return EOK;
2083 }
2084
2085 /* Sort test */
2086 static int sort_test(void)
2087 {
2088 struct collection_item *level1 = NULL;
2089 struct collection_item *level2a = NULL;
2090 struct collection_item *level2b = NULL;
2091 struct collection_item *level3 = NULL;
2092 int error = 0;
2093
2094 COLOUT(printf("\n\n==== SORT TEST ====\n\n"));
2095
2096 if ((error = col_create_collection(&level1, "level1", 0)) ||
2097 (error = col_create_collection(&level2a, "level2a", 0)) ||
2098 (error = col_add_collection_to_collection(level1, NULL, NULL, level2a, COL_ADD_MODE_REFERENCE)) ||
2099 (error = col_create_collection(&level2b, "level2b", 0)) ||
2100 (error = col_add_collection_to_collection(level1, NULL, NULL, level2b, COL_ADD_MODE_REFERENCE)) ||
2101 (error = col_create_collection(&level3, "level3", 0)) ||
2102 (error = col_add_collection_to_collection(level1, "level2a", NULL, level3, COL_ADD_MODE_REFERENCE)) ||
2103 (error = col_add_collection_to_collection(level1, "level2b", NULL, level3, COL_ADD_MODE_REFERENCE)) ||
2104 (error = col_add_int_property(level1, NULL, "int3", 1)) ||
2105 (error = col_add_int_property(level1, NULL, "int2", 2)) ||
2106 (error = col_add_int_property(level1, NULL, "int1", 3)) ||
2107 (error = col_add_bool_property(level1, NULL, "bool3", 1)) ||
2108 (error = col_add_bool_property(level1, NULL, "bool2", 1)) ||
2109 (error = col_add_bool_property(level1, NULL, "bool1", 0)) ||
2110 (error = col_add_unsigned_property(level1, NULL, "unsigned1", 2)) ||
2111 (error = col_add_unsigned_property(level1, NULL, "unsigned3", 1)) ||
2112 (error = col_add_unsigned_property(level1, NULL, "unsigned2", 3)) ||
2113 (error = col_add_long_property(level1, NULL, "long3", 1)) ||
2114 (error = col_add_long_property(level1, NULL, "long2", 2)) ||
2115 (error = col_add_long_property(level1, NULL, "long1", 3)) ||
2116 (error = col_add_ulong_property(level1, NULL, "ulong1", 2)) ||
2117 (error = col_add_ulong_property(level1, NULL, "ulong3", 1)) ||
2118 (error = col_add_ulong_property(level1, NULL, "ulong2", 3)) ||
2119 (error = col_add_double_property(level1, NULL, "double1", 2.2)) ||
2120 (error = col_add_double_property(level1, NULL, "double3", 1.1)) ||
2121 (error = col_add_double_property(level1, NULL, "double2", 3.3)) ||
2122 (error = col_add_int_property(level3, NULL, "int3L3", 1)) ||
2123 (error = col_add_int_property(level3, NULL, "int2L3", 2)) ||
2124 (error = col_add_int_property(level3, NULL, "int1L3", 3)) ||
2125 (error = col_add_unsigned_property(level1, "level2a!level3", "unsigned1L3", 2)) ||
2126 (error = col_add_unsigned_property(level1, "level2a!level3", "unsigned3L3", 1)) ||
2127 (error = col_add_unsigned_property(level1, "level2a!level3", "unsigned2L3", 3)) ||
2128 (error = col_add_long_property(level1, "level2b!level3", "long3L3", 1)) ||
2129 (error = col_add_long_property(level1, "level2b!level3", "long2L3", 2)) ||
2130 (error = col_add_long_property(level1, "level2b!level3", "long1L3", 3)) ||
2131 (error = col_add_ulong_property(level1, "level3", "ulong1L3", 2)) ||
2132 (error = col_add_ulong_property(level1, "level3", "ulong3L3", 1)) ||
2133 (error = col_add_ulong_property(level1, "level3", "ulong2L3", 3)) ||
2134 (error = col_add_bool_property(level3, NULL, "bool3", 1)) ||
2135 (error = col_add_bool_property(level3, NULL, "bool2", 1)) ||
2136 (error = col_add_bool_property(level3, NULL, "bool1", 0)) ||
2137 (error = col_add_double_property(level3, NULL, "double1L3", 2.2)) ||
2138 (error = col_add_double_property(level3, NULL, "double3L3", 1.1)) ||
2139 (error = col_add_double_property(level3, NULL, "double2L3", 3.3))) {
2140 col_destroy_collection(level1);
2141 col_destroy_collection(level2a);
2142 col_destroy_collection(level2b);
2143 col_destroy_collection(level3);
2144 printf("Failed to build test. Error %d\n", error);
2145 return error;
2146 }
2147
2148 COLOUT(printf("\nUNSORTED COLLECTION\n\n"));
2149 COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
2150
2151 error = col_sort_collection(level1, COL_CMPIN_PROP_EQU, COL_SORT_SUB | COL_SORT_MYSUB);
2152 if (error) {
2153 col_destroy_collection(level1);
2154 col_destroy_collection(level2a);
2155 col_destroy_collection(level2b);
2156 col_destroy_collection(level3);
2157 printf("Failed sort. Error %d\n", error);
2158 return error;
2159 }
2160
2161 COLOUT(printf("\nSORTED BUT SKIPPING REFERENCES\n\n"));
2162 COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
2163
2164 error = col_sort_collection(level1, COL_CMPIN_PROP_EQU, COL_SORT_SUB);
2165 if (error) {
2166 col_destroy_collection(level1);
2167 col_destroy_collection(level2a);
2168 col_destroy_collection(level2b);
2169 col_destroy_collection(level3);
2170 printf("Failed sort. Error %d\n", error);
2171 return error;
2172 }
2173
2174 COLOUT(printf("\nSORTED BUT NOT SKIPPING REFERENCES\n\n"));
2175 COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
2176
2177 error = col_sort_collection(level1, COL_CMPIN_DATA_LEN, COL_SORT_SUB | COL_SORT_DESC);
2178 if (error) {
2179 col_destroy_collection(level1);
2180 col_destroy_collection(level2a);
2181 col_destroy_collection(level2b);
2182 col_destroy_collection(level3);
2183 printf("Failed sort. Error %d\n", error);
2184 return error;
2185 }
2186
2187 COLOUT(printf("\nSORTED DESC NOT SKIPPING BY LENGTH OF DATA\n\n"));
2188 COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
2189
2190 error = col_sort_collection(level1, COL_CMPIN_PROP_LEN, COL_SORT_SUB | COL_SORT_DESC);
2191 if (error) {
2192 col_destroy_collection(level1);
2193 col_destroy_collection(level2a);
2194 col_destroy_collection(level2b);
2195 col_destroy_collection(level3);
2196 printf("Failed sort. Error %d\n", error);
2197 return error;
2198 }
2199
2200 COLOUT(printf("\nSORTED DESC NOT SKIPPING BY LENGTH OF PROPERTY\n\n"));
2201 COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
2202
2203 error = col_sort_collection(level1, COL_CMPIN_DATA, COL_SORT_SUB | COL_SORT_DESC);
2204 if (error) {
2205 col_destroy_collection(level1);
2206 col_destroy_collection(level2a);
2207 col_destroy_collection(level2b);
2208 col_destroy_collection(level3);
2209 printf("Failed sort. Error %d\n", error);
2210 return error;
2211 }
2212
2213 COLOUT(printf("\nSORTED DESC NOT SKIPPING BY DATA\n\n"));
2214 COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
2215
2216 col_destroy_collection(level1);
2217 col_destroy_collection(level2a);
2218 col_destroy_collection(level2b);
2219 col_destroy_collection(level3);
2220
2221 COLOUT(printf("\n\n==== SORT TEST END ====\n\n"));
2222
2223 return EOK;
2224 }
2225
2226 /* Main function of the unit test */
2227
2228 int main(int argc, char *argv[])
2229 {
2230 int error = 0;
2231 test_fn tests[] = { ref_collection_test,
2232 single_collection_test,
2233 add_collection_test,
2234 mixed_collection_test,
2235 iterator_test,
2236 insert_extract_test,
2237 delete_test,
2238 search_test,
2239 sort_test,
2240 dup_test,
2241 NULL };
2242 test_fn t;
2243 int i = 0;
2244
2245 if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
2246
2247 printf("Start\n");
2248
2249 while ((t = tests[i++])) {
2250 error = t();
2251 if (error) {
2252 printf("Failed!\n");
2253 return error;
2254 }
2255 }
2256
2257 printf("Success!\n");
2258 return 0;
2259
2260 }