Imported Upstream version 2.9.0
[debian/cc1111] / support / regression / fwk / lib / testfwk.c
1 /** Test framework support functions.
2  */
3 #include <testfwk.h>
4 #ifndef NO_VARARGS
5 #include <stdarg.h>
6 #endif
7
8 #ifdef SDCC_ds390
9 #include <tinibios.h> /* main() must see the ISR declarations */
10 #endif
11
12 #ifdef SDCC_mcs51
13 /* until changed, isr's must have a prototype in the module containing main */
14 void T2_isr (void) interrupt 5;
15 #define MEMSPACE_BUF idata
16 #else
17 #define MEMSPACE_BUF
18 #endif
19
20 /** Define this if the port's div or mod functions are broken.
21     A slow loop based method will be substituded.
22 */
23 //#define BROKEN_DIV_MOD                1
24
25 extern void _putchar(char c);
26 extern void _initEmu(void);
27 extern void _exitEmu(void);
28
29 int __numTests = 0;
30 static int __numFailures = 0;
31
32 #if BROKEN_DIV_MOD
33 static int
34 __div(int num, int denom)
35 {
36     int q = 0;
37     while (num >= denom) {
38         q++;
39         num -= denom;
40     }
41     return q;
42 }
43
44 static int
45 __mod(int num, int denom)
46 {
47     while (num >= denom) {
48         num -= denom;
49     }
50     return num;
51 }
52 #else
53 #define __div(num, denom) ((num) / (denom))
54 #define __mod(num, denom) ((num) % (denom))
55 #endif
56
57 void
58 __prints(const char *s)
59 {
60   char c;
61
62   while ('\0' != (c = *s)) {
63     _putchar(c);
64     ++s;
65   }
66 }
67
68 void
69 __printn(int n)
70 {
71   if (0 == n) {
72     _putchar('0');
73   }
74   else {
75     static char MEMSPACE_BUF buf[6];
76     char MEMSPACE_BUF *p = &buf[sizeof(buf) - 1];
77     char neg = 0;
78
79     buf[sizeof(buf) - 1] = '\0';
80
81     if (0 > n) {
82       n = -n;
83       neg = 1;
84     }
85   
86     while (0 != n) {
87       *--p = '0' + __mod(n, 10);
88       n = __div(n, 10);
89     }
90
91     if (neg)
92       _putchar('-');
93
94     __prints(p);
95   }
96 }
97
98 #ifndef NO_VARARGS
99 void
100 __printf(const char *szFormat, ...)
101 {
102   va_list ap;
103   va_start(ap, szFormat);
104
105   while (*szFormat) {
106     if (*szFormat == '%') {
107       switch (*++szFormat) {
108       case 's': {
109         char *sz = va_arg(ap, char *);
110         __prints(sz);
111         break;
112       }
113       case 'u': {
114         int i = va_arg(ap, int);
115         __printn(i);
116         break;
117       }
118       case '%':
119         _putchar('%');
120         break;
121       default:
122         break;
123       }
124     }
125     else {
126       _putchar(*szFormat);
127     }
128     szFormat++;
129   }
130   va_end(ap);
131 }
132
133 void
134 __fail(code const char *szMsg, code const char *szCond, code const char *szFile, int line)
135 {
136   __printf("--- FAIL: \"%s\" on %s at %s:%u\n", szMsg, szCond, szFile, line);
137   __numFailures++;
138 }
139
140 int
141 main(void)
142 {
143   _initEmu();
144
145   __printf("--- Running: %s\n", __getSuiteName());
146
147   __runSuite();
148
149   __printf("--- Summary: %u/%u/%u: %u failed of %u tests in %u cases.\n",
150      __numFailures, __numTests, __numCases,
151      __numFailures, __numTests, __numCases
152      );
153
154   _exitEmu();
155
156   return 0;
157 }
158 #else
159 void
160 __fail(code const char *szMsg, code const char *szCond, code const char *szFile, int line)
161 {
162   __prints("--- FAIL: \"");
163   __prints(szMsg);
164   __prints("\" on ");
165   __prints(szCond);
166   __prints(" at ");
167   __prints(szFile);
168   _putchar(':');
169   __printn(line);
170   _putchar('\n');
171
172   __numFailures++;
173 }
174
175 int
176 main(void)
177 {
178   _initEmu();
179
180   __prints("--- Running: ");
181   __prints(__getSuiteName());
182   _putchar('\n');
183
184   __runSuite();
185
186   __prints("--- Summary: ");
187   __printn(__numFailures);
188   _putchar('/');
189   __printn(__numTests);
190   _putchar('/');
191   __printn(__numCases);
192   __prints(": ");
193   __printn(__numFailures);
194   __prints(" failed of ");
195   __printn(__numTests);
196   __prints(" tests in ");
197   __printn(__numCases);
198   __prints(" cases.\n");
199
200   _exitEmu();
201
202   return 0;
203 }
204 #endif