• Main Page
  • Classes
  • Files
  • File List

src/testing.h

00001 #ifndef TESTING_H
00002 #define TESTING_H
00003 
00004 #include <iostream>
00005 #include <fstream>
00006 #include <list>
00007 #include <vector>
00008 #include <string>
00009 #include <cmath>
00010 
00011 class TestSuite
00012 {
00013         private:
00014                 std::vector< int (*)(TestSuite*) > tests;
00015                 std::fstream testlog;
00016                 std::vector<std::string> names;
00017         public:
00018                 TestSuite ( const char* logfile ) { testlog.open( logfile, std::ios::out ); }
00019                 ~TestSuite () { testlog.close(); }
00020                 void addTest ( int (*test)(TestSuite*), const char* testname ) { tests.push_back ( test ); names.push_back ( std::string(testname) ); }
00021                 int runTests ( void ) {
00022                         int failed(0);
00023                         unsigned int i;
00024                         // std::list< int (*)(TestSuite*) > i;
00025                         // for ( i=tests.begin(); i!=tests.end(); ++i )
00026                         //      failed += (*i)(this);
00027                         for ( i=0; i<tests.size(); i++ ) {
00028                                 std::clog << "=============> " << names[i] << " <============\n";
00029                                 failed += tests[i](this);
00030                         }
00031                         std::clog << "\n==> " << failed << " tests failed\n";
00032                         return failed;
00033                 }
00034                 int isequal ( double x, double y, const char* testname, double accuracy=1e-7 ) {
00035                         if ( fabs(x-y)<accuracy ) {
00036                                 std::clog << "[ OK ]   " << testname << " value: " << x << "\n";
00037                                 return 0;
00038                         } else {
00039                                 std::clog << "[FAIL]   " << testname << " value: " << x << " should be: " << y << "\n";
00040                                 testlog << testname << " value: " << x << " should be: " << y << "\n";
00041                                 return 1;
00042                         }
00043                 }
00044                 int isequal_rel ( double x, double y, const char* testname, double accuracy=1e-7 ) {
00045                         if ( fabs ( log10 ( x/y ) ) < accuracy ) {
00046                                 std::clog << "[ OK ]   " << testname << " value: " << x << "\n";
00047                                 return 0;
00048                         } else {
00049                                 std::clog << "[FAIL]   " << testname << " value: " << x << " should be: " << y << "\n";
00050                                 testlog << testname << " value: " << x << " should be: " << y << "\n";
00051                                 return 1;
00052                         }
00053                 }
00054                 int isless ( double x, double y, const char* testname ) {
00055                         if ( x<y ) {
00056                                 std::clog << "[ OK ]   " << testname << " value: " << x << " < " << y << "\n";
00057                                 return 0;
00058                         } else {
00059                                 std::clog << "[FAIL]   " << testname << " value: " << x << " should be less than " << y << "\n";
00060                                 testlog << testname << " value: " << x << " should be less than " << y << "\n";
00061                                 return 1;
00062                         }
00063                 }
00064                 int ismore ( double x, double y, const char* testname ) {
00065                         if ( x>y ) {
00066                                 std::clog << "[ OK ]   " << testname << " value: " << x << " > " << y << "\n";
00067                                 return 0;
00068                         } else {
00069                                 std::clog << "[FAIL]   " << testname << " value: " << x << " should be less than " << y << "\n";
00070                                 testlog << testname << " value: " << x << " should be less than " << y << "\n";
00071                                 return 1;
00072                         }
00073                 }
00074 
00075                 int conditional ( bool condition, const char * testname ) {
00076                         if ( condition ) {
00077                                 std::clog << "[ OK ]   " << testname << " good \n";
00078                                 return 0;
00079                         } else {
00080                                 std::clog << "[FAIL]   " << testname << "\n";
00081                                 testlog << testname << "\n";
00082                                 return 1;
00083                         }
00084                 }
00085 };
00086 
00087 #endif

Generated on Mon Jul 4 2011 14:52:04 for Psi++ by  doxygen 1.7.1