check_list.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Check: a unit test framework for C
  3. * Copyright (C) 2001, 2002 Arien Malec
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the
  17. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include "../lib/libcompat.h"
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "check.h"
  24. #include "check_list.h"
  25. #include "check_check.h"
  26. START_TEST(test_create)
  27. {
  28. List *lp = NULL;
  29. ck_assert_msg (check_list_val(lp) == NULL,
  30. "Current list value should be NULL for NULL list");
  31. lp = check_list_create();
  32. ck_assert_msg (check_list_val(lp) == NULL,
  33. "Current list value should be NULL for newly created list");
  34. ck_assert_msg (check_list_at_end(lp),
  35. "Newly created list should be at end");
  36. check_list_advance(lp);
  37. ck_assert_msg (check_list_at_end(lp),
  38. "Advancing a list at end should produce a list at end");
  39. check_list_free (lp);
  40. }
  41. END_TEST
  42. START_TEST(test_free)
  43. {
  44. List *lp = check_list_create();
  45. char data_abc[] = "abc";
  46. char data_123[] = "123";
  47. check_list_add_end (lp, data_abc);
  48. check_list_add_end (lp, data_123);
  49. check_list_add_end (lp, NULL);
  50. check_list_free (lp);
  51. }
  52. END_TEST
  53. START_TEST(test_add_end)
  54. {
  55. List * lp = check_list_create();
  56. char tval[] = "abc";
  57. check_list_add_end (lp, tval);
  58. ck_assert_msg (check_list_val (lp) != NULL,
  59. "List current val should not be null after new insertion");
  60. ck_assert_msg (!check_list_at_end (lp),
  61. "List should be at end after new insertion");
  62. ck_assert_msg (strcmp(tval, (char *) check_list_val (lp)) == 0,
  63. "List current val should equal newly inserted val");
  64. check_list_free (lp);
  65. }
  66. END_TEST
  67. START_TEST(test_add_front)
  68. {
  69. List * lp = check_list_create();
  70. char tval[] = "abc";
  71. check_list_add_front (lp, tval);
  72. ck_assert_msg (check_list_val (lp) != NULL,
  73. "List current val should not be null after new insertion");
  74. ck_assert_msg (strcmp(tval, (char *) check_list_val (lp)) == 0,
  75. "List current val should equal newly inserted val");
  76. check_list_free (lp);
  77. }
  78. END_TEST
  79. START_TEST(test_add_end_and_next)
  80. {
  81. List *lp = check_list_create();
  82. char tval1[] = "abc";
  83. char tval2[] = "123";
  84. check_list_add_end (lp, tval1);
  85. check_list_add_end (lp, tval2);
  86. check_list_front(lp);
  87. ck_assert_msg (strcmp (tval1, (char *)check_list_val (lp)) == 0,
  88. "List head val should equal first inserted val");
  89. check_list_advance (lp);
  90. ck_assert_msg (!check_list_at_end (lp),
  91. "List should not be at end after two adds and one next");
  92. ck_assert_msg (strcmp (tval2, (char *)check_list_val (lp)) == 0,
  93. "List val should equal second inserted val");
  94. check_list_advance(lp);
  95. ck_assert_msg (check_list_at_end (lp),
  96. "List should be at and after two adds and two nexts");
  97. check_list_free (lp);
  98. }
  99. END_TEST
  100. START_TEST(test_add_front_and_next)
  101. {
  102. List * lp = check_list_create();
  103. char tval1[] = "abc";
  104. char tval2[] = "123";
  105. check_list_add_front (lp, tval1);
  106. check_list_add_front (lp, tval2);
  107. check_list_front(lp);
  108. ck_assert_msg (strcmp (tval2, (char *)check_list_val (lp)) == 0,
  109. "List head val should equal last inserted val");
  110. check_list_advance (lp);
  111. ck_assert_msg (!check_list_at_end (lp),
  112. "List should not be at end after two adds and one next");
  113. ck_assert_msg (strcmp (tval1, (char *)check_list_val (lp)) == 0,
  114. "List val should equal first inserted val");
  115. check_list_advance(lp);
  116. ck_assert_msg (check_list_at_end (lp),
  117. "List should be at and after two adds and two nexts");
  118. check_list_free (lp);
  119. }
  120. END_TEST
  121. START_TEST(test_add_a_bunch)
  122. {
  123. int i, j;
  124. char tval1[] = "abc";
  125. char tval2[] = "123";
  126. for (i = 0; i < 3; i++) {
  127. List *lp = check_list_create();
  128. for (j = 0; j < 1000; j++) {
  129. check_list_add_end (lp, tval1);
  130. check_list_add_front (lp, tval2);
  131. }
  132. check_list_free(lp);
  133. }
  134. }
  135. END_TEST
  136. START_TEST(test_list_abuse)
  137. {
  138. check_list_advance(NULL);
  139. /* Should not crash */
  140. }
  141. END_TEST
  142. START_TEST(test_contains)
  143. {
  144. List *lp = check_list_create();
  145. char otherData[] = "other";
  146. char goalData[] = "goal";
  147. int index;
  148. ck_assert_msg (check_list_contains(lp, goalData) == 0,
  149. "The goal data should not be in the list yet");
  150. for(index = 0; index < 10; index++)
  151. {
  152. check_list_add_end (lp, otherData);
  153. ck_assert_msg (check_list_contains(lp, goalData) == 0,
  154. "The goal data should not be in the list yet");
  155. }
  156. check_list_add_end (lp, goalData);
  157. ck_assert_msg (check_list_contains(lp, goalData) ,
  158. "The goal data should be in the list");
  159. check_list_free(lp);
  160. }
  161. END_TEST
  162. Suite *make_list_suite (void)
  163. {
  164. Suite *s = suite_create("Lists");
  165. TCase * tc = tcase_create("Core");
  166. suite_add_tcase (s, tc);
  167. tcase_add_test (tc, test_create);
  168. tcase_add_test (tc, test_free);
  169. tcase_add_test (tc, test_add_end);
  170. tcase_add_test (tc, test_add_front);
  171. tcase_add_test (tc, test_add_end_and_next);
  172. tcase_add_test (tc, test_add_front_and_next);
  173. tcase_add_test (tc, test_add_a_bunch);
  174. tcase_add_test (tc, test_list_abuse);
  175. tcase_add_test (tc, test_contains);
  176. return s;
  177. }