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 CuStringResize(CuString* str, int newSize)
52 str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize);
56 void CuStringAppend(CuString* str, const char* text)
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);
71 void CuStringAppendChar(CuString* str, char ch)
76 CuStringAppend(str, text);
79 void CuStringAppendFormat(CuString* str, const char* format, ...)
82 char buf[HUGE_STRING_LEN];
83 va_start(argp, format);
84 vsprintf(buf, format, argp);
86 CuStringAppend(str, buf);
89 void CuStringInsert(CuString* str, const char* text, int pos)
91 int length = strlen(text);
92 if (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);
101 /*-------------------------------------------------------------------------*
103 *-------------------------------------------------------------------------*/
105 void CuTestInit(CuTest* t, const char* name, TestFunction function)
107 t->name = CuStrCopy(name);
111 t->function = function;
115 CuTest* CuTestNew(const char* name, TestFunction function)
117 CuTest* tc = CU_ALLOC(CuTest);
118 CuTestInit(tc, name, function);
122 void CuTestRun(CuTest* tc)
126 if (setjmp(buf) == 0)
134 static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string)
136 char buf[HUGE_STRING_LEN];
138 sprintf(buf, "%s:%d: ", file, line);
139 CuStringInsert(string, buf, 0);
142 tc->message = string->buffer;
144 extern void lwt_dump(const char *);
147 if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
150 void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message)
154 CuStringInit(&string);
155 if (message2 != NULL)
157 CuStringAppend(&string, message2);
158 CuStringAppend(&string, ": ");
160 CuStringAppend(&string, message);
161 CuFailInternal(tc, file, line, &string);
164 void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition)
166 if (condition) return;
167 CuFail_Line(tc, file, line, NULL, message);
170 void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
171 const char* expected, const char* actual)
174 if ((expected == NULL && actual == NULL) ||
175 (expected != NULL && actual != NULL &&
176 strcmp(expected, actual) == 0))
181 CuStringInit(&string);
184 CuStringAppend(&string, message);
185 CuStringAppend(&string, ": ");
187 CuStringAppend(&string, "expected <");
188 CuStringAppend(&string, expected);
189 CuStringAppend(&string, "> but was <");
190 CuStringAppend(&string, actual);
191 CuStringAppend(&string, ">");
192 CuFailInternal(tc, file, line, &string);
195 void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
196 int expected, int actual)
198 char buf[STRING_MAX];
199 if (expected == actual) return;
200 sprintf(buf, "expected <%d> but was <%d>", expected, actual);
201 CuFail_Line(tc, file, line, message, buf);
204 void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
205 double expected, double actual, double delta)
207 char buf[STRING_MAX];
208 if (fabs(expected - actual) <= delta) return;
209 sprintf(buf, "expected <%lf> but was <%lf>", expected, actual);
210 CuFail_Line(tc, file, line, message, buf);
213 void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
214 void* expected, void* actual)
216 char buf[STRING_MAX];
217 if (expected == actual) return;
218 sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual);
219 CuFail_Line(tc, file, line, message, buf);
223 /*-------------------------------------------------------------------------*
225 *-------------------------------------------------------------------------*/
227 void CuSuiteInit(CuSuite* testSuite)
229 testSuite->count = 0;
230 testSuite->failCount = 0;
233 CuSuite* CuSuiteNew(void)
235 CuSuite* testSuite = CU_ALLOC(CuSuite);
236 CuSuiteInit(testSuite);
240 void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase)
242 assert(testSuite->count < MAX_TEST_CASES);
243 testSuite->list[testSuite->count] = testCase;
247 void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2)
250 for (i = 0 ; i < testSuite2->count ; ++i)
252 CuTest* testCase = testSuite2->list[i];
253 CuSuiteAdd(testSuite, testCase);
257 void CuSuiteRun(CuSuite* testSuite)
260 for (i = 0 ; i < testSuite->count ; ++i)
262 CuTest* testCase = testSuite->list[i];
264 if (testCase->failed) { testSuite->failCount += 1; }
268 void CuSuiteSummary(CuSuite* testSuite, CuString* summary)
271 for (i = 0 ; i < testSuite->count ; ++i)
273 CuTest* testCase = testSuite->list[i];
274 CuStringAppend(summary, testCase->failed ? "F" : ".");
276 CuStringAppend(summary, "\n\n");
279 void CuSuiteDetails(CuSuite* testSuite, CuString* details)
284 if (testSuite->failCount == 0)
286 int passCount = testSuite->count - testSuite->failCount;
287 const char* testWord = passCount == 1 ? "test" : "tests";
288 CuStringAppendFormat(details, "OK (%d %s)\n", passCount, testWord);
292 if (testSuite->failCount == 1)
293 CuStringAppend(details, "There was 1 failure:\n");
295 CuStringAppendFormat(details, "There were %d failures:\n", testSuite->failCount);
297 for (i = 0 ; i < testSuite->count ; ++i)
299 CuTest* testCase = testSuite->list[i];
300 if (testCase->failed)
303 CuStringAppendFormat(details, "%d) %s: %s\n",
304 failCount, testCase->name, testCase->message);
307 CuStringAppend(details, "\n!!!FAILURES!!!\n");
309 CuStringAppendFormat(details, "Runs: %d ", testSuite->count);
310 CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount);
311 CuStringAppendFormat(details, "Fails: %d\n", testSuite->failCount);