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