3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program 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 General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "src/shared/util.h"
31 #include "src/shared/queue.h"
32 #include "src/shared/tester.h"
34 static void test_basic(const void *data)
40 g_assert(queue != NULL);
42 for (n = 0; n < 1024; n++) {
43 for (i = 1; i < n + 2; i++)
44 queue_push_tail(queue, UINT_TO_PTR(i));
46 g_assert(queue_length(queue) == n + 1);
48 for (i = 1; i < n + 2; i++) {
51 ptr = queue_pop_head(queue);
52 g_assert(ptr != NULL);
53 g_assert(i == PTR_TO_UINT(ptr));
56 g_assert(queue_isempty(queue) == true);
59 queue_destroy(queue, NULL);
63 static void foreach_destroy(void *data, void *user_data)
65 struct queue *queue = user_data;
67 queue_destroy(queue, NULL);
70 static void test_foreach_destroy(const void *data)
75 g_assert(queue != NULL);
77 queue_push_tail(queue, UINT_TO_PTR(1));
78 queue_push_tail(queue, UINT_TO_PTR(2));
80 queue_foreach(queue, foreach_destroy, queue);
84 static void foreach_remove(void *data, void *user_data)
86 struct queue *queue = user_data;
88 g_assert(queue_remove(queue, data));
91 static void test_foreach_remove(const void *data)
96 g_assert(queue != NULL);
98 queue_push_tail(queue, UINT_TO_PTR(1));
99 queue_push_tail(queue, UINT_TO_PTR(2));
101 queue_foreach(queue, foreach_remove, queue);
102 queue_destroy(queue, NULL);
103 tester_test_passed();
106 static void foreach_remove_all(void *data, void *user_data)
108 struct queue *queue = user_data;
110 queue_remove_all(queue, NULL, NULL, NULL);
113 static void test_foreach_remove_all(const void *data)
118 g_assert(queue != NULL);
120 queue_push_tail(queue, UINT_TO_PTR(1));
121 queue_push_tail(queue, UINT_TO_PTR(2));
123 queue_foreach(queue, foreach_remove_all, queue);
124 queue_destroy(queue, NULL);
125 tester_test_passed();
128 static void foreach_remove_backward(void *data, void *user_data)
130 struct queue *queue = user_data;
132 queue_remove(queue, UINT_TO_PTR(2));
133 queue_remove(queue, UINT_TO_PTR(1));
136 static void test_foreach_remove_backward(const void *data)
141 g_assert(queue != NULL);
143 queue_push_tail(queue, UINT_TO_PTR(1));
144 queue_push_tail(queue, UINT_TO_PTR(2));
146 queue_foreach(queue, foreach_remove_backward, queue);
147 queue_destroy(queue, NULL);
148 tester_test_passed();
151 static struct queue *static_queue;
153 static void destroy_remove(void *user_data)
155 queue_remove(static_queue, user_data);
158 static void test_destroy_remove(const void *data)
160 static_queue = queue_new();
162 g_assert(static_queue != NULL);
164 queue_push_tail(static_queue, UINT_TO_PTR(1));
165 queue_push_tail(static_queue, UINT_TO_PTR(2));
167 queue_destroy(static_queue, destroy_remove);
168 tester_test_passed();
171 static void test_push_after(const void *data)
177 g_assert(queue != NULL);
180 * Pre-populate queue. Initial elements are:
183 g_assert(queue_push_tail(queue, NULL));
184 g_assert(queue_push_tail(queue, UINT_TO_PTR(2)));
185 g_assert(queue_push_tail(queue, UINT_TO_PTR(5)));
186 g_assert(queue_length(queue) == 3);
188 /* Invalid insertion */
189 g_assert(!queue_push_after(queue, UINT_TO_PTR(6), UINT_TO_PTR(1)));
191 /* Valid insertions */
192 g_assert(queue_push_after(queue, NULL, UINT_TO_PTR(1)));
193 g_assert(queue_push_after(queue, UINT_TO_PTR(2), UINT_TO_PTR(3)));
194 g_assert(queue_push_after(queue, UINT_TO_PTR(3), UINT_TO_PTR(4)));
195 g_assert(queue_push_after(queue, UINT_TO_PTR(5), UINT_TO_PTR(6)));
197 g_assert(queue_peek_head(queue) == NULL);
198 g_assert(queue_peek_tail(queue) == UINT_TO_PTR(6));
201 * Queue should contain 7 elements:
202 * [ NULL, 1, 2, 3, 4, 5, 6 ]
204 len = queue_length(queue);
207 for (i = 0; i < 7; i++)
208 g_assert(queue_pop_head(queue) == UINT_TO_PTR(i));
210 /* Test with identical elements */
211 g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
212 g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
213 g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
214 g_assert(queue_push_after(queue, UINT_TO_PTR(1), UINT_TO_PTR(0)));
216 g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
217 g_assert(queue_pop_head(queue) == UINT_TO_PTR(0));
218 g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
219 g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
221 queue_destroy(queue, NULL);
222 tester_test_passed();
225 static bool match_int(const void *a, const void *b)
227 int i = PTR_TO_INT(a);
228 int j = PTR_TO_INT(b);
233 static bool match_ptr(const void *a, const void *b)
238 static void test_remove_all(const void *data)
243 g_assert(queue != NULL);
245 g_assert(queue_push_tail(queue, INT_TO_PTR(10)));
247 g_assert(queue_remove_all(queue, match_int, INT_TO_PTR(10), NULL) == 1);
248 g_assert(queue_isempty(queue));
250 g_assert(queue_push_tail(queue, NULL));
251 g_assert(queue_remove_all(queue, match_ptr, NULL, NULL) == 1);
252 g_assert(queue_isempty(queue));
254 g_assert(queue_push_tail(queue, UINT_TO_PTR(0)));
255 g_assert(queue_remove_all(queue, match_int, UINT_TO_PTR(0), NULL) == 1);
256 g_assert(queue_isempty(queue));
258 queue_destroy(queue, NULL);
259 tester_test_passed();
262 int main(int argc, char *argv[])
264 tester_init(&argc, &argv);
266 tester_add("/queue/basic", NULL, NULL, test_basic, NULL);
267 tester_add("/queue/foreach_destroy", NULL, NULL,
268 test_foreach_destroy, NULL);
269 tester_add("/queue/foreach_remove", NULL, NULL,
270 test_foreach_remove, NULL);
271 tester_add("/queue/foreach_remove_all", NULL, NULL,
272 test_foreach_remove_all, NULL);
273 tester_add("/queue/foreach_remove_backward", NULL, NULL,
274 test_foreach_remove_backward, NULL);
275 tester_add("/queue/destroy_remove", NULL, NULL,
276 test_destroy_remove, NULL);
277 tester_add("/queue/push_after", NULL, NULL, test_push_after, NULL);
278 tester_add("/queue/remove_all", NULL, NULL, test_remove_all, NULL);