medfall

A super great game engine
Log | Files | Refs

ggunit.h (782B)


      1 #pragma once
      2 
      3 #if defined( UNITTESTS )
      4 
      5 #include <stdio.h>
      6 
      7 #define CONCAT_HELPER( a, b ) a##b
      8 #define CONCAT( a, b ) CONCAT_HELPER( a, b )
      9 #define COUNTER_NAME( x ) CONCAT( x, __COUNTER__ )
     10 
     11 #define AT_STARTUP( code ) \
     12 	namespace COUNTER_NAME( StartupCode ) { \
     13 		static struct AtStartup { \
     14 			AtStartup() { code; } \
     15 		} AtStartupInstance; \
     16 	}
     17 
     18 #define UNITTEST( name, body ) \
     19 	namespace { \
     20 		AT_STARTUP( \
     21 			int passed = 0; \
     22 			int failed = 0; \
     23 			puts( name ); \
     24 			body; \
     25 			printf( "%d passed, %d failed\n\n", passed, failed ); \
     26 		) \
     27 	}
     28 
     29 #define TEST( p ) \
     30 	if( !( p ) ) { \
     31 		failed++; \
     32 		puts( "    FAIL: " #p ); \
     33 	} \
     34 	else { \
     35 		passed++; \
     36 	}
     37 
     38 #define private public
     39 #define protected public
     40 
     41 #else
     42 
     43 #define UNITTEST( name, body )
     44 #define TEST( p )
     45 
     46 #endif