File: test-queue.c

package info (click to toggle)
bluez 5.23-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 13,264 kB
  • sloc: ansic: 252,436; sh: 11,494; python: 3,509; makefile: 618; xml: 126
file content (111 lines) | stat: -rw-r--r-- 2,557 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2012  Intel Corporation. All rights reserved.
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>

#include "src/shared/util.h"
#include "src/shared/queue.h"

static void test_basic(void)
{
	struct queue *queue;
	unsigned int n, i;

	queue = queue_new();
	g_assert(queue != NULL);

	for (n = 0; n < 1024; n++) {
		for (i = 1; i < n + 2; i++)
			queue_push_tail(queue, UINT_TO_PTR(i));

		g_assert(queue_length(queue) == n + 1);

		for (i = 1; i < n + 2; i++) {
			void *ptr;

			ptr = queue_pop_head(queue);
			g_assert(ptr != NULL);
			g_assert(i == PTR_TO_UINT(ptr));
		}

		g_assert(queue_isempty(queue) == true);
	}

	queue_destroy(queue, NULL);
}

static void foreach_destroy(void *data, void *user_data)
{
	struct queue *queue = user_data;

	queue_destroy(queue, NULL);
}

static void test_foreach_destroy(void)
{
	struct queue *queue;

	queue = queue_new();
	g_assert(queue != NULL);

	queue_push_tail(queue, UINT_TO_PTR(1));
	queue_push_tail(queue, UINT_TO_PTR(2));

	queue_foreach(queue, foreach_destroy, queue);
}

static void foreach_remove_all(void *data, void *user_data)
{
	struct queue *queue = user_data;

	queue_remove_all(queue, NULL, NULL, NULL);
}

static void test_foreach_remove_all(void)
{
	struct queue *queue;

	queue = queue_new();
	g_assert(queue != NULL);

	queue_push_tail(queue, UINT_TO_PTR(1));
	queue_push_tail(queue, UINT_TO_PTR(2));

	queue_foreach(queue, foreach_remove_all, queue);
	queue_destroy(queue, NULL);
}

int main(int argc, char *argv[])
{
	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/queue/basic", test_basic);
	g_test_add_func("/queue/foreach_destroy", test_foreach_destroy);
	g_test_add_func("/queue/foreach_remove_all", test_foreach_remove_all);

	return g_test_run();
}