summaryrefslogtreecommitdiffstats
path: root/pceplib/test/pcep_utils_queue_test.c
blob: 6e62c75636f1a20e015efc104963135733dc6709 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * This file is part of the PCEPlib, a PCEP protocol library.
 *
 * Copyright (C) 2020 Volta Networks https://voltanet.io/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Author : Brady Johnson <brady@voltanet.io>
 *
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <CUnit/CUnit.h>

#include "pcep_utils_queue.h"
#include "pcep_utils_queue_test.h"

typedef struct node_data_ {
	int int_data;

} node_data;


void test_empty_queue()
{
	queue_handle *handle = queue_initialize();

	CU_ASSERT_PTR_NOT_NULL(handle);
	assert(handle != NULL);
	CU_ASSERT_PTR_NULL(handle->head);
	CU_ASSERT_EQUAL(handle->num_entries, 0);

	queue_destroy(handle);
}


void test_null_queue_handle()
{
	/* test each method handles a NULL handle without crashing */
	node_data data;
	queue_destroy(NULL);
	void *ptr = queue_enqueue(NULL, &data);
	CU_ASSERT_PTR_NULL(ptr);

	ptr = queue_dequeue(NULL);
	CU_ASSERT_PTR_NULL(ptr);
}


void test_enqueue()
{
	node_data data1, data2, data3;
	data1.int_data = 1;
	data2.int_data = 2;
	data3.int_data = 3;

	queue_handle *handle = queue_initialize();

	queue_enqueue(handle, &data1);
	queue_enqueue(handle, &data2);
	queue_enqueue(handle, &data3);

	CU_ASSERT_EQUAL(handle->num_entries, 3);

	queue_node *node = handle->head;
	CU_ASSERT_PTR_EQUAL(node->data, &data1);

	node = node->next_node;
	CU_ASSERT_PTR_EQUAL(node->data, &data2);

	node = node->next_node;
	CU_ASSERT_PTR_EQUAL(node->data, &data3);

	node = node->next_node;
	CU_ASSERT_PTR_NULL(node);

	queue_destroy(handle);
}


void test_enqueue_with_limit()
{
	node_data data1, data2, data3;
	data1.int_data = 1;
	data2.int_data = 2;
	data3.int_data = 3;

	queue_handle *handle = queue_initialize_with_size(2);

	queue_node *node = queue_enqueue(handle, &data1);
	CU_ASSERT_PTR_NOT_NULL(node);

	node = queue_enqueue(handle, &data2);
	CU_ASSERT_PTR_NOT_NULL(node);

	node = queue_enqueue(handle, &data3);
	CU_ASSERT_PTR_NULL(node);

	CU_ASSERT_EQUAL(handle->num_entries, 2);

	node = handle->head;
	CU_ASSERT_PTR_EQUAL(node->data, &data1);

	node = node->next_node;
	CU_ASSERT_PTR_EQUAL(node->data, &data2);

	node = node->next_node;
	CU_ASSERT_PTR_NULL(node);

	queue_destroy(handle);
}


void test_dequeue()
{
	node_data data1, data2, data3;
	data1.int_data = 1;
	data2.int_data = 2;
	data3.int_data = 3;

	queue_handle *handle = queue_initialize();

	/* first test dequeue handles an empty queue */
	void *node_data = queue_dequeue(handle);
	CU_ASSERT_PTR_NULL(node_data);

	queue_enqueue(handle, &data1);
	queue_enqueue(handle, &data2);
	queue_enqueue(handle, &data3);

	node_data = queue_dequeue(handle);
	CU_ASSERT_PTR_EQUAL(node_data, &data1);
	CU_ASSERT_EQUAL(handle->num_entries, 2);

	node_data = queue_dequeue(handle);
	CU_ASSERT_PTR_EQUAL(node_data, &data2);
	CU_ASSERT_EQUAL(handle->num_entries, 1);

	node_data = queue_dequeue(handle);
	CU_ASSERT_PTR_EQUAL(node_data, &data3);
	CU_ASSERT_EQUAL(handle->num_entries, 0);

	node_data = queue_dequeue(handle);
	CU_ASSERT_PTR_NULL(node_data);

	queue_destroy(handle);
}