1 #ifndef TESTUTILITY_H 2 #define TESTUTILITY_H 3 /* 4 * history --- 5 * 2001.10 macros and functions for double are added by Eiji Yamauchi mailto:easy@aed.mci.mei.co.jp 6 DBLEQUAL DBLEQUAL0 DBLNOTEQUAL DBLNOTEQUAL0 assert_dbleq0 assert_dbleq assert_dblneq0 assert_dblneq 7 */ 8 /***** test primitives *****/ 9 10 #include <string.h> 11 #include <stdio.h> 12 13 /******** sorry printing staff ****/ 14 #define TERPRI() printf( "\n" ); 15 16 /******** assertion macros */ 17 18 #define TEST_FAIL( msg ) printf( "%s\n", msg ) 19 20 #define GASSERT( msg, form ) assert_cond( __FILE__, __LINE__, msg, #form, (form) ) 21 #define GFAIL( msg, form ) assert_cond( __FILE__, __LINE__, msg, #form, (form) ) 22 23 /* Integers */ 24 #define NEQUAL( msg, v1, v2 ) assert_equal( __FILE__, __LINE__, msg, v1, v2 ) 25 #define NEQUAL0( v1, v2 ) assert_equal0( __FILE__, __LINE__, v1, v2 ) 26 #define NNOTEQUAL( msg, v1, v2 ) assert_nequal( __FILE__, __LINE__, msg, v1, v2 ) 27 #define NNOTEQUAL0( v1, v2 ) assert_nequal0( __FILE__, __LINE__, v1, v2 ) 28 29 /* Floating points */ 30 #define DBLEQUAL(msg, v1, v2, v3) assert_dbleq(__FILE__, __LINE__, msg, v1, v2, v3) 31 #define DBLEQUAL0(v1, v2, v3) assert_dbleq(__FILE__, __LINE__, v1, v2, v3) 32 #define DBLNOTEQUAL(msg, v1, v2, v3) assert_dblneq(__FILE__, __LINE__, msg, v1, v2, v3) 33 #define DBLNOTEQUAL0(v1, v2, v3) assert_dblneq(__FILE__, __LINE__, v1, v2, v3) 34 35 /* Pointers */ 36 #define PEQUAL( msg, v1, v2 ) assert_ptreq( __FILE__, __LINE__, msg, v1, v2 ) 37 #define PEQUAL0( v1, v2 ) assert_ptreq0( __FILE__, __LINE__, v1, v2 ) 38 #define PNOTEQUAL( msg, v1, v2 ) assert_ptrneq( __FILE__, __LINE__, msg, v1, v2 ) 39 #define PNOTEQUAL0( v1, v2 ) assert_ptrneq0( __FILE__, __LINE__, v1, v2 ) 40 41 /* Strings */ 42 #define SEQUAL( msg, v1, v2 ) assert_streq( __FILE__, __LINE__, msg, v1, v2 ) 43 #define SEQUAL0( v1, v2 ) assert_streq0( __FILE__, __LINE__, v1, v2 ) 44 #define SNOTEQUAL( msg, v1, v2 ) assert_strneq( __FILE__, __LINE__, msg, v1, v2 ) 45 #define SNOTEQUAL0( v1, v2 ) assert_strneq0( __FILE__, __LINE__, v1, v2 ) 46 47 /* Memory Block */ 48 /* ex. MEQUAL( "mem test", "\023\990EFf", &x, 5 ); 49 */ 50 #define MEQUAL( msg, v1, v2, n ) assert_memeq( __FILE__, __LINE__, msg, v1, v2, n ) 51 #define MEQUAL0( v1, v2, n ) assert_memeq0( __FILE__, __LINE__, v1, v2, n ) 52 #define MNOTEQUAL( msg, v1, v2, n ) assert_memneq( __FILE__, __LINE__, msg, v1, v2, n ) 53 #define MNOTEQUAL0( v1, v2, n ) assert_memneq0( __FILE__, __LINE__, v1, v2, n ) 54 55 /******** functions *******/ 56 /* macros introduce __FILE__, __LINE__. function executes the job 57 */ 58 void assert_cond( char* file, int line, const char *msg, const char* form, int value ); 59 void fail_cond( char* file, int line, const char *msg, const char* form, int value ); 60 void assert_equal0( char* file, int line, int v1, int v2 ); 61 void assert_equal( char* file, int line, const char *msg, int v1, int v2 ); 62 void assert_nequal0( char* file, int line, int v1, int v2 ); 63 void assert_nequal( char* file, int line, const char *msg, int v1, int v2 ); 64 void assert_dbleq0( char *file, int line, double v1, double v2, double v3 ); 65 void assert_dbleq( char *file, int line, const char *msg, double v1, double v2, double v3 ); 66 void assert_dblneq0( char *file, int line, double v1, double v2, double v3 ); 67 void assert_dblneq( char *file, int line, const char *msg, double v1, double v2, double v3 ); 68 void assert_ptreq0( char* file, int line, const void *p1, const void *p2 ); 69 void assert_ptreq( char* file, int line, const char *msg, const void *p1, const void *p2 ); 70 void assert_ptrneq0( char* file, int line, const void *p1, const void *p2 ); 71 void assert_ptrneq( char* file, int line, const char *msg, const void *p1, const void *p2 ); 72 void assert_streq0( char* file, int line, const char* expect, const char* value ); 73 void assert_streq( char* file, int line, const char* msg, const char* expect, const char* value ); 74 void assert_strneq0( char* file, int line, const char* expect, const char* value ); 75 void assert_strneq( char* file, int line, const char* msg, const char* expect, const char* value ); 76 void assert_memeq0( char* file, int line, const char* expect, const char* addr, size_t len ); 77 void assert_memeq( char* file, int line, const char* msg, const char* expect, const char* addr, size_t len ); 78 void assert_memneq0( char* file, int line, const char* expect, const char* addr, size_t len ); 79 void assert_memneq( char* file, int line, const char* msg, const char* expect, const char* addr, size_t len ); 80 81 /******** framework of test *****/ 82 /* 83 * an instance of test_fn run the test. 84 * TEST_FRAME separate the environments for every test_fn 85 * TEST_FRAME( test_function ); // test_function is an instance of test_fn 86 * TEST_FRAME() allocate and release the data 87 */ 88 89 typedef void (*setup_fn)( void** data ); 90 typedef void (*teardown_fn)( void** data ); 91 92 typedef struct { 93 setup_fn setUp; 94 teardown_fn tearDown; 95 void *data; 96 } TEST_ENV; 97 98 setup_fn setSetup( TEST_ENV *env, setup_fn fn ); 99 teardown_fn setTeardown( TEST_ENV *env, teardown_fn fn ); 100 101 typedef void (*test_fn)( void *data ); 102 103 void test_frame( const char* name, test_fn fn, TEST_ENV* env ); 104 105 #define TEST_FRAME( X, E ) test_frame( #X, X, E ) 106 107 #define TEST_FRAME0( X ) test_frame( #X, X, NULL ) 108 109 #define RUN_TEST( setup, teardown, testfn ) {\ 110 TEST_ENV env;\ 111 setSetup( &env, setup );\ 112 setTeardown( &env, teardown );\ 113 TEST_FRAME( testfn, &env );\ 114 } 115 116 #ifdef HAVE_FAILBLOCK 117 int fail_block( test_fn fn, TEST_ENV* env ); 118 119 #endif 120 121 /* for SDLSKK */ 122 #include "sdlskk_internal.h" 123 #define SSSEQUAL( msg, exp, val ) assert_sssequal( __FILE__, __LINE__, msg, exp, val ) 124 #define SSSNEQUAL( msg, exp, val ) assert_sssneq( __FILE__, __LINE__, msg, exp, val ) 125 #define SSSEQUAL0( exp, val ) assert_sssequal( __FILE__, __LINE__, "", exp, val ) 126 void assert_sssequal( char* file, int line, const char* msg, SDLSKK_Char* expect, SDLSKK_Char* value ); 127 void assert_sssneq( char* file, int line, const char* msg, SDLSKK_Char* expect, SDLSKK_Char* value ); 128 129 #endif 130