]> pd.if.org Git - nbds/blob - test/CuTest.c
all structures now support arbitrary type keys with a fast path for integers
[nbds] / test / CuTest.c
1 #include <assert.h>
2 #include <setjmp.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <math.h>
7
8 #include "CuTest.h"
9
10 /*-------------------------------------------------------------------------*
11  * CuStr
12  *-------------------------------------------------------------------------*/
13
14 char* CuStrAlloc(int size)
15 {
16         char* newStr = (char*) malloc( sizeof(char) * (size) );
17         return newStr;
18 }
19
20 char* CuStrCopy(const char* old)
21 {
22         int len = strlen(old);
23         char* newStr = CuStrAlloc(len + 1);
24         strcpy(newStr, old);
25         return newStr;
26 }
27
28 /*-------------------------------------------------------------------------*
29  * CuString
30  *-------------------------------------------------------------------------*/
31
32 void CuStringInit(CuString* str)
33 {
34         str->length = 0;
35         str->size = STRING_MAX;
36         str->buffer = (char*) malloc(sizeof(char) * str->size);
37         str->buffer[0] = '\0';
38 }
39
40 CuString* CuStringNew(void)
41 {
42         CuString* str = (CuString*) malloc(sizeof(CuString));
43         str->length = 0;
44         str->size = STRING_MAX;
45         str->buffer = (char*) malloc(sizeof(char) * str->size);
46         str->buffer[0] = '\0';
47         return str;
48 }
49
50 void CuStringResize(CuString* str, int newSize)
51 {
52         str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize);
53         str->size = newSize;
54 }
55
56 void CuStringAppend(CuString* str, const char* text)
57 {
58         int length;
59
60         if (text == NULL) {
61                 text = "NULL";
62         }
63
64         length = strlen(text);
65         if (str->length + length + 1 >= str->size)
66                 CuStringResize(str, str->length + length + 1 + STRING_INC);
67         str->length += length;
68         strcat(str->buffer, text);
69 }
70
71 void CuStringAppendChar(CuString* str, char ch)
72 {
73         char text[2];
74         text[0] = ch;
75         text[1] = '\0';
76         CuStringAppend(str, text);
77 }
78
79 void CuStringAppendFormat(CuString* str, const char* format, ...)
80 {
81         va_list argp;
82         char buf[HUGE_STRING_LEN];
83         va_start(argp, format);
84         vsprintf(buf, format, argp);
85         va_end(argp);
86         CuStringAppend(str, buf);
87 }
88
89 void CuStringInsert(CuString* str, const char* text, int pos)
90 {
91         int length = strlen(text);
92         if (pos > str->length)
93                 pos = str->length;
94         if (str->length + length + 1 >= str->size)
95                 CuStringResize(str, str->length + length + 1 + STRING_INC);
96         memmove(str->buffer + pos + length, str->buffer + pos, (str->length - pos) + 1);
97         str->length += length;
98         memcpy(str->buffer + pos, text, length);
99 }
100
101 /*-------------------------------------------------------------------------*
102  * CuTest
103  *-------------------------------------------------------------------------*/
104
105 void CuTestInit(CuTest* t, const char* name, TestFunction function)
106 {
107         t->name = CuStrCopy(name);
108         t->failed = 0;
109         t->ran = 0;
110         t->message = NULL;
111         t->function = function;
112         t->jumpBuf = NULL;
113 }
114
115 CuTest* CuTestNew(const char* name, TestFunction function)
116 {
117         CuTest* tc = CU_ALLOC(CuTest);
118         CuTestInit(tc, name, function);
119         return tc;
120 }
121
122 void CuTestRun(CuTest* tc)
123 {
124         jmp_buf buf;
125         tc->jumpBuf = &buf;
126         if (setjmp(buf) == 0)
127         {
128                 tc->ran = 1;
129                 (tc->function)(tc);
130         }
131         tc->jumpBuf = 0;
132 }
133
134 static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string)
135 {
136         char buf[HUGE_STRING_LEN];
137
138         sprintf(buf, "%s:%d: ", file, line);
139         CuStringInsert(string, buf, 0);
140
141         tc->failed = 1;
142         tc->message = string->buffer;
143     extern void lwt_halt(void);
144     extern void lwt_dump(const char *);
145     lwt_dump(tc->name);
146         if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
147 }
148
149 void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message)
150 {
151         CuString string;
152
153         CuStringInit(&string);
154         if (message2 != NULL) 
155         {
156                 CuStringAppend(&string, message2);
157                 CuStringAppend(&string, ": ");
158         }
159         CuStringAppend(&string, message);
160         CuFailInternal(tc, file, line, &string);
161 }
162
163 void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition)
164 {
165         if (condition) return;
166         CuFail_Line(tc, file, line, NULL, message);
167 }
168
169 void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, 
170         const char* expected, const char* actual)
171 {
172         CuString string;
173         if ((expected == NULL && actual == NULL) ||
174             (expected != NULL && actual != NULL &&
175              strcmp(expected, actual) == 0))
176         {
177                 return;
178         }
179
180         CuStringInit(&string);
181         if (message != NULL) 
182         {
183                 CuStringAppend(&string, message);
184                 CuStringAppend(&string, ": ");
185         }
186         CuStringAppend(&string, "expected <");
187         CuStringAppend(&string, expected);
188         CuStringAppend(&string, "> but was <");
189         CuStringAppend(&string, actual);
190         CuStringAppend(&string, ">");
191         CuFailInternal(tc, file, line, &string);
192 }
193
194 void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, 
195         int expected, int actual)
196 {
197         char buf[STRING_MAX];
198         if (expected == actual) return;
199         sprintf(buf, "expected <%d> but was <%d>", expected, actual);
200         CuFail_Line(tc, file, line, message, buf);
201 }
202
203 void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, 
204         double expected, double actual, double delta)
205 {
206         char buf[STRING_MAX];
207         if (fabs(expected - actual) <= delta) return;
208         sprintf(buf, "expected <%lf> but was <%lf>", expected, actual);
209         CuFail_Line(tc, file, line, message, buf);
210 }
211
212 void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, 
213         void* expected, void* actual)
214 {
215         char buf[STRING_MAX];
216         if (expected == actual) return;
217         sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual);
218         CuFail_Line(tc, file, line, message, buf);
219 }
220
221
222 /*-------------------------------------------------------------------------*
223  * CuSuite
224  *-------------------------------------------------------------------------*/
225
226 void CuSuiteInit(CuSuite* testSuite)
227 {
228         testSuite->count = 0;
229         testSuite->failCount = 0;
230 }
231
232 CuSuite* CuSuiteNew(void)
233 {
234         CuSuite* testSuite = CU_ALLOC(CuSuite);
235         CuSuiteInit(testSuite);
236         return testSuite;
237 }
238
239 void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase)
240 {
241         assert(testSuite->count < MAX_TEST_CASES);
242         testSuite->list[testSuite->count] = testCase;
243         testSuite->count++;
244 }
245
246 void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2)
247 {
248         int i;
249         for (i = 0 ; i < testSuite2->count ; ++i)
250         {
251                 CuTest* testCase = testSuite2->list[i];
252                 CuSuiteAdd(testSuite, testCase);
253         }
254 }
255
256 void CuSuiteRun(CuSuite* testSuite)
257 {
258         int i;
259         for (i = 0 ; i < testSuite->count ; ++i)
260         {
261                 CuTest* testCase = testSuite->list[i];
262                 CuTestRun(testCase);
263                 if (testCase->failed) { testSuite->failCount += 1; }
264         }
265 }
266
267 void CuSuiteSummary(CuSuite* testSuite, CuString* summary)
268 {
269         int i;
270         for (i = 0 ; i < testSuite->count ; ++i)
271         {
272                 CuTest* testCase = testSuite->list[i];
273                 CuStringAppend(summary, testCase->failed ? "F" : ".");
274         }
275         CuStringAppend(summary, "\n\n");
276 }
277
278 void CuSuiteDetails(CuSuite* testSuite, CuString* details)
279 {
280         int i;
281         int failCount = 0;
282
283         if (testSuite->failCount == 0)
284         {
285                 int passCount = testSuite->count - testSuite->failCount;
286                 const char* testWord = passCount == 1 ? "test" : "tests";
287                 CuStringAppendFormat(details, "OK (%d %s)\n", passCount, testWord);
288         }
289         else
290         {
291                 if (testSuite->failCount == 1)
292                         CuStringAppend(details, "There was 1 failure:\n");
293                 else
294                         CuStringAppendFormat(details, "There were %d failures:\n", testSuite->failCount);
295
296                 for (i = 0 ; i < testSuite->count ; ++i)
297                 {
298                         CuTest* testCase = testSuite->list[i];
299                         if (testCase->failed)
300                         {
301                                 failCount++;
302                                 CuStringAppendFormat(details, "%d) %s: %s\n",
303                                         failCount, testCase->name, testCase->message);
304                         }
305                 }
306                 CuStringAppend(details, "\n!!!FAILURES!!!\n");
307
308                 CuStringAppendFormat(details, "Runs: %d ",   testSuite->count);
309                 CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount);
310                 CuStringAppendFormat(details, "Fails: %d\n",  testSuite->failCount);
311         }
312 }