766954a697bc171d4641df4e649fd41deb704569
[fw/sdcc] / support / regression / fwk / lib / testfwk.c
1 /** Test framework support functions.
2  */
3 #include <testfwk.h>
4 #include <stdarg.h>
5
6 #ifdef __ds390
7 #include <tinibios.h> /* main() must see the ISR declarations */
8 #endif
9
10 /** Define this if the port's div or mod functions are broken.
11     A slow loop based method will be substituded.
12 */
13 //#define BROKEN_DIV_MOD                1
14
15 void _putchar(char c);
16 void _exitEmu(void);
17
18 #if BROKEN_DIV_MOD
19 int __div(int num, int denom)
20 {
21     int q = 0;
22     while (num >= denom) {
23         q++;
24         num -= denom;
25     }
26     return q;
27 }
28
29 int __mod(int num, int denom)
30 {
31     while (num >= denom) {
32         num -= denom;
33     }
34     return num;
35 }
36 #else
37 int __div(int num, int denom)
38 {
39     return num/denom;
40 }
41
42 int __mod(int num, int denom)
43 {
44     return num%denom;
45 }
46 #endif
47
48 static void _printn(int n) 
49 {
50     int rem;
51
52     if (n < 0) {
53         _putchar('-');
54         n = -n;
55     }
56
57     rem = __mod(n, 10);
58     if (rem != n) {
59         _printn(__div(n, 10));
60     }
61     _putchar('0' + rem);
62 }
63
64 void __printf(const char *szFormat, ...) REENTRANT
65 {
66     va_list ap;
67     va_start(ap, szFormat);
68
69     while (*szFormat) {
70         if (*szFormat == '%') {
71             switch (*++szFormat) {
72             case 's': {
73                 char *sz = va_arg(ap, char *);
74                 while (*sz) {
75                     _putchar(*sz++);
76                 }
77                 break;
78             }
79             case 'u': {
80                 int i = va_arg(ap, int);
81                 _printn(i);
82                 break;
83             }
84             case '%':
85                 _putchar('%');
86                 break;
87             default:
88                 break;
89             }
90         }
91         else {
92             _putchar(*szFormat);
93         }
94         szFormat++;
95     }
96     va_end(ap);
97 }
98
99 int __numTests;
100 int __numFailures;
101
102 void 
103 __fail(const char *szMsg, const char *szCond, const char *szFile, int line)
104 {
105     __printf("--- FAIL: \"%s\" on %s at %s:%u\n", szMsg, szCond, szFile, line);
106     __numFailures++;
107 }
108
109 int 
110 main(void)
111 {
112     TESTFUNP *cases;
113     int numCases = 0;
114
115     __printf("--- Running: %s\n", getSuiteName());
116
117     cases = suite();
118
119     while (*cases) {
120         __printf("Running %u\n", numCases);
121         (*cases)();
122         cases++;
123         numCases++;
124     }
125     
126     __printf("--- Summary: %u/%u/%u: %u failed of %u tests in %u cases.\n", 
127            __numFailures, __numTests, numCases,
128            __numFailures, __numTests, numCases
129            );
130
131     _exitEmu();
132
133     return 0;
134 }