unittest.h

Go to the documentation of this file.
00001 00004 /* 00005 * Copyright © 2000, 2001 Paul Hollingsworth 00006 * 00007 * This material is provided "as is", with absolutely no warranty 00008 * expressed or implied. Any use is at your own risk. Permission to 00009 * use or copy this software for any purpose is hereby granted without 00010 * fee, provided the above notices are retained on all copies. 00011 * Permission to modify the code and to distribute modified code is 00012 * granted, provided the above notices are retained, and a notice that 00013 * the code was modified is included with the above copyright notice. 00014 * 00015 * This header is part of comet. 00016 * http://www.lambdasoft.dk/comet 00017 */ 00018 00019 #ifndef COMET_UNITTEST_H 00020 #define COMET_UNITTEST_H 00021 00022 #include <exception> 00023 #include <iostream> 00024 00025 namespace comet { 00027 template<int n> 00028 struct test 00029 { 00030 void run() {} 00031 }; 00032 00033 int current_test = 0; 00034 00036 00038 template<int n> 00039 struct tester 00040 { 00041 inline static void runtests() 00042 { 00043 tester<n-1>::runtests(); 00044 current_test = n; 00045 test<n>().run(); 00046 } 00047 }; 00048 00049 template<> 00050 struct tester<0> 00051 { 00052 static void runtests() {} 00053 }; 00054 } // namespace comet 00055 00057 template<typename T> 00058 struct main_t 00059 { 00061 static int call(int argc, const char * const argv[]) 00062 { 00063 using std::cout; 00064 using std::cerr; 00065 using std::endl; 00066 try { 00067 comet::tester<64>::runtests(); 00068 } catch (const std::exception &e) { 00069 std::cerr << "Test #" << comet::current_test << " failed - error message: <" << e.what() << ">" << endl; 00070 return 1; 00071 } 00072 catch(...) { 00073 std::cerr << "Test #" << comet::current_test << " failed with an unrecognized exception" << endl; 00074 return 2; 00075 } 00076 std::cout << "Ran all tests successfully" <<endl; 00077 return 0; 00078 } 00079 }; 00080 #define COMET_TEST_MAIN \ 00081 int main(int argc, const char * const argv[]) \ 00082 {\ 00083 return main_t<void>::call(argc,argv);\ 00084 } 00085 00086 #endif