Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / unit / test-queue.c
1 /*
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 #include "src/shared/tester.h"
33
34 static void test_basic(const void *data)
35 {
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);
60         tester_test_passed();
61 }
62
63 static void foreach_destroy(void *data, void *user_data)
64 {
65         struct queue *queue = user_data;
66
67         queue_destroy(queue, NULL);
68 }
69
70 static void test_foreach_destroy(const void *data)
71 {
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);
81         tester_test_passed();
82 }
83
84 static 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
91 static void test_foreach_remove(const void *data)
92 {
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);
103         tester_test_passed();
104 }
105
106 static 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
113 static void test_foreach_remove_all(const void *data)
114 {
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);
124         queue_destroy(queue, NULL);
125         tester_test_passed();
126 }
127
128 static 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
136 static void test_foreach_remove_backward(const void *data)
137 {
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);
148         tester_test_passed();
149 }
150
151 static struct queue *static_queue;
152
153 static void destroy_remove(void *user_data)
154 {
155         queue_remove(static_queue, user_data);
156 }
157
158 static void test_destroy_remove(const void *data)
159 {
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);
168         tester_test_passed();
169 }
170
171 static void test_push_after(const void *data)
172 {
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));
220
221         queue_destroy(queue, NULL);
222         tester_test_passed();
223 }
224
225 static 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
233 static bool match_ptr(const void *a, const void *b)
234 {
235         return a == b;
236 }
237
238 static void test_remove_all(const void *data)
239 {
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
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));
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
258         queue_destroy(queue, NULL);
259         tester_test_passed();
260 }
261
262 int main(int argc, char *argv[])
263 {
264         tester_init(&argc, &argv);
265
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);
279
280         return tester_run();
281 }