testhelp.h是作者为redis量身定做的单元测试框架,对于redis这种规模的项目,就没有必要上GTEST这种大杀器了,作者18行代码搞定。
不过很遗憾,在2.4.10这个版本的版本的redis中,只有sds用了这个测试框架,不知其他代码作者是如何做测试的。我慢慢摸索,摸索到了告诉大家。
      
         1
      
      
        #ifndef __TESTHELP_H
      
      
         2
      
      
        #define
      
       __TESTHELP_H
      
         3
      
      
         4
      
      
        int
      
       __failed_tests = 
      
        0
      
      
        ;            //失败的测试用例数
      
      
         5
      
      
        int
      
       __test_num = 
      
        0
      
      
        ;                //总的测试用例数
      
      
         6
      
      
        #define
      
       test_cond(descr,_c) do { \
      
         7
      
           __test_num++; printf(
      
        "
      
      
        %d - %s: 
      
      
        "
      
      
        , __test_num, descr); \
      
      
         8
      
      
        if
      
      (_c) printf(
      
        "
      
      
        PASSED\n
      
      
        "
      
      ); 
      
        else
      
       {printf(
      
        "
      
      
        FAILED\n
      
      
        "
      
      ); __failed_tests++
      
        ;} \
      
      
         9
      
       } 
      
        while
      
      (
      
        0
      
      
        );
      
      
        10
      
      
        #define
      
       test_report() do { \
      
        11
      
           printf(
      
        "
      
      
        %d tests, %d passed, %d failed\n
      
      
        "
      
      
        , __test_num, \
      
      
        12
      
                           __test_num-
      
        __failed_tests, __failed_tests); \
      
      
        13
      
      
        if
      
      
         (__failed_tests) { \
      
      
        14
      
               printf(
      
        "
      
      
        === WARNING === We have failed tests here...\n
      
      
        "
      
      
        ); \
      
      
        15
      
      
            } \
      
      
        16
      
       } 
      
        while
      
      (
      
        0
      
      
        );
      
      
        17
      
      
        18
      
      
        #endif  //完全用宏实现的,一目了然,不多说了
      
    
  


 
					 
					