blob: d912a6416cc25e7c4663c2fd0dd1e809c11de495 [file] [log] [blame]
Marcel Holtmann611f14d2014-01-25 11:30:52 -08001/*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
6 *
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include <glib.h>
29
30#include "src/shared/util.h"
31#include "src/shared/queue.h"
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +053032#include "src/shared/tester.h"
Marcel Holtmann611f14d2014-01-25 11:30:52 -080033
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +053034static void test_basic(const void *data)
Marcel Holtmann611f14d2014-01-25 11:30:52 -080035{
36 struct queue *queue;
37 unsigned int n, i;
38
39 queue = queue_new();
40 g_assert(queue != NULL);
41
42 for (n = 0; n < 1024; n++) {
43 for (i = 1; i < n + 2; i++)
44 queue_push_tail(queue, UINT_TO_PTR(i));
45
46 g_assert(queue_length(queue) == n + 1);
47
48 for (i = 1; i < n + 2; i++) {
49 void *ptr;
50
51 ptr = queue_pop_head(queue);
52 g_assert(ptr != NULL);
53 g_assert(i == PTR_TO_UINT(ptr));
54 }
55
56 g_assert(queue_isempty(queue) == true);
57 }
58
59 queue_destroy(queue, NULL);
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +053060 tester_test_passed();
Marcel Holtmann611f14d2014-01-25 11:30:52 -080061}
62
Luiz Augusto von Dentz661d8452014-05-22 12:14:18 +030063static void foreach_destroy(void *data, void *user_data)
64{
65 struct queue *queue = user_data;
66
67 queue_destroy(queue, NULL);
68}
69
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +053070static void test_foreach_destroy(const void *data)
Luiz Augusto von Dentz661d8452014-05-22 12:14:18 +030071{
72 struct queue *queue;
73
74 queue = queue_new();
75 g_assert(queue != NULL);
76
77 queue_push_tail(queue, UINT_TO_PTR(1));
78 queue_push_tail(queue, UINT_TO_PTR(2));
79
80 queue_foreach(queue, foreach_destroy, queue);
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +053081 tester_test_passed();
Luiz Augusto von Dentz661d8452014-05-22 12:14:18 +030082}
83
Luiz Augusto von Dentz7dade6c2014-12-08 13:44:15 +020084static void foreach_remove(void *data, void *user_data)
85{
86 struct queue *queue = user_data;
87
88 g_assert(queue_remove(queue, data));
89}
90
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +053091static void test_foreach_remove(const void *data)
Luiz Augusto von Dentz7dade6c2014-12-08 13:44:15 +020092{
93 struct queue *queue;
94
95 queue = queue_new();
96 g_assert(queue != NULL);
97
98 queue_push_tail(queue, UINT_TO_PTR(1));
99 queue_push_tail(queue, UINT_TO_PTR(2));
100
101 queue_foreach(queue, foreach_remove, queue);
102 queue_destroy(queue, NULL);
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530103 tester_test_passed();
Luiz Augusto von Dentz7dade6c2014-12-08 13:44:15 +0200104}
105
Luiz Augusto von Dentzd03c4da2014-05-22 13:07:22 +0300106static void foreach_remove_all(void *data, void *user_data)
107{
108 struct queue *queue = user_data;
109
110 queue_remove_all(queue, NULL, NULL, NULL);
111}
112
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530113static void test_foreach_remove_all(const void *data)
Luiz Augusto von Dentzd03c4da2014-05-22 13:07:22 +0300114{
115 struct queue *queue;
116
117 queue = queue_new();
118 g_assert(queue != NULL);
119
120 queue_push_tail(queue, UINT_TO_PTR(1));
121 queue_push_tail(queue, UINT_TO_PTR(2));
122
123 queue_foreach(queue, foreach_remove_all, queue);
Luiz Augusto von Dentz3053eb72014-06-27 13:51:46 +0300124 queue_destroy(queue, NULL);
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530125 tester_test_passed();
Luiz Augusto von Dentzd03c4da2014-05-22 13:07:22 +0300126}
127
Luiz Augusto von Dentzc9528d92014-12-08 13:51:38 +0200128static void foreach_remove_backward(void *data, void *user_data)
129{
130 struct queue *queue = user_data;
131
132 queue_remove(queue, UINT_TO_PTR(2));
133 queue_remove(queue, UINT_TO_PTR(1));
134}
135
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530136static void test_foreach_remove_backward(const void *data)
Luiz Augusto von Dentzc9528d92014-12-08 13:51:38 +0200137{
138 struct queue *queue;
139
140 queue = queue_new();
141 g_assert(queue != NULL);
142
143 queue_push_tail(queue, UINT_TO_PTR(1));
144 queue_push_tail(queue, UINT_TO_PTR(2));
145
146 queue_foreach(queue, foreach_remove_backward, queue);
147 queue_destroy(queue, NULL);
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530148 tester_test_passed();
Luiz Augusto von Dentzc9528d92014-12-08 13:51:38 +0200149}
150
Luiz Augusto von Dentzdbf12262014-11-17 16:31:52 +0200151static struct queue *static_queue;
152
153static void destroy_remove(void *user_data)
154{
155 queue_remove(static_queue, user_data);
156}
157
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530158static void test_destroy_remove(const void *data)
Luiz Augusto von Dentzdbf12262014-11-17 16:31:52 +0200159{
160 static_queue = queue_new();
161
162 g_assert(static_queue != NULL);
163
164 queue_push_tail(static_queue, UINT_TO_PTR(1));
165 queue_push_tail(static_queue, UINT_TO_PTR(2));
166
167 queue_destroy(static_queue, destroy_remove);
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530168 tester_test_passed();
Luiz Augusto von Dentzdbf12262014-11-17 16:31:52 +0200169}
170
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530171static void test_push_after(const void *data)
Arman Uguray28e66d92014-11-26 15:03:10 -0800172{
173 struct queue *queue;
174 unsigned int len, i;
175
176 queue = queue_new();
177 g_assert(queue != NULL);
178
179 /*
180 * Pre-populate queue. Initial elements are:
181 * [ NULL, 2, 5 ]
182 */
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);
187
188 /* Invalid insertion */
189 g_assert(!queue_push_after(queue, UINT_TO_PTR(6), UINT_TO_PTR(1)));
190
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)));
196
197 g_assert(queue_peek_head(queue) == NULL);
198 g_assert(queue_peek_tail(queue) == UINT_TO_PTR(6));
199
200 /*
201 * Queue should contain 7 elements:
202 * [ NULL, 1, 2, 3, 4, 5, 6 ]
203 */
204 len = queue_length(queue);
205 g_assert(len == 7);
206
207 for (i = 0; i < 7; i++)
208 g_assert(queue_pop_head(queue) == UINT_TO_PTR(i));
209
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)));
215
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));
Luiz Augusto von Dentz7f576692014-12-03 20:54:39 +0200220
221 queue_destroy(queue, NULL);
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530222 tester_test_passed();
Arman Uguray28e66d92014-11-26 15:03:10 -0800223}
224
Szymon Janc047c1822015-01-07 16:50:09 +0100225static bool match_int(const void *a, const void *b)
226{
227 int i = PTR_TO_INT(a);
228 int j = PTR_TO_INT(b);
229
230 return i == j;
231}
232
Szymon Jancb68c03b2015-01-07 17:26:02 +0100233static bool match_ptr(const void *a, const void *b)
234{
235 return a == b;
236}
237
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530238static void test_remove_all(const void *data)
Szymon Janc047c1822015-01-07 16:50:09 +0100239{
240 struct queue *queue;
241
242 queue = queue_new();
243 g_assert(queue != NULL);
244
245 g_assert(queue_push_tail(queue, INT_TO_PTR(10)));
246
247 g_assert(queue_remove_all(queue, match_int, INT_TO_PTR(10), NULL) == 1);
248 g_assert(queue_isempty(queue));
249
Szymon Jancb68c03b2015-01-07 17:26:02 +0100250 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));
253
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));
257
Szymon Janc047c1822015-01-07 16:50:09 +0100258 queue_destroy(queue, NULL);
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530259 tester_test_passed();
Szymon Janc047c1822015-01-07 16:50:09 +0100260}
261
Marcel Holtmann611f14d2014-01-25 11:30:52 -0800262int main(int argc, char *argv[])
263{
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530264 tester_init(&argc, &argv);
Marcel Holtmann611f14d2014-01-25 11:30:52 -0800265
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530266 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);
Marcel Holtmann611f14d2014-01-25 11:30:52 -0800279
Gowtham Anandha Babu3fb9cbb2015-05-19 20:31:02 +0530280 return tester_run();
Marcel Holtmann611f14d2014-01-25 11:30:52 -0800281}