blob: 6261612443a07935f7acce46e47e739b46515ebb [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 Dentzdbf12262014-11-17 16:31:52 +0200123static struct queue *static_queue;
124
125static void destroy_remove(void *user_data)
126{
127 queue_remove(static_queue, user_data);
128}
129
130static void test_destroy_remove(void)
131{
132 static_queue = queue_new();
133
134 g_assert(static_queue != NULL);
135
136 queue_push_tail(static_queue, UINT_TO_PTR(1));
137 queue_push_tail(static_queue, UINT_TO_PTR(2));
138
139 queue_destroy(static_queue, destroy_remove);
140}
141
Arman Uguray28e66d92014-11-26 15:03:10 -0800142static void test_push_after(void)
143{
144 struct queue *queue;
145 unsigned int len, i;
146
147 queue = queue_new();
148 g_assert(queue != NULL);
149
150 /*
151 * Pre-populate queue. Initial elements are:
152 * [ NULL, 2, 5 ]
153 */
154 g_assert(queue_push_tail(queue, NULL));
155 g_assert(queue_push_tail(queue, UINT_TO_PTR(2)));
156 g_assert(queue_push_tail(queue, UINT_TO_PTR(5)));
157 g_assert(queue_length(queue) == 3);
158
159 /* Invalid insertion */
160 g_assert(!queue_push_after(queue, UINT_TO_PTR(6), UINT_TO_PTR(1)));
161
162 /* Valid insertions */
163 g_assert(queue_push_after(queue, NULL, UINT_TO_PTR(1)));
164 g_assert(queue_push_after(queue, UINT_TO_PTR(2), UINT_TO_PTR(3)));
165 g_assert(queue_push_after(queue, UINT_TO_PTR(3), UINT_TO_PTR(4)));
166 g_assert(queue_push_after(queue, UINT_TO_PTR(5), UINT_TO_PTR(6)));
167
168 g_assert(queue_peek_head(queue) == NULL);
169 g_assert(queue_peek_tail(queue) == UINT_TO_PTR(6));
170
171 /*
172 * Queue should contain 7 elements:
173 * [ NULL, 1, 2, 3, 4, 5, 6 ]
174 */
175 len = queue_length(queue);
176 g_assert(len == 7);
177
178 for (i = 0; i < 7; i++)
179 g_assert(queue_pop_head(queue) == UINT_TO_PTR(i));
180
181 /* Test with identical elements */
182 g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
183 g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
184 g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
185 g_assert(queue_push_after(queue, UINT_TO_PTR(1), UINT_TO_PTR(0)));
186
187 g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
188 g_assert(queue_pop_head(queue) == UINT_TO_PTR(0));
189 g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
190 g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
Luiz Augusto von Dentz7f576692014-12-03 20:54:39 +0200191
192 queue_destroy(queue, NULL);
Arman Uguray28e66d92014-11-26 15:03:10 -0800193}
194
Marcel Holtmann611f14d2014-01-25 11:30:52 -0800195int main(int argc, char *argv[])
196{
197 g_test_init(&argc, &argv, NULL);
198
199 g_test_add_func("/queue/basic", test_basic);
Luiz Augusto von Dentz661d8452014-05-22 12:14:18 +0300200 g_test_add_func("/queue/foreach_destroy", test_foreach_destroy);
Luiz Augusto von Dentz7dade6c2014-12-08 13:44:15 +0200201 g_test_add_func("/queue/foreach_remove", test_foreach_remove);
Luiz Augusto von Dentzd03c4da2014-05-22 13:07:22 +0300202 g_test_add_func("/queue/foreach_remove_all", test_foreach_remove_all);
Luiz Augusto von Dentzdbf12262014-11-17 16:31:52 +0200203 g_test_add_func("/queue/destroy_remove", test_destroy_remove);
Arman Uguray28e66d92014-11-26 15:03:10 -0800204 g_test_add_func("/queue/push_after", test_push_after);
Marcel Holtmann611f14d2014-01-25 11:30:52 -0800205
206 return g_test_run();
207}