Updated to newer version of ptest
This commit is contained in:
164
tests/ptest.c
164
tests/ptest.c
@@ -26,31 +26,46 @@ static int suite_passing = 0;
|
|||||||
/* Colors */
|
/* Colors */
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
BLACK = 0x0,
|
BLACK = 0,
|
||||||
BLUE = 0x1,
|
BLUE = 1,
|
||||||
GREEN = 0x2,
|
GREEN = 2,
|
||||||
AQUA = 0x3,
|
AQUA = 3,
|
||||||
RED = 0x4,
|
RED = 4,
|
||||||
PURPLE = 0x5,
|
PURPLE = 5,
|
||||||
YELLOW = 0x6,
|
YELLOW = 6,
|
||||||
WHITE = 0x7,
|
WHITE = 7,
|
||||||
GRAY = 0x8,
|
GRAY = 8,
|
||||||
LIGHT_BLUE = 0x9,
|
|
||||||
LIGHT_GREEN = 0xA,
|
LIGHT_BLUE = 9,
|
||||||
LIGHT_AQUA = 0xB,
|
LIGHT_GREEN = 10,
|
||||||
LIGHT_RED = 0xC,
|
LIGHT_AQUA = 11,
|
||||||
LIGHT_PURPLE = 0xD,
|
LIGHT_RED = 12,
|
||||||
LIGHT_YELLOW = 0xE,
|
LIGHT_PURPLE = 13,
|
||||||
LIGHT_WHITE = 0xF
|
LIGHT_YELLOW = 14,
|
||||||
|
LIGHT_WHITE = 15,
|
||||||
|
|
||||||
|
DEFAULT = 16,
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
|
static WORD defaults;
|
||||||
|
static int defaults_loaded = 0;
|
||||||
|
|
||||||
static void pt_color(int color) {
|
static void pt_color(int color) {
|
||||||
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
||||||
SetConsoleTextAttribute(hCon, color);
|
HANDLE cnsl = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
|
||||||
|
if (!defaults_loaded) {
|
||||||
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
||||||
|
GetConsoleScreenBufferInfo(cnsl, &info);
|
||||||
|
defaults = info.wAttributes;
|
||||||
|
defaults_loaded = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
SetConsoleTextAttribute(cnsl, color == DEFAULT ? defaults : color);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
@@ -71,13 +86,12 @@ static const char* colors[] = {
|
|||||||
"\x1B[31m",
|
"\x1B[31m",
|
||||||
"\x1B[35m",
|
"\x1B[35m",
|
||||||
"\x1B[33m",
|
"\x1B[33m",
|
||||||
"\x1B[37m"
|
"\x1B[37m",
|
||||||
|
"\x1B[39m",
|
||||||
};
|
};
|
||||||
|
|
||||||
static void pt_color(int color) {
|
static void pt_color(int color) {
|
||||||
|
|
||||||
printf("%s", colors[color]);
|
printf("%s", colors[color]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -92,16 +106,18 @@ static char assert_err[MAX_ERROR];
|
|||||||
static char assert_err_buff[MAX_ERROR];
|
static char assert_err_buff[MAX_ERROR];
|
||||||
static int assert_err_num = 0;
|
static int assert_err_num = 0;
|
||||||
|
|
||||||
void pt_assert_run(int result, const char* expr, const char* func, const char* file, int line) {
|
void pt_assert_run(
|
||||||
|
int result, const char* expr, const char* func, const char* file, int line) {
|
||||||
|
|
||||||
(void) func;
|
|
||||||
num_asserts++;
|
num_asserts++;
|
||||||
test_passing = test_passing && result;
|
test_passing = test_passing && result;
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
num_assert_passes++;
|
num_assert_passes++;
|
||||||
} else {
|
} else {
|
||||||
sprintf(assert_err_buff, " %i. Assert [ %s ] (%s:%i)\n", assert_err_num+1, expr, file, line );
|
sprintf(assert_err_buff,
|
||||||
|
" %i. Assert [ %s ] (%s:%i)\n",
|
||||||
|
assert_err_num+1, expr, file, line );
|
||||||
strcat(assert_err, assert_err_buff);
|
strcat(assert_err, assert_err_buff);
|
||||||
assert_err_num++;
|
assert_err_num++;
|
||||||
num_assert_fails++;
|
num_assert_fails++;
|
||||||
@@ -114,18 +130,25 @@ static void ptest_signal(int sig) {
|
|||||||
test_passing = 0;
|
test_passing = 0;
|
||||||
|
|
||||||
switch( sig ) {
|
switch( sig ) {
|
||||||
case SIGFPE: sprintf(assert_err_buff, " %i. Division by Zero\n", assert_err_num+1); break;
|
case SIGFPE: sprintf(assert_err_buff,
|
||||||
case SIGILL: sprintf(assert_err_buff, " %i. Illegal Instruction\n", assert_err_num+1); break;
|
" %i. Division by Zero\n", assert_err_num+1);
|
||||||
case SIGSEGV: sprintf(assert_err_buff, " %i. Segmentation Fault\n", assert_err_num+1); break;
|
break;
|
||||||
default: break;
|
case SIGILL: sprintf(assert_err_buff,
|
||||||
|
" %i. Illegal Instruction\n", assert_err_num+1);
|
||||||
|
break;
|
||||||
|
case SIGSEGV: sprintf(assert_err_buff,
|
||||||
|
" %i. Segmentation Fault\n", assert_err_num+1);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_err_num++;
|
assert_err_num++;
|
||||||
strcat(assert_err, assert_err_buff);
|
strcat(assert_err, assert_err_buff);
|
||||||
|
|
||||||
pt_color(WHITE); pt_color(RED); printf("Failed! \n\n%s\n", assert_err); pt_color(WHITE);
|
pt_color(RED);
|
||||||
|
printf("Failed! \n\n%s\n", assert_err);
|
||||||
|
pt_color(DEFAULT);
|
||||||
|
|
||||||
printf(" | Stopping Execution.\n");
|
puts(" | Stopping Execution.");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|
||||||
@@ -136,7 +159,7 @@ static void ptest_signal(int sig) {
|
|||||||
static void pt_title_case(char* output, const char* input) {
|
static void pt_title_case(char* output, const char* input) {
|
||||||
|
|
||||||
int space = 1;
|
int space = 1;
|
||||||
size_t i;
|
unsigned int i;
|
||||||
|
|
||||||
strcpy(output, input);
|
strcpy(output, input);
|
||||||
|
|
||||||
@@ -177,15 +200,18 @@ void pt_add_test(void (*func)(void), const char* name, const char* suite) {
|
|||||||
test_t test;
|
test_t test;
|
||||||
|
|
||||||
if (num_tests == MAX_TESTS) {
|
if (num_tests == MAX_TESTS) {
|
||||||
printf("ERROR: Exceeded maximum test count of %i!\n", MAX_TESTS); abort();
|
printf("ERROR: Exceeded maximum test count of %i!\n",
|
||||||
|
MAX_TESTS); abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(name) >= MAX_NAME) {
|
if (strlen(name) >= MAX_NAME) {
|
||||||
printf("ERROR: Test name '%s' too long (Maximum is %i characters)\n", name, MAX_NAME); abort();
|
printf("ERROR: Test name '%s' too long (Maximum is %i characters)\n",
|
||||||
|
name, MAX_NAME); abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(suite) >= MAX_NAME) {
|
if (strlen(suite) >= MAX_NAME) {
|
||||||
printf("ERROR: Test suite '%s' too long (Maximum is %i characters)\n", suite, MAX_NAME); abort();
|
printf("ERROR: Test suite '%s' too long (Maximum is %i characters)\n",
|
||||||
|
suite, MAX_NAME); abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
test.func = func;
|
test.func = func;
|
||||||
@@ -194,7 +220,6 @@ void pt_add_test(void (*func)(void), const char* name, const char* suite) {
|
|||||||
|
|
||||||
tests[num_tests] = test;
|
tests[num_tests] = test;
|
||||||
num_tests++;
|
num_tests++;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Suites */
|
/* Suites */
|
||||||
@@ -215,17 +240,18 @@ static char current_suite[MAX_NAME];
|
|||||||
|
|
||||||
int pt_run(void) {
|
int pt_run(void) {
|
||||||
|
|
||||||
int i;
|
unsigned int i;
|
||||||
double total;
|
double total;
|
||||||
|
test_t test;
|
||||||
|
|
||||||
printf(" \n");
|
puts("");
|
||||||
printf(" +-------------------------------------------+\n");
|
puts(" +-------------------------------------------+");
|
||||||
printf(" | ptest MicroTesting Magic for C |\n");
|
puts(" | ptest MicroTesting Magic for C |");
|
||||||
printf(" | |\n");
|
puts(" | |");
|
||||||
printf(" | http://github.com/orangeduck/ptest |\n");
|
puts(" | http://github.com/orangeduck/ptest |");
|
||||||
printf(" | |\n");
|
puts(" | |");
|
||||||
printf(" | Daniel Holden (contact@theorangeduck.com) |\n");
|
puts(" | Daniel Holden (contact@theorangeduck.com) |");
|
||||||
printf(" +-------------------------------------------+\n");
|
puts(" +-------------------------------------------+");
|
||||||
|
|
||||||
signal(SIGFPE, ptest_signal);
|
signal(SIGFPE, ptest_signal);
|
||||||
signal(SIGILL, ptest_signal);
|
signal(SIGILL, ptest_signal);
|
||||||
@@ -236,7 +262,7 @@ int pt_run(void) {
|
|||||||
|
|
||||||
for(i = 0; i < num_tests; i++) {
|
for(i = 0; i < num_tests; i++) {
|
||||||
|
|
||||||
test_t test = tests[i];
|
test = tests[i];
|
||||||
|
|
||||||
/* Check for transition to a new suite */
|
/* Check for transition to a new suite */
|
||||||
if (strcmp(test.suite, current_suite)) {
|
if (strcmp(test.suite, current_suite)) {
|
||||||
@@ -262,6 +288,7 @@ int pt_run(void) {
|
|||||||
strcpy(assert_err_buff, "");
|
strcpy(assert_err_buff, "");
|
||||||
assert_err_num = 0;
|
assert_err_num = 0;
|
||||||
printf(" | %s ... ", test.name);
|
printf(" | %s ... ", test.name);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
test.func();
|
test.func();
|
||||||
|
|
||||||
@@ -269,10 +296,14 @@ int pt_run(void) {
|
|||||||
|
|
||||||
if (test_passing) {
|
if (test_passing) {
|
||||||
num_tests_passes++;
|
num_tests_passes++;
|
||||||
pt_color(GREEN); printf("Passed! \n"); pt_color(WHITE);
|
pt_color(GREEN);
|
||||||
|
puts("Passed!");
|
||||||
|
pt_color(DEFAULT);
|
||||||
} else {
|
} else {
|
||||||
num_tests_fails++;
|
num_tests_fails++;
|
||||||
pt_color(RED); printf("Failed! \n\n%s\n", assert_err); pt_color(WHITE);
|
pt_color(RED);
|
||||||
|
printf("Failed! \n\n%s\n", assert_err);
|
||||||
|
pt_color(DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -285,28 +316,37 @@ int pt_run(void) {
|
|||||||
|
|
||||||
end = clock();
|
end = clock();
|
||||||
|
|
||||||
printf(" \n");
|
puts("");
|
||||||
printf(" +---------------------------------------------------+\n");
|
puts(" +---------------------------------------------------+");
|
||||||
printf(" | Summary |\n");
|
puts(" | Summary |");
|
||||||
printf(" +---------++------------+-------------+-------------+\n");
|
puts(" +---------++------------+-------------+-------------+");
|
||||||
|
|
||||||
printf(" | Suites ||");
|
printf(" | Suites ||");
|
||||||
pt_color(YELLOW); printf(" Total %4d ", num_suites); pt_color(WHITE); printf("|");
|
pt_color(YELLOW); printf(" Total %4d ", num_suites);
|
||||||
pt_color(GREEN); printf(" Passed %4d ", num_suites_passes); pt_color(WHITE); printf("|");
|
pt_color(DEFAULT); putchar('|');
|
||||||
pt_color(RED); printf(" Failed %4d ", num_suites_fails); pt_color(WHITE); printf("|\n");
|
pt_color(GREEN); printf(" Passed %4d ", num_suites_passes);
|
||||||
|
pt_color(DEFAULT); putchar('|');
|
||||||
|
pt_color(RED); printf(" Failed %4d ", num_suites_fails);
|
||||||
|
pt_color(DEFAULT); puts("|");
|
||||||
|
|
||||||
printf(" | Tests ||");
|
printf(" | Tests ||");
|
||||||
pt_color(YELLOW); printf(" Total %4d ", num_tests); pt_color(WHITE); printf("|");
|
pt_color(YELLOW); printf(" Total %4d ", num_tests);
|
||||||
pt_color(GREEN); printf(" Passed %4d ", num_tests_passes); pt_color(WHITE); printf("|");
|
pt_color(DEFAULT); putchar('|');
|
||||||
pt_color(RED); printf(" Failed %4d ", num_tests_fails); pt_color(WHITE); printf("|\n");
|
pt_color(GREEN); printf(" Passed %4d ", num_tests_passes);
|
||||||
|
pt_color(DEFAULT); putchar('|');
|
||||||
|
pt_color(RED); printf(" Failed %4d ", num_tests_fails);
|
||||||
|
pt_color(DEFAULT); puts("|");
|
||||||
|
|
||||||
printf(" | Asserts ||");
|
printf(" | Asserts ||");
|
||||||
pt_color(YELLOW); printf(" Total %4d ", num_asserts); pt_color(WHITE); printf("|");
|
pt_color(YELLOW); printf(" Total %4d ", num_asserts);
|
||||||
pt_color(GREEN); printf(" Passed %4d ", num_assert_passes); pt_color(WHITE); printf("|");
|
pt_color(DEFAULT); putchar('|');
|
||||||
pt_color(RED); printf(" Failed %4d ", num_assert_fails); pt_color(WHITE); printf("|\n");
|
pt_color(GREEN); printf(" Passed %4d ", num_assert_passes);
|
||||||
|
pt_color(DEFAULT); putchar('|');
|
||||||
|
pt_color(RED); printf(" Failed %4d ", num_assert_fails);
|
||||||
|
pt_color(DEFAULT); puts("|");
|
||||||
|
|
||||||
printf(" +---------++------------+-------------+-------------+\n");
|
puts(" +---------++------------+-------------+-------------+");
|
||||||
printf(" \n");
|
puts("");
|
||||||
|
|
||||||
total = (double)(end - start) / CLOCKS_PER_SEC;
|
total = (double)(end - start) / CLOCKS_PER_SEC;
|
||||||
|
|
||||||
|
@@ -19,4 +19,3 @@ void pt_add_suite(void (*func)(void));
|
|||||||
int pt_run(void);
|
int pt_run(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user