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