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"
27 #include "check_list.h"
28 #include "check_error.h"
29 #include "check_impl.h"
30 #include "check_str.h"
32 static const char *tr_type_str(TestResult * tr);
33 static int percent_passed(TestStats * t);
35 char *tr_str(TestResult * tr)
37 const char *exact_msg;
40 exact_msg = (tr->rtype == CK_ERROR) ? "(after this point) " : "";
42 rstr = ck_strdup_printf("%s:%d:%s:%s:%s:%d: %s%s",
44 tr_type_str(tr), tr->tcname, tr->tname, tr->iter,
50 char *tr_short_str(TestResult * tr)
52 const char *exact_msg;
55 exact_msg = (tr->rtype == CK_ERROR) ? "(after this point) " : "";
57 rstr = ck_strdup_printf("%s:%d: %s%s",
58 tr->file, tr->line, exact_msg, tr->msg);
63 char *sr_stat_str(SRunner * sr)
70 str = ck_strdup_printf("%d%%: Checks: %d, Failures: %d, Errors: %d",
71 percent_passed(ts), ts->n_checked, ts->n_failed,
77 char *ck_strdup_printf(const char *fmt, ...)
79 /* Guess we need no more than 100 bytes. */
84 p = (char *)emalloc(size);
89 /* Try to print in the allocated space. */
91 n = vsnprintf(p, size, fmt, ap);
93 /* If that worked, return the string. */
94 if(n > -1 && n < (int)size)
97 /* Else try again with more space. */
98 if(n > -1) /* C99 conform vsnprintf() */
99 size = (size_t) n + 1; /* precisely what is needed */
101 size *= 2; /* twice the old size */
103 p = (char *)erealloc(p, size);
107 static const char *tr_type_str(TestResult * tr)
109 if(tr->ctx == CK_CTX_TEST)
111 if(tr->rtype == CK_PASS)
113 if(tr->rtype == CK_FAILURE)
115 if(tr->rtype == CK_ERROR)
122 static int percent_passed(TestStats * t)
124 if(t->n_failed == 0 && t->n_errors == 0)
126 if(t->n_checked == 0)
128 return (int)((float)(t->n_checked - (t->n_failed + t->n_errors)) /
129 (float)t->n_checked * 100);