test.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00041 #ifndef BASE_TEST_H
00042 #define BASE_TEST_H
00043
00044 #include "fastlib/base/common.h"
00045
00051 #define TEST_ASSERT(x) \
00052 (likely(x) ? NOP : FATAL("Assertion failed: %s", #x))
00053
00054 #define TEST_DOUBLE_EXACT(a, b) \
00055 if (unlikely((a) != (b))) \
00056 FATAL("%.10e (%s) != %.10e (%s)", (double)(a), #a, (double)(b), #b); else
00057
00058 #define TEST_DOUBLE_APPROX(a, b, absolute_eps) \
00059 if (unlikely(fabs((a) - (b)) > absolute_eps)) \
00060 FATAL("%.10e (%s) !~= %.10e (%s)", (double)(a), #a, (double)(b), #b); else
00061
00067 #define TEST_SUITE_BEGIN(suite_name) \
00068 namespace {
00069
00071 typedef void (*test__void_func)();
00072
00076 #define TEST_SUITE_END(suite_name, functions ...) \
00077 int execute_tests(int which_test) { \
00078 test__void_func tests[] = { functions }; \
00079 int n_tests = sizeof(tests) / sizeof(tests[0]); \
00080 \
00081 if (which_test < 0) { \
00082 for (int i = 0; i < n_tests; i++) { \
00083 tests[i](); \
00084 } \
00085 } else { \
00086 if (which_test >= n_tests) { \
00087 fprintf(stderr, "NO MORE TESTS!\n"); \
00088 return 3; \
00089 } \
00090 } \
00091 fprintf(stderr, "ALL TESTS PASSED!\n"); \
00092 return 0; \
00093 } \
00094 }; \
00095 int main(int argc, char *argv[]) { \
00096 return execute_tests( \
00097 argc <= 1 ? -1 : atoi(argv[1])); \
00098 }
00099
00100 #endif