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>
28 #include <CUnit/CUnit.h>
30 #include "pcep_utils_ordered_list.h"
31 #include "pcep_utils_ordered_list_test.h"
33 typedef struct node_data_
{
39 int node_data_compare(void *list_entry
, void *new_entry
)
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
47 return ((node_data
*)new_entry
)->int_data
48 - ((node_data
*)list_entry
)->int_data
;
52 void test_empty_list()
54 ordered_list_handle
*handle
=
55 ordered_list_initialize(node_data_compare
);
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);
62 ordered_list_destroy(handle
);
66 void test_null_list_handle()
69 ordered_list_node node_data
;
71 void *ptr
= ordered_list_add_node(NULL
, &data
);
72 CU_ASSERT_PTR_NULL(ptr
);
74 ptr
= ordered_list_find(NULL
, &data
);
75 CU_ASSERT_PTR_NULL(ptr
);
77 ptr
= ordered_list_remove_first_node(NULL
);
78 CU_ASSERT_PTR_NULL(ptr
);
80 ptr
= ordered_list_remove_first_node_equals(NULL
, &data
);
81 CU_ASSERT_PTR_NULL(ptr
);
83 ptr
= ordered_list_remove_node(NULL
, &node_data
, &node_data
);
84 CU_ASSERT_PTR_NULL(ptr
);
88 void test_add_to_list()
90 node_data data1
, data2
, data3
;
95 ordered_list_handle
*handle
=
96 ordered_list_initialize(node_data_compare
);
98 ordered_list_add_node(handle
, &data3
);
99 ordered_list_add_node(handle
, &data1
);
100 ordered_list_add_node(handle
, &data2
);
102 CU_ASSERT_EQUAL(handle
->num_entries
, 3);
104 ordered_list_node
*node
= handle
->head
;
105 CU_ASSERT_PTR_EQUAL(node
->data
, &data1
);
107 node
= node
->next_node
;
108 CU_ASSERT_PTR_EQUAL(node
->data
, &data2
);
110 node
= node
->next_node
;
111 CU_ASSERT_PTR_EQUAL(node
->data
, &data3
);
113 node
= node
->next_node
;
114 CU_ASSERT_PTR_EQUAL(node
, NULL
);
116 ordered_list_destroy(handle
);
122 node_data data1
, data2
, data3
, data_not_inList
;
126 data_not_inList
.int_data
= 5;
128 ordered_list_handle
*handle
=
129 ordered_list_initialize(node_data_compare
);
131 ordered_list_add_node(handle
, &data3
);
132 ordered_list_add_node(handle
, &data2
);
133 ordered_list_add_node(handle
, &data1
);
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
);
139 node
= ordered_list_find(handle
, &data2
);
140 CU_ASSERT_PTR_NOT_NULL(node
);
141 CU_ASSERT_PTR_EQUAL(node
->data
, &data2
);
143 node
= ordered_list_find(handle
, &data3
);
144 CU_ASSERT_PTR_NOT_NULL(node
);
145 CU_ASSERT_PTR_EQUAL(node
->data
, &data3
);
147 node
= ordered_list_find(handle
, &data_not_inList
);
148 CU_ASSERT_PTR_NULL(node
);
150 ordered_list_destroy(handle
);
154 void test_remove_first_node()
156 node_data data1
, data2
, data3
;
161 ordered_list_handle
*handle
=
162 ordered_list_initialize(node_data_compare
);
164 ordered_list_add_node(handle
, &data1
);
165 ordered_list_add_node(handle
, &data2
);
166 ordered_list_add_node(handle
, &data3
);
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);
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);
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
);
184 node_data
= ordered_list_remove_first_node(handle
);
185 CU_ASSERT_PTR_NULL(node_data
);
187 ordered_list_destroy(handle
);
191 void test_remove_first_node_equals()
193 node_data data1
, data2
, data3
;
198 ordered_list_handle
*handle
=
199 ordered_list_initialize(node_data_compare
);
201 ordered_list_add_node(handle
, &data1
);
202 ordered_list_add_node(handle
, &data2
);
203 ordered_list_add_node(handle
, &data3
);
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);
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);
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);
220 node_data
= ordered_list_remove_first_node_equals(handle
, &data1
);
221 CU_ASSERT_PTR_NULL(node_data
);
223 ordered_list_destroy(handle
);
227 void test_remove_node()
229 node_data data1
, data2
, data3
;
234 ordered_list_handle
*handle
=
235 ordered_list_initialize(node_data_compare
);
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
);
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);
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);
251 ordered_list_destroy(handle
);