test.h 9.7 KB

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