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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
29 #include "check_error.h"
30 #include "check_list.h"
31 #include "check_impl.h"
32 #include "check_msg.h"
35 #include <unistd.h> /* for _POSIX_VERSION */
38 #ifndef DEFAULT_TIMEOUT
39 #define DEFAULT_TIMEOUT 4
42 int check_major_version = CHECK_MAJOR_VERSION;
43 int check_minor_version = CHECK_MINOR_VERSION;
44 int check_micro_version = CHECK_MICRO_VERSION;
46 static int non_pass (int val);
47 static Fixture *fixture_create (SFun fun, int ischecked);
48 static void tcase_add_fixture (TCase * tc, SFun setup, SFun teardown,
50 static void tr_init (TestResult * tr);
51 static void suite_free (Suite * s);
52 static void tcase_free (TCase * tc);
55 suite_create (const char *name)
58 s = emalloc (sizeof (Suite)); /* freed in suite_free */
63 s->tclst = check_list_create ();
68 suite_free (Suite * s)
74 for (list_front (l); !list_at_end (l); list_advance (l)) {
75 tcase_free (list_val (l));
82 tcase_create (const char *name)
85 int timeout = DEFAULT_TIMEOUT;
86 TCase *tc = emalloc (sizeof (TCase)); /*freed in tcase_free */
92 env = getenv ("CK_DEFAULT_TIMEOUT");
100 env = getenv ("CK_TIMEOUT_MULTIPLIER");
102 int tmp = atoi (env);
104 timeout = timeout * tmp;
108 tc->timeout = timeout;
109 tc->tflst = check_list_create ();
110 tc->unch_sflst = check_list_create ();
111 tc->ch_sflst = check_list_create ();
112 tc->unch_tflst = check_list_create ();
113 tc->ch_tflst = check_list_create ();
120 tcase_free (TCase * tc)
122 list_apply (tc->tflst, free);
123 list_apply (tc->unch_sflst, free);
124 list_apply (tc->ch_sflst, free);
125 list_apply (tc->unch_tflst, free);
126 list_apply (tc->ch_tflst, free);
127 list_free (tc->tflst);
128 list_free (tc->unch_sflst);
129 list_free (tc->ch_sflst);
130 list_free (tc->unch_tflst);
131 list_free (tc->ch_tflst);
137 suite_add_tcase (Suite * s, TCase * tc)
139 if (s == NULL || tc == NULL)
141 list_add_end (s->tclst, tc);
145 _tcase_add_test (TCase * tc, TFun fn, const char *name, int _signal,
146 int allowed_exit_value, int start, int end)
149 if (tc == NULL || fn == NULL || name == NULL)
151 tf = emalloc (sizeof (TF)); /* freed in tcase_free */
153 tf->loop_start = start;
155 tf->signal = _signal; /* 0 means no signal expected */
156 tf->allowed_exit_value = allowed_exit_value; /* 0 is default successful exit */
158 list_add_end (tc->tflst, tf);
162 fixture_create (SFun fun, int ischecked)
165 f = emalloc (sizeof (Fixture));
167 f->ischecked = ischecked;
173 tcase_add_unchecked_fixture (TCase * tc, SFun setup, SFun teardown)
175 tcase_add_fixture (tc, setup, teardown, 0);
179 tcase_add_checked_fixture (TCase * tc, SFun setup, SFun teardown)
181 tcase_add_fixture (tc, setup, teardown, 1);
185 tcase_add_fixture (TCase * tc, SFun setup, SFun teardown, int ischecked)
189 list_add_end (tc->ch_sflst, fixture_create (setup, ischecked));
191 list_add_end (tc->unch_sflst, fixture_create (setup, ischecked));
194 /* Add teardowns at front so they are run in reverse order. */
197 list_add_front (tc->ch_tflst, fixture_create (teardown, ischecked));
199 list_add_front (tc->unch_tflst, fixture_create (teardown, ischecked));
204 tcase_set_timeout (TCase * tc, int timeout)
207 char *env = getenv ("CK_TIMEOUT_MULTIPLIER");
209 int tmp = atoi (env);
211 timeout = timeout * tmp;
214 tc->timeout = timeout;
219 tcase_fn_start (const char *fname CK_ATTRIBUTE_UNUSED, const char *file,
222 send_ctx_info (CK_CTX_TEST);
223 send_loc_info (file, line);
227 _mark_point (const char *file, int line)
229 send_loc_info (file, line);
233 _fail_unless (int result, const char *file, int line, const char *expr, ...)
237 send_loc_info (file, line);
243 msg = (const char *) va_arg (ap, char *);
246 vsnprintf (buf, BUFSIZ, msg, ap);
248 send_failure_info (buf);
249 if (cur_fork_status () == CK_FORK) {
250 #ifdef _POSIX_VERSION
252 #endif /* _POSIX_VERSION */
258 srunner_create (Suite * s)
260 SRunner *sr = emalloc (sizeof (SRunner)); /* freed in srunner_free */
261 sr->slst = check_list_create ();
263 list_add_end (sr->slst, s);
264 sr->stats = emalloc (sizeof (TestStats)); /* freed in srunner_free */
265 sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0;
266 sr->resultlst = check_list_create ();
267 sr->log_fname = NULL;
268 sr->xml_fname = NULL;
270 sr->fstat = CK_FORK_GETENV;
275 srunner_add_suite (SRunner * sr, Suite * s)
280 list_add_end (sr->slst, s);
284 srunner_free (SRunner * sr)
293 for (list_front (l); !list_at_end (l); list_advance (l)) {
294 suite_free (list_val (l));
296 list_free (sr->slst);
299 for (list_front (l); !list_at_end (l); list_advance (l)) {
305 list_free (sr->resultlst);
311 srunner_ntests_failed (SRunner * sr)
313 return sr->stats->n_failed + sr->stats->n_errors;
317 srunner_ntests_run (SRunner * sr)
319 return sr->stats->n_checked;
323 srunner_failures (SRunner * sr)
326 TestResult **trarray;
328 trarray = malloc (sizeof (trarray[0]) * srunner_ntests_failed (sr));
330 rlst = sr->resultlst;
331 for (list_front (rlst); !list_at_end (rlst); list_advance (rlst)) {
332 TestResult *tr = list_val (rlst);
333 if (non_pass (tr->rtype))
341 srunner_results (SRunner * sr)
344 TestResult **trarray;
347 trarray = malloc (sizeof (trarray[0]) * srunner_ntests_run (sr));
349 rlst = sr->resultlst;
350 for (list_front (rlst); !list_at_end (rlst); list_advance (rlst)) {
351 trarray[i++] = list_val (rlst);
359 return val != CK_PASS;
367 tr = emalloc (sizeof (TestResult));
373 tr_reset (TestResult * tr)
379 tr_init (TestResult * tr)
381 tr->ctx = CK_CTX_INVALID;
383 tr->rtype = CK_TEST_RESULT_INVALID;
392 tr_msg (TestResult * tr)
398 tr_lno (TestResult * tr)
404 tr_lfile (TestResult * tr)
410 tr_rtype (TestResult * tr)
416 tr_ctx (TestResult * tr)
422 tr_tcname (TestResult * tr)
427 static int _fstat = CK_FORK;
430 set_fork_status (enum fork_status fstat)
432 if (fstat == CK_FORK || fstat == CK_NOFORK || fstat == CK_FORK_GETENV)
435 eprintf ("Bad status in set_fork_status", __FILE__, __LINE__);
439 cur_fork_status (void)