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