]> git.proxmox.com Git - mirror_frr.git/blob - pceplib/test/pcep_utils_ordered_list_test.c
Merge pull request #8441 from mjstapp/fix_topo_pylint1
[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 <CUnit/CUnit.h>
29
30 #include "pcep_utils_ordered_list.h"
31 #include "pcep_utils_ordered_list_test.h"
32
33 typedef struct node_data_ {
34 int int_data;
35
36 } node_data;
37
38
39 int node_data_compare(void *list_entry, void *new_entry)
40 {
41 /*
42 * < 0 if new_entry < list_entry
43 * == 0 if new_entry == list_entry (new_entry will be inserted after
44 * list_entry) > 0 if new_entry > list_entry
45 */
46
47 return ((node_data *)new_entry)->int_data
48 - ((node_data *)list_entry)->int_data;
49 }
50
51
52 void test_empty_list()
53 {
54 ordered_list_handle *handle =
55 ordered_list_initialize(node_data_compare);
56
57 CU_ASSERT_PTR_NOT_NULL(handle);
58 CU_ASSERT_PTR_NULL(handle->head);
59 CU_ASSERT_PTR_NOT_NULL(handle->compare_function);
60 CU_ASSERT_EQUAL(handle->num_entries, 0);
61
62 ordered_list_destroy(handle);
63 }
64
65
66 void test_null_list_handle()
67 {
68 node_data data;
69 ordered_list_node node_data;
70
71 void *ptr = ordered_list_add_node(NULL, &data);
72 CU_ASSERT_PTR_NULL(ptr);
73
74 ptr = ordered_list_find(NULL, &data);
75 CU_ASSERT_PTR_NULL(ptr);
76
77 ptr = ordered_list_remove_first_node(NULL);
78 CU_ASSERT_PTR_NULL(ptr);
79
80 ptr = ordered_list_remove_first_node_equals(NULL, &data);
81 CU_ASSERT_PTR_NULL(ptr);
82
83 ptr = ordered_list_remove_node(NULL, &node_data, &node_data);
84 CU_ASSERT_PTR_NULL(ptr);
85 }
86
87
88 void test_add_to_list()
89 {
90 node_data data1, data2, data3;
91 data1.int_data = 1;
92 data2.int_data = 2;
93 data3.int_data = 3;
94
95 ordered_list_handle *handle =
96 ordered_list_initialize(node_data_compare);
97
98 ordered_list_add_node(handle, &data3);
99 ordered_list_add_node(handle, &data1);
100 ordered_list_add_node(handle, &data2);
101
102 CU_ASSERT_EQUAL(handle->num_entries, 3);
103
104 ordered_list_node *node = handle->head;
105 CU_ASSERT_PTR_EQUAL(node->data, &data1);
106
107 node = node->next_node;
108 CU_ASSERT_PTR_EQUAL(node->data, &data2);
109
110 node = node->next_node;
111 CU_ASSERT_PTR_EQUAL(node->data, &data3);
112
113 node = node->next_node;
114 CU_ASSERT_PTR_EQUAL(node, NULL);
115
116 ordered_list_destroy(handle);
117 }
118
119
120 void test_find()
121 {
122 node_data data1, data2, data3, data_not_inList;
123 data1.int_data = 1;
124 data2.int_data = 2;
125 data3.int_data = 3;
126 data_not_inList.int_data = 5;
127
128 ordered_list_handle *handle =
129 ordered_list_initialize(node_data_compare);
130
131 ordered_list_add_node(handle, &data3);
132 ordered_list_add_node(handle, &data2);
133 ordered_list_add_node(handle, &data1);
134
135 ordered_list_node *node = ordered_list_find(handle, &data1);
136 CU_ASSERT_PTR_NOT_NULL(node);
137 CU_ASSERT_PTR_EQUAL(node->data, &data1);
138
139 node = ordered_list_find(handle, &data2);
140 CU_ASSERT_PTR_NOT_NULL(node);
141 CU_ASSERT_PTR_EQUAL(node->data, &data2);
142
143 node = ordered_list_find(handle, &data3);
144 CU_ASSERT_PTR_NOT_NULL(node);
145 CU_ASSERT_PTR_EQUAL(node->data, &data3);
146
147 node = ordered_list_find(handle, &data_not_inList);
148 CU_ASSERT_PTR_NULL(node);
149
150 ordered_list_destroy(handle);
151 }
152
153
154 void test_remove_first_node()
155 {
156 node_data data1, data2, data3;
157 data1.int_data = 1;
158 data2.int_data = 2;
159 data3.int_data = 3;
160
161 ordered_list_handle *handle =
162 ordered_list_initialize(node_data_compare);
163
164 ordered_list_add_node(handle, &data1);
165 ordered_list_add_node(handle, &data2);
166 ordered_list_add_node(handle, &data3);
167
168 void *node_data = ordered_list_remove_first_node(handle);
169 CU_ASSERT_PTR_NOT_NULL(node_data);
170 CU_ASSERT_PTR_EQUAL(node_data, &data1);
171 CU_ASSERT_EQUAL(handle->num_entries, 2);
172
173 node_data = ordered_list_remove_first_node(handle);
174 CU_ASSERT_PTR_NOT_NULL(node_data);
175 CU_ASSERT_PTR_EQUAL(node_data, &data2);
176 CU_ASSERT_EQUAL(handle->num_entries, 1);
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, &data3);
181 CU_ASSERT_EQUAL(handle->num_entries, 0);
182 CU_ASSERT_PTR_NULL(handle->head);
183
184 node_data = ordered_list_remove_first_node(handle);
185 CU_ASSERT_PTR_NULL(node_data);
186
187 ordered_list_destroy(handle);
188 }
189
190
191 void test_remove_first_node_equals()
192 {
193 node_data data1, data2, data3;
194 data1.int_data = 1;
195 data2.int_data = 2;
196 data3.int_data = 3;
197
198 ordered_list_handle *handle =
199 ordered_list_initialize(node_data_compare);
200
201 ordered_list_add_node(handle, &data1);
202 ordered_list_add_node(handle, &data2);
203 ordered_list_add_node(handle, &data3);
204
205 void *node_data = ordered_list_remove_first_node_equals(handle, &data2);
206 CU_ASSERT_PTR_NOT_NULL(node_data);
207 CU_ASSERT_PTR_EQUAL(node_data, &data2);
208 CU_ASSERT_EQUAL(handle->num_entries, 2);
209
210 node_data = ordered_list_remove_first_node_equals(handle, &data3);
211 CU_ASSERT_PTR_NOT_NULL(node_data);
212 CU_ASSERT_PTR_EQUAL(node_data, &data3);
213 CU_ASSERT_EQUAL(handle->num_entries, 1);
214
215 node_data = ordered_list_remove_first_node_equals(handle, &data1);
216 CU_ASSERT_PTR_NOT_NULL(node_data);
217 CU_ASSERT_PTR_EQUAL(node_data, &data1);
218 CU_ASSERT_EQUAL(handle->num_entries, 0);
219
220 node_data = ordered_list_remove_first_node_equals(handle, &data1);
221 CU_ASSERT_PTR_NULL(node_data);
222
223 ordered_list_destroy(handle);
224 }
225
226
227 void test_remove_node()
228 {
229 node_data data1, data2, data3;
230 data1.int_data = 1;
231 data2.int_data = 2;
232 data3.int_data = 3;
233
234 ordered_list_handle *handle =
235 ordered_list_initialize(node_data_compare);
236
237 ordered_list_node *node1 = ordered_list_add_node(handle, &data1);
238 ordered_list_node *node2 = ordered_list_add_node(handle, &data2);
239 ordered_list_node *node3 = ordered_list_add_node(handle, &data3);
240
241 void *node_data = ordered_list_remove_node(handle, node2, node3);
242 CU_ASSERT_PTR_NOT_NULL(node_data);
243 CU_ASSERT_PTR_EQUAL(node_data, &data3);
244 CU_ASSERT_EQUAL(handle->num_entries, 2);
245
246 node_data = ordered_list_remove_node(handle, node1, node2);
247 CU_ASSERT_PTR_NOT_NULL(node_data);
248 CU_ASSERT_PTR_EQUAL(node_data, &data2);
249 CU_ASSERT_EQUAL(handle->num_entries, 1);
250
251 ordered_list_destroy(handle);
252 }