/* Initializer/finalizer sample for MSVC and GCC.
2010 Joe Lowe. Released into the public domain.
*/
#include
#include
#ifdef _MSC_VER
#define CCALL __cdecl
#pragma section(".CRT$XCU",read)
#define INITIALIZER(f) \
static void __cdecl f(void); \
__declspec(allocate(".CRT$XCU")) void (__cdecl* EXP_LINE(f,_) )(void) = f; \
static void __cdecl f(void)
#elif defined(__GNUC__)
#define CCALL
#define INITIALIZER(f) \
static void f(void) __attribute__((constructor));\
static void f(void)
#endif
typedef void (*UnitTest_FP)(void);
struct {
const char * name;
UnitTest_FP fp;
} static s_all_unit_tests[100];
static void register_unit_test(const char * name, UnitTest_FP fp)
{
static int test_idx = 0;
s_all_unit_tests[test_idx].name = name;
s_all_unit_tests[test_idx].fp = fp;
test_idx ++;
}
#define EXP_LINE(a, l) a##l
#define CONCAT(a, b) EXP_LINE(a,b)
#define UNIT_TEST(a) static void a(void);\
INITIALIZER( CONCAT(reg_##a, __LINE__)) \
{ \
register_unit_test(#a, a); \
} \
void a(void)
static void CCALL finalize(void)
{
printf("finalize\n");
}
UNIT_TEST(my_test_1)
{
printf( "calling \n");
}
UNIT_TEST(my_test_2)
{
printf( "calling \n");
}
int CCALL main(int argc,const char*const* argv)
{
int i = 0;
for(; i < sizeof(s_all_unit_tests) / sizeof(s_all_unit_tests[0]); ++i)
{
if(!s_all_unit_tests[i]. name) break;
printf("function: %s\n", s_all_unit_tests[i].name );
(*s_all_unit_tests[i].fp) ();
}
return 0;
}
阅读(1007) | 评论(0) | 转发(0) |