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>
24 #include <CUnit/CUnit.h>
26 #include "pcep_utils_ordered_list.h"
27 #include "pcep_utils_ordered_list_test.h"
29 typedef struct node_data_
{
35 int node_data_compare(void *list_entry
, void *new_entry
)
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
43 return ((node_data
*)new_entry
)->int_data
44 - ((node_data
*)list_entry
)->int_data
;
48 void test_empty_list()
50 ordered_list_handle
*handle
=
51 ordered_list_initialize(node_data_compare
);
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);
58 ordered_list_destroy(handle
);
62 void test_null_list_handle()
65 ordered_list_node node_data
;
67 void *ptr
= ordered_list_add_node(NULL
, &data
);
68 CU_ASSERT_PTR_NULL(ptr
);
70 ptr
= ordered_list_find(NULL
, &data
);
71 CU_ASSERT_PTR_NULL(ptr
);
73 ptr
= ordered_list_remove_first_node(NULL
);
74 CU_ASSERT_PTR_NULL(ptr
);
76 ptr
= ordered_list_remove_first_node_equals(NULL
, &data
);
77 CU_ASSERT_PTR_NULL(ptr
);
79 ptr
= ordered_list_remove_node(NULL
, &node_data
, &node_data
);
80 CU_ASSERT_PTR_NULL(ptr
);
84 void test_add_to_list()
86 node_data data1
, data2
, data3
;
91 ordered_list_handle
*handle
=
92 ordered_list_initialize(node_data_compare
);
94 ordered_list_add_node(handle
, &data3
);
95 ordered_list_add_node(handle
, &data1
);
96 ordered_list_add_node(handle
, &data2
);
98 CU_ASSERT_EQUAL(handle
->num_entries
, 3);
100 ordered_list_node
*node
= handle
->head
;
101 CU_ASSERT_PTR_EQUAL(node
->data
, &data1
);
103 node
= node
->next_node
;
104 CU_ASSERT_PTR_EQUAL(node
->data
, &data2
);
106 node
= node
->next_node
;
107 CU_ASSERT_PTR_EQUAL(node
->data
, &data3
);
109 node
= node
->next_node
;
110 CU_ASSERT_PTR_EQUAL(node
, NULL
);
112 ordered_list_destroy(handle
);
118 node_data data1
, data2
, data3
, data_not_inList
;
122 data_not_inList
.int_data
= 5;
124 ordered_list_handle
*handle
=
125 ordered_list_initialize(node_data_compare
);
127 ordered_list_add_node(handle
, &data3
);
128 ordered_list_add_node(handle
, &data2
);
129 ordered_list_add_node(handle
, &data1
);
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
);
135 node
= ordered_list_find(handle
, &data2
);
136 CU_ASSERT_PTR_NOT_NULL(node
);
137 CU_ASSERT_PTR_EQUAL(node
->data
, &data2
);
139 node
= ordered_list_find(handle
, &data3
);
140 CU_ASSERT_PTR_NOT_NULL(node
);
141 CU_ASSERT_PTR_EQUAL(node
->data
, &data3
);
143 node
= ordered_list_find(handle
, &data_not_inList
);
144 CU_ASSERT_PTR_NULL(node
);
146 ordered_list_destroy(handle
);
150 void test_remove_first_node()
152 node_data data1
, data2
, data3
;
157 ordered_list_handle
*handle
=
158 ordered_list_initialize(node_data_compare
);
160 ordered_list_add_node(handle
, &data1
);
161 ordered_list_add_node(handle
, &data2
);
162 ordered_list_add_node(handle
, &data3
);
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);
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);
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
);
180 node_data
= ordered_list_remove_first_node(handle
);
181 CU_ASSERT_PTR_NULL(node_data
);
183 ordered_list_destroy(handle
);
187 void test_remove_first_node_equals()
189 node_data data1
, data2
, data3
;
194 ordered_list_handle
*handle
=
195 ordered_list_initialize(node_data_compare
);
197 ordered_list_add_node(handle
, &data1
);
198 ordered_list_add_node(handle
, &data2
);
199 ordered_list_add_node(handle
, &data3
);
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);
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);
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);
216 node_data
= ordered_list_remove_first_node_equals(handle
, &data1
);
217 CU_ASSERT_PTR_NULL(node_data
);
219 ordered_list_destroy(handle
);
223 void test_remove_node()
225 node_data data1
, data2
, data3
;
230 ordered_list_handle
*handle
=
231 ordered_list_initialize(node_data_compare
);
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
);
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);
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);
247 ordered_list_destroy(handle
);