1 | /*******************
|
---|
2 |
|
---|
3 | LICENSE
|
---|
4 |
|
---|
5 | Copyright (c) 2003 Asim Jalis
|
---|
6 |
|
---|
7 | This software is provided 'as-is', without any express or implied
|
---|
8 | warranty. In no event will the authors be held liable for any damages
|
---|
9 | arising from the use of this software.
|
---|
10 |
|
---|
11 | Permission is granted to anyone to use this software for any purpose,
|
---|
12 | including commercial applications, and to alter it and redistribute it
|
---|
13 | freely, subject to the following restrictions:
|
---|
14 |
|
---|
15 | 1. The origin of this software must not be misrepresented; you must not
|
---|
16 | claim that you wrote the original software. If you use this software in
|
---|
17 | a product, an acknowledgment in the product documentation would be
|
---|
18 | appreciated but is not required.
|
---|
19 |
|
---|
20 | 2. Altered source versions must be plainly marked as such, and must not
|
---|
21 | be misrepresented as being the original software.
|
---|
22 |
|
---|
23 | 3. This notice may not be removed or altered from any source
|
---|
24 | distribution.
|
---|
25 |
|
---|
26 | **********************/
|
---|
27 |
|
---|
28 | #include <assert.h>
|
---|
29 | #include <setjmp.h>
|
---|
30 | #include <stdlib.h>
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <string.h>
|
---|
33 | #include <math.h>
|
---|
34 |
|
---|
35 | #include "CuTest.h"
|
---|
36 |
|
---|
37 | /*-------------------------------------------------------------------------*
|
---|
38 | * CuStr
|
---|
39 | *-------------------------------------------------------------------------*/
|
---|
40 |
|
---|
41 | char* CuStrAlloc(int size)
|
---|
42 | {
|
---|
43 | char* newStr = (char*) malloc( sizeof(char) * (size) );
|
---|
44 | return newStr;
|
---|
45 | }
|
---|
46 |
|
---|
47 | char* CuStrCopy(const char* old)
|
---|
48 | {
|
---|
49 | int len = strlen(old);
|
---|
50 | char* newStr = CuStrAlloc(len + 1);
|
---|
51 | strcpy(newStr, old);
|
---|
52 | return newStr;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /*-------------------------------------------------------------------------*
|
---|
56 | * CuString
|
---|
57 | *-------------------------------------------------------------------------*/
|
---|
58 |
|
---|
59 | void CuStringInit(CuString* str)
|
---|
60 | {
|
---|
61 | str->length = 0;
|
---|
62 | str->size = STRING_MAX;
|
---|
63 | str->buffer = (char*) malloc(sizeof(char) * str->size);
|
---|
64 | if (str->buffer)
|
---|
65 | str->buffer[0] = '\0';
|
---|
66 | else
|
---|
67 | {
|
---|
68 | perror("CuStringInit");
|
---|
69 | _exit (EXIT_FAILURE);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | CuString* CuStringNew(void)
|
---|
74 | {
|
---|
75 | CuString* str = (CuString*) malloc(sizeof(CuString));
|
---|
76 | str->length = 0;
|
---|
77 | str->size = STRING_MAX;
|
---|
78 | str->buffer = (char*) malloc(sizeof(char) * str->size);
|
---|
79 | if (str->buffer)
|
---|
80 | str->buffer[0] = '\0';
|
---|
81 | else
|
---|
82 | {
|
---|
83 | perror("CuStringNew");
|
---|
84 | _exit (EXIT_FAILURE);
|
---|
85 | }
|
---|
86 | return str;
|
---|
87 | }
|
---|
88 |
|
---|
89 | void CuStringResize(CuString* str, int newSize)
|
---|
90 | {
|
---|
91 | str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize);
|
---|
92 | str->size = newSize;
|
---|
93 | }
|
---|
94 |
|
---|
95 | void CuStringAppend(CuString* str, const char* text)
|
---|
96 | {
|
---|
97 | int length;
|
---|
98 |
|
---|
99 | if (text == NULL) {
|
---|
100 | text = "NULL";
|
---|
101 | }
|
---|
102 |
|
---|
103 | length = strlen(text);
|
---|
104 | if (str->length + length + 1 >= str->size)
|
---|
105 | CuStringResize(str, str->length + length + 1 + STRING_INC);
|
---|
106 | str->length += length;
|
---|
107 | strcat(str->buffer, text);
|
---|
108 | }
|
---|
109 |
|
---|
110 | void CuStringAppendChar(CuString* str, char ch)
|
---|
111 | {
|
---|
112 | char text[2];
|
---|
113 | text[0] = ch;
|
---|
114 | text[1] = '\0';
|
---|
115 | CuStringAppend(str, text);
|
---|
116 | }
|
---|
117 |
|
---|
118 | void CuStringAppendFormat(CuString* str, const char* format, ...)
|
---|
119 | {
|
---|
120 | va_list argp;
|
---|
121 | char buf[HUGE_STRING_LEN];
|
---|
122 | va_start(argp, format);
|
---|
123 | vsprintf(buf, format, argp);
|
---|
124 | va_end(argp);
|
---|
125 | CuStringAppend(str, buf);
|
---|
126 | }
|
---|
127 |
|
---|
128 | void CuStringInsert(CuString* str, const char* text, int pos)
|
---|
129 | {
|
---|
130 | int length = strlen(text);
|
---|
131 | if (pos > str->length)
|
---|
132 | pos = str->length;
|
---|
133 | if (str->length + length + 1 >= str->size)
|
---|
134 | CuStringResize(str, str->length + length + 1 + STRING_INC);
|
---|
135 | memmove(str->buffer + pos + length, str->buffer + pos, (str->length - pos) + 1);
|
---|
136 | str->length += length;
|
---|
137 | memcpy(str->buffer + pos, text, length);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*-------------------------------------------------------------------------*
|
---|
141 | * CuTest
|
---|
142 | *-------------------------------------------------------------------------*/
|
---|
143 |
|
---|
144 | void CuTestInit(CuTest* t, const char* name, TestFunction function)
|
---|
145 | {
|
---|
146 | t->name = CuStrCopy(name);
|
---|
147 | t->failed = 0;
|
---|
148 | t->ran = 0;
|
---|
149 | t->message = NULL;
|
---|
150 | t->function = function;
|
---|
151 | t->jumpBuf = NULL;
|
---|
152 | }
|
---|
153 |
|
---|
154 | CuTest* CuTestNew(const char* name, TestFunction function)
|
---|
155 | {
|
---|
156 | CuTest* tc = CU_ALLOC(CuTest);
|
---|
157 | CuTestInit(tc, name, function);
|
---|
158 | return tc;
|
---|
159 | }
|
---|
160 |
|
---|
161 | void CuTestRun(CuTest* tc)
|
---|
162 | {
|
---|
163 | jmp_buf buf;
|
---|
164 | tc->jumpBuf = &buf;
|
---|
165 | if (setjmp(buf) == 0)
|
---|
166 | {
|
---|
167 | tc->ran = 1;
|
---|
168 | (tc->function)(tc);
|
---|
169 | }
|
---|
170 | tc->jumpBuf = 0;
|
---|
171 | }
|
---|
172 |
|
---|
173 | static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string)
|
---|
174 | {
|
---|
175 | char buf[HUGE_STRING_LEN];
|
---|
176 |
|
---|
177 | sprintf(buf, "%s:%d: ", file, line);
|
---|
178 | CuStringInsert(string, buf, 0);
|
---|
179 |
|
---|
180 | tc->failed = 1;
|
---|
181 | tc->message = string->buffer;
|
---|
182 | if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
|
---|
183 | }
|
---|
184 |
|
---|
185 | void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message)
|
---|
186 | {
|
---|
187 | CuString string;
|
---|
188 |
|
---|
189 | CuStringInit(&string);
|
---|
190 | if (message2 != NULL)
|
---|
191 | {
|
---|
192 | CuStringAppend(&string, message2);
|
---|
193 | CuStringAppend(&string, ": ");
|
---|
194 | }
|
---|
195 | CuStringAppend(&string, message);
|
---|
196 | CuFailInternal(tc, file, line, &string);
|
---|
197 | }
|
---|
198 |
|
---|
199 | void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition)
|
---|
200 | {
|
---|
201 | if (condition) return;
|
---|
202 | CuFail_Line(tc, file, line, NULL, message);
|
---|
203 | }
|
---|
204 |
|
---|
205 | void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
|
---|
206 | const char* expected, const char* actual)
|
---|
207 | {
|
---|
208 | CuString string;
|
---|
209 | if ((expected == NULL && actual == NULL) ||
|
---|
210 | (expected != NULL && actual != NULL &&
|
---|
211 | strcmp(expected, actual) == 0))
|
---|
212 | {
|
---|
213 | return;
|
---|
214 | }
|
---|
215 |
|
---|
216 | CuStringInit(&string);
|
---|
217 | if (message != NULL)
|
---|
218 | {
|
---|
219 | CuStringAppend(&string, message);
|
---|
220 | CuStringAppend(&string, ": ");
|
---|
221 | }
|
---|
222 | CuStringAppend(&string, "expected <");
|
---|
223 | CuStringAppend(&string, expected);
|
---|
224 | CuStringAppend(&string, "> but was <");
|
---|
225 | CuStringAppend(&string, actual);
|
---|
226 | CuStringAppend(&string, ">");
|
---|
227 | CuFailInternal(tc, file, line, &string);
|
---|
228 | }
|
---|
229 |
|
---|
230 | void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
|
---|
231 | int expected, int actual)
|
---|
232 | {
|
---|
233 | char buf[STRING_MAX];
|
---|
234 | if (expected == actual) return;
|
---|
235 | sprintf(buf, "expected <%d> but was <%d>", expected, actual);
|
---|
236 | CuFail_Line(tc, file, line, message, buf);
|
---|
237 | }
|
---|
238 |
|
---|
239 | void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
|
---|
240 | double expected, double actual, double delta)
|
---|
241 | {
|
---|
242 | char buf[STRING_MAX];
|
---|
243 | if (fabs(expected - actual) <= delta) return;
|
---|
244 | sprintf(buf, "expected <%lf> but was <%lf>", expected, actual);
|
---|
245 | CuFail_Line(tc, file, line, message, buf);
|
---|
246 | }
|
---|
247 |
|
---|
248 | void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
|
---|
249 | void* expected, void* actual)
|
---|
250 | {
|
---|
251 | char buf[STRING_MAX];
|
---|
252 | if (expected == actual) return;
|
---|
253 | sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual);
|
---|
254 | CuFail_Line(tc, file, line, message, buf);
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | /*-------------------------------------------------------------------------*
|
---|
259 | * CuSuite
|
---|
260 | *-------------------------------------------------------------------------*/
|
---|
261 |
|
---|
262 | void CuSuiteInit(CuSuite* testSuite)
|
---|
263 | {
|
---|
264 | testSuite->count = 0;
|
---|
265 | testSuite->failCount = 0;
|
---|
266 | }
|
---|
267 |
|
---|
268 | CuSuite* CuSuiteNew(void)
|
---|
269 | {
|
---|
270 | CuSuite* testSuite = CU_ALLOC(CuSuite);
|
---|
271 | CuSuiteInit(testSuite);
|
---|
272 | return testSuite;
|
---|
273 | }
|
---|
274 |
|
---|
275 | void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase)
|
---|
276 | {
|
---|
277 | assert(testSuite->count < MAX_TEST_CASES);
|
---|
278 | testSuite->list[testSuite->count] = testCase;
|
---|
279 | testSuite->count++;
|
---|
280 | }
|
---|
281 |
|
---|
282 | void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2)
|
---|
283 | {
|
---|
284 | int i;
|
---|
285 | for (i = 0 ; i < testSuite2->count ; ++i)
|
---|
286 | {
|
---|
287 | CuTest* testCase = testSuite2->list[i];
|
---|
288 | CuSuiteAdd(testSuite, testCase);
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | void CuSuiteRun(CuSuite* testSuite)
|
---|
293 | {
|
---|
294 | int i;
|
---|
295 | for (i = 0 ; i < testSuite->count ; ++i)
|
---|
296 | {
|
---|
297 | CuTest* testCase = testSuite->list[i];
|
---|
298 | CuTestRun(testCase);
|
---|
299 | if (testCase->failed) { testSuite->failCount += 1; }
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | void CuSuiteSummary(CuSuite* testSuite, CuString* summary)
|
---|
304 | {
|
---|
305 | int i;
|
---|
306 | for (i = 0 ; i < testSuite->count ; ++i)
|
---|
307 | {
|
---|
308 | CuTest* testCase = testSuite->list[i];
|
---|
309 | CuStringAppend(summary, testCase->failed ? "F" : ".");
|
---|
310 | }
|
---|
311 | CuStringAppend(summary, "\n\n");
|
---|
312 | }
|
---|
313 |
|
---|
314 | void CuSuiteDetails(CuSuite* testSuite, CuString* details)
|
---|
315 | {
|
---|
316 | int i;
|
---|
317 | int failCount = 0;
|
---|
318 |
|
---|
319 | if (testSuite->failCount == 0)
|
---|
320 | {
|
---|
321 | int passCount = testSuite->count - testSuite->failCount;
|
---|
322 | const char* testWord = passCount == 1 ? "test" : "tests";
|
---|
323 | CuStringAppendFormat(details, "OK (%d %s)\n", passCount, testWord);
|
---|
324 | }
|
---|
325 | else
|
---|
326 | {
|
---|
327 | if (testSuite->failCount == 1)
|
---|
328 | CuStringAppend(details, "There was 1 failure:\n");
|
---|
329 | else
|
---|
330 | CuStringAppendFormat(details, "There were %d failures:\n", testSuite->failCount);
|
---|
331 |
|
---|
332 | for (i = 0 ; i < testSuite->count ; ++i)
|
---|
333 | {
|
---|
334 | CuTest* testCase = testSuite->list[i];
|
---|
335 | if (testCase->failed)
|
---|
336 | {
|
---|
337 | failCount++;
|
---|
338 | CuStringAppendFormat(details, "%d) %s: %s\n",
|
---|
339 | failCount, testCase->name, testCase->message);
|
---|
340 | }
|
---|
341 | }
|
---|
342 | CuStringAppend(details, "\n!!!FAILURES!!!\n");
|
---|
343 |
|
---|
344 | CuStringAppendFormat(details, "Runs: %d ", testSuite->count);
|
---|
345 | CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount);
|
---|
346 | CuStringAppendFormat(details, "Fails: %d\n", testSuite->failCount);
|
---|
347 | }
|
---|
348 | }
|
---|