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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21 #include "../lib/libcompat.h"
22
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27
28 #include "check.h"
29 #include "check_error.h"
30 #include "check_list.h"
31 #include "check_impl.h"
32 #include "check_msg.h"
33
34 #ifndef DEFAULT_TIMEOUT
35 #define DEFAULT_TIMEOUT 4
36 #endif
37
38 int check_major_version = CHECK_MAJOR_VERSION;
39 int check_minor_version = CHECK_MINOR_VERSION;
40 int check_micro_version = CHECK_MICRO_VERSION;
41
42 static int non_pass (int val);
43 static Fixture *fixture_create (SFun fun, int ischecked);
44 static void tcase_add_fixture (TCase *tc, SFun setup, SFun teardown,
45 int ischecked);
46 static void tr_init (TestResult *tr);
47 static void suite_free (Suite *s);
48 static void tcase_free (TCase *tc);
49
suite_create(const char * name)50 Suite *suite_create (const char *name)
51 {
52 Suite *s;
53 s = emalloc (sizeof(Suite)); /* freed in suite_free */
54 if (name == NULL)
55 s->name = "";
56 else
57 s->name = name;
58 s->tclst = check_list_create();
59 return s;
60 }
61
suite_free(Suite * s)62 static void suite_free (Suite *s)
63 {
64 List *l;
65 if (s == NULL)
66 return;
67 l = s->tclst;
68 for (list_front(l); !list_at_end(l); list_advance (l)) {
69 tcase_free (list_val(l));
70 }
71 list_free (s->tclst);
72 free(s);
73 }
74
tcase_create(const char * name)75 TCase *tcase_create (const char *name)
76 {
77 char *env;
78 int timeout = DEFAULT_TIMEOUT;
79 TCase *tc = emalloc (sizeof(TCase)); /*freed in tcase_free */
80 if (name == NULL)
81 tc->name = "";
82 else
83 tc->name = name;
84
85 env = getenv("CK_DEFAULT_TIMEOUT");
86 if (env != NULL) {
87 int tmp = atoi(env);
88 if (tmp >= 0) {
89 timeout = tmp;
90 }
91 }
92
93 env = getenv("CK_TIMEOUT_MULTIPLIER");
94 if (env != NULL) {
95 int tmp = atoi(env);
96 if (tmp >= 0) {
97 timeout = timeout * tmp;
98 }
99 }
100
101 tc->timeout = timeout;
102 tc->tflst = check_list_create();
103 tc->unch_sflst = check_list_create();
104 tc->ch_sflst = check_list_create();
105 tc->unch_tflst = check_list_create();
106 tc->ch_tflst = check_list_create();
107
108 return tc;
109 }
110
111
tcase_free(TCase * tc)112 static void tcase_free (TCase *tc)
113 {
114 list_apply (tc->tflst, free);
115 list_apply (tc->unch_sflst, free);
116 list_apply (tc->ch_sflst, free);
117 list_apply (tc->unch_tflst, free);
118 list_apply (tc->ch_tflst, free);
119 list_free(tc->tflst);
120 list_free(tc->unch_sflst);
121 list_free(tc->ch_sflst);
122 list_free(tc->unch_tflst);
123 list_free(tc->ch_tflst);
124
125 free(tc);
126 }
127
suite_add_tcase(Suite * s,TCase * tc)128 void suite_add_tcase (Suite *s, TCase *tc)
129 {
130 if (s == NULL || tc == NULL)
131 return;
132 list_add_end (s->tclst, tc);
133 }
134
_tcase_add_test(TCase * tc,TFun fn,const char * name,int _signal,int allowed_exit_value,int start,int end)135 void _tcase_add_test (TCase *tc, TFun fn, const char *name, int _signal, int allowed_exit_value, int start, int end)
136 {
137 TF * tf;
138 if (tc == NULL || fn == NULL || name == NULL)
139 return;
140 tf = emalloc (sizeof(TF)); /* freed in tcase_free */
141 tf->fn = fn;
142 tf->loop_start = start;
143 tf->loop_end = end;
144 tf->signal = _signal; /* 0 means no signal expected */
145 tf->allowed_exit_value = allowed_exit_value; /* 0 is default successful exit */
146 tf->name = name;
147 list_add_end (tc->tflst, tf);
148 }
149
fixture_create(SFun fun,int ischecked)150 static Fixture *fixture_create (SFun fun, int ischecked)
151 {
152 Fixture *f;
153 f = emalloc (sizeof(Fixture));
154 f->fun = fun;
155 f->ischecked = ischecked;
156
157 return f;
158 }
159
tcase_add_unchecked_fixture(TCase * tc,SFun setup,SFun teardown)160 void tcase_add_unchecked_fixture (TCase *tc, SFun setup, SFun teardown)
161 {
162 tcase_add_fixture(tc,setup,teardown,0);
163 }
164
tcase_add_checked_fixture(TCase * tc,SFun setup,SFun teardown)165 void tcase_add_checked_fixture (TCase *tc, SFun setup, SFun teardown)
166 {
167 tcase_add_fixture (tc,setup,teardown,1);
168 }
169
tcase_add_fixture(TCase * tc,SFun setup,SFun teardown,int ischecked)170 static void tcase_add_fixture (TCase *tc, SFun setup, SFun teardown,
171 int ischecked)
172 {
173 if (setup) {
174 if (ischecked)
175 list_add_end (tc->ch_sflst, fixture_create(setup, ischecked));
176 else
177 list_add_end (tc->unch_sflst, fixture_create(setup, ischecked));
178 }
179
180 /* Add teardowns at front so they are run in reverse order. */
181 if (teardown) {
182 if (ischecked)
183 list_add_front (tc->ch_tflst, fixture_create(teardown, ischecked));
184 else
185 list_add_front (tc->unch_tflst, fixture_create(teardown, ischecked));
186 }
187 }
188
tcase_set_timeout(TCase * tc,int timeout)189 void tcase_set_timeout (TCase *tc, int timeout)
190 {
191 if (timeout >= 0) {
192 char *env = getenv("CK_TIMEOUT_MULTIPLIER");
193 if (env != NULL) {
194 int tmp = atoi(env);
195 if (tmp >= 0) {
196 timeout = timeout * tmp;
197 }
198 }
199 tc->timeout = timeout;
200 }
201 }
202
tcase_fn_start(const char * fname CK_ATTRIBUTE_UNUSED,const char * file,int line)203 void tcase_fn_start (const char *fname CK_ATTRIBUTE_UNUSED, const char *file, int line)
204 {
205 send_ctx_info (CK_CTX_TEST);
206 send_loc_info (file, line);
207 }
208
_mark_point(const char * file,int line)209 void _mark_point (const char *file, int line)
210 {
211 send_loc_info (file, line);
212 }
213
_fail_unless(int result,const char * file,int line,const char * expr,...)214 void _fail_unless (int result, const char *file,
215 int line, const char *expr, ...)
216 {
217 const char *msg;
218
219 send_loc_info (file, line);
220 if (!result) {
221 va_list ap;
222 char buf[BUFSIZ];
223
224 va_start(ap,expr);
225 msg = (const char*)va_arg(ap, char *);
226 if (msg == NULL)
227 msg = expr;
228 vsnprintf(buf, BUFSIZ, msg, ap);
229 va_end(ap);
230 send_failure_info (buf);
231 if (cur_fork_status() == CK_FORK) {
232 #ifdef _POSIX_VERSION
233 exit(1);
234 #endif /* _POSIX_VERSION */
235 }
236 }
237 }
238
srunner_create(Suite * s)239 SRunner *srunner_create (Suite *s)
240 {
241 SRunner *sr = emalloc (sizeof(SRunner)); /* freed in srunner_free */
242 sr->slst = check_list_create();
243 if (s != NULL)
244 list_add_end(sr->slst, s);
245 sr->stats = emalloc (sizeof(TestStats)); /* freed in srunner_free */
246 sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0;
247 sr->resultlst = check_list_create();
248 sr->log_fname = NULL;
249 sr->xml_fname = NULL;
250 sr->loglst = NULL;
251 sr->fstat = CK_FORK_GETENV;
252 return sr;
253 }
254
srunner_add_suite(SRunner * sr,Suite * s)255 void srunner_add_suite (SRunner *sr, Suite *s)
256 {
257 if (s == NULL)
258 return;
259
260 list_add_end(sr->slst, s);
261 }
262
srunner_free(SRunner * sr)263 void srunner_free (SRunner *sr)
264 {
265 List *l;
266 TestResult *tr;
267 if (sr == NULL)
268 return;
269
270 free (sr->stats);
271 l = sr->slst;
272 for (list_front(l); !list_at_end(l); list_advance(l)) {
273 suite_free(list_val(l));
274 }
275 list_free(sr->slst);
276
277 l = sr->resultlst;
278 for (list_front(l); !list_at_end(l); list_advance(l)) {
279 tr = list_val(l);
280 free(tr->file);
281 free(tr->msg);
282 free(tr);
283 }
284 list_free (sr->resultlst);
285
286 free (sr);
287 }
288
srunner_ntests_failed(SRunner * sr)289 int srunner_ntests_failed (SRunner *sr)
290 {
291 return sr->stats->n_failed + sr->stats->n_errors;
292 }
293
srunner_ntests_run(SRunner * sr)294 int srunner_ntests_run (SRunner *sr)
295 {
296 return sr->stats->n_checked;
297 }
298
srunner_failures(SRunner * sr)299 TestResult **srunner_failures (SRunner *sr)
300 {
301 int i = 0;
302 TestResult **trarray;
303 List *rlst;
304 trarray = malloc (sizeof(trarray[0]) * srunner_ntests_failed (sr));
305
306 rlst = sr->resultlst;
307 for (list_front(rlst); !list_at_end(rlst); list_advance(rlst)) {
308 TestResult *tr = list_val(rlst);
309 if (non_pass(tr->rtype))
310 trarray[i++] = tr;
311
312 }
313 return trarray;
314 }
315
srunner_results(SRunner * sr)316 TestResult **srunner_results (SRunner *sr)
317 {
318 int i = 0;
319 TestResult **trarray;
320 List *rlst;
321
322 trarray = malloc (sizeof(trarray[0]) * srunner_ntests_run (sr));
323
324 rlst = sr->resultlst;
325 for (list_front(rlst); !list_at_end(rlst); list_advance(rlst)) {
326 trarray[i++] = list_val(rlst);
327 }
328 return trarray;
329 }
330
non_pass(int val)331 static int non_pass (int val)
332 {
333 return val != CK_PASS;
334 }
335
tr_create(void)336 TestResult *tr_create(void)
337 {
338 TestResult *tr;
339
340 tr = emalloc (sizeof(TestResult));
341 tr_init (tr);
342 return tr;
343 }
344
tr_reset(TestResult * tr)345 void tr_reset(TestResult *tr)
346 {
347 tr_init(tr);
348 }
349
tr_init(TestResult * tr)350 static void tr_init (TestResult *tr)
351 {
352 tr->ctx = CK_CTX_INVALID;
353 tr->line = -1;
354 tr->rtype = CK_TEST_RESULT_INVALID;
355 tr->msg = NULL;
356 tr->file = NULL;
357 tr->tcname = NULL;
358 tr->tname = NULL;
359 }
360
361
tr_msg(TestResult * tr)362 const char *tr_msg (TestResult *tr)
363 {
364 return tr->msg;
365 }
366
tr_lno(TestResult * tr)367 int tr_lno (TestResult *tr)
368 {
369 return tr->line;
370 }
371
tr_lfile(TestResult * tr)372 const char *tr_lfile (TestResult *tr)
373 {
374 return tr->file;
375 }
376
tr_rtype(TestResult * tr)377 int tr_rtype (TestResult *tr)
378 {
379 return tr->rtype;
380 }
381
tr_ctx(TestResult * tr)382 enum ck_result_ctx tr_ctx (TestResult *tr)
383 {
384 return tr->ctx;
385 }
386
tr_tcname(TestResult * tr)387 const char *tr_tcname (TestResult *tr)
388 {
389 return tr->tcname;
390 }
391
392 static int _fstat = CK_FORK;
393
set_fork_status(enum fork_status fstat)394 void set_fork_status (enum fork_status fstat)
395 {
396 if (fstat == CK_FORK || fstat == CK_NOFORK || fstat == CK_FORK_GETENV)
397 _fstat = fstat;
398 else
399 eprintf ("Bad status in set_fork_status", __FILE__, __LINE__);
400 }
401
cur_fork_status(void)402 enum fork_status cur_fork_status (void)
403 {
404 return _fstat;
405 }
406