blob: 55273e33b9bd3a579bc07f3adaa7d175840d10ea [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
Marcel Holtmann611f14d2014-01-25 11:30:52 -080081int main(int argc, char *argv[])
82{
83 g_test_init(&argc, &argv, NULL);
84
85 g_test_add_func("/queue/basic", test_basic);
Luiz Augusto von Dentz661d8452014-05-22 12:14:18 +030086 g_test_add_func("/queue/foreach_destroy", test_foreach_destroy);
Marcel Holtmann611f14d2014-01-25 11:30:52 -080087
88 return g_test_run();
89}