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