test.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
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 { /* begin the private 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     }; /* end the private namespace */ \
00095     int main(int argc, char *argv[]) { \
00096       return execute_tests( \
00097          argc <= 1 ? -1 : atoi(argv[1])); \
00098     }
00099 
00100 #endif
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3