Imported Upstream version 0.14.0
[platform/upstream/check.git] / tests / check_list.c
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
21 #include "../lib/libcompat.h"
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "check.h"
27 #include "check_list.h"
28 #include "check_check.h"
29
30 START_TEST(test_create)
31 {
32   List *lp = NULL;
33
34   ck_assert_msg (check_list_val(lp) == NULL,
35                "Current list value should be NULL for NULL list");
36
37   lp = check_list_create();
38
39   ck_assert_msg (check_list_val(lp) == NULL,
40                "Current list value should be NULL for newly created list");
41
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");
47   check_list_free (lp);
48 }
49 END_TEST
50
51 START_TEST(test_free)
52 {
53   List *lp = check_list_create();
54   char data_abc[] = "abc";
55   char data_123[] = "123";
56
57   check_list_add_end (lp, data_abc);
58   check_list_add_end (lp, data_123);
59   check_list_add_end (lp, NULL);
60   check_list_free (lp);
61 }
62 END_TEST
63
64 START_TEST(test_add_end)
65 {
66   List * lp = check_list_create();
67   char tval[] = "abc";
68   
69   check_list_add_end (lp, tval);
70   
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");
77   check_list_free (lp);
78 }
79 END_TEST
80
81 START_TEST(test_add_front)
82 {
83   List * lp = check_list_create();
84   char tval[] = "abc";
85   
86   check_list_add_front (lp, tval);
87   
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");
92   check_list_free (lp);
93 }
94 END_TEST
95
96 START_TEST(test_add_end_and_next)
97 {
98   List *lp = check_list_create();
99   char tval1[] = "abc";
100   char tval2[] = "123";
101   
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);
116 }
117 END_TEST
118
119
120 START_TEST(test_add_front_and_next)
121 {
122   List * lp = check_list_create();
123   char tval1[] = "abc";
124   char tval2[] = "123";
125   
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);
140 }
141 END_TEST
142
143 START_TEST(test_add_a_bunch)
144 {
145   int i, j;
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);
153     }
154     check_list_free(lp);
155   }
156 }
157 END_TEST
158
159 START_TEST(test_list_abuse)
160 {
161     check_list_advance(NULL);
162     /* Should not crash */
163 }
164 END_TEST
165
166 START_TEST(test_contains)
167 {
168     List *lp = check_list_create();
169
170     char otherData[] = "other";
171     char goalData[] = "goal";
172     int index;
173
174     ck_assert_msg (check_list_contains(lp, goalData) == 0,
175                        "The goal data should not be in the list yet");
176
177     for(index = 0; index < 10; index++)
178     {
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");
182     }
183
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");
187
188     check_list_free(lp);
189 }
190 END_TEST
191
192 Suite *make_list_suite (void)
193 {
194   Suite *s = suite_create("Lists");
195   TCase * tc = tcase_create("Core");
196
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);
207
208   return s;
209 }