Debian Codegraph @

collection/collection_stack_ut.c

raw symbols SCIP
1 /*
2 STACK INTERFACE
3
4 Stack 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 #define TRACE_HOME
26 #include "trace.h"
27 #include "collection_stack.h"
28 #include "collection_tools.h"
29
30 typedef int (*test_fn)(void);
31
32 int verbose = 0;
33
34 #define COLOUT(foo) \
35 do { \
36 if (verbose) foo; \
37 } while(0)
38
39
40
41 static int stack_test(void)
42 {
43 struct collection_item *stack = NULL;
44 char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
45 struct collection_item *item1 = NULL;
46 struct collection_item *item2 = NULL;
47
48 int error = EOK;
49
50 TRACE_FLOW_STRING("stack_test", "Entry.");
51
52 COLOUT(printf("\n\nSTACK TEST!!!.\n\n\n"));
53
54 if ((error = col_create_stack(&stack)) ||
55 (error = col_push_str_property(stack, "item1", "value 1", 0)) ||
56 (error = col_push_int_property(stack, "item2", -1)) ||
57 (error = col_push_unsigned_property(stack, "item3", 1)) ||
58 (error = col_push_long_property(stack, "item4", 100)) ||
59 (error = col_push_ulong_property(stack, "item5", 1000)) ||
60 (error = col_push_double_property(stack, "item6", 1.1)) ||
61 (error = col_push_bool_property(stack, "item7", 1)) ||
62 (error = col_push_binary_property(stack, "item8", binary_dump, sizeof(binary_dump)))) {
63 printf("Failed to push property. Error %d\n", error);
64 col_destroy_collection(stack);
65 return error;
66 }
67
68 COLOUT(col_debug_collection(stack, COL_TRAVERSE_DEFAULT));
69
70 COLOUT(printf("Swapping last two items by popping and pushing them back.\n"));
71
72 if ((error = col_pop_item(stack, &item1)) ||
73 (error = col_pop_item(stack, &item2))) {
74 printf("Failed to pop items. Error %d\n", error);
75 col_destroy_collection(stack);
76 return error;
77 }
78
79 COLOUT(printf("\nPopped two last items.\n"));
80 COLOUT(col_debug_collection(stack, COL_TRAVERSE_DEFAULT));
81
82 COLOUT(printf("\nLast item.\n"));
83 COLOUT(col_debug_item(item1));
84
85 COLOUT(printf("\nPrevious item.\n"));
86 COLOUT(col_debug_item(item2));
87
88 if ((error = col_push_item(stack, item1)) ||
89 (error = col_push_item(stack, item2))) {
90 printf("Failed to pop or push items. Error %d\n", error);
91 col_destroy_collection(stack);
92 return error;
93 }
94
95 COLOUT(printf("\n\nPushed two items again in reverse order.\n\n"));
96
97 COLOUT(col_debug_collection(stack, COL_TRAVERSE_DEFAULT));
98 col_destroy_collection(stack);
99 TRACE_FLOW_NUMBER("stack_test. Returning", error);
100
101 COLOUT(printf("\n\nEND OF STACK TEST!!!.\n\n"));
102
103 return error;
104 }
105
106 /* Main function of the unit test */
107
108 int main(int argc, char *argv[])
109 {
110 int error = 0;
111 test_fn tests[] = { stack_test,
112 NULL };
113 test_fn t;
114 int i = 0;
115
116 if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
117
118 printf("Start\n");
119
120 while ((t = tests[i++])) {
121 error = t();
122 if (error) {
123 printf("Failed!\n");
124 return error;
125 }
126 }
127
128 printf("Success!\n");
129 return 0;
130 }