blob: 12319b01fcffbe39223d00f19fba47356a3e692f [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"
32
33static void test_basic(void)
34{
35 struct queue *queue;
36 unsigned int n, i;
37
38 queue = queue_new();
39 g_assert(queue != NULL);
40
41 for (n = 0; n < 1024; n++) {
42 for (i = 1; i < n + 2; i++)
43 queue_push_tail(queue, UINT_TO_PTR(i));
44
45 g_assert(queue_length(queue) == n + 1);
46
47 for (i = 1; i < n + 2; i++) {
48 void *ptr;
49
50 ptr = queue_pop_head(queue);
51 g_assert(ptr != NULL);
52 g_assert(i == PTR_TO_UINT(ptr));
53 }
54
55 g_assert(queue_isempty(queue) == true);
56 }
57
58 queue_destroy(queue, NULL);
59}
60
Luiz Augusto von Dentz661d8452014-05-22 12:14:18 +030061static void foreach_destroy(void *data, void *user_data)
62{
63 struct queue *queue = user_data;
64
65 queue_destroy(queue, NULL);
66}
67
68static void test_foreach_destroy(void)
69{
70 struct queue *queue;
71
72 queue = queue_new();
73 g_assert(queue != NULL);
74
75 queue_push_tail(queue, UINT_TO_PTR(1));
76 queue_push_tail(queue, UINT_TO_PTR(2));
77
78 queue_foreach(queue, foreach_destroy, queue);
79}
80
Luiz Augusto von Dentz7dade6c2014-12-08 13:44:15 +020081static void foreach_remove(void *data, void *user_data)
82{
83 struct queue *queue = user_data;
84
85 g_assert(queue_remove(queue, data));
86}
87
88static void test_foreach_remove(void)
89{
90 struct queue *queue;
91
92 queue = queue_new();
93 g_assert(queue != NULL);
94
95 queue_push_tail(queue, UINT_TO_PTR(1));
96 queue_push_tail(queue, UINT_TO_PTR(2));
97
98 queue_foreach(queue, foreach_remove, queue);
99 queue_destroy(queue, NULL);
100}
101
Luiz Augusto von Dentzd03c4da2014-05-22 13:07:22 +0300102static void foreach_remove_all(void *data, void *user_data)
103{
104 struct queue *queue = user_data;
105
106 queue_remove_all(queue, NULL, NULL, NULL);
107}
108
109static void test_foreach_remove_all(void)
110{
111 struct queue *queue;
112
113 queue = queue_new();
114 g_assert(queue != NULL);
115
116 queue_push_tail(queue, UINT_TO_PTR(1));
117 queue_push_tail(queue, UINT_TO_PTR(2));
118
119 queue_foreach(queue, foreach_remove_all, queue);
Luiz Augusto von Dentz3053eb72014-06-27 13:51:46 +0300120 queue_destroy(queue, NULL);
Luiz Augusto von Dentzd03c4da2014-05-22 13:07:22 +0300121}
122
Luiz Augusto von Dentzc9528d92014-12-08 13:51:38 +0200123static void foreach_remove_backward(void *data, void *user_data)
124{
125 struct queue *queue = user_data;
126
127 queue_remove(queue, UINT_TO_PTR(2));
128 queue_remove(queue, UINT_TO_PTR(1));
129}
130
131static void test_foreach_remove_backward(void)
132{
133 struct queue *queue;
134
135 queue = queue_new();
136 g_assert(queue != NULL);
137
138 queue_push_tail(queue, UINT_TO_PTR(1));
139 queue_push_tail(queue, UINT_TO_PTR(2));
140
141 queue_foreach(queue, foreach_remove_backward, queue);
142 queue_destroy(queue, NULL);
143}
144
Luiz Augusto von Dentzdbf12262014-11-17 16:31:52 +0200145static struct queue *static_queue;
146
147static void destroy_remove(void *user_data)
148{
149 queue_remove(static_queue, user_data);
150}
151
152static void test_destroy_remove(void)
153{
154 static_queue = queue_new();
155
156 g_assert(static_queue != NULL);
157
158 queue_push_tail(static_queue, UINT_TO_PTR(1));
159 queue_push_tail(static_queue, UINT_TO_PTR(2));
160
161 queue_destroy(static_queue, destroy_remove);
162}
163
Arman Uguray28e66d92014-11-26 15:03:10 -0800164static void test_push_after(void)
165{
166 struct queue *queue;
167 unsigned int len, i;
168
169 queue = queue_new();
170 g_assert(queue != NULL);
171
172 /*
173 * Pre-populate queue. Initial elements are:
174 * [ NULL, 2, 5 ]
175 */
176 g_assert(queue_push_tail(queue, NULL));
177 g_assert(queue_push_tail(queue, UINT_TO_PTR(2)));
178 g_assert(queue_push_tail(queue, UINT_TO_PTR(5)));
179 g_assert(queue_length(queue) == 3);
180
181 /* Invalid insertion */
182 g_assert(!queue_push_after(queue, UINT_TO_PTR(6), UINT_TO_PTR(1)));
183
184 /* Valid insertions */
185 g_assert(queue_push_after(queue, NULL, UINT_TO_PTR(1)));
186 g_assert(queue_push_after(queue, UINT_TO_PTR(2), UINT_TO_PTR(3)));
187 g_assert(queue_push_after(queue, UINT_TO_PTR(3), UINT_TO_PTR(4)));
188 g_assert(queue_push_after(queue, UINT_TO_PTR(5), UINT_TO_PTR(6)));
189
190 g_assert(queue_peek_head(queue) == NULL);
191 g_assert(queue_peek_tail(queue) == UINT_TO_PTR(6));
192
193 /*
194 * Queue should contain 7 elements:
195 * [ NULL, 1, 2, 3, 4, 5, 6 ]
196 */
197 len = queue_length(queue);
198 g_assert(len == 7);
199
200 for (i = 0; i < 7; i++)
201 g_assert(queue_pop_head(queue) == UINT_TO_PTR(i));
202
203 /* Test with identical elements */
204 g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
205 g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
206 g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
207 g_assert(queue_push_after(queue, UINT_TO_PTR(1), UINT_TO_PTR(0)));
208
209 g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
210 g_assert(queue_pop_head(queue) == UINT_TO_PTR(0));
211 g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
212 g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
Luiz Augusto von Dentz7f576692014-12-03 20:54:39 +0200213
214 queue_destroy(queue, NULL);
Arman Uguray28e66d92014-11-26 15:03:10 -0800215}
216
Szymon Janc047c1822015-01-07 16:50:09 +0100217static bool match_int(const void *a, const void *b)
218{
219 int i = PTR_TO_INT(a);
220 int j = PTR_TO_INT(b);
221
222 return i == j;
223}
224
Szymon Jancb68c03b2015-01-07 17:26:02 +0100225static bool match_ptr(const void *a, const void *b)
226{
227 return a == b;
228}
229
Szymon Janc047c1822015-01-07 16:50:09 +0100230static void test_remove_all(void)
231{
232 struct queue *queue;
233
234 queue = queue_new();
235 g_assert(queue != NULL);
236
237 g_assert(queue_push_tail(queue, INT_TO_PTR(10)));
238
239 g_assert(queue_remove_all(queue, match_int, INT_TO_PTR(10), NULL) == 1);
240 g_assert(queue_isempty(queue));
241
Szymon Jancb68c03b2015-01-07 17:26:02 +0100242 g_assert(queue_push_tail(queue, NULL));
243 g_assert(queue_remove_all(queue, match_ptr, NULL, NULL) == 1);
244 g_assert(queue_isempty(queue));
245
246 g_assert(queue_push_tail(queue, UINT_TO_PTR(0)));
247 g_assert(queue_remove_all(queue, match_int, UINT_TO_PTR(0), NULL) == 1);
248 g_assert(queue_isempty(queue));
249
Szymon Janc047c1822015-01-07 16:50:09 +0100250 queue_destroy(queue, NULL);
251}
252
Marcel Holtmann611f14d2014-01-25 11:30:52 -0800253int main(int argc, char *argv[])
254{
255 g_test_init(&argc, &argv, NULL);
256
257 g_test_add_func("/queue/basic", test_basic);
Luiz Augusto von Dentz661d8452014-05-22 12:14:18 +0300258 g_test_add_func("/queue/foreach_destroy", test_foreach_destroy);
Luiz Augusto von Dentz7dade6c2014-12-08 13:44:15 +0200259 g_test_add_func("/queue/foreach_remove", test_foreach_remove);
Luiz Augusto von Dentzd03c4da2014-05-22 13:07:22 +0300260 g_test_add_func("/queue/foreach_remove_all", test_foreach_remove_all);
Luiz Augusto von Dentzc9528d92014-12-08 13:51:38 +0200261 g_test_add_func("/queue/foreach_remove_backward",
262 test_foreach_remove_backward);
Luiz Augusto von Dentzdbf12262014-11-17 16:31:52 +0200263 g_test_add_func("/queue/destroy_remove", test_destroy_remove);
Arman Uguray28e66d92014-11-26 15:03:10 -0800264 g_test_add_func("/queue/push_after", test_push_after);
Szymon Janc047c1822015-01-07 16:50:09 +0100265 g_test_add_func("/queue/remove_all", test_remove_all);
Marcel Holtmann611f14d2014-01-25 11:30:52 -0800266
267 return g_test_run();
268}