/third_party/libSBML-5.9.0-Source/src/sbml/util/test/TestList.c

https://github.com/alexdarling/roadrunner · C · 435 lines · 236 code · 118 blank · 81 comment · 41 complexity · b021aae43083c026a3df82768b7606a3 MD5 · raw file

  1. /**
  2. * \file TestList.c
  3. * \brief List unit tests
  4. * \author Ben Bornstein
  5. *
  6. * <!--------------------------------------------------------------------------
  7. * This file is part of libSBML. Please visit http://sbml.org for more
  8. * information about SBML, and the latest version of libSBML.
  9. *
  10. * Copyright (C) 2009-2013 jointly by the following organizations:
  11. * 1. California Institute of Technology, Pasadena, CA, USA
  12. * 2. EMBL European Bioinformatics Institute (EBML-EBI), Hinxton, UK
  13. *
  14. * Copyright (C) 2006-2008 by the California Institute of Technology,
  15. * Pasadena, CA, USA
  16. *
  17. * Copyright (C) 2002-2005 jointly by the following organizations:
  18. * 1. California Institute of Technology, Pasadena, CA, USA
  19. * 2. Japan Science and Technology Agency, Japan
  20. *
  21. * This library is free software; you can redistribute it and/or modify it
  22. * under the terms of the GNU Lesser General Public License as published by
  23. * the Free Software Foundation. A copy of the license agreement is provided
  24. * in the file named "LICENSE.txt" included with this software distribution
  25. * and also available online as http://sbml.org/software/libsbml/license.html
  26. * ---------------------------------------------------------------------- -->*/
  27. #include <check.h>
  28. #include <sbml/common/common.h>
  29. #include <sbml/util/List.h>
  30. static List_t *L;
  31. void
  32. ListTest_setup (void)
  33. {
  34. L = List_create();
  35. if (L == NULL)
  36. {
  37. fail("List_create() returned a NULL pointer.");
  38. }
  39. }
  40. void
  41. ListTest_teardown (void)
  42. {
  43. List_free(L);
  44. }
  45. START_TEST (test_List_create)
  46. {
  47. fail_unless(List_size(L) == 0);
  48. /*
  49. fail_unless(L->head == NULL);
  50. fail_unless(L->tail == NULL);
  51. */
  52. }
  53. END_TEST
  54. START_TEST (test_ListNode_create)
  55. {
  56. char *s = "foo";
  57. ListNode_t *node = ListNode_create(s);
  58. /*
  59. fail_unless(node->item == s );
  60. fail_unless(node->next == NULL);
  61. */
  62. ListNode_free(node);
  63. }
  64. END_TEST
  65. START_TEST (test_List_free_NULL)
  66. {
  67. List_free(NULL);
  68. }
  69. END_TEST
  70. START_TEST (test_List_add_1)
  71. {
  72. List_add(L, "foo");
  73. fail_unless(List_size(L) == 1);
  74. /*
  75. fail_unless( !strcmp(L->head->item, "foo") );
  76. fail_unless(L->head == L->tail);
  77. fail_unless(L->head->next == NULL );
  78. */
  79. }
  80. END_TEST
  81. START_TEST (test_List_add_2)
  82. {
  83. List_add(L, "foo");
  84. List_add(L, "bar");
  85. fail_unless(List_size(L) == 2);
  86. /*
  87. fail_unless( !strcmp(L->head->item , "foo") );
  88. fail_unless( !strcmp(L->head->next->item, "bar") );
  89. fail_unless(L->head->next == L->tail);
  90. fail_unless(L->tail->next == NULL );
  91. */
  92. }
  93. END_TEST
  94. int
  95. myPredicate (const void *item)
  96. {
  97. const char *s = (char *) item;
  98. return s[1] == 'a';
  99. }
  100. START_TEST (test_List_countIf)
  101. {
  102. const char *foo = "foo";
  103. const char *bar = "bar";
  104. const char *baz = "baz";
  105. const char *bop = "bop";
  106. List_add(L, (void *) foo);
  107. List_add(L, (void *) bop);
  108. fail_unless( List_countIf(L, myPredicate) == 0 );
  109. List_add(L, (void *) foo);
  110. List_add(L, (void *) bar);
  111. List_add(L, (void *) baz);
  112. List_add(L, (void *) bop);
  113. fail_unless( List_countIf(L, myPredicate) == 2 );
  114. List_add(L, (void *) baz);
  115. fail_unless( List_countIf(L, myPredicate) == 3 );
  116. }
  117. END_TEST
  118. START_TEST (test_List_findIf)
  119. {
  120. List_t *list;
  121. const char *foo = "foo";
  122. const char *bar = "bar";
  123. const char *baz = "baz";
  124. const char *bop = "bop";
  125. List_add(L, (void *) foo);
  126. List_add(L, (void *) bop);
  127. list = List_findIf(L, myPredicate);
  128. fail_unless( list != NULL );
  129. fail_unless( List_size(list) == 0 );
  130. List_free(list);
  131. List_add(L, (void *) foo);
  132. List_add(L, (void *) bar);
  133. List_add(L, (void *) baz);
  134. List_add(L, (void *) bop);
  135. list = List_findIf(L, myPredicate);
  136. fail_unless( list != NULL );
  137. fail_unless( List_size(list) == 2 );
  138. fail_unless( List_get(list, 0) == bar );
  139. fail_unless( List_get(list, 1) == baz );
  140. List_free(list);
  141. List_add(L, (void *) baz);
  142. list = List_findIf(L, myPredicate);
  143. fail_unless( list != NULL );
  144. fail_unless( List_size(list) == 3 );
  145. fail_unless( List_get(list, 0) == bar );
  146. fail_unless( List_get(list, 1) == baz );
  147. fail_unless( List_get(list, 2) == baz );
  148. List_free(list);
  149. }
  150. END_TEST
  151. int
  152. myStrCmp (const void *s1, const void *s2)
  153. {
  154. return strcmp((const char *) s1, (const char *) s2);
  155. }
  156. START_TEST (test_List_find)
  157. {
  158. const char *foo = "foo";
  159. const char *bar = "bar";
  160. const char *baz = "baz";
  161. const char *bop = "bop";
  162. List_add(L, (void *) foo);
  163. List_add(L, (void *) bar);
  164. List_add(L, (void *) baz);
  165. fail_unless( List_find(L, (void *) foo, myStrCmp) == foo );
  166. fail_unless( List_find(L, (void *) bar, myStrCmp) == bar );
  167. fail_unless( List_find(L, (void *) baz, myStrCmp) == baz );
  168. fail_unless( List_find(L, (void *) bop, myStrCmp) == NULL );
  169. }
  170. END_TEST
  171. START_TEST (test_List_get)
  172. {
  173. List_add(L, "foo");
  174. List_add(L, "bar");
  175. fail_unless(List_size(L) == 2);
  176. fail_unless( !strcmp(List_get(L, 0), "foo") );
  177. fail_unless( !strcmp(List_get(L, 1), "bar") );
  178. fail_unless(List_get(L, -1) == NULL);
  179. fail_unless(List_get(L, 2) == NULL);
  180. }
  181. END_TEST
  182. START_TEST (test_List_prepend_1)
  183. {
  184. List_prepend(L, "foo");
  185. fail_unless(List_size(L) == 1);
  186. /*
  187. fail_unless( !strcmp(L->head->item, "foo") );
  188. fail_unless(L->head == L->tail);
  189. fail_unless(L->head->next == NULL );
  190. */
  191. }
  192. END_TEST
  193. START_TEST (test_List_prepend_2)
  194. {
  195. List_prepend(L, "foo");
  196. List_prepend(L, "bar");
  197. fail_unless(List_size(L) == 2);
  198. /*
  199. fail_unless( !strcmp(L->head->item , "bar") );
  200. fail_unless( !strcmp(L->head->next->item, "foo") );
  201. fail_unless(L->head->next == L->tail);
  202. fail_unless(L->tail->next == NULL );
  203. */
  204. }
  205. END_TEST
  206. START_TEST (test_List_remove_1)
  207. {
  208. List_add(L, "foo");
  209. fail_unless( !strcmp(List_remove(L, 0), "foo") );
  210. fail_unless(List_size(L) == 0);
  211. /*
  212. fail_unless(L->head == NULL);
  213. fail_unless(L->tail == NULL);
  214. */
  215. }
  216. END_TEST
  217. START_TEST (test_List_remove_2)
  218. {
  219. List_add(L, "foo");
  220. List_add(L, "bar");
  221. fail_unless( !strcmp(List_remove(L, 1), "bar") );
  222. fail_unless(List_size(L) == 1);
  223. fail_unless( !strcmp( List_get(L, 0), "foo" ) );
  224. /*
  225. fail_unless(L->head == L->tail);
  226. fail_unless(L->head->next == NULL );
  227. */
  228. }
  229. END_TEST
  230. START_TEST (test_List_remove_3)
  231. {
  232. List_add(L, "foo");
  233. List_add(L, "bar");
  234. List_add(L, "baz");
  235. fail_unless( !strcmp( List_remove(L, 1), "bar" ) );
  236. fail_unless(List_size(L) == 2);
  237. fail_unless( !strcmp( List_get(L, 0), "foo" ) );
  238. fail_unless( !strcmp( List_get(L, 1), "baz" ) );
  239. /*
  240. fail_unless(L->head != L->tail);
  241. fail_unless(L->tail->next == NULL );
  242. */
  243. }
  244. END_TEST
  245. START_TEST (test_List_remove_4)
  246. {
  247. List_add(L, "foo");
  248. List_add(L, "bar");
  249. List_add(L, "baz");
  250. fail_unless( !strcmp( List_remove(L, 2), "baz" ) );
  251. fail_unless(List_size(L) == 2);
  252. fail_unless( !strcmp( List_get(L, 0), "foo" ) );
  253. fail_unless( !strcmp( List_get(L, 1), "bar" ) );
  254. /*
  255. fail_unless(L->head != L->tail);
  256. fail_unless(L->tail->next == NULL );
  257. */
  258. }
  259. END_TEST
  260. START_TEST (test_List_freeItems)
  261. {
  262. List_add(L, safe_strdup("foo"));
  263. List_add(L, safe_strdup("bar"));
  264. List_add(L, safe_strdup("baz"));
  265. fail_unless(List_size(L) == 3);
  266. List_freeItems(L, safe_free, void);
  267. fail_unless(List_size(L) == 0);
  268. /*
  269. fail_unless(L->head == NULL);
  270. fail_unless(L->tail == NULL);
  271. */
  272. }
  273. END_TEST
  274. START_TEST (test_List_accessWithNULL)
  275. {
  276. // test null arguments
  277. List_add (NULL, NULL);
  278. fail_unless( List_countIf (NULL, NULL) == 0 );
  279. fail_unless( List_find (NULL, NULL, NULL) == NULL );
  280. fail_unless( List_findIf (NULL, NULL) == NULL );
  281. List_free(NULL);
  282. fail_unless( List_get (NULL, 0) == NULL );
  283. List_prepend(NULL, NULL);
  284. fail_unless( List_remove (NULL, 0) == NULL );
  285. fail_unless( List_size (NULL) == 0 );
  286. }
  287. END_TEST
  288. Suite *
  289. create_suite_List (void)
  290. {
  291. Suite *suite = suite_create("List");
  292. TCase *tcase = tcase_create("List");
  293. tcase_add_checked_fixture(tcase, ListTest_setup, ListTest_teardown);
  294. tcase_add_test( tcase, test_List_create );
  295. tcase_add_test( tcase, test_List_free_NULL );
  296. tcase_add_test( tcase, test_ListNode_create );
  297. tcase_add_test( tcase, test_List_add_1 );
  298. tcase_add_test( tcase, test_List_add_2 );
  299. tcase_add_test( tcase, test_List_get );
  300. tcase_add_test( tcase, test_List_countIf );
  301. tcase_add_test( tcase, test_List_findIf );
  302. tcase_add_test( tcase, test_List_find );
  303. tcase_add_test( tcase, test_List_prepend_1 );
  304. tcase_add_test( tcase, test_List_prepend_2 );
  305. tcase_add_test( tcase, test_List_remove_1 );
  306. tcase_add_test( tcase, test_List_remove_2 );
  307. tcase_add_test( tcase, test_List_remove_3 );
  308. tcase_add_test( tcase, test_List_remove_4 );
  309. tcase_add_test( tcase, test_List_freeItems );
  310. tcase_add_test( tcase, test_List_accessWithNULL );
  311. suite_add_tcase(suite, tcase);
  312. return suite;
  313. }