src/util/test.h (raw)
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 |
#ifndef TEST_H #define TEST_H #include "bool.h" #include "print.h" typedef void Test(Status *test_result_); void register_test_(Test *test, const char *name); #define TEST(name) \ void test_##name(Status *test_result_); \ __attribute__((constructor)) void test_register_##name() \ { \ register_test_(test_##name, #name); \ } \ void test_##name(Status *test_result_) void run_all_tests(bool verbose); #define FAIL_TEST_ \ *test_result_ = FAILURE; \ return; #define ASSERT(value) \ if (!(value)) { \ FAIL_TEST_ \ } #define ASSERT_EQUAL(a, b) \ if (!(a == b)) { \ printf("%s:%d: Assertion failed: %s == %s: ", __FILE__, __LINE__, #a, #b); \ print(a); \ printf(" != "); \ print(b); \ FAIL_TEST_ \ } #define ASSERT_DIFFERENT(a, b) \ if (a == b) { \ printf("%s:%d: Assertion failed: %s != %s: ", __FILE__, __LINE__, #a, #b); \ print(a); \ printf(" == "); \ print(b); \ FAIL_TEST_ \ } #define ASSERT_STR_EQUAL(a, b) \ if (strcmp(a, b) != 0) { \ printf("%s:%d: Assertion failed: %s == %s: ", __FILE__, __LINE__, #a, #b); \ print(a); \ printf(" != "); \ print(b); \ FAIL_TEST_ \ } #define ASSERT_STR_DIFFERENT(a, b) \ if (strcmp(a, b) == 0) { \ printf("%s:%d: Assertion failed: %s != %s: ", __FILE__, __LINE__, #a, #b); \ print(a); \ printf(" == "); \ print(b); \ FAIL_TEST_ \ } #define ASSERT_TRUE(value) ASSERT_EQUAL(value, 1) #define ASSERT_FALSE(value) ASSERT_EQUAL(value, 0) #define ASSERT_NULL(value) ASSERT_EQUAL(value, NULL) #define ASSERT_NON_NULL(value) ASSERT_DIFFERENT(value, NULL) #endif |