1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * This file is part of the PCEPlib, a PCEP protocol library.
5 * Copyright (C) 2020 Volta Networks https://voltanet.io/
7 * Author : Brady Johnson <brady@voltanet.io>
17 #include <CUnit/CUnit.h>
19 #include "pcep_utils_ordered_list.h"
20 #include "pcep_utils_ordered_list_test.h"
22 typedef struct node_data_
{
28 int node_data_compare(void *list_entry
, void *new_entry
)
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
36 return ((node_data
*)new_entry
)->int_data
37 - ((node_data
*)list_entry
)->int_data
;
41 void test_empty_list()
43 ordered_list_handle
*handle
=
44 ordered_list_initialize(node_data_compare
);
46 CU_ASSERT_PTR_NOT_NULL(handle
);
47 assert(handle
!= NULL
);
48 CU_ASSERT_PTR_NULL(handle
->head
);
49 CU_ASSERT_PTR_NOT_NULL(handle
->compare_function
);
50 CU_ASSERT_EQUAL(handle
->num_entries
, 0);
52 ordered_list_destroy(handle
);
56 void test_null_list_handle()
59 ordered_list_node node_data
;
61 void *ptr
= ordered_list_add_node(NULL
, &data
);
62 CU_ASSERT_PTR_NULL(ptr
);
64 ptr
= ordered_list_find(NULL
, &data
);
65 CU_ASSERT_PTR_NULL(ptr
);
67 ptr
= ordered_list_remove_first_node(NULL
);
68 CU_ASSERT_PTR_NULL(ptr
);
70 ptr
= ordered_list_remove_first_node_equals(NULL
, &data
);
71 CU_ASSERT_PTR_NULL(ptr
);
73 ptr
= ordered_list_remove_node(NULL
, &node_data
, &node_data
);
74 CU_ASSERT_PTR_NULL(ptr
);
78 void test_add_to_list()
80 node_data data1
, data2
, data3
;
85 ordered_list_handle
*handle
=
86 ordered_list_initialize(node_data_compare
);
88 ordered_list_add_node(handle
, &data3
);
89 ordered_list_add_node(handle
, &data1
);
90 ordered_list_add_node(handle
, &data2
);
92 CU_ASSERT_EQUAL(handle
->num_entries
, 3);
94 ordered_list_node
*node
= handle
->head
;
95 CU_ASSERT_PTR_EQUAL(node
->data
, &data1
);
97 node
= node
->next_node
;
98 CU_ASSERT_PTR_EQUAL(node
->data
, &data2
);
100 node
= node
->next_node
;
101 CU_ASSERT_PTR_EQUAL(node
->data
, &data3
);
103 node
= node
->next_node
;
104 CU_ASSERT_PTR_EQUAL(node
, NULL
);
106 ordered_list_destroy(handle
);
112 node_data data1
, data2
, data3
, data_not_inList
;
116 data_not_inList
.int_data
= 5;
118 ordered_list_handle
*handle
=
119 ordered_list_initialize(node_data_compare
);
121 ordered_list_add_node(handle
, &data3
);
122 ordered_list_add_node(handle
, &data2
);
123 ordered_list_add_node(handle
, &data1
);
125 ordered_list_node
*node
= ordered_list_find(handle
, &data1
);
126 CU_ASSERT_PTR_NOT_NULL(node
);
127 assert(node
!= NULL
);
128 CU_ASSERT_PTR_EQUAL(node
->data
, &data1
);
130 node
= ordered_list_find(handle
, &data2
);
131 CU_ASSERT_PTR_NOT_NULL(node
);
132 assert(node
!= NULL
);
133 CU_ASSERT_PTR_EQUAL(node
->data
, &data2
);
135 node
= ordered_list_find(handle
, &data3
);
136 CU_ASSERT_PTR_NOT_NULL(node
);
137 assert(node
!= NULL
);
138 CU_ASSERT_PTR_EQUAL(node
->data
, &data3
);
140 node
= ordered_list_find(handle
, &data_not_inList
);
141 CU_ASSERT_PTR_NULL(node
);
143 ordered_list_destroy(handle
);
147 void test_remove_first_node()
149 node_data data1
, data2
, data3
;
154 ordered_list_handle
*handle
=
155 ordered_list_initialize(node_data_compare
);
157 ordered_list_add_node(handle
, &data1
);
158 ordered_list_add_node(handle
, &data2
);
159 ordered_list_add_node(handle
, &data3
);
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);
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);
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
);
177 node_data
= ordered_list_remove_first_node(handle
);
178 CU_ASSERT_PTR_NULL(node_data
);
180 ordered_list_destroy(handle
);
184 void test_remove_first_node_equals()
186 node_data data1
, data2
, data3
;
191 ordered_list_handle
*handle
=
192 ordered_list_initialize(node_data_compare
);
194 ordered_list_add_node(handle
, &data1
);
195 ordered_list_add_node(handle
, &data2
);
196 ordered_list_add_node(handle
, &data3
);
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);
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);
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);
213 node_data
= ordered_list_remove_first_node_equals(handle
, &data1
);
214 CU_ASSERT_PTR_NULL(node_data
);
216 ordered_list_destroy(handle
);
220 void test_remove_node()
222 node_data data1
, data2
, data3
;
227 ordered_list_handle
*handle
=
228 ordered_list_initialize(node_data_compare
);
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
);
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);
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);
244 ordered_list_destroy(handle
);