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"
29 #if defined(HAVE_FORK) && HAVE_FORK==1
31 #endif /* HAVE_FORK */
34 #include "check_error.h"
35 #include "check_list.h"
36 #include "check_impl.h"
37 #include "check_msg.h"
39 #ifndef DEFAULT_TIMEOUT
40 #define DEFAULT_TIMEOUT 4
44 * When a process exits either normally, with exit(), or
45 * by an uncaught signal, The lower 0x377 bits are passed
46 * to the parent. Of those, only the lower 8 bits are
47 * returned by the WEXITSTATUS() macro.
49 #define WEXITSTATUS_MASK 0xFF
51 int check_major_version = CHECK_MAJOR_VERSION;
52 int check_minor_version = CHECK_MINOR_VERSION;
53 int check_micro_version = CHECK_MICRO_VERSION;
55 const char* current_test_name = NULL;
57 static int non_pass(int val);
58 static Fixture *fixture_create(SFun fun, int ischecked);
59 static void tcase_add_fixture(TCase * tc, SFun setup, SFun teardown,
61 static void tr_init(TestResult * tr);
62 static void suite_free(Suite * s);
63 static void tcase_free(TCase * tc);
65 Suite *suite_create(const char *name)
69 s = (Suite *)emalloc(sizeof(Suite)); /* freed in suite_free */
74 s->tclst = check_list_create();
78 int suite_tcase(Suite * s, const char *tcname)
86 for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
88 TCase *tc = (TCase *)check_list_val(l);
89 if(strcmp(tcname, tc->name) == 0)
96 static void suite_free(Suite * s)
103 for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
105 tcase_free((TCase *)check_list_val(l));
107 check_list_free(s->tclst);
112 TCase *tcase_create(const char *name)
115 double timeout_sec = DEFAULT_TIMEOUT;
117 TCase *tc = (TCase *)emalloc(sizeof(TCase)); /*freed in tcase_free */
124 env = getenv("CK_DEFAULT_TIMEOUT");
128 double tmp = strtod(env, &endptr);
130 if(tmp >= 0 && endptr != env && (*endptr) == '\0')
136 env = getenv("CK_TIMEOUT_MULTIPLIER");
140 double tmp = strtod(env, &endptr);
142 if(tmp >= 0 && endptr != env && (*endptr) == '\0')
144 timeout_sec = timeout_sec * tmp;
148 tc->timeout.tv_sec = (time_t) floor(timeout_sec);
149 tc->timeout.tv_nsec =
150 (long)((timeout_sec -
151 floor(timeout_sec)) * (double)NANOS_PER_SECONDS);
153 tc->tflst = check_list_create();
154 tc->unch_sflst = check_list_create();
155 tc->ch_sflst = check_list_create();
156 tc->unch_tflst = check_list_create();
157 tc->ch_tflst = check_list_create();
158 tc->tags = check_list_create();
164 * Helper function to create a list of tags from
165 * a space separated string.
167 List *tag_string_to_list(const char *tags_string)
173 list = check_list_create();
175 if (NULL == tags_string)
180 tags = strdup(tags_string);
181 tag = strtok(tags, " ");
184 check_list_add_end(list, strdup(tag));
185 tag = strtok(NULL, " ");
191 void tcase_set_tags(TCase * tc, const char *tags_orig)
193 /* replace any pre-existing list */
196 check_list_apply(tc->tags, free);
197 check_list_free(tc->tags);
199 tc->tags = tag_string_to_list(tags_orig);
202 static void tcase_free(TCase * tc)
204 check_list_apply(tc->tflst, free);
205 check_list_apply(tc->unch_sflst, free);
206 check_list_apply(tc->ch_sflst, free);
207 check_list_apply(tc->unch_tflst, free);
208 check_list_apply(tc->ch_tflst, free);
209 check_list_apply(tc->tags, free);
210 check_list_free(tc->tflst);
211 check_list_free(tc->unch_sflst);
212 check_list_free(tc->ch_sflst);
213 check_list_free(tc->unch_tflst);
214 check_list_free(tc->ch_tflst);
215 check_list_free(tc->tags);
219 unsigned int tcase_matching_tag(TCase *tc, List *check_for)
222 if (NULL == check_for)
227 for(check_list_front(check_for); !check_list_at_end(check_for);
228 check_list_advance(check_for))
230 for(check_list_front(tc->tags); !check_list_at_end(tc->tags);
231 check_list_advance(tc->tags))
233 if (0 == strcmp((const char *)check_list_val(tc->tags),
234 (const char *)check_list_val(check_for)))
243 void suite_add_tcase(Suite * s, TCase * tc)
245 if(s == NULL || tc == NULL || check_list_contains(s->tclst, tc))
250 check_list_add_end(s->tclst, tc);
253 void _tcase_add_test(TCase * tc, const TTest * ttest,
254 int _signal, int allowed_exit_value,
259 if(tc == NULL || ttest == NULL)
261 tf = (TF *)emalloc(sizeof(TF)); /* freed in tcase_free */
263 tf->loop_start = start;
265 tf->signal = _signal; /* 0 means no signal expected */
266 tf->allowed_exit_value =
267 (WEXITSTATUS_MASK & allowed_exit_value); /* 0 is default successful exit */
268 check_list_add_end(tc->tflst, tf);
271 static Fixture *fixture_create(SFun fun, int ischecked)
275 f = (Fixture *)emalloc(sizeof(Fixture));
277 f->ischecked = ischecked;
282 void tcase_add_unchecked_fixture(TCase * tc, SFun setup, SFun teardown)
284 tcase_add_fixture(tc, setup, teardown, 0);
287 void tcase_add_checked_fixture(TCase * tc, SFun setup, SFun teardown)
289 tcase_add_fixture(tc, setup, teardown, 1);
292 static void tcase_add_fixture(TCase * tc, SFun setup, SFun teardown,
298 check_list_add_end(tc->ch_sflst,
299 fixture_create(setup, ischecked));
301 check_list_add_end(tc->unch_sflst,
302 fixture_create(setup, ischecked));
305 /* Add teardowns at front so they are run in reverse order. */
309 check_list_add_front(tc->ch_tflst,
310 fixture_create(teardown, ischecked));
312 check_list_add_front(tc->unch_tflst,
313 fixture_create(teardown, ischecked));
317 void tcase_set_timeout(TCase * tc, double timeout)
319 #if defined(HAVE_FORK)
322 char *env = getenv("CK_TIMEOUT_MULTIPLIER");
327 double tmp = strtod(env, &endptr);
329 if(tmp >= 0 && endptr != env && (*endptr) == '\0')
331 timeout = timeout * tmp;
335 tc->timeout.tv_sec = (time_t) floor(timeout);
336 tc->timeout.tv_nsec =
337 (long)((timeout - floor(timeout)) * (double)NANOS_PER_SECONDS);
342 /* Ignoring, as Check is not compiled with fork support. */
343 #endif /* HAVE_FORK */
346 void tcase_fn_start(const char *fname, const char *file,
349 send_ctx_info(CK_CTX_TEST);
350 send_loc_info(file, line);
352 current_test_name = fname;
355 const char* tcase_name(void)
357 return current_test_name;
360 void _mark_point(const char *file, int line)
362 send_loc_info(file, line);
365 void _ck_assert_failed(const char *file, int line, const char *expr, ...)
372 send_loc_info(file, line);
375 msg = (const char *)va_arg(ap, char *);
378 * If a message was passed, format it with vsnprintf.
379 * Otherwise, print the expression as is.
383 vsnprintf(buf, BUFSIZ, msg, ap);
392 send_failure_info(to_send);
393 if(cur_fork_status() == CK_FORK)
395 #if defined(HAVE_FORK) && HAVE_FORK==1
397 #endif /* HAVE_FORK */
401 longjmp(error_jmp_buffer, 1);
405 SRunner *srunner_create(Suite * s)
407 SRunner *sr = (SRunner *)emalloc(sizeof(SRunner)); /* freed in srunner_free */
409 sr->slst = check_list_create();
411 check_list_add_end(sr->slst, s);
412 sr->stats = (TestStats *)emalloc(sizeof(TestStats)); /* freed in srunner_free */
413 sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0;
414 sr->resultlst = check_list_create();
415 sr->log_fname = NULL;
416 sr->xml_fname = NULL;
417 sr->tap_fname = NULL;
420 #if defined(HAVE_FORK)
421 sr->fstat = CK_FORK_GETENV;
424 * Overriding the default of running tests in fork mode,
425 * as this system does not have fork()
427 sr->fstat = CK_NOFORK;
428 #endif /* HAVE_FORK */
433 void srunner_add_suite(SRunner * sr, Suite * s)
438 check_list_add_end(sr->slst, s);
441 void srunner_free(SRunner * sr)
450 for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
452 suite_free((Suite *)check_list_val(l));
454 check_list_free(sr->slst);
457 for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
459 TestResult *tr = (TestResult *)check_list_val(l);
462 check_list_free(sr->resultlst);
467 int srunner_ntests_failed(SRunner * sr)
469 return sr->stats->n_failed + sr->stats->n_errors;
472 int srunner_ntests_run(SRunner * sr)
474 return sr->stats->n_checked;
477 TestResult **srunner_failures(SRunner * sr)
480 TestResult **trarray;
483 trarray = (TestResult **)emalloc(sizeof(trarray[0]) * srunner_ntests_failed(sr));
485 rlst = sr->resultlst;
486 for(check_list_front(rlst); !check_list_at_end(rlst);
487 check_list_advance(rlst))
489 TestResult *tr = (TestResult *)check_list_val(rlst);
491 if(non_pass(tr->rtype))
498 TestResult **srunner_results(SRunner * sr)
501 TestResult **trarray;
504 trarray =(TestResult **) emalloc(sizeof(trarray[0]) * srunner_ntests_run(sr));
506 rlst = sr->resultlst;
507 for(check_list_front(rlst); !check_list_at_end(rlst);
508 check_list_advance(rlst))
510 trarray[i++] = (TestResult *)check_list_val(rlst);
515 static int non_pass(int val)
517 return val != CK_PASS;
520 TestResult *tr_create(void)
524 tr = (TestResult *)emalloc(sizeof(TestResult));
529 static void tr_init(TestResult * tr)
531 tr->ctx = CK_CTX_INVALID;
533 tr->rtype = CK_TEST_RESULT_INVALID;
541 void tr_free(TestResult * tr)
549 const char *tr_msg(TestResult * tr)
554 int tr_lno(TestResult * tr)
559 const char *tr_lfile(TestResult * tr)
564 int tr_rtype(TestResult * tr)
569 enum ck_result_ctx tr_ctx(TestResult * tr)
574 const char *tr_tcname(TestResult * tr)
579 static enum fork_status _fstat = CK_FORK;
581 void set_fork_status(enum fork_status fstat)
583 if(fstat == CK_FORK || fstat == CK_NOFORK || fstat == CK_FORK_GETENV)
586 eprintf("Bad status in set_fork_status", __FILE__, __LINE__);
589 enum fork_status cur_fork_status(void)
595 * Not all systems support the same clockid_t's. This call checks
596 * if the CLOCK_MONOTONIC clockid_t is valid. If so, that is returned,
597 * otherwise, CLOCK_REALTIME is returned.
599 * The clockid_t that was found to work on the first call is
600 * cached for subsequent calls.
602 clockid_t check_get_clockid()
604 static clockid_t clockid = -1;
607 * Only check if we have librt available. Otherwise, the clockid
608 * will be ignored anyway, as the clock_gettime() and
609 * timer_create() functions will be re-implemented in libcompat.
610 * Worse, if librt and alarm() are unavailable, this check
611 * will result in an assert(0).
616 if(timer_create(CLOCK_MONOTONIC, NULL, &timerid) == 0)
618 timer_delete(timerid);
619 clockid = CLOCK_MONOTONIC;
623 clockid = CLOCK_REALTIME;
626 clockid = CLOCK_MONOTONIC;