File: test-queue.c

package info (click to toggle)
ell 0.81-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,344 kB
  • sloc: ansic: 61,546; sh: 4,896; makefile: 574
file content (99 lines) | stat: -rw-r--r-- 1,932 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
/*
 * Embedded Linux library
 * Copyright (C) 2011-2014  Intel Corporation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

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

#include <stdio.h>
#include <assert.h>

#include <ell/ell.h>

static void test_push_pop(const void *data)
{
	struct l_queue *queue;
	unsigned int n, i;

	queue = l_queue_new();
	assert(queue);

	for (n = 0; n < 1024; n++) {
		for (i = 1; i < n + 2; i++) {
			l_queue_push_tail(queue, L_UINT_TO_PTR(i));
		}

		if (l_queue_length(queue) != n + 1) {
			printf("[%u] length mismatch: %u\n", n,
						l_queue_length(queue));
		}

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

			if (!ptr) {
				printf("[%u] entry empty: %u\n", n, i);
				continue;
			}

			if (i != L_PTR_TO_UINT(ptr)) {
				printf("[%u] entry misatch: %u != %u\n",
						n, i, L_PTR_TO_UINT(ptr));
			}
		}

		if (!l_queue_isempty(queue)) {
			printf("[%u] not empty: %u\n", n,
						l_queue_length(queue));
		}
	}

	l_queue_destroy(queue, NULL);
}

static int queue_compare(const void *a, const void *b, void *user)
{
	int ai = L_PTR_TO_INT(a);
	int bi = L_PTR_TO_INT(b);

	return ai - bi;
}

static void test_insert(const void *data)
{
	int unsorted[] = { 0, 50, 10, 20, 30, 5, 30 };
	int sorted[] = { 0, 5, 10, 20, 30, 30, 50 };
	struct l_queue *queue;
	const struct l_queue_entry *entry;
	unsigned int i;
	int n;

	queue = l_queue_new();
	assert(queue);

	for (i = 0; i < L_ARRAY_SIZE(unsorted); i++)
		l_queue_insert(queue, L_INT_TO_PTR(unsorted[i]),
						queue_compare, NULL);

	for (i = 0, entry = l_queue_get_entries(queue); entry;
					entry = entry->next, i++) {
		n = L_PTR_TO_INT(entry->data);
		assert(n == sorted[i]);
	}

	l_queue_destroy(queue, NULL);
}

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

	l_test_add("queue push & pop", test_push_pop, NULL);
	l_test_add("queue insert", test_insert, NULL);

	return l_test_run();
}