00001
00002
00003 #include <iostream>
00004 #include <cstdlib>
00005
00006 #ifndef WIBBLE_TEST_H
00007 #define WIBBLE_TEST_H
00008
00009
00010 extern int assertFailure;
00011
00012 struct Location {
00013 const char *file;
00014 int line;
00015 std::string stmt;
00016 Location( const char *f, int l, std::string st )
00017 : file( f ), line( l ), stmt( st ) {}
00018 };
00019
00020 #define LOCATION(stmt) Location( __FILE__, __LINE__, stmt )
00021 #define assert(x) assert_fn( LOCATION( #x ), x )
00022 #define assert_eq(x, y) assert_eq_fn( LOCATION( #x " == " #y ), x, y )
00023 #define assert_neq(x, y) assert_neq_fn( LOCATION( #x " != " #y ), x, y )
00024
00025 #define CHECK_ASSERT(x) \
00026 do { \
00027 if ( x ) return; \
00028 else if ( assertFailure ) \
00029 { \
00030 ++assertFailure; \
00031 return; \
00032 } \
00033 } while (0)
00034
00035 static inline std::ostream &assert_failed( Location l )
00036 {
00037 return std::cerr << l.file << ": " << l.line
00038 << ": assertion `" << l.stmt << "' failed;";
00039 }
00040
00041 template< typename X >
00042 void assert_fn( Location l, X x )
00043 {
00044 CHECK_ASSERT( x );
00045 assert_failed( l ) << std::endl;
00046 abort();
00047 }
00048
00049 template< typename X, typename Y >
00050 void assert_eq_fn( Location l, X x, Y y )
00051
00052 {
00053 CHECK_ASSERT( x == y );
00054 assert_failed( l ) << " got ["
00055 << x << "] != [" << y << "] instead" << std::endl;
00056 abort();
00057 }
00058
00059 template< typename X, typename Y >
00060 void assert_neq_fn( Location l, X x, Y y )
00061
00062 {
00063 CHECK_ASSERT( x != y );
00064 assert_failed( l ) << " got ["
00065 << x << "] == [" << y << "] instead" << std::endl;
00066 abort();
00067 }
00068
00069 inline void beginAssertFailure() {
00070 assertFailure = 1;
00071 }
00072
00073 inline void endAssertFailure() {
00074 int f = assertFailure;
00075 assertFailure = 0;
00076 assert( f > 1 );
00077 }
00078
00079 struct AssertFailure {
00080 AssertFailure() { beginAssertFailure(); }
00081 ~AssertFailure() { endAssertFailure(); }
00082 };
00083
00084 typedef void Test;
00085
00086 #endif