Debian Codegraph @

collection/collection_queue_ut.c

raw symbols SCIP
1 /*
2 QUEUE INTERFACE
3
4 Queue 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_queue.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 static int queue_test(void)
41 {
42 struct collection_item *queue = NULL;
43 char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
44 struct collection_item *item = NULL;
45 int i;
46 unsigned count;
47 int error = EOK;
48
49 TRACE_FLOW_STRING("queue_test","Entry.");
50
51 COLOUT(printf("\n\nQUEUE TEST!!!.\n\n\n"));
52
53 if((error = col_create_queue(&queue)) ||
54 (error = col_enqueue_str_property(queue, "item1","value 1" ,0)) ||
55 (error = col_enqueue_int_property(queue, "item2", -1)) ||
56 (error = col_enqueue_unsigned_property(queue, "item3", 1)) ||
57 (error = col_enqueue_long_property(queue, "item4", 100)) ||
58 (error = col_enqueue_ulong_property(queue, "item5", 1000)) ||
59 (error = col_enqueue_double_property(queue, "item6", 1.1)) ||
60 (error = col_enqueue_bool_property(queue, "item7", 1)) ||
61 (error = col_enqueue_binary_property(queue, "item8", binary_dump, sizeof(binary_dump)))) {
62 printf("Failed to enqueue property. Error %d\n", error);
63 col_destroy_collection(queue);
64 return error;
65 }
66
67 COLOUT(col_debug_collection(queue,COL_TRAVERSE_DEFAULT));
68
69 error = col_get_collection_count(queue, &count);
70 if (error) {
71 printf("Failed to get count. Error %d\n", error);
72 col_destroy_collection(queue);
73 return error;
74 }
75
76 count--;
77
78 COLOUT(printf("Rotate the queue.\n"));
79
80 for (i = 0; i < count; i++) {
81 if ((error = col_dequeue_item(queue, &item)) ||
82 (error = col_enqueue_item(queue, item))) {
83 printf("Failed to dequeue or enqueue items. Error %d\n", error);
84 col_destroy_collection(queue);
85 return error;
86 }
87 COLOUT(col_debug_collection(queue,COL_TRAVERSE_DEFAULT));
88 }
89
90 col_destroy_collection(queue);
91 TRACE_FLOW_NUMBER("queue_test. Returning", error);
92
93 COLOUT(printf("\n\nEND OF QUEUE TEST!!!.\n\n\n"));
94
95 return error;
96 }
97
98
99 static int empty_test(void)
100 {
101 struct collection_item *queue = NULL;
102 struct collection_item *item = NULL;
103 int i;
104 unsigned count;
105 int error = EOK;
106
107 TRACE_FLOW_STRING("empty_test","Entry.");
108
109 COLOUT(printf("\n\nEMPTY QUEUE TEST!!!.\n\n\n"));
110
111 if((error = col_create_queue(&queue)) ||
112 (error = col_enqueue_str_property(queue, "item1","value 1" ,0)) ||
113 (error = col_enqueue_int_property(queue, "item2", -1)) ||
114 (error = col_enqueue_unsigned_property(queue, "item3", 1))) {
115 printf("Failed to enqueue property. Error %d\n", error);
116 col_destroy_collection(queue);
117 return error;
118 }
119
120 COLOUT(col_debug_collection(queue,COL_TRAVERSE_DEFAULT));
121
122 error = col_get_collection_count(queue, &count);
123 if (error) {
124 printf("Failed to get count. Error %d\n", error);
125 col_destroy_collection(queue);
126 return error;
127 }
128
129 count--;
130
131 COLOUT(printf("Empty the queue.\n"));
132
133 for (i = 0; i < count; i++) {
134 if ((error = col_dequeue_item(queue, &item))) {
135 printf("Failed to dequeue or enqueue items. Error %d\n", error);
136 col_destroy_collection(queue);
137 return error;
138 }
139
140 col_delete_item(item);
141
142 COLOUT(col_debug_collection(queue,COL_TRAVERSE_DEFAULT));
143 }
144
145 COLOUT(printf("Add elements again.\n"));
146 if((error = col_enqueue_str_property(queue, "item1","value 1" ,0)) ||
147 (error = col_enqueue_int_property(queue, "item2", -1)) ||
148 (error = col_enqueue_unsigned_property(queue, "item3", 1))) {
149 printf("Failed to enqueue property. Error %d\n", error);
150 col_destroy_collection(queue);
151 return error;
152 }
153
154 COLOUT(col_debug_collection(queue,COL_TRAVERSE_DEFAULT));
155
156 error = col_get_collection_count(queue, &count);
157 if (error) {
158 printf("Failed to get count. Error %d\n", error);
159 col_destroy_collection(queue);
160 return error;
161 }
162
163 count--;
164
165 COLOUT(printf("Empty the queue again.\n"));
166
167 for (i = 0; i < count; i++) {
168 if ((error = col_dequeue_item(queue, &item))) {
169 printf("Failed to dequeue or enqueue items. Error %d\n", error);
170 col_destroy_collection(queue);
171 return error;
172 }
173
174 col_delete_item(item);
175
176 COLOUT(col_debug_collection(queue,COL_TRAVERSE_DEFAULT));
177 }
178
179 col_destroy_collection(queue);
180
181 TRACE_FLOW_NUMBER("empty_test. Returning", error);
182
183 COLOUT(printf("\n\nEND OF QUEUE TEST!!!.\n\n\n"));
184
185 return error;
186 }
187
188
189 /* Main function of the unit test */
190 int main(int argc, char *argv[])
191 {
192 int error = 0;
193 test_fn tests[] = { queue_test,
194 empty_test,
195 NULL };
196 test_fn t;
197 int i = 0;
198
199 if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
200
201 printf("Start\n");
202
203 while ((t = tests[i++])) {
204 error = t();
205 if (error) {
206 printf("Failed!\n");
207 return error;
208 }
209 }
210
211 printf("Success!\n");
212 return 0;
213 }