10 /*-------------------------------------------------------------------------*
12 *-------------------------------------------------------------------------*/
14 char* CuStrAlloc(int size)
16 char* newStr = (char*) malloc( sizeof(char) * (size) );
20 char* CuStrCopy(const char* old)
22 int len = strlen(old);
23 char* newStr = CuStrAlloc(len + 1);
28 /*-------------------------------------------------------------------------*
30 *-------------------------------------------------------------------------*/
32 void CuStringInit(CuString* str)
35 str->size = STRING_MAX;
36 str->buffer = (char*) malloc(sizeof(char) * str->size);
37 str->buffer[0] = '\0';
40 CuString* CuStringNew(void)
42 CuString* str = (CuString*) malloc(sizeof(CuString));
44 str->size = STRING_MAX;
45 str->buffer = (char*) malloc(sizeof(char) * str->size);
46 str->buffer[0] = '\0';
50 void CuStringDelete(CuString *str)
57 void CuStringResize(CuString* str, int newSize)
59 str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize);
63 void CuStringAppend(CuString* str, const char* text)
71 length = strlen(text);
72 if (str->length + length + 1 >= str->size)
73 CuStringResize(str, str->length + length + 1 + STRING_INC);
74 str->length += length;
75 strcat(str->buffer, text);
78 void CuStringAppendChar(CuString* str, char ch)
83 CuStringAppend(str, text);
86 void CuStringAppendFormat(CuString* str, const char* format, ...)
89 char buf[HUGE_STRING_LEN];
90 va_start(argp, format);
91 vsprintf(buf, format, argp);
93 CuStringAppend(str, buf);
96 void CuStringInsert(CuString* str, const char* text, int pos)
98 int length = strlen(text);
99 if (pos > str->length)
101 if (str->length + length + 1 >= str->size)
102 CuStringResize(str, str->length + length + 1 + STRING_INC);
103 memmove(str->buffer + pos + length, str->buffer + pos, (str->length - pos) + 1);
104 str->length += length;
105 memcpy(str->buffer + pos, text, length);
108 /*-------------------------------------------------------------------------*
110 *-------------------------------------------------------------------------*/
112 void CuTestInit(CuTest* t, const char* name, TestFunction function)
114 t->name = CuStrCopy(name);
118 t->function = function;
122 CuTest* CuTestNew(const char* name, TestFunction function)
124 CuTest* tc = CU_ALLOC(CuTest);
125 CuTestInit(tc, name, function);
129 void CuTestDelete(CuTest *t)
136 void CuTestRun(CuTest* tc)
140 if (setjmp(buf) == 0)
148 static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string)
150 char buf[HUGE_STRING_LEN];
152 sprintf(buf, "%s:%d: ", file, line);
153 CuStringInsert(string, buf, 0);
156 tc->message = string->buffer;
157 if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
160 void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message)
164 CuStringInit(&string);
165 if (message2 != NULL)
167 CuStringAppend(&string, message2);
168 CuStringAppend(&string, ": ");
170 CuStringAppend(&string, message);
171 CuFailInternal(tc, file, line, &string);
174 void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition)
176 if (condition) return;
177 CuFail_Line(tc, file, line, NULL, message);
180 void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
181 const char* expected, const char* actual)
184 if ((expected == NULL && actual == NULL) ||
185 (expected != NULL && actual != NULL &&
186 strcmp(expected, actual) == 0))
191 CuStringInit(&string);
194 CuStringAppend(&string, message);
195 CuStringAppend(&string, ": ");
197 CuStringAppend(&string, "expected <");
198 CuStringAppend(&string, expected);
199 CuStringAppend(&string, "> but was <");
200 CuStringAppend(&string, actual);
201 CuStringAppend(&string, ">");
202 CuFailInternal(tc, file, line, &string);
205 void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
206 int expected, int actual)
208 char buf[STRING_MAX];
209 if (expected == actual) return;
210 sprintf(buf, "expected <%d> but was <%d>", expected, actual);
211 CuFail_Line(tc, file, line, message, buf);
214 void CuAssertLongIntEquals_LineMsg(CuTest *tc, const char *file, int line, const char *message,
215 long int expected, long int actual)
217 char buf[STRING_MAX];
218 if (expected == actual) return;
219 sprintf(buf, "expected <%ld> but was <%ld>", expected, actual);
220 CuFail_Line(tc, file, line, message, buf);
223 void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
224 double expected, double actual, double delta)
226 char buf[STRING_MAX];
227 if (fabs(expected - actual) <= delta) return;
228 sprintf(buf, "expected <%f> but was <%f>", expected, actual);
230 CuFail_Line(tc, file, line, message, buf);
233 void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
234 const void* expected, const void* actual)
236 char buf[STRING_MAX];
237 if (expected == actual) return;
238 sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual);
239 CuFail_Line(tc, file, line, message, buf);
243 /*-------------------------------------------------------------------------*
245 *-------------------------------------------------------------------------*/
247 void CuSuiteInit(CuSuite* testSuite)
249 testSuite->count = 0;
250 testSuite->failCount = 0;
251 memset(testSuite->list, 0, sizeof(testSuite->list));
254 CuSuite* CuSuiteNew(void)
256 CuSuite* testSuite = CU_ALLOC(CuSuite);
257 CuSuiteInit(testSuite);
261 void CuSuiteDelete(CuSuite *testSuite)
264 for (n=0; n < MAX_TEST_CASES; n++)
266 if (testSuite->list[n])
268 CuTestDelete(testSuite->list[n]);
275 void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase)
277 assert(testSuite->count < MAX_TEST_CASES);
278 testSuite->list[testSuite->count] = testCase;
282 void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2)
285 for (i = 0 ; i < testSuite2->count ; ++i)
287 CuTest* testCase = testSuite2->list[i];
288 CuSuiteAdd(testSuite, testCase);
292 void CuSuiteRun(CuSuite* testSuite)
295 for (i = 0 ; i < testSuite->count ; ++i)
297 CuTest* testCase = testSuite->list[i];
299 if (testCase->failed) { testSuite->failCount += 1; }
303 void CuSuiteSummary(CuSuite* testSuite, CuString* summary)
306 for (i = 0 ; i < testSuite->count ; ++i)
308 CuTest* testCase = testSuite->list[i];
309 CuStringAppend(summary, testCase->failed ? "F" : ".");
311 CuStringAppend(summary, "\n\n");
314 void CuSuiteDetails(CuSuite* testSuite, CuString* details)
319 if (testSuite->failCount == 0)
321 int passCount = testSuite->count - testSuite->failCount;
322 const char* testWord = passCount == 1 ? "test" : "tests";
323 CuStringAppendFormat(details, "OK (%d %s)\n", passCount, testWord);
327 if (testSuite->failCount == 1)
328 CuStringAppend(details, "There was 1 failure:\n");
330 CuStringAppendFormat(details, "There were %d failures:\n", testSuite->failCount);
332 for (i = 0 ; i < testSuite->count ; ++i)
334 CuTest* testCase = testSuite->list[i];
335 if (testCase->failed)
338 CuStringAppendFormat(details, "%d) %s: %s\n",
339 failCount, testCase->name, testCase->message);
342 CuStringAppend(details, "\n!!!FAILURES!!!\n");
344 CuStringAppendFormat(details, "Runs: %d ", testSuite->count);
345 CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount);
346 CuStringAppendFormat(details, "Fails: %d\n", testSuite->failCount);