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