Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2315264
  • 博文数量: 527
  • 博客积分: 10343
  • 博客等级: 上将
  • 技术积分: 5565
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-26 23:05
文章分类

全部博文(527)

文章存档

2014年(4)

2012年(13)

2011年(19)

2010年(91)

2009年(136)

2008年(142)

2007年(80)

2006年(29)

2005年(13)

我的朋友

分类: C/C++

2010-09-07 18:24:10


/* 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;
}

阅读(979) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~