]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: LGPL-2.1-or-later |
74971473 JG |
2 | /* |
3 | * This file is part of the PCEPlib, a PCEP protocol library. | |
4 | * | |
5 | * Copyright (C) 2020 Volta Networks https://voltanet.io/ | |
6 | * | |
74971473 JG |
7 | * Author : Brady Johnson <brady@voltanet.io> |
8 | * | |
9 | */ | |
10 | ||
11 | ||
1f8031f7 DL |
12 | #ifdef HAVE_CONFIG_H |
13 | #include "config.h" | |
14 | #endif | |
15 | ||
fadf00aa | 16 | #include <assert.h> |
74971473 JG |
17 | #include <CUnit/CUnit.h> |
18 | ||
19 | #include "pcep_utils_ordered_list.h" | |
20 | #include "pcep_utils_ordered_list_test.h" | |
21 | ||
22 | typedef struct node_data_ { | |
23 | int int_data; | |
24 | ||
25 | } node_data; | |
26 | ||
27 | ||
28 | int node_data_compare(void *list_entry, void *new_entry) | |
29 | { | |
30 | /* | |
31 | * < 0 if new_entry < list_entry | |
32 | * == 0 if new_entry == list_entry (new_entry will be inserted after | |
33 | * list_entry) > 0 if new_entry > list_entry | |
34 | */ | |
35 | ||
36 | return ((node_data *)new_entry)->int_data | |
37 | - ((node_data *)list_entry)->int_data; | |
38 | } | |
39 | ||
40 | ||
41 | void test_empty_list() | |
42 | { | |
43 | ordered_list_handle *handle = | |
44 | ordered_list_initialize(node_data_compare); | |
45 | ||
46 | CU_ASSERT_PTR_NOT_NULL(handle); | |
fadf00aa | 47 | assert(handle != NULL); |
74971473 JG |
48 | CU_ASSERT_PTR_NULL(handle->head); |
49 | CU_ASSERT_PTR_NOT_NULL(handle->compare_function); | |
50 | CU_ASSERT_EQUAL(handle->num_entries, 0); | |
51 | ||
52 | ordered_list_destroy(handle); | |
53 | } | |
54 | ||
55 | ||
56 | void test_null_list_handle() | |
57 | { | |
58 | node_data data; | |
59 | ordered_list_node node_data; | |
60 | ||
61 | void *ptr = ordered_list_add_node(NULL, &data); | |
62 | CU_ASSERT_PTR_NULL(ptr); | |
63 | ||
64 | ptr = ordered_list_find(NULL, &data); | |
65 | CU_ASSERT_PTR_NULL(ptr); | |
66 | ||
67 | ptr = ordered_list_remove_first_node(NULL); | |
68 | CU_ASSERT_PTR_NULL(ptr); | |
69 | ||
70 | ptr = ordered_list_remove_first_node_equals(NULL, &data); | |
71 | CU_ASSERT_PTR_NULL(ptr); | |
72 | ||
73 | ptr = ordered_list_remove_node(NULL, &node_data, &node_data); | |
74 | CU_ASSERT_PTR_NULL(ptr); | |
75 | } | |
76 | ||
77 | ||
78 | void test_add_to_list() | |
79 | { | |
80 | node_data data1, data2, data3; | |
81 | data1.int_data = 1; | |
82 | data2.int_data = 2; | |
83 | data3.int_data = 3; | |
84 | ||
85 | ordered_list_handle *handle = | |
86 | ordered_list_initialize(node_data_compare); | |
87 | ||
88 | ordered_list_add_node(handle, &data3); | |
89 | ordered_list_add_node(handle, &data1); | |
90 | ordered_list_add_node(handle, &data2); | |
91 | ||
92 | CU_ASSERT_EQUAL(handle->num_entries, 3); | |
93 | ||
94 | ordered_list_node *node = handle->head; | |
95 | CU_ASSERT_PTR_EQUAL(node->data, &data1); | |
96 | ||
97 | node = node->next_node; | |
98 | CU_ASSERT_PTR_EQUAL(node->data, &data2); | |
99 | ||
100 | node = node->next_node; | |
101 | CU_ASSERT_PTR_EQUAL(node->data, &data3); | |
102 | ||
103 | node = node->next_node; | |
104 | CU_ASSERT_PTR_EQUAL(node, NULL); | |
105 | ||
106 | ordered_list_destroy(handle); | |
107 | } | |
108 | ||
109 | ||
110 | void test_find() | |
111 | { | |
112 | node_data data1, data2, data3, data_not_inList; | |
113 | data1.int_data = 1; | |
114 | data2.int_data = 2; | |
115 | data3.int_data = 3; | |
116 | data_not_inList.int_data = 5; | |
117 | ||
118 | ordered_list_handle *handle = | |
119 | ordered_list_initialize(node_data_compare); | |
120 | ||
121 | ordered_list_add_node(handle, &data3); | |
122 | ordered_list_add_node(handle, &data2); | |
123 | ordered_list_add_node(handle, &data1); | |
124 | ||
125 | ordered_list_node *node = ordered_list_find(handle, &data1); | |
126 | CU_ASSERT_PTR_NOT_NULL(node); | |
fadf00aa | 127 | assert(node != NULL); |
74971473 JG |
128 | CU_ASSERT_PTR_EQUAL(node->data, &data1); |
129 | ||
130 | node = ordered_list_find(handle, &data2); | |
131 | CU_ASSERT_PTR_NOT_NULL(node); | |
fadf00aa | 132 | assert(node != NULL); |
74971473 JG |
133 | CU_ASSERT_PTR_EQUAL(node->data, &data2); |
134 | ||
135 | node = ordered_list_find(handle, &data3); | |
136 | CU_ASSERT_PTR_NOT_NULL(node); | |
fadf00aa | 137 | assert(node != NULL); |
74971473 JG |
138 | CU_ASSERT_PTR_EQUAL(node->data, &data3); |
139 | ||
140 | node = ordered_list_find(handle, &data_not_inList); | |
141 | CU_ASSERT_PTR_NULL(node); | |
142 | ||
143 | ordered_list_destroy(handle); | |
144 | } | |
145 | ||
146 | ||
147 | void test_remove_first_node() | |
148 | { | |
149 | node_data data1, data2, data3; | |
150 | data1.int_data = 1; | |
151 | data2.int_data = 2; | |
152 | data3.int_data = 3; | |
153 | ||
154 | ordered_list_handle *handle = | |
155 | ordered_list_initialize(node_data_compare); | |
156 | ||
157 | ordered_list_add_node(handle, &data1); | |
158 | ordered_list_add_node(handle, &data2); | |
159 | ordered_list_add_node(handle, &data3); | |
160 | ||
161 | void *node_data = ordered_list_remove_first_node(handle); | |
162 | CU_ASSERT_PTR_NOT_NULL(node_data); | |
163 | CU_ASSERT_PTR_EQUAL(node_data, &data1); | |
164 | CU_ASSERT_EQUAL(handle->num_entries, 2); | |
165 | ||
166 | node_data = ordered_list_remove_first_node(handle); | |
167 | CU_ASSERT_PTR_NOT_NULL(node_data); | |
168 | CU_ASSERT_PTR_EQUAL(node_data, &data2); | |
169 | CU_ASSERT_EQUAL(handle->num_entries, 1); | |
170 | ||
171 | node_data = ordered_list_remove_first_node(handle); | |
172 | CU_ASSERT_PTR_NOT_NULL(node_data); | |
173 | CU_ASSERT_PTR_EQUAL(node_data, &data3); | |
174 | CU_ASSERT_EQUAL(handle->num_entries, 0); | |
175 | CU_ASSERT_PTR_NULL(handle->head); | |
176 | ||
177 | node_data = ordered_list_remove_first_node(handle); | |
178 | CU_ASSERT_PTR_NULL(node_data); | |
179 | ||
180 | ordered_list_destroy(handle); | |
181 | } | |
182 | ||
183 | ||
184 | void test_remove_first_node_equals() | |
185 | { | |
186 | node_data data1, data2, data3; | |
187 | data1.int_data = 1; | |
188 | data2.int_data = 2; | |
189 | data3.int_data = 3; | |
190 | ||
191 | ordered_list_handle *handle = | |
192 | ordered_list_initialize(node_data_compare); | |
193 | ||
194 | ordered_list_add_node(handle, &data1); | |
195 | ordered_list_add_node(handle, &data2); | |
196 | ordered_list_add_node(handle, &data3); | |
197 | ||
198 | void *node_data = ordered_list_remove_first_node_equals(handle, &data2); | |
199 | CU_ASSERT_PTR_NOT_NULL(node_data); | |
200 | CU_ASSERT_PTR_EQUAL(node_data, &data2); | |
201 | CU_ASSERT_EQUAL(handle->num_entries, 2); | |
202 | ||
203 | node_data = ordered_list_remove_first_node_equals(handle, &data3); | |
204 | CU_ASSERT_PTR_NOT_NULL(node_data); | |
205 | CU_ASSERT_PTR_EQUAL(node_data, &data3); | |
206 | CU_ASSERT_EQUAL(handle->num_entries, 1); | |
207 | ||
208 | node_data = ordered_list_remove_first_node_equals(handle, &data1); | |
209 | CU_ASSERT_PTR_NOT_NULL(node_data); | |
210 | CU_ASSERT_PTR_EQUAL(node_data, &data1); | |
211 | CU_ASSERT_EQUAL(handle->num_entries, 0); | |
212 | ||
213 | node_data = ordered_list_remove_first_node_equals(handle, &data1); | |
214 | CU_ASSERT_PTR_NULL(node_data); | |
215 | ||
216 | ordered_list_destroy(handle); | |
217 | } | |
218 | ||
219 | ||
220 | void test_remove_node() | |
221 | { | |
222 | node_data data1, data2, data3; | |
223 | data1.int_data = 1; | |
224 | data2.int_data = 2; | |
225 | data3.int_data = 3; | |
226 | ||
227 | ordered_list_handle *handle = | |
228 | ordered_list_initialize(node_data_compare); | |
229 | ||
230 | ordered_list_node *node1 = ordered_list_add_node(handle, &data1); | |
231 | ordered_list_node *node2 = ordered_list_add_node(handle, &data2); | |
232 | ordered_list_node *node3 = ordered_list_add_node(handle, &data3); | |
233 | ||
234 | void *node_data = ordered_list_remove_node(handle, node2, node3); | |
235 | CU_ASSERT_PTR_NOT_NULL(node_data); | |
236 | CU_ASSERT_PTR_EQUAL(node_data, &data3); | |
237 | CU_ASSERT_EQUAL(handle->num_entries, 2); | |
238 | ||
239 | node_data = ordered_list_remove_node(handle, node1, node2); | |
240 | CU_ASSERT_PTR_NOT_NULL(node_data); | |
241 | CU_ASSERT_PTR_EQUAL(node_data, &data2); | |
242 | CU_ASSERT_EQUAL(handle->num_entries, 1); | |
243 | ||
244 | ordered_list_destroy(handle); | |
245 | } |