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