C言語テストごっこ
#include <stdio.h> | |
#include "tester.h" | |
int test1(); | |
int test2(); | |
int test3(); | |
int main () | |
{ | |
test_start(); | |
TEST(test1, "Answer"); | |
TEST(test2, "1/2 in int is 0"); | |
TEST(test3, "Pythagorean theorem"); | |
test_end(); | |
} | |
int test1() | |
{ | |
return 42; | |
} | |
int test2() | |
{ | |
return (1/2 == 0); | |
} | |
int test3() | |
{ | |
return (3*3 + 4*4 == 5*5); | |
} |
// tester.h | |
// Test helper | |
#define ANSI_COLOR_RED "\x1b[31m" | |
#define ANSI_COLOR_GREEN "\x1b[32m" | |
#define ANSI_COLOR_RESET "\x1b[0m" | |
#define TEST(t,s) \ | |
if ((t)()) { \ | |
printf(ANSI_COLOR_GREEN "%s ok ... %s" ANSI_COLOR_RESET "\n", \ | |
#t, \ | |
(s)); \ | |
test_count++, \ | |
pass_count++; \ | |
} else { \ | |
printf(ANSI_COLOR_RED "%s FAILED!! ... %s" ANSI_COLOR_RESET "\n", \ | |
#t, \ | |
(s)); \ | |
test_count++; \ | |
printf( \ | |
ANSI_COLOR_RED " %s : line %d" ANSI_COLOR_RESET "\n", \ | |
__FILE__, \ | |
__LINE__ \ | |
); \ | |
} | |
static int test_count; | |
static int pass_count; | |
void test_start() | |
{ | |
test_count = 0; | |
pass_count = 0; | |
} | |
void test_end() | |
{ | |
if (pass_count == test_count) { | |
printf("\n"); | |
printf(ANSI_COLOR_GREEN "ALL TEST PASSED!\t%d/%d" ANSI_COLOR_RESET "\n\n", | |
pass_count, | |
test_count); | |
} else { | |
printf("\n"); | |
printf(ANSI_COLOR_RED "TEST FAILED!\t%d/%d" ANSI_COLOR_RESET "\n\n", | |
pass_count, | |
test_count); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment