]> git.proxmox.com Git - mirror_frr.git/blob - pceplib/test/pcep_utils_ordered_list_test.c
Merge pull request #8593 from idryzhov/cmd-ambiguous
[mirror_frr.git] / pceplib / test / pcep_utils_ordered_list_test.c
1 /*
2 * This file is part of the PCEPlib, a PCEP protocol library.
3 *
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Author : Brady Johnson <brady@voltanet.io>
20 *
21 */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <assert.h>
29 #include <CUnit/CUnit.h>
30
31 #include "pcep_utils_ordered_list.h"
32 #include "pcep_utils_ordered_list_test.h"
33
34 typedef struct node_data_ {
35 int int_data;
36
37 } node_data;
38
39
40 int node_data_compare(void *list_entry, void *new_entry)
41 {
42 /*
43 * < 0 if new_entry < list_entry
44 * == 0 if new_entry == list_entry (new_entry will be inserted after
45 * list_entry) > 0 if new_entry > list_entry
46 */
47
48 return ((node_data *)new_entry)->int_data
49 - ((node_data *)list_entry)->int_data;
50 }
51
52
53 void test_empty_list()
54 {
55 ordered_list_handle *handle =
56 ordered_list_initialize(node_data_compare);
57
58 CU_ASSERT_PTR_NOT_NULL(handle);
59 assert(handle != NULL);
60 CU_ASSERT_PTR_NULL(handle->head);
61 CU_ASSERT_PTR_NOT_NULL(handle->compare_function);
62 CU_ASSERT_EQUAL(handle->num_entries, 0);
63
64 ordered_list_destroy(handle);
65 }
66
67
68 void test_null_list_handle()
69 {
70 node_data data;
71 ordered_list_node node_data;
72
73 void *ptr = ordered_list_add_node(NULL, &data);
74 CU_ASSERT_PTR_NULL(ptr);
75
76 ptr = ordered_list_find(NULL, &data);
77 CU_ASSERT_PTR_NULL(ptr);
78
79 ptr = ordered_list_remove_first_node(NULL);
80 CU_ASSERT_PTR_NULL(ptr);
81
82 ptr = ordered_list_remove_first_node_equals(NULL, &data);
83 CU_ASSERT_PTR_NULL(ptr);
84
85 ptr = ordered_list_remove_node(NULL, &node_data, &node_data);
86 CU_ASSERT_PTR_NULL(ptr);
87 }
88
89
90 void test_add_to_list()
91 {
92 node_data data1, data2, data3;
93 data1.int_data = 1;
94 data2.int_data = 2;
95 data3.int_data = 3;
96
97 ordered_list_handle *handle =
98 ordered_list_initialize(node_data_compare);
99
100 ordered_list_add_node(handle, &data3);
101 ordered_list_add_node(handle, &data1);
102 ordered_list_add_node(handle, &data2);
103
104 CU_ASSERT_EQUAL(handle->num_entries, 3);
105
106 ordered_list_node *node = handle->head;
107 CU_ASSERT_PTR_EQUAL(node->data, &data1);
108
109 node = node->next_node;
110 CU_ASSERT_PTR_EQUAL(node->data, &data2);
111
112 node = node->next_node;
113 CU_ASSERT_PTR_EQUAL(node->data, &data3);
114
115 node = node->next_node;
116 CU_ASSERT_PTR_EQUAL(node, NULL);
117
118 ordered_list_destroy(handle);
119 }
120
121
122 void test_find()
123 {
124 node_data data1, data2, data3, data_not_inList;
125 data1.int_data = 1;
126 data2.int_data = 2;
127 data3.int_data = 3;
128 data_not_inList.int_data = 5;
129
130 ordered_list_handle *handle =
131 ordered_list_initialize(node_data_compare);
132
133 ordered_list_add_node(handle, &data3);
134 ordered_list_add_node(handle, &data2);
135 ordered_list_add_node(handle, &data1);
136
137 ordered_list_node *node = ordered_list_find(handle, &data1);
138 CU_ASSERT_PTR_NOT_NULL(node);
139 assert(node != NULL);
140 CU_ASSERT_PTR_EQUAL(node->data, &data1);
141
142 node = ordered_list_find(handle, &data2);
143 CU_ASSERT_PTR_NOT_NULL(node);
144 assert(node != NULL);
145 CU_ASSERT_PTR_EQUAL(node->data, &data2);
146
147 node = ordered_list_find(handle, &data3);
148 CU_ASSERT_PTR_NOT_NULL(node);
149 assert(node != NULL);
150 CU_ASSERT_PTR_EQUAL(node->data, &data3);
151
152 node = ordered_list_find(handle, &data_not_inList);
153 CU_ASSERT_PTR_NULL(node);
154
155 ordered_list_destroy(handle);
156 }
157
158
159 void test_remove_first_node()
160 {
161 node_data data1, data2, data3;
162 data1.int_data = 1;
163 data2.int_data = 2;
164 data3.int_data = 3;
165
166 ordered_list_handle *handle =
167 ordered_list_initialize(node_data_compare);
168
169 ordered_list_add_node(handle, &data1);
170 ordered_list_add_node(handle, &data2);
171 ordered_list_add_node(handle, &data3);
172
173 void *node_data = ordered_list_remove_first_node(handle);
174 CU_ASSERT_PTR_NOT_NULL(node_data);
175 CU_ASSERT_PTR_EQUAL(node_data, &data1);
176 CU_ASSERT_EQUAL(handle->num_entries, 2);
177
178 node_data = ordered_list_remove_first_node(handle);
179 CU_ASSERT_PTR_NOT_NULL(node_data);
180 CU_ASSERT_PTR_EQUAL(node_data, &data2);
181 CU_ASSERT_EQUAL(handle->num_entries, 1);
182
183 node_data = ordered_list_remove_first_node(handle);
184 CU_ASSERT_PTR_NOT_NULL(node_data);
185 CU_ASSERT_PTR_EQUAL(node_data, &data3);
186 CU_ASSERT_EQUAL(handle->num_entries, 0);
187 CU_ASSERT_PTR_NULL(handle->head);
188
189 node_data = ordered_list_remove_first_node(handle);
190 CU_ASSERT_PTR_NULL(node_data);
191
192 ordered_list_destroy(handle);
193 }
194
195
196 void test_remove_first_node_equals()
197 {
198 node_data data1, data2, data3;
199 data1.int_data = 1;
200 data2.int_data = 2;
201 data3.int_data = 3;
202
203 ordered_list_handle *handle =
204 ordered_list_initialize(node_data_compare);
205
206 ordered_list_add_node(handle, &data1);
207 ordered_list_add_node(handle, &data2);
208 ordered_list_add_node(handle, &data3);
209
210 void *node_data = ordered_list_remove_first_node_equals(handle, &data2);
211 CU_ASSERT_PTR_NOT_NULL(node_data);
212 CU_ASSERT_PTR_EQUAL(node_data, &data2);
213 CU_ASSERT_EQUAL(handle->num_entries, 2);
214
215 node_data = ordered_list_remove_first_node_equals(handle, &data3);
216 CU_ASSERT_PTR_NOT_NULL(node_data);
217 CU_ASSERT_PTR_EQUAL(node_data, &data3);
218 CU_ASSERT_EQUAL(handle->num_entries, 1);
219
220 node_data = ordered_list_remove_first_node_equals(handle, &data1);
221 CU_ASSERT_PTR_NOT_NULL(node_data);
222 CU_ASSERT_PTR_EQUAL(node_data, &data1);
223 CU_ASSERT_EQUAL(handle->num_entries, 0);
224
225 node_data = ordered_list_remove_first_node_equals(handle, &data1);
226 CU_ASSERT_PTR_NULL(node_data);
227
228 ordered_list_destroy(handle);
229 }
230
231
232 void test_remove_node()
233 {
234 node_data data1, data2, data3;
235 data1.int_data = 1;
236 data2.int_data = 2;
237 data3.int_data = 3;
238
239 ordered_list_handle *handle =
240 ordered_list_initialize(node_data_compare);
241
242 ordered_list_node *node1 = ordered_list_add_node(handle, &data1);
243 ordered_list_node *node2 = ordered_list_add_node(handle, &data2);
244 ordered_list_node *node3 = ordered_list_add_node(handle, &data3);
245
246 void *node_data = ordered_list_remove_node(handle, node2, node3);
247 CU_ASSERT_PTR_NOT_NULL(node_data);
248 CU_ASSERT_PTR_EQUAL(node_data, &data3);
249 CU_ASSERT_EQUAL(handle->num_entries, 2);
250
251 node_data = ordered_list_remove_node(handle, node1, node2);
252 CU_ASSERT_PTR_NOT_NULL(node_data);
253 CU_ASSERT_PTR_EQUAL(node_data, &data2);
254 CU_ASSERT_EQUAL(handle->num_entries, 1);
255
256 ordered_list_destroy(handle);
257 }