1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2010, 2013 Oracle and/or its affiliates. All rights reserved.
*
* $Id$
*/
#include <assert.h>
#include <errno.h>
#include <setjmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "CuTest.h"
/*-------------------------------------------------------------------------*
* Functions added by Berkeley DB, that allow more control over which tests
* are run. They assume a populated TestSuite array called g_suites
* is available globally.
*-------------------------------------------------------------------------*/
extern TestSuite g_suites[];
int RunAllSuites(void)
{
int failCount, i;
CuString *output;
failCount = 0;
for(i = 0; strlen(g_suites[i].name) != 0; i++) {
printf("Running suite %s\n", g_suites[i].name);
output = CuStringNew();
failCount += g_suites[i].fn(output);
printf("%s\n", output->buffer);
CuStringDelete(output);
printf("Finished suite %s\n", g_suites[i].name);
}
return (failCount);
}
int RunSuite(const char * suite)
{
int failCount, i;
CuString *output;
failCount = 0;
for(i = 0; strlen(g_suites[i].name) != 0; i++)
if (strcmp(g_suites[i].name, suite) == 0) {
output = CuStringNew();
failCount = g_suites[i].fn(output);
printf("%s\n", output->buffer);
CuStringDelete(output);
break;
}
return (failCount);
}
int RunTest(const char * suite, const char *test)
{
fprintf(stderr, "TODO: Implement RunTest: %s:%s.\n", suite, test);
return (1);
}
/*-------------------------------------------------------------------------*
* CuStr
*-------------------------------------------------------------------------*/
char* CuStrAlloc(int size)
{
char* newStr = (char*) malloc( sizeof(char) * (size) );
return newStr;
}
char* CuStrCopy(const char* old)
{
int len = (int)strlen(old);
char* newStr = CuStrAlloc(len + 1);
if (newStr != NULL)
strcpy(newStr, old);
else
fprintf(stderr, "%s: malloc in CuStrCopy.\n", CU_FAIL_HEADER);
return newStr;
}
/*-------------------------------------------------------------------------*
* CuString
*-------------------------------------------------------------------------*/
void CuStringInit(CuString* str)
{
str->length = 0;
str->size = STRING_MAX;
str->buffer = (char*) malloc(sizeof(char) * str->size);
str->buffer[0] = '\0';
}
CuString* CuStringNew(void)
{
CuString* str = (CuString*) malloc(sizeof(CuString));
CuStringInit(str);
return str;
}
void CuStringDelete(CuString *str)
{
if (!str) return;
free(str->buffer);
free(str);
}
int CuStringResize(CuString* str, int newSize)
{
char *newStr;
newStr = (char*) realloc(str->buffer, sizeof(char) * newSize);
if (newStr != 0) {
str->size = newSize;
str->buffer = newStr;
return (0);
}
return (ENOMEM);
}
int CuStringAppend(CuString* str, const char* text, int dump)
{
int length;
if (text == NULL) {
text = "NULL";
}
length = (int)strlen(text);
if ((str->length + length + 1 >= str->size) &&
CuStringResize(str, str->length + length + 1 + STRING_INC) != 0) {
if (dump) {
fprintf(stderr, "%s:%s\n%s\n", CU_FAIL_HEADER,
"String append in test framework failed due to"
"malloc failure. Outputting appended text instead.",
text);
}
return (ENOMEM);
}
str->length += length;
strcat(str->buffer, text);
return (0);
}
int CuStringAppendChar(CuString* str, char ch, int dump)
{
char text[2];
text[0] = ch;
text[1] = '\0';
return (CuStringAppend(str, text, dump));
}
int CuStringAppendFormat(CuString* str, int dump, const char* format, ...)
{
va_list argp;
char buf[HUGE_STRING_LEN];
va_start(argp, format);
vsprintf(buf, format, argp);
va_end(argp);
return (CuStringAppend(str, buf, dump));
}
int CuStringInsert(CuString* str, const char* text, int pos, int dump)
{
int length = (int)strlen(text);
if (pos > str->length)
pos = str->length;
if ((str->length + length + 1 >= str->size) &&
CuStringResize(str, str->length + length + 1 + STRING_INC) != 0) {
if (dump) {
fprintf(stderr, "%s:%s\n%s\n", CU_FAIL_HEADER,
"String append in test framework failed due to"
"malloc failure. Outputting appended text instead.",
text);
}
return (ENOMEM);
}
memmove(str->buffer + pos + length, str->buffer + pos,
(str->length - pos) + 1);
str->length += length;
memcpy(str->buffer + pos, text, length);
return (0);
}
/*-------------------------------------------------------------------------*
* CuTest
*-------------------------------------------------------------------------*/
void CuTestInit(CuTest* t, const char* name, TestFunction function, TestSetupFunction setup, TestTeardownFunction teardown)
{
t->name = CuStrCopy(name);
t->failed = 0;
t->ran = 0;
t->message = NULL;
t->function = function;
t->jumpBuf = NULL;
t->TestSetup = setup;
t->TestTeardown = teardown;
}
CuTest* CuTestNew(const char* name, TestFunction function, TestSetupFunction setup, TestTeardownFunction teardown)
{
CuTest* tc = CU_ALLOC(CuTest);
if (tc != NULL)
CuTestInit(tc, name, function, setup, teardown);
else
fprintf(stderr, "%s: %s%s\n", CU_FAIL_HEADER,
"Error initializing test case: ", name);
return tc;
}
void CuTestDelete(CuTest *t)
{
if (!t) return;
free(t->name);
free(t);
}
void CuTestRun(CuTest* tc)
{
jmp_buf buf;
if (tc->TestSetup != NULL)
(tc->TestSetup)(tc);
tc->jumpBuf = &buf;
if (setjmp(buf) == 0)
{
tc->ran = 1;
(tc->function)(tc);
}
if (tc->TestTeardown != NULL)
(tc->TestTeardown)(tc);
tc->jumpBuf = 0;
}
static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string)
{
char buf[HUGE_STRING_LEN];
sprintf(buf, "%s:%d: ", file, line);
(void)CuStringInsert(string, buf, 0, 1);
if (tc == NULL)
{
/*
* Output the message now, it's come from overriding
* __db_assert.
* TODO: It'd be nice to somehow map this onto a CuTest, so the
* assert can be effectively trapped. Since Berkeley DB
* doesn't necessarily return error after an assert, so
* a test case can "pass" even after a __db_assert.
* Could trap this output, and map it back to the test
* case, but I want to be careful about allowing multi-
* threaded use at some stage.
*/
fprintf(stderr, "DB internal assert: %s\n", string->buffer);
} else {
tc->failed = 1;
tc->message = string->buffer;
if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
}
}
void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message)
{
CuString string;
CuStringInit(&string);
if (message2 != NULL)
{
CuStringAppend(&string, message2, 1);
CuStringAppend(&string, ": ", 1);
}
CuStringAppend(&string, message, 1);
CuFailInternal(tc, file, line, &string);
}
void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition)
{
if (condition) return;
CuFail_Line(tc, file, line, NULL, message);
}
void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
const char* expected, const char* actual)
{
CuString string;
if ((expected == NULL && actual == NULL) ||
(expected != NULL && actual != NULL &&
strcmp(expected, actual) == 0))
{
return;
}
CuStringInit(&string);
if (message != NULL)
{
CuStringAppend(&string, message, 1);
CuStringAppend(&string, ": ", 1);
}
CuStringAppend(&string, "expected <", 1);
CuStringAppend(&string, expected, 1);
CuStringAppend(&string, "> but was <", 1);
CuStringAppend(&string, actual, 1);
CuStringAppend(&string, ">", 1);
CuFailInternal(tc, file, line, &string);
}
void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
int expected, int actual)
{
char buf[STRING_MAX];
if (expected == actual) return;
sprintf(buf, "expected <%d> but was <%d>", expected, actual);
CuFail_Line(tc, file, line, message, buf);
}
void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
double expected, double actual, double delta)
{
char buf[STRING_MAX];
if (fabs(expected - actual) <= delta) return;
sprintf(buf, "expected <%f> but was <%f>", expected, actual);
CuFail_Line(tc, file, line, message, buf);
}
void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
void* expected, void* actual)
{
char buf[STRING_MAX];
if (expected == actual) return;
sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual);
CuFail_Line(tc, file, line, message, buf);
}
/*-------------------------------------------------------------------------*
* CuSuite
*-------------------------------------------------------------------------*/
void CuSuiteInit(CuSuite* testSuite, const char *name,
SuiteSetupFunction setup, SuiteTeardownFunction teardown)
{
testSuite->name = name;
testSuite->count = 0;
testSuite->failCount = 0;
testSuite->SuiteSetup = setup;
testSuite->SuiteTeardown = teardown;
testSuite->context = NULL;
memset(testSuite->list, 0, sizeof(testSuite->list));
}
CuSuite* CuSuiteNew(const char *name,
SuiteSetupFunction setup, SuiteTeardownFunction teardown)
{
CuSuite* testSuite = CU_ALLOC(CuSuite);
if (testSuite != NULL)
CuSuiteInit(testSuite, name, setup, teardown);
else
fprintf(stderr, "%s: %s%s\n", CU_FAIL_HEADER,
"Error initializing test suite: ", name);
return testSuite;
}
void CuSuiteDelete(CuSuite *testSuite)
{
unsigned int n;
for (n=0; n < MAX_TEST_CASES; n++)
{
if (testSuite->list[n])
{
CuTestDelete(testSuite->list[n]);
}
}
free(testSuite);
}
void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase)
{
assert(testSuite->count < MAX_TEST_CASES);
testSuite->list[testSuite->count] = testCase;
testSuite->count++;
testCase->suite = testSuite;
}
void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2)
{
int i;
for (i = 0 ; i < testSuite2->count ; ++i)
{
CuTest* testCase = testSuite2->list[i];
CuSuiteAdd(testSuite, testCase);
}
}
void CuSuiteRun(CuSuite* testSuite)
{
int i;
for (i = 0 ; i < testSuite->count ; ++i)
{
CuTest* testCase = testSuite->list[i];
if (testSuite->SuiteSetup != NULL)
(testSuite->SuiteSetup)(testSuite);
CuTestRun(testCase);
if (testSuite->SuiteTeardown != NULL)
(testSuite->SuiteTeardown)(testSuite);
if (testCase->failed) { testSuite->failCount += 1; }
}
}
void CuSuiteSummary(CuSuite* testSuite, CuString* summary)
{
int i;
for (i = 0 ; i < testSuite->count ; ++i)
{
CuTest* testCase = testSuite->list[i];
CuStringAppend(summary, testCase->failed ? "F" : ".", 1);
}
CuStringAppend(summary, "\n\n", 1);
}
void CuSuiteDetails(CuSuite* testSuite, CuString* details)
{
int i;
int failCount = 0;
if (testSuite->failCount == 0)
{
int passCount = testSuite->count - testSuite->failCount;
const char* testWord = passCount == 1 ? "test" : "tests";
CuStringAppendFormat(details, 1, "OK (%d %s)\n", passCount, testWord);
}
else
{
if (testSuite->failCount == 1)
CuStringAppend(details, "There was 1 failure:\n", 1);
else
CuStringAppendFormat(details, 1, "There were %d failures:\n", testSuite->failCount);
for (i = 0 ; i < testSuite->count ; ++i)
{
CuTest* testCase = testSuite->list[i];
if (testCase->failed)
{
failCount++;
CuStringAppendFormat(details, 1, "%d) %s: %s\n",
failCount, testCase->name, testCase->message);
}
}
CuStringAppend(details, "\n!!!FAILURES!!!\n", 1);
CuStringAppendFormat(details, 1, "Runs: %d ", testSuite->count);
CuStringAppendFormat(details, 1, "Passes: %d ", testSuite->count - testSuite->failCount);
CuStringAppendFormat(details, 1, "Fails: %d\n", testSuite->failCount);
}
}
|