1 #ifndef TEST_H 2 #define TEST_H 3 4 #include "bool.h" 5 #include "print.h" 6 7 typedef void Test(Status *test_result_); 8 9 void register_test_(Test *test, const char *name); 10 11 #define TEST(name) \ 12 void test_##name(Status *test_result_); \ 13 __attribute__((constructor)) void test_register_##name() \ 14 { \ 15 register_test_(test_##name, #name); \ 16 } \ 17 void test_##name(Status *test_result_) 18 19 void run_all_tests(bool verbose); 20 21 #define FAIL_TEST_ \ 22 *test_result_ = FAILURE; \ 23 return; 24 25 #define ASSERT(value) \ 26 if (!(value)) { \ 27 FAIL_TEST_ \ 28 } 29 30 #define ASSERT_EQUAL(a, b) \ 31 if (!(a == b)) { \ 32 printf("%s:%d: Assertion failed: %s == %s: ", __FILE__, __LINE__, #a, #b); \ 33 print(a); \ 34 printf(" != "); \ 35 print(b); \ 36 FAIL_TEST_ \ 37 } 38 39 #define ASSERT_DIFFERENT(a, b) \ 40 if (a == b) { \ 41 printf("%s:%d: Assertion failed: %s != %s: ", __FILE__, __LINE__, #a, #b); \ 42 print(a); \ 43 printf(" == "); \ 44 print(b); \ 45 FAIL_TEST_ \ 46 } 47 48 49 #define ASSERT_STR_EQUAL(a, b) \ 50 if (strcmp(a, b) != 0) { \ 51 printf("%s:%d: Assertion failed: %s == %s: ", __FILE__, __LINE__, #a, #b); \ 52 print(a); \ 53 printf(" != "); \ 54 print(b); \ 55 FAIL_TEST_ \ 56 } 57 58 #define ASSERT_STR_DIFFERENT(a, b) \ 59 if (strcmp(a, b) == 0) { \ 60 printf("%s:%d: Assertion failed: %s != %s: ", __FILE__, __LINE__, #a, #b); \ 61 print(a); \ 62 printf(" == "); \ 63 print(b); \ 64 FAIL_TEST_ \ 65 } 66 67 #define ASSERT_TRUE(value) ASSERT_EQUAL(value, 1) 68 #define ASSERT_FALSE(value) ASSERT_EQUAL(value, 0) 69 #define ASSERT_NULL(value) ASSERT_EQUAL(value, NULL) 70 #define ASSERT_NON_NULL(value) ASSERT_DIFFERENT(value, NULL) 71 72 #endif 73