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,
18 * Boston, MA 02110-1301, USA.
21 #include "../lib/libcompat.h"
30 #include "check_error.h"
31 #include "check_list.h"
32 #include "check_impl.h"
33 #include "check_msg.h"
35 #ifndef DEFAULT_TIMEOUT
36 #define DEFAULT_TIMEOUT 4
40 * When a process exits either normally, with exit(), or
41 * by an uncaught signal, The lower 0x377 bits are passed
42 * to the parent. Of those, only the lower 8 bits are
43 * returned by the WEXITSTATUS() macro.
45 #define WEXITSTATUS_MASK 0xFF
47 int check_major_version = CHECK_MAJOR_VERSION;
48 int check_minor_version = CHECK_MINOR_VERSION;
49 int check_micro_version = CHECK_MICRO_VERSION;
51 static int non_pass (int val);
52 static Fixture *fixture_create (SFun fun, int ischecked);
53 static void tcase_add_fixture (TCase * tc, SFun setup, SFun teardown,
55 static void tr_init (TestResult * tr);
56 static void suite_free (Suite * s);
57 static void tcase_free (TCase * tc);
60 suite_create (const char *name)
64 s = (Suite *) emalloc (sizeof (Suite)); /* freed in suite_free */
69 s->tclst = check_list_create ();
74 suite_tcase (Suite * s, const char *tcname)
83 for (check_list_front (l); !check_list_at_end (l); check_list_advance (l)) {
84 tc = (TCase *) check_list_val (l);
85 if (strcmp (tcname, tc->name) == 0)
93 suite_free (Suite * s)
100 for (check_list_front (l); !check_list_at_end (l); check_list_advance (l)) {
101 tcase_free ((TCase *) check_list_val (l));
103 check_list_free (s->tclst);
108 tcase_create (const char *name)
111 double timeout_sec = DEFAULT_TIMEOUT;
113 TCase *tc = (TCase *) emalloc (sizeof (TCase)); /*freed in tcase_free */
120 env = getenv ("CK_DEFAULT_TIMEOUT");
123 double tmp = strtod (env, &endptr);
125 if (tmp >= 0 && endptr != env && (*endptr) == '\0') {
130 env = getenv ("CK_TIMEOUT_MULTIPLIER");
133 double tmp = strtod (env, &endptr);
135 if (tmp >= 0 && endptr != env && (*endptr) == '\0') {
136 timeout_sec = timeout_sec * tmp;
140 tc->timeout.tv_sec = (time_t) floor (timeout_sec);
141 tc->timeout.tv_nsec =
142 (long) ((timeout_sec - floor (timeout_sec)) * (double) NANOS_PER_SECONDS);
144 tc->tflst = check_list_create ();
145 tc->unch_sflst = check_list_create ();
146 tc->ch_sflst = check_list_create ();
147 tc->unch_tflst = check_list_create ();
148 tc->ch_tflst = check_list_create ();
155 tcase_free (TCase * tc)
157 check_list_apply (tc->tflst, free);
158 check_list_apply (tc->unch_sflst, free);
159 check_list_apply (tc->ch_sflst, free);
160 check_list_apply (tc->unch_tflst, free);
161 check_list_apply (tc->ch_tflst, free);
162 check_list_free (tc->tflst);
163 check_list_free (tc->unch_sflst);
164 check_list_free (tc->ch_sflst);
165 check_list_free (tc->unch_tflst);
166 check_list_free (tc->ch_tflst);
172 suite_add_tcase (Suite * s, TCase * tc)
174 if (s == NULL || tc == NULL)
176 check_list_add_end (s->tclst, tc);
180 _tcase_add_test (TCase * tc, TFun fn, const char *name, int _signal,
181 int allowed_exit_value, int start, int end)
185 if (tc == NULL || fn == NULL || name == NULL)
187 tf = (TF *) emalloc (sizeof (TF)); /* freed in tcase_free */
189 tf->loop_start = start;
191 tf->signal = _signal; /* 0 means no signal expected */
192 tf->allowed_exit_value = (WEXITSTATUS_MASK & allowed_exit_value); /* 0 is default successful exit */
194 check_list_add_end (tc->tflst, tf);
198 fixture_create (SFun fun, int ischecked)
202 f = (Fixture *) emalloc (sizeof (Fixture));
204 f->ischecked = ischecked;
210 tcase_add_unchecked_fixture (TCase * tc, SFun setup, SFun teardown)
212 tcase_add_fixture (tc, setup, teardown, 0);
216 tcase_add_checked_fixture (TCase * tc, SFun setup, SFun teardown)
218 tcase_add_fixture (tc, setup, teardown, 1);
222 tcase_add_fixture (TCase * tc, SFun setup, SFun teardown, int ischecked)
226 check_list_add_end (tc->ch_sflst, fixture_create (setup, ischecked));
228 check_list_add_end (tc->unch_sflst, fixture_create (setup, ischecked));
231 /* Add teardowns at front so they are run in reverse order. */
234 check_list_add_front (tc->ch_tflst, fixture_create (teardown, ischecked));
236 check_list_add_front (tc->unch_tflst,
237 fixture_create (teardown, ischecked));
242 tcase_set_timeout (TCase * tc, double timeout)
244 #if defined(HAVE_FORK)
246 char *env = getenv ("CK_TIMEOUT_MULTIPLIER");
250 double tmp = strtod (env, &endptr);
252 if (tmp >= 0 && endptr != env && (*endptr) == '\0') {
253 timeout = timeout * tmp;
257 tc->timeout.tv_sec = (time_t) floor (timeout);
258 tc->timeout.tv_nsec =
259 (long) ((timeout - floor (timeout)) * (double) NANOS_PER_SECONDS);
265 ("This version does not support timeouts, as fork is not supported",
267 #endif /* HAVE_FORK */
271 tcase_fn_start (const char *fname CK_ATTRIBUTE_UNUSED, const char *file,
274 send_ctx_info (CK_CTX_TEST);
275 send_loc_info (file, line);
279 _mark_point (const char *file, int line)
281 send_loc_info (file, line);
285 _ck_assert_failed (const char *file, int line, const char *expr, ...)
291 send_loc_info (file, line);
294 msg = (const char *) va_arg (ap, char *);
298 vsnprintf (buf, BUFSIZ, msg, ap);
300 send_failure_info (buf);
301 if (cur_fork_status () == CK_FORK) {
302 #if defined(HAVE_FORK) && HAVE_FORK==1
304 #endif /* HAVE_FORK */
306 longjmp (error_jmp_buffer, 1);
311 srunner_create (Suite * s)
313 SRunner *sr = (SRunner *) emalloc (sizeof (SRunner)); /* freed in srunner_free */
315 sr->slst = check_list_create ();
317 check_list_add_end (sr->slst, s);
318 sr->stats = (TestStats *) emalloc (sizeof (TestStats)); /* freed in srunner_free */
319 sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0;
320 sr->resultlst = check_list_create ();
321 sr->log_fname = NULL;
322 sr->xml_fname = NULL;
323 sr->tap_fname = NULL;
326 #if defined(HAVE_FORK)
327 sr->fstat = CK_FORK_GETENV;
330 * Overriding the default of running tests in fork mode,
331 * as this system does not have fork()
333 sr->fstat = CK_NOFORK;
334 #endif /* HAVE_FORK */
340 srunner_add_suite (SRunner * sr, Suite * s)
345 check_list_add_end (sr->slst, s);
349 srunner_free (SRunner * sr)
359 for (check_list_front (l); !check_list_at_end (l); check_list_advance (l)) {
360 suite_free ((Suite *) check_list_val (l));
362 check_list_free (sr->slst);
365 for (check_list_front (l); !check_list_at_end (l); check_list_advance (l)) {
366 tr = (TestResult *) check_list_val (l);
369 check_list_free (sr->resultlst);
375 srunner_ntests_failed (SRunner * sr)
377 return sr->stats->n_failed + sr->stats->n_errors;
381 srunner_ntests_run (SRunner * sr)
383 return sr->stats->n_checked;
387 srunner_failures (SRunner * sr)
390 TestResult **trarray;
394 (TestResult **) emalloc (sizeof (trarray[0]) *
395 srunner_ntests_failed (sr));
397 rlst = sr->resultlst;
398 for (check_list_front (rlst); !check_list_at_end (rlst);
399 check_list_advance (rlst)) {
400 TestResult *tr = (TestResult *) check_list_val (rlst);
402 if (non_pass (tr->rtype))
410 srunner_results (SRunner * sr)
413 TestResult **trarray;
417 (TestResult **) emalloc (sizeof (trarray[0]) * srunner_ntests_run (sr));
419 rlst = sr->resultlst;
420 for (check_list_front (rlst); !check_list_at_end (rlst);
421 check_list_advance (rlst)) {
422 trarray[i++] = (TestResult *) check_list_val (rlst);
430 return val != CK_PASS;
438 tr = (TestResult *) emalloc (sizeof (TestResult));
444 tr_init (TestResult * tr)
446 tr->ctx = CK_CTX_INVALID;
448 tr->rtype = CK_TEST_RESULT_INVALID;
457 tr_free (TestResult * tr)
466 tr_msg (TestResult * tr)
472 tr_lno (TestResult * tr)
478 tr_lfile (TestResult * tr)
484 tr_rtype (TestResult * tr)
490 tr_ctx (TestResult * tr)
496 tr_tcname (TestResult * tr)
501 static enum fork_status _fstat = CK_FORK;
504 set_fork_status (enum fork_status fstat)
506 if (fstat == CK_FORK || fstat == CK_NOFORK || fstat == CK_FORK_GETENV)
509 eprintf ("Bad status in set_fork_status", __FILE__, __LINE__);
513 cur_fork_status (void)
519 * Not all systems support the same clockid_t's. This call checks
520 * if the CLOCK_MONOTONIC clockid_t is valid. If so, that is returned,
521 * otherwise, CLOCK_REALTIME is returned.
523 * The clockid_t that was found to work on the first call is
524 * cached for subsequent calls.
529 static clockid_t clockid = -1;
533 * Only check if we have librt available. Otherwise, the clockid
534 * will be ignored anyway, as the clock_gettime() and
535 * timer_create() functions will be re-implemented in libcompat.
536 * Worse, if librt and alarm() are unavailable, this check
537 * will result in an assert(0).
542 if (timer_create (CLOCK_MONOTONIC, NULL, &timerid) == 0) {
543 timer_delete (timerid);
544 clockid = CLOCK_MONOTONIC;
546 clockid = CLOCK_REALTIME;
549 clockid = CLOCK_MONOTONIC;