2 #include "opensrf/osrf_list.h"
4 osrfList *testOsrfList;
6 //Keep track of how many items have been freed using osrfCustomListFree
7 unsigned int freedItemsSize;
9 //Define a custom freeing function for list items
10 void osrfCustomListFree() {
14 //Set up the test fixture
17 //Set up a list of size 10, define the freeing function, add some items to test with
18 testOsrfList = osrfNewListSize(10);
19 testOsrfList->freeItem = (void(*)(void*)) osrfCustomListFree;
24 osrfListPush(testOsrfList, (int *)item1);
25 osrfListPush(testOsrfList, NULL);
26 osrfListPush(testOsrfList, (int *)item3);
29 //Clean up the test fixture
31 osrfListFree(testOsrfList);
36 START_TEST(test_osrf_list_osrfNewList)
37 osrfList *newList = osrfNewList();
38 fail_if(newList == NULL, "osrfList object not successfully created");
39 fail_unless(newList->arrsize == 48, "the osrfList is not the default size of 48");
42 START_TEST(test_osrf_list_osrfNewListSize)
43 osrfList *smallList = osrfNewListSize(5);
44 fail_if(smallList == NULL, "smallList not successfully created");
45 fail_unless(smallList->arrsize == 5, "smallList wasn't created with the size 5");
46 fail_unless(smallList->freeItem == NULL, "freeItem should be null by default");
48 for (i = 0 ; i < smallList->arrsize ; i++) {
49 fail_if(smallList->arrlist[i] != NULL, "Every value in smallList->arrlist should be null");
52 //List created with size <= 0
53 osrfList *sizelessList = osrfNewListSize(0);
54 fail_unless(sizelessList->arrsize == 16,
55 "osrfNewListSize called with a size of 0 or less should have an array size of 16");
58 START_TEST(test_osrf_list_osrfListPush)
59 fail_unless(osrfListPush(NULL, NULL) == -1,
60 "Passing a null list to osrfListPush should return -1");
62 fail_unless(osrfListPush(testOsrfList, (int *) listItem) == 0,
63 "osrfListPush should return 0 if successful");
64 fail_unless(testOsrfList->size == 4,
65 "testOsrfList->size did not update correctly, should be 4");
66 fail_unless(osrfListGetIndex(testOsrfList, 3) == (int *) listItem,
67 "listItem did not add to the end of testOsrfList");
70 START_TEST(test_osrf_list_osrfListPushFirst)
71 fail_unless(osrfListPushFirst(NULL, NULL) == -1,
72 "Passing a null list to osrfListPushFirst should return -1");
74 fail_unless(osrfListPushFirst(testOsrfList, (int *) listItem) == 3,
75 "osrfListPushFirst should return a size of 3");
76 fail_unless(osrfListGetIndex(testOsrfList, 1) == (int *) listItem,
77 "listItem should be in index 1 because it is the first that is null");
80 START_TEST(test_osrf_list_osrfListSet)
82 fail_unless(osrfListSet(NULL, NULL, 1) == NULL,
83 "Given a null list arg, osrfListSet should return null");
85 //Adding an item to an existing, NULL position in the list
87 fail_unless(osrfListSet(testOsrfList, (int *) listItem, 4) == NULL,
88 "Calling osrfListSet on an empty index should return NULL");
89 fail_unless(osrfListGetIndex(testOsrfList, 4) == (int *) listItem,
90 "osrfListSet is not assigning item pointer to the correct position");
91 fail_unless(testOsrfList->size == 5,
92 "osrfListSet should update a lists size after adding an item to that list");
94 //Adding an item to an exisiting, occupied position in the
95 //list when there is a freeing function defined on the list
97 fail_unless(osrfListSet(testOsrfList, (int *) listItem2, 4) == NULL,
98 "Calling osrfListSet on an index that held a value, \
99 on a list that has a custom freeing function, should return NULL");
100 fail_unless(osrfListGetIndex(testOsrfList, 4) == (int *) listItem2,
101 "When called on a position that already has a value, \
102 osrfListSet should replace that value with the new item");
103 fail_unless(testOsrfList->size == 5,
104 "osrfListSet shouldn't update a lists size if the item is \
105 not added beyond the current size");
107 //Adding an item to an exisiting, occupied position in the list
108 //when there is NOT a freeing function defined on the list
109 testOsrfList->freeItem = NULL;
111 fail_unless(osrfListSet(testOsrfList, (int *) listItem3, 4) == (int *) listItem2,
112 "Calling osrfListSet on an index that held a value should \
113 return the reference to that value");
114 fail_unless(osrfListGetIndex(testOsrfList, 4) == (int *) listItem3,
115 "When called on a position that already has a value, \
116 osrfListSet should replace that value with the new item");
117 fail_unless(testOsrfList->size == 5,
118 "osrfListSet shouldn't update a lists size if the item is \
119 not added beyond the current size");
121 //Adding an item to a position outside of the current array size
123 fail_unless(osrfListSet(testOsrfList, (int *) listItem4, 18) == NULL,
124 "Calling osrfListSet on an empty index should return NULL, \
125 even if the index does not exist yet");
126 fail_unless(testOsrfList->arrsize == 266,
127 "New arrsize should be 266 since it was 10 before, and grows \
128 in increments of 256 when expanded");
129 fail_unless(testOsrfList->size == 19,
130 "List should have a size value of 19");
131 fail_unless(osrfListGetIndex(testOsrfList, 18) == (int *) listItem4,
132 "Value not added to correct index of list");
135 START_TEST(test_osrf_list_osrfListGetIndex)
136 fail_unless(osrfListGetIndex(NULL, 1) == NULL,
137 "Calling osrfListGetIndex with a null list should return null");
138 fail_unless(osrfListGetIndex(testOsrfList, 8) == NULL,
139 "Calling osrfListGetIndex with a value outside the range of \
140 occupied indexes should return NULL");
141 fail_unless(osrfListGetIndex(testOsrfList, 2) == (int *) 15,
142 "osrfListGetIndex should return the value of the list at the given index");
145 START_TEST(test_osrf_list_osrfListFree)
146 //Set up a new list to be freed
147 osrfList *myList = osrfNewList();
148 myList->freeItem = (void(*)(void*)) osrfCustomListFree;
149 int myListItem1 = 123;
150 int myListItem2 = 456;
151 osrfListSet(myList, (int *) myListItem1, 0);
152 osrfListSet(myList, (int *) myListItem2, 1);
153 osrfListFree(myList);
154 fail_unless(freedItemsSize == 2,
155 "osrfListFree should free each item in the list if there is a custom \
156 freeing function defined");
159 START_TEST(test_osrf_list_osrfListClear)
160 //Set up a new list with items to be freed
161 osrfList *myList = osrfNewList();
162 myList->freeItem = (void(*)(void*)) osrfCustomListFree;
163 int myListItem1 = 123;
164 int myListItem2 = 456;
165 osrfListSet(myList, (int *) myListItem1, 0);
166 osrfListSet(myList, (int *) myListItem2, 1);
167 osrfListClear(myList);
169 fail_unless(freedItemsSize == 2,
170 "osrfListClear should free each item in the list if there is a custom \
171 freeing function defined");
172 fail_unless(myList->arrlist[0] == NULL && myList->arrlist[1] == NULL,
173 "osrfListClear should make all previously used slots in the list NULL");
174 fail_unless(myList->size == 0,
175 "osrfListClear should set the list's size to 0");
178 START_TEST(test_osrf_list_osrfListSwap)
179 //Prepare a second list to swap
180 osrfList *secondOsrfList = osrfNewListSize(7);
183 osrfListPush(secondOsrfList, NULL);
184 osrfListPush(secondOsrfList, (int *) item2);
185 osrfListPush(secondOsrfList, (int *) item3);
187 osrfListSwap(testOsrfList, secondOsrfList);
189 osrfListGetIndex(testOsrfList, 0) == NULL &&
190 osrfListGetIndex(testOsrfList, 1) == (int *) 8 &&
191 osrfListGetIndex(testOsrfList, 2) == (int *) 16,
192 "After osrfListSwap, first list should now contain \
193 the contents of the second list"
196 osrfListGetIndex(secondOsrfList, 0) == (int *) 7 &&
197 osrfListGetIndex(secondOsrfList, 1) == NULL &&
198 osrfListGetIndex(secondOsrfList, 2) == (int *) 15,
199 "After osrfListSwap, second list should now contain \
200 the contents of the first list"
204 START_TEST(test_osrf_list_osrfListRemove)
205 fail_unless(osrfListRemove(NULL, 2) == NULL,
206 "osrfListRemove should return NULL when not given a list");
207 fail_unless(osrfListRemove(testOsrfList, 1000) == NULL,
208 "osrfListRemove should return NULL when given a position \
209 exceeding the size of the list");
210 fail_unless(osrfListRemove(testOsrfList, 2) == NULL,
211 "osrfListRemove should return NULL if there is a custom freeing \
212 function defined on the list");
213 fail_unless(osrfListGetIndex(testOsrfList, 2) == NULL,
214 "osrfListRemove should remove the value from the list");
215 fail_unless(testOsrfList->size == 2,
216 "osrfListRemove should adjust the size of the list if the last \
217 element is removed");
218 fail_unless(freedItemsSize == 1,
219 "osrfListRemove should call a custom item freeing function if \
221 testOsrfList->freeItem = NULL;
222 fail_unless(osrfListRemove(testOsrfList, 0) == (int *) 7,
223 "osrfListRemove should return the value that it has removed from \
224 the list if no custom freeing function is defined on the list");
225 fail_unless(osrfListGetIndex(testOsrfList, 0) == NULL,
226 "osrfListRemove should remove the value from the list and make \
228 fail_unless(testOsrfList->size == 2, "osrfListRemove should not touch \
229 the size of the list if it isn't removing the last element in the \
233 START_TEST(test_osrf_list_osrfListExtract)
234 fail_unless(osrfListExtract(NULL, 2) == NULL,
235 "osrfListExtract should return NULL when not given a list");
236 fail_unless(osrfListExtract(testOsrfList, 1000) == NULL,
237 "osrfListExtract should return NULL when given a position \
238 exceeding the size of the list");
239 fail_unless(osrfListExtract(testOsrfList, 2) == (int *) 15,
240 "osrfListExtract should return the value that it has removed \
242 fail_unless(osrfListGetIndex(testOsrfList, 2) == NULL,
243 "osrfListExtract should remove the value from the list and \
244 make the position NULL");
245 fail_unless(testOsrfList->size == 2,
246 "osrfListExtract should adjust the size of the list if the \
247 last element is removed");
248 fail_unless(osrfListExtract(testOsrfList, 0) == (int *) 7,
249 "osrfListExtract should return the value that it has removed \
251 fail_unless(osrfListGetIndex(testOsrfList, 0) == NULL,
252 "osrfListExtract should remove the value from the list and \
253 make the position NULL");
254 fail_unless(testOsrfList->size == 2,
255 "osrfListExtract should not touch the size of the list if it \
256 isn't removing the last element in the list");
259 START_TEST(test_osrf_list_osrfListFind)
260 fail_unless(osrfListFind(NULL, (int *) 2) == -1,
261 "osrfListFind should return -1 when not given a list");
262 fail_unless(osrfListFind(testOsrfList, NULL) == -1,
263 "osrfListFind should return -1 when not given an addr");
264 fail_unless(osrfListFind(testOsrfList, (int *) 15) == 2,
265 "osrfListFind should return the index where the first instance \
266 of addr is located");
267 fail_unless(osrfListFind(testOsrfList, (int *) 199) == -1,
268 "osrfListFind should return -1 when the addr does not exist in \
272 START_TEST(test_osrf_list_osrfListGetCount)
273 fail_unless(osrfListGetCount(NULL) == -1,
274 "osrfListGetCount should return -1 when no list is given");
275 fail_unless(osrfListGetCount(testOsrfList) == 3,
276 "osrfListGetCount should return list->size when given a list");
279 START_TEST(test_osrf_list_osrfListPop)
280 fail_unless(osrfListPop(NULL) == NULL,
281 "osrfListPop should return NULL when no list is given");
282 fail_unless(osrfListPop(testOsrfList) == NULL,
283 "osrfListPop should return NULL if there is a custom freeing \
284 function defined on the list");
285 fail_unless(testOsrfList->arrlist[2] == NULL,
286 "osrfListPop should remove the last item from the list");
287 testOsrfList->freeItem = NULL;
289 osrfListPush(testOsrfList, (int *) item);
290 fail_unless(osrfListPop(testOsrfList) == (int *) 10,
291 "osrfListPop should return the last item from the list");
292 fail_unless(testOsrfList->arrlist[2] == NULL,
293 "osrfListPop should remove the last item from the list");
296 START_TEST(test_osrf_list_osrfNewListIterator)
297 fail_unless(osrfNewListIterator(NULL) == NULL,
298 "osrfNewListIterator should return NULL when no list is given");
299 osrfListIterator *testListItr = osrfNewListIterator(testOsrfList);
300 fail_if(testListItr == NULL,
301 "osrfNewListIterator should create a osrfListIterator object");
302 fail_unless(testListItr->list == testOsrfList,
303 "osrfNewListIterator should set the osrfListIterator->list \
304 attribute to the given list");
305 fail_unless(testListItr->current == 0,
306 "osrfNewListIterator should set its current position to 0 by \
310 START_TEST(test_osrf_list_osrfListIteratorNext)
311 fail_unless(osrfListIteratorNext(NULL) == NULL,
312 "osrfListIteratorNext should return NULL when no list given");
313 osrfListIterator *testListItr = osrfNewListIterator(testOsrfList);
314 fail_unless(osrfListIteratorNext(testListItr) == (int *) 7,
315 "osrfListIteratorNext should return the value stored at the current \
316 index in the list, then increment");
317 fail_unless(osrfListIteratorNext(testListItr) == NULL,
318 "osrfListIteratorNext should return the value stored at the current \
319 index in the list, then increment");
320 fail_unless(osrfListIteratorNext(testListItr) == (int *) 15,
321 "osrfListIteratorNext should return the value stored at the current \
322 index in the list, then increment");
323 fail_unless(osrfListIteratorNext(testListItr) == NULL,
324 "osrfListIteratorNext should return NULL when it reaches the end of \
326 testListItr->list = NULL;
327 fail_unless(osrfListIteratorNext(testListItr) == NULL,
328 "osrfListIteratorNext should return NULL if osrfListIterator->list \
332 START_TEST(test_osrf_list_osrfListIteratorFree)
335 START_TEST(test_osrf_list_osrfListIteratorReset)
336 osrfListIterator *testListItr = osrfNewListIterator(testOsrfList);
337 osrfListIteratorNext(testListItr);
338 osrfListIteratorReset(testListItr);
339 fail_unless(testListItr->current == 0,
340 "osrfListIteratorReset should reset the iterator's current position to 0");
343 START_TEST(test_osrf_list_osrfListSetDefaultFree)
348 Suite *osrf_list_suite(void) {
349 //Create test suite, test case, initialize fixture
350 Suite *s = suite_create("osrf_list");
351 TCase *tc_core = tcase_create("Core");
352 tcase_add_checked_fixture(tc_core, setup, teardown);
354 //Add tests to test case
355 tcase_add_test(tc_core, test_osrf_list_osrfNewList);
356 tcase_add_test(tc_core, test_osrf_list_osrfNewListSize);
357 tcase_add_test(tc_core, test_osrf_list_osrfListPush);
358 tcase_add_test(tc_core, test_osrf_list_osrfListPushFirst);
359 tcase_add_test(tc_core, test_osrf_list_osrfListSet);
360 tcase_add_test(tc_core, test_osrf_list_osrfListGetIndex);
361 tcase_add_test(tc_core, test_osrf_list_osrfListFree);
362 tcase_add_test(tc_core, test_osrf_list_osrfListClear);
363 tcase_add_test(tc_core, test_osrf_list_osrfListSwap);
364 tcase_add_test(tc_core, test_osrf_list_osrfListRemove);
365 tcase_add_test(tc_core, test_osrf_list_osrfListExtract);
366 tcase_add_test(tc_core, test_osrf_list_osrfListFind);
367 tcase_add_test(tc_core, test_osrf_list_osrfListGetCount);
368 tcase_add_test(tc_core, test_osrf_list_osrfListPop);
369 tcase_add_test(tc_core, test_osrf_list_osrfNewListIterator);
370 tcase_add_test(tc_core, test_osrf_list_osrfListIteratorNext);
371 tcase_add_test(tc_core, test_osrf_list_osrfListIteratorFree);
372 tcase_add_test(tc_core, test_osrf_list_osrfListIteratorReset);
373 tcase_add_test(tc_core, test_osrf_list_osrfListSetDefaultFree);
375 //Add test case to test suite
376 suite_add_tcase(s, tc_core);
381 void run_tests(SRunner *sr) {
382 srunner_add_suite(sr, osrf_list_suite());