Skip to content

Instantly share code, notes, and snippets.

@safchain
Created January 6, 2015 13:09
Show Gist options
  • Save safchain/313445c3c282d5a82756 to your computer and use it in GitHub Desktop.
Save safchain/313445c3c282d5a82756 to your computer and use it in GitHub Desktop.
One file naive UT Framework
#define TEST_F(X) void X(int *ret); const char *wrapper_##X(int *ret) {\
const char *test_name = get_test_name(__FUNCTION__); \
X(ret);\
return test_name; \
} \
void X(int *ret)
#define TEST(X) wrapper_##X
typedef const char *(*TEST_SUITE)(int *);
typedef const char *(*TEST)(int *);
#define ASSERT_EQUALS(X,Y) if (X != Y) { *ret = 0; return; }
const char *get_test_name(const char *name) {
return name + 8;
}
static void print_tests_number(TEST *test_suite) {
TEST *test = test_suite;
int total = 1;
while(*test != NULL) {
total++;
test++;
}
printf("1..%d\n", total);
}
static int RUN_TESTS(TEST *test_suite) {
TEST *test = test_suite;
int ret, result = 1, total = 1;
const char *test_name;
print_tests_number(test_suite);
while(*test != NULL) {
ret = 1;
test_name = (*test)(&ret);
if (ret) {
fprintf(stderr, "ok %d %s\n", total, test_name);
} else {
fprintf(stderr, "not ok %d %s\n", total, test_name);
result = 0;
}
total++;
test++;
}
return result;
}
/* below unit tests */
TEST_F(test_ko) {
ASSERT_EQUALS(0,1);
}
TEST_F(test_ok) {
ASSERT_EQUALS(2, 2);
}
void main(void) {
TEST_SUITE test_suite[] = {
TEST(test_ok),
TEST(test_ko),
NULL
};
RUN_TESTS(test_suite);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment