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., 51 Franklin St, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
19 */
20
21 #include "libcompat/libcompat.h"
22
23 #include <stdio.h>
24 #include <stdarg.h>
25
26 #include "internal-check.h"
27 #include "check_list.h"
28 #include "check_error.h"
29 #include "check_impl.h"
30 #include "check_str.h"
31
32 static const char *tr_type_str (TestResult * tr);
33 static int percent_passed (TestStats * t);
34
35 char *
tr_str(TestResult * tr)36 tr_str (TestResult * tr)
37 {
38 const char *exact_msg;
39 char *rstr;
40
41 exact_msg = (tr->rtype == CK_ERROR) ? "(after this point) " : "";
42
43 rstr = ck_strdup_printf ("%s:%d:%s:%s:%s:%d: %s%s",
44 tr->file, tr->line,
45 tr_type_str (tr), tr->tcname, tr->tname, tr->iter, exact_msg, tr->msg);
46
47 return rstr;
48 }
49
50 char *
tr_short_str(TestResult * tr)51 tr_short_str (TestResult * tr)
52 {
53 const char *exact_msg;
54 char *rstr;
55
56 exact_msg = (tr->rtype == CK_ERROR) ? "(after this point) " : "";
57
58 rstr = ck_strdup_printf ("%s:%d: %s%s",
59 tr->file, tr->line, exact_msg, tr->msg);
60
61 return rstr;
62 }
63
64 char *
sr_stat_str(SRunner * sr)65 sr_stat_str (SRunner * sr)
66 {
67 char *str;
68 TestStats *ts;
69
70 ts = sr->stats;
71
72 str = ck_strdup_printf ("%d%%: Checks: %d, Failures: %d, Errors: %d",
73 percent_passed (ts), ts->n_checked, ts->n_failed, ts->n_errors);
74
75 return str;
76 }
77
78 char *
ck_strdup_printf(const char * fmt,...)79 ck_strdup_printf (const char *fmt, ...)
80 {
81 /* Guess we need no more than 100 bytes. */
82 int n;
83 size_t size = 100;
84 char *p;
85 va_list ap;
86
87 p = (char *) emalloc (size);
88
89 while (1) {
90 /* Try to print in the allocated space. */
91 va_start (ap, fmt);
92 n = vsnprintf (p, size, fmt, ap);
93 va_end (ap);
94 /* If that worked, return the string. */
95 if (n > -1 && n < (int) size)
96 return p;
97
98 /* Else try again with more space. */
99 if (n > -1) /* C99 conform vsnprintf() */
100 size = (size_t) n + 1; /* precisely what is needed */
101 else /* glibc 2.0 */
102 size *= 2; /* twice the old size */
103
104 p = (char *) erealloc (p, size);
105 }
106 }
107
108 static const char *
tr_type_str(TestResult * tr)109 tr_type_str (TestResult * tr)
110 {
111 const char *str = NULL;
112
113 if (tr->ctx == CK_CTX_TEST) {
114 if (tr->rtype == CK_PASS)
115 str = "P";
116 else if (tr->rtype == CK_FAILURE)
117 str = "F";
118 else if (tr->rtype == CK_ERROR)
119 str = "E";
120 } else
121 str = "S";
122
123 return str;
124 }
125
126 static int
percent_passed(TestStats * t)127 percent_passed (TestStats * t)
128 {
129 if (t->n_failed == 0 && t->n_errors == 0)
130 return 100;
131 else if (t->n_checked == 0)
132 return 0;
133 else
134 return (int) ((float) (t->n_checked - (t->n_failed + t->n_errors)) /
135 (float) t->n_checked * 100);
136 }
137