test.h
上传用户:awang829
上传日期:2019-07-14
资源大小:2356k
文件大小:10k
源码类别:

网络

开发平台:

Unix_Linux

  1. /* Copyright (c) 2001-2003, Roger Dingledine.
  2.  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3.  * Copyright (c) 2007-2009, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef _TOR_TEST_H
  6. #define _TOR_TEST_H
  7. /**
  8.  * file test.h
  9.  * brief Macros used by unit tests.
  10.  */
  11. #include "compat.h"
  12. #ifdef __GNUC__
  13. #define PRETTY_FUNCTION __PRETTY_FUNCTION__
  14. #else
  15. #define PRETTY_FUNCTION ""
  16. #endif
  17. #define test_fail_msg(msg)                                      
  18.   STMT_BEGIN                                                    
  19.     have_failed = 1;                                            
  20.     printf("nFile %s: line %d (%s): %s",                       
  21.       _SHORT_FILE_,                                             
  22.       __LINE__,                                                 
  23.       PRETTY_FUNCTION,                                          
  24.       msg);                                                     
  25.     goto done;                                                  
  26.   STMT_END
  27. #define test_fail() test_fail_msg("Assertion failed.")
  28. #define test_assert(expr)                                       
  29.   STMT_BEGIN                                                    
  30.   if (expr) { printf("."); fflush(stdout); } else {             
  31.     have_failed = 1;                                            
  32.     printf("nFile %s: line %d (%s): assertion failed: (%s)n", 
  33.       _SHORT_FILE_,                                             
  34.       __LINE__,                                                 
  35.       PRETTY_FUNCTION,                                          
  36.       #expr);                                                   
  37.     goto done;                                                  
  38.   } STMT_END
  39. #define test_eq_type(tp, fmt, expr1, expr2) 
  40.   STMT_BEGIN                                                            
  41.   tp _test_v1=(tp)(expr1);                                              
  42.   tp _test_v2=(tp)(expr2);                                              
  43.   if (_test_v1==_test_v2) { printf("."); fflush(stdout); } else {       
  44.     have_failed = 1;                                                    
  45.     printf("nFile %s: line %d (%s): Assertion failed: (%s==%s)n"      
  46.            "      "fmt "!="fmt"n",                                     
  47.              _SHORT_FILE_,                                              
  48.              __LINE__,                                                  
  49.              PRETTY_FUNCTION,                                           
  50.              #expr1, #expr2,                                            
  51.            _test_v1, _test_v2);                                         
  52.     goto done;                                                          
  53.   } STMT_END
  54. #define test_eq(expr1, expr2)                   
  55.   test_eq_type(long, "%ld", expr1, expr2)
  56. #define test_eq_ptr(expr1, expr2)               
  57.   test_eq_type(void*, "%p", expr1, expr2)
  58. #define test_neq_type(tp, fmt, expr1, expr2)                            
  59.   STMT_BEGIN                                                            
  60.   tp _test_v1=(tp)(expr1);                                              
  61.   tp _test_v2=(tp)(expr2);                                              
  62.   if (_test_v1!=_test_v2) { printf("."); fflush(stdout); } else {       
  63.     have_failed = 1;                                                    
  64.     printf("nFile %s: line %d (%s): Assertion failed: (%s!=%s)n"      
  65.            "      ("fmt" == "fmt")n",                                  
  66.            _SHORT_FILE_,                                                
  67.            __LINE__,                                                    
  68.            PRETTY_FUNCTION,                                             
  69.            #expr1, #expr2,                                              
  70.            _test_v1, _test_v2);                                         
  71.     goto done;                                                          
  72.   } STMT_END
  73. #define test_neq(expr1, expr2)                  
  74.   test_neq_type(long, "%ld", expr1, expr2)
  75. #define test_neq_ptr(expr1, expr2)              
  76.   test_neq_type(void *, "%p", expr1, expr2)
  77. #define test_streq(expr1, expr2)                                
  78.   STMT_BEGIN                                                    
  79.     const char *_test_v1=(expr1), *_test_v2=(expr2);                        
  80.     if (!strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { 
  81.     have_failed = 1;                                            
  82.     printf("nFile %s: line %d (%s): Assertion failed: (%s==%s)n"
  83.            "      ("%s" != "%s")n",                        
  84.       _SHORT_FILE_,                                             
  85.       __LINE__,                                                 
  86.       PRETTY_FUNCTION,                                          
  87.       #expr1, #expr2,                                           
  88.       _test_v1, _test_v2);                                      
  89.     goto done;                                                  
  90.   } STMT_END
  91. #define test_strneq(expr1, expr2)                               
  92.   STMT_BEGIN                                                    
  93.     const char *_test_v1=(expr1), *_test_v2=(expr2);                        
  94.     if (strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else {  
  95.     have_failed = 1;                                            
  96.     printf("nFile %s: line %d (%s): Assertion failed: (%s!=%s)n"
  97.            "      ("%s" == "%s")n",                        
  98.       _SHORT_FILE_,                                             
  99.       __LINE__,                                                 
  100.       PRETTY_FUNCTION,                                          
  101.       #expr1, #expr2,                                           
  102.       _test_v1, _test_v2);                                      
  103.     goto done;                                                  
  104.   } STMT_END
  105. #define test_memeq(expr1, expr2, len)                           
  106.   STMT_BEGIN                                                    
  107.     const void *_test_v1=(expr1), *_test_v2=(expr2);            
  108.     char *mem1, *mem2;                                          
  109.     if (!memcmp(_test_v1,_test_v2,(len))) {                     
  110.       printf("."); fflush(stdout); } else {                     
  111.     have_failed = 1;                                            
  112.     mem1 = tor_malloc(len*2+1);                                    
  113.     mem2 = tor_malloc(len*2+1);                                    
  114.     base16_encode(mem1, len*2+1, _test_v1, len);                   
  115.     base16_encode(mem2, len*2+1, _test_v2, len);                   
  116.     printf("nFile %s: line %d (%s): Assertion failed: (%s==%s)n" 
  117.            "      %s != %sn",                                     
  118.       _SHORT_FILE_,                                             
  119.       __LINE__,                                                 
  120.       PRETTY_FUNCTION,                                          
  121.       #expr1, #expr2, mem1, mem2);                              
  122.     tor_free(mem1);                                             
  123.     tor_free(mem2);                                             
  124.     goto done;                                                  
  125.   } STMT_END
  126. #define test_memeq_hex(expr1, hex)                                      
  127.   STMT_BEGIN                                                            
  128.     const char *_test_v1 = (char*)(expr1);                              
  129.     const char *_test_v2 = (hex);                                       
  130.     size_t _len_v2 = strlen(_test_v2);                                  
  131.     char *_mem2 = tor_malloc(_len_v2/2);                                
  132.     tor_assert((_len_v2 & 1) == 0);                                     
  133.     base16_decode(_mem2, _len_v2/2, _test_v2, _len_v2);                 
  134.     if (!memcmp(_mem2, _test_v1, _len_v2/2)) {                          
  135.       printf("."); fflush(stdout); } else {                             
  136.       char *_mem1 = tor_malloc(_len_v2+1);                              
  137.       base16_encode(_mem1, _len_v2+1, _test_v1, _len_v2/2);             
  138.       printf("nFile %s: line %d (%s): Assertion failed: (%s==%s)n"    
  139.              "      %s != %sn",                                        
  140.              _SHORT_FILE_,                                              
  141.              __LINE__,                                                  
  142.              PRETTY_FUNCTION,                                           
  143.              #expr1, _test_v2, _mem1, _test_v2);                        
  144.       tor_free(_mem1);                                                  
  145.       tor_free(_mem2);                                                  
  146.       goto done;                                                        
  147.     }                                                                   
  148.     tor_free(_mem2);                                                    
  149.   STMT_END
  150. #define test_memneq(expr1, expr2, len)                          
  151.   STMT_BEGIN                                                    
  152.    void *_test_v1=(expr1), *_test_v2=(expr2);                   
  153.    if (memcmp(_test_v1,_test_v2,(len))) {                       
  154.      printf("."); fflush(stdout);                               
  155.    } else {                                                     
  156.     have_failed = 1;                                            
  157.     printf("nFile %s: line %d (%s): Assertion failed: (%s!=%s)n", 
  158.       _SHORT_FILE_,                                             
  159.       __LINE__,                                                 
  160.       PRETTY_FUNCTION,                                          
  161.       #expr1, #expr2);                                          
  162.     goto done;                                                  
  163.   } STMT_END
  164. #endif