2 * Check: a unit test framework for C
3 * Copyright (C) 2001, 2002 Arien Malec
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.
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.
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,
21 #include "../lib/libcompat.h"
27 #include "check_list.h"
28 #include "check_check.h"
30 START_TEST(test_create)
34 ck_assert_msg (check_list_val(lp) == NULL,
35 "Current list value should be NULL for NULL list");
37 lp = check_list_create();
39 ck_assert_msg (check_list_val(lp) == NULL,
40 "Current list value should be NULL for newly created list");
42 ck_assert_msg (check_list_at_end(lp),
43 "Newly created list should be at end");
44 check_list_advance(lp);
45 ck_assert_msg (check_list_at_end(lp),
46 "Advancing a list at end should produce a list at end");
53 List *lp = check_list_create();
54 char data_abc[] = "abc";
55 char data_123[] = "123";
57 check_list_add_end (lp, data_abc);
58 check_list_add_end (lp, data_123);
59 check_list_add_end (lp, NULL);
64 START_TEST(test_add_end)
66 List * lp = check_list_create();
69 check_list_add_end (lp, tval);
71 ck_assert_msg (check_list_val (lp) != NULL,
72 "List current val should not be null after new insertion");
73 ck_assert_msg (!check_list_at_end (lp),
74 "List should be at end after new insertion");
75 ck_assert_msg (strcmp(tval, (char *) check_list_val (lp)) == 0,
76 "List current val should equal newly inserted val");
81 START_TEST(test_add_front)
83 List * lp = check_list_create();
86 check_list_add_front (lp, tval);
88 ck_assert_msg (check_list_val (lp) != NULL,
89 "List current val should not be null after new insertion");
90 ck_assert_msg (strcmp(tval, (char *) check_list_val (lp)) == 0,
91 "List current val should equal newly inserted val");
96 START_TEST(test_add_end_and_next)
98 List *lp = check_list_create();
100 char tval2[] = "123";
102 check_list_add_end (lp, tval1);
103 check_list_add_end (lp, tval2);
104 check_list_front(lp);
105 ck_assert_msg (strcmp (tval1, (char *)check_list_val (lp)) == 0,
106 "List head val should equal first inserted val");
107 check_list_advance (lp);
108 ck_assert_msg (!check_list_at_end (lp),
109 "List should not be at end after two adds and one next");
110 ck_assert_msg (strcmp (tval2, (char *)check_list_val (lp)) == 0,
111 "List val should equal second inserted val");
112 check_list_advance(lp);
113 ck_assert_msg (check_list_at_end (lp),
114 "List should be at and after two adds and two nexts");
115 check_list_free (lp);
120 START_TEST(test_add_front_and_next)
122 List * lp = check_list_create();
123 char tval1[] = "abc";
124 char tval2[] = "123";
126 check_list_add_front (lp, tval1);
127 check_list_add_front (lp, tval2);
128 check_list_front(lp);
129 ck_assert_msg (strcmp (tval2, (char *)check_list_val (lp)) == 0,
130 "List head val should equal last inserted val");
131 check_list_advance (lp);
132 ck_assert_msg (!check_list_at_end (lp),
133 "List should not be at end after two adds and one next");
134 ck_assert_msg (strcmp (tval1, (char *)check_list_val (lp)) == 0,
135 "List val should equal first inserted val");
136 check_list_advance(lp);
137 ck_assert_msg (check_list_at_end (lp),
138 "List should be at and after two adds and two nexts");
139 check_list_free (lp);
143 START_TEST(test_add_a_bunch)
146 char tval1[] = "abc";
147 char tval2[] = "123";
148 for (i = 0; i < 3; i++) {
149 List *lp = check_list_create();
150 for (j = 0; j < 1000; j++) {
151 check_list_add_end (lp, tval1);
152 check_list_add_front (lp, tval2);
159 START_TEST(test_list_abuse)
161 check_list_advance(NULL);
162 /* Should not crash */
166 START_TEST(test_contains)
168 List *lp = check_list_create();
170 char otherData[] = "other";
171 char goalData[] = "goal";
174 ck_assert_msg (check_list_contains(lp, goalData) == 0,
175 "The goal data should not be in the list yet");
177 for(index = 0; index < 10; index++)
179 check_list_add_end (lp, otherData);
180 ck_assert_msg (check_list_contains(lp, goalData) == 0,
181 "The goal data should not be in the list yet");
184 check_list_add_end (lp, goalData);
185 ck_assert_msg (check_list_contains(lp, goalData) ,
186 "The goal data should be in the list");
192 Suite *make_list_suite (void)
194 Suite *s = suite_create("Lists");
195 TCase * tc = tcase_create("Core");
197 suite_add_tcase (s, tc);
198 tcase_add_test (tc, test_create);
199 tcase_add_test (tc, test_free);
200 tcase_add_test (tc, test_add_end);
201 tcase_add_test (tc, test_add_front);
202 tcase_add_test (tc, test_add_end_and_next);
203 tcase_add_test (tc, test_add_front_and_next);
204 tcase_add_test (tc, test_add_a_bunch);
205 tcase_add_test (tc, test_list_abuse);
206 tcase_add_test (tc, test_contains);