2 * This file is part of the PCEPlib, a PCEP protocol library.
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
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.
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.
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/>.
19 * Author : Brady Johnson <brady@voltanet.io>
29 #include <CUnit/CUnit.h>
31 #include "pcep_utils_ordered_list.h"
32 #include "pcep_utils_ordered_list_test.h"
34 typedef struct node_data_
{
40 int node_data_compare(void *list_entry
, void *new_entry
)
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
48 return ((node_data
*)new_entry
)->int_data
49 - ((node_data
*)list_entry
)->int_data
;
53 void test_empty_list()
55 ordered_list_handle
*handle
=
56 ordered_list_initialize(node_data_compare
);
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);
64 ordered_list_destroy(handle
);
68 void test_null_list_handle()
71 ordered_list_node node_data
;
73 void *ptr
= ordered_list_add_node(NULL
, &data
);
74 CU_ASSERT_PTR_NULL(ptr
);
76 ptr
= ordered_list_find(NULL
, &data
);
77 CU_ASSERT_PTR_NULL(ptr
);
79 ptr
= ordered_list_remove_first_node(NULL
);
80 CU_ASSERT_PTR_NULL(ptr
);
82 ptr
= ordered_list_remove_first_node_equals(NULL
, &data
);
83 CU_ASSERT_PTR_NULL(ptr
);
85 ptr
= ordered_list_remove_node(NULL
, &node_data
, &node_data
);
86 CU_ASSERT_PTR_NULL(ptr
);
90 void test_add_to_list()
92 node_data data1
, data2
, data3
;
97 ordered_list_handle
*handle
=
98 ordered_list_initialize(node_data_compare
);
100 ordered_list_add_node(handle
, &data3
);
101 ordered_list_add_node(handle
, &data1
);
102 ordered_list_add_node(handle
, &data2
);
104 CU_ASSERT_EQUAL(handle
->num_entries
, 3);
106 ordered_list_node
*node
= handle
->head
;
107 CU_ASSERT_PTR_EQUAL(node
->data
, &data1
);
109 node
= node
->next_node
;
110 CU_ASSERT_PTR_EQUAL(node
->data
, &data2
);
112 node
= node
->next_node
;
113 CU_ASSERT_PTR_EQUAL(node
->data
, &data3
);
115 node
= node
->next_node
;
116 CU_ASSERT_PTR_EQUAL(node
, NULL
);
118 ordered_list_destroy(handle
);
124 node_data data1
, data2
, data3
, data_not_inList
;
128 data_not_inList
.int_data
= 5;
130 ordered_list_handle
*handle
=
131 ordered_list_initialize(node_data_compare
);
133 ordered_list_add_node(handle
, &data3
);
134 ordered_list_add_node(handle
, &data2
);
135 ordered_list_add_node(handle
, &data1
);
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
);
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
);
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
);
152 node
= ordered_list_find(handle
, &data_not_inList
);
153 CU_ASSERT_PTR_NULL(node
);
155 ordered_list_destroy(handle
);
159 void test_remove_first_node()
161 node_data data1
, data2
, data3
;
166 ordered_list_handle
*handle
=
167 ordered_list_initialize(node_data_compare
);
169 ordered_list_add_node(handle
, &data1
);
170 ordered_list_add_node(handle
, &data2
);
171 ordered_list_add_node(handle
, &data3
);
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);
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);
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
);
189 node_data
= ordered_list_remove_first_node(handle
);
190 CU_ASSERT_PTR_NULL(node_data
);
192 ordered_list_destroy(handle
);
196 void test_remove_first_node_equals()
198 node_data data1
, data2
, data3
;
203 ordered_list_handle
*handle
=
204 ordered_list_initialize(node_data_compare
);
206 ordered_list_add_node(handle
, &data1
);
207 ordered_list_add_node(handle
, &data2
);
208 ordered_list_add_node(handle
, &data3
);
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);
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);
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);
225 node_data
= ordered_list_remove_first_node_equals(handle
, &data1
);
226 CU_ASSERT_PTR_NULL(node_data
);
228 ordered_list_destroy(handle
);
232 void test_remove_node()
234 node_data data1
, data2
, data3
;
239 ordered_list_handle
*handle
=
240 ordered_list_initialize(node_data_compare
);
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
);
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);
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);
256 ordered_list_destroy(handle
);