2 * Copyright (c) 2002-2006 Asim Jalis
4 * This library is released under the zlib/libpng license as described at
6 * http://www.opensource.org/licenses/zlib-license.html
8 * Here is the statement of the license:
10 * This software is provided 'as-is', without any express or implied warranty.
11 * In no event will the authors be held liable for any damages arising from
12 * the use of this software.
14 * Permission is granted to anyone to use this software for any purpose,
15 * including commercial applications, and to alter it and redistribute it
16 * freely, subject to the following restrictions:
18 * 1. The origin of this software must not be misrepresented; you must not
19 * claim that you wrote the original software. If you use this software in a
20 * product, an acknowledgment in the product documentation would be
21 * appreciated but is not required.
23 * 2. Altered source versions must be plainly marked as such, and must not be
24 * misrepresented as being the original software.
26 * 3. This notice may not be removed or altered from any source distribution.
29 * This file has been modified from the original distribution.
40 static int verbose = 0;
42 void CuInit(int argc, char *argv[])
46 /* Windows doesn't have getopt, so we have to fake it. We can't use
47 * apr_getopt, because CuTest is meant to be a stand-alone test suite
49 for (i = 0; i < argc; i++) {
50 if (!strcmp(argv[i], "-v")) {
56 /*-------------------------------------------------------------------------*
58 *-------------------------------------------------------------------------*/
60 char* CuStrAlloc(int size)
62 char* new = (char*) malloc( sizeof(char) * (size) );
66 char* CuStrCopy(const char* old)
68 int len = strlen(old);
69 char* new = CuStrAlloc(len + 1);
74 /*-------------------------------------------------------------------------*
76 *-------------------------------------------------------------------------*/
78 void CuStringInit(CuString* str)
81 str->size = STRING_MAX;
82 str->buffer = (char*) malloc(sizeof(char) * str->size);
83 str->buffer[0] = '\0';
86 CuString* CuStringNew(void)
88 CuString* str = (CuString*) malloc(sizeof(CuString));
90 str->size = STRING_MAX;
91 str->buffer = (char*) malloc(sizeof(char) * str->size);
92 str->buffer[0] = '\0';
96 void CuStringResize(CuString* str, int newSize)
98 str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize);
102 void CuStringAppend(CuString* str, const char* text)
104 int length = strlen(text);
105 if (str->length + length + 1 >= str->size)
106 CuStringResize(str, str->length + length + 1 + STRING_INC);
107 str->length += length;
108 strcat(str->buffer, text);
111 void CuStringAppendChar(CuString* str, char ch)
116 CuStringAppend(str, text);
119 void CuStringAppendFormat(CuString* str, const char* format, ...)
122 char buf[HUGE_STRING_LEN];
123 va_start(argp, format);
124 vsprintf(buf, format, argp);
126 CuStringAppend(str, buf);
129 void CuStringRead(CuString *str, char *path)
131 path = strdup(str->buffer);
134 /*-------------------------------------------------------------------------*
136 *-------------------------------------------------------------------------*/
138 void CuTestInit(CuTest* t, char* name, TestFunction function)
140 t->name = CuStrCopy(name);
145 t->function = function;
149 CuTest* CuTestNew(char* name, TestFunction function)
151 CuTest* tc = CU_ALLOC(CuTest);
152 CuTestInit(tc, name, function);
156 void CuNotImpl(CuTest* tc, const char* message)
158 CuString* newstr = CuStringNew();
159 CuStringAppend(newstr, message);
160 CuStringAppend(newstr, " not implemented on this platform");
162 tc->message = CuStrCopy(newstr->buffer);
163 if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
166 void CuFail(CuTest* tc, const char* message)
169 tc->message = CuStrCopy(message);
170 if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
173 void CuAssert(CuTest* tc, const char* message, int condition)
175 if (condition) return;
179 void CuAssertTrue(CuTest* tc, int condition)
181 if (condition) return;
182 CuFail(tc, "assert failed");
185 void CuAssertStrNEquals(CuTest* tc, const char* expected, const char* actual,
189 if (strncmp(expected, actual, n) == 0) return;
190 message = CuStringNew();
191 CuStringAppend(message, "expected\n---->\n");
192 CuStringAppend(message, expected);
193 CuStringAppend(message, "\n<----\nbut saw\n---->\n");
194 CuStringAppend(message, actual);
195 CuStringAppend(message, "\n<----");
196 CuFail(tc, message->buffer);
199 void CuAssertStrEquals(CuTest* tc, const char* expected, const char* actual)
202 if (strcmp(expected, actual) == 0) return;
203 message = CuStringNew();
204 CuStringAppend(message, "expected\n---->\n");
205 CuStringAppend(message, expected);
206 CuStringAppend(message, "\n<----\nbut saw\n---->\n");
207 CuStringAppend(message, actual);
208 CuStringAppend(message, "\n<----");
209 CuFail(tc, message->buffer);
212 void CuAssertIntEquals(CuTest* tc, int expected, int actual)
214 char buf[STRING_MAX];
215 if (expected == actual) return;
216 sprintf(buf, "expected <%d> but was <%d>", expected, actual);
220 void CuAssertPtrEquals(CuTest* tc, const void* expected, const void* actual)
222 char buf[STRING_MAX];
223 if (expected == actual) return;
224 sprintf(buf, "expected pointer <%p> but was <%p>", expected, actual);
228 void CuAssertPtrNotNull(CuTest* tc, const void* pointer)
230 char buf[STRING_MAX];
231 if (pointer != NULL ) return;
232 sprintf(buf, "null pointer unexpected, but was <%p>", pointer);
236 void CuTestRun(CuTest* tc)
240 if (setjmp(buf) == 0)
248 /*-------------------------------------------------------------------------*
250 *-------------------------------------------------------------------------*/
252 void CuSuiteInit(CuSuite* testSuite, char *name)
254 testSuite->name = strdup(name);
255 testSuite->count = 0;
256 testSuite->failCount = 0;
257 testSuite->notimplCount = 0;
260 CuSuite* CuSuiteNew(char *name)
262 CuSuite* testSuite = CU_ALLOC(CuSuite);
263 CuSuiteInit(testSuite, name);
267 void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase)
269 assert(testSuite->count < MAX_TEST_CASES);
270 testSuite->list[testSuite->count] = testCase;
274 void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2)
277 for (i = 0 ; i < testSuite2->count ; ++i)
279 CuTest* testCase = testSuite2->list[i];
280 CuSuiteAdd(testSuite, testCase);
284 void CuSuiteRun(CuSuite* testSuite)
287 for (i = 0 ; i < testSuite->count ; ++i)
289 CuTest* testCase = testSuite->list[i];
291 if (testCase->failed) { testSuite->failCount += 1; }
292 if (testCase->notimpl) { testSuite->notimplCount += 1; }
296 void CuSuiteSummary(CuSuite* testSuite, CuString* summary)
299 for (i = 0 ; i < testSuite->count ; ++i)
301 CuTest* testCase = testSuite->list[i];
302 CuStringAppend(summary, testCase->failed ? "F" :
303 testCase->notimpl ? "N": ".");
305 CuStringAppend(summary, "\n");
308 void CuSuiteOverView(CuSuite* testSuite, CuString* details)
310 CuStringAppendFormat(details, "%d %s run: %d passed, %d failed, "
311 "%d not implemented.\n",
313 testSuite->count == 1 ? "test" : "tests",
314 testSuite->count - testSuite->failCount -
315 testSuite->notimplCount,
316 testSuite->failCount, testSuite->notimplCount);
319 void CuSuiteDetails(CuSuite* testSuite, CuString* details)
324 if (testSuite->failCount != 0 && verbose)
326 CuStringAppendFormat(details, "\nFailed tests in %s:\n", testSuite->name);
327 for (i = 0 ; i < testSuite->count ; ++i)
329 CuTest* testCase = testSuite->list[i];
330 if (testCase->failed)
333 CuStringAppendFormat(details, "%d) %s: %s\n",
334 failCount, testCase->name, testCase->message);
338 if (testSuite->notimplCount != 0 && verbose)
340 CuStringAppendFormat(details, "\nNot Implemented tests in %s:\n", testSuite->name);
341 for (i = 0 ; i < testSuite->count ; ++i)
343 CuTest* testCase = testSuite->list[i];
344 if (testCase->notimpl)
347 CuStringAppendFormat(details, "%d) %s: %s\n",
348 failCount, testCase->name, testCase->message);
354 /*-------------------------------------------------------------------------*
356 *-------------------------------------------------------------------------*/
358 CuSuiteList* CuSuiteListNew(char *name)
360 CuSuiteList* testSuite = CU_ALLOC(CuSuiteList);
361 testSuite->name = strdup(name);
362 testSuite->count = 0;
366 void CuSuiteListAdd(CuSuiteList *suites, CuSuite *origsuite)
368 assert(suites->count < MAX_TEST_CASES);
369 suites->list[suites->count] = origsuite;
373 void CuSuiteListRun(CuSuiteList* testSuite)
376 for (i = 0 ; i < testSuite->count ; ++i)
378 CuSuite* testCase = testSuite->list[i];
379 CuSuiteRun(testCase);
383 static const char *genspaces(int i)
385 char *str = malloc((i + 1) * sizeof(char));
391 void CuSuiteListRunWithSummary(CuSuiteList* testSuite)
395 printf("%s:\n", testSuite->name);
396 for (i = 0 ; i < testSuite->count ; ++i)
398 CuSuite* testCase = testSuite->list[i];
399 CuString *str = CuStringNew();
401 printf(" %s:%s", testCase->name,
402 genspaces(21 - strlen(testCase->name)));
404 CuSuiteRun(testCase);
405 CuSuiteSummary(testCase, str);
406 printf(" %s", str->buffer);
412 void CuSuiteListSummary(CuSuiteList* testSuite, CuString* summary)
415 CuStringAppendFormat(summary, "%s:\n", testSuite->name);
416 for (i = 0 ; i < testSuite->count ; ++i)
418 CuSuite* testCase = testSuite->list[i];
419 CuString *str = CuStringNew();
420 CuSuiteSummary(testCase, str);
421 CuStringAppend(summary, " ");
422 CuStringAppend(summary, str->buffer);
424 CuStringAppend(summary, "\n");
427 int CuSuiteListDetails(CuSuiteList* testSuite, CuString* details)
431 int notImplCount = 0;
434 for (i = 0 ; i < testSuite->count ; ++i)
436 failCount += testSuite->list[i]->failCount;
437 notImplCount += testSuite->list[i]->notimplCount;
438 count += testSuite->list[i]->count;
440 CuStringAppendFormat(details, "%d %s run: %d passed, %d failed, "
441 "%d not implemented.\n",
443 count == 1 ? "test" : "tests",
444 count - failCount - notImplCount,
445 failCount, notImplCount);
447 if (failCount != 0 && verbose)
449 for (i = 0 ; i < testSuite->count ; ++i)
451 CuString *str = CuStringNew();
452 CuSuite* testCase = testSuite->list[i];
453 if (testCase->failCount)
455 CuSuiteDetails(testCase, str);
456 CuStringAppend(details, str->buffer);
460 if (notImplCount != 0 && verbose)
462 for (i = 0 ; i < testSuite->count ; ++i)
464 CuString *str = CuStringNew();
465 CuSuite* testCase = testSuite->list[i];
466 if (testCase->notimplCount)
468 CuSuiteDetails(testCase, str);
469 CuStringAppend(details, str->buffer);