test.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 _TOR_TEST_H
  7. #define _TOR_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. #define test_fail_msg(msg) \
  20. STMT_BEGIN \
  21. have_failed = 1; \
  22. printf("\nFile %s: line %d (%s): %s", \
  23. _SHORT_FILE_, \
  24. __LINE__, \
  25. PRETTY_FUNCTION, \
  26. msg); \
  27. goto done; \
  28. STMT_END
  29. #define test_fail() test_fail_msg("Assertion failed.")
  30. #define test_assert(expr) \
  31. STMT_BEGIN \
  32. if (expr) { printf("."); fflush(stdout); } else { \
  33. have_failed = 1; \
  34. printf("\nFile %s: line %d (%s): assertion failed: (%s)\n", \
  35. _SHORT_FILE_, \
  36. __LINE__, \
  37. PRETTY_FUNCTION, \
  38. #expr); \
  39. goto done; \
  40. } STMT_END
  41. #define test_eq_type(tp, fmt, expr1, expr2) \
  42. STMT_BEGIN \
  43. tp _test_v1=(tp)(expr1); \
  44. tp _test_v2=(tp)(expr2); \
  45. if (_test_v1==_test_v2) { printf("."); fflush(stdout); } else { \
  46. have_failed = 1; \
  47. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
  48. " "fmt "!="fmt"\n", \
  49. _SHORT_FILE_, \
  50. __LINE__, \
  51. PRETTY_FUNCTION, \
  52. #expr1, #expr2, \
  53. _test_v1, _test_v2); \
  54. goto done; \
  55. } STMT_END
  56. #define test_eq(expr1, expr2) \
  57. test_eq_type(long, "%ld", expr1, expr2)
  58. #define test_eq_ptr(expr1, expr2) \
  59. test_eq_type(void*, "%p", expr1, expr2)
  60. #define test_neq_type(tp, fmt, expr1, expr2) \
  61. STMT_BEGIN \
  62. tp _test_v1=(tp)(expr1); \
  63. tp _test_v2=(tp)(expr2); \
  64. if (_test_v1!=_test_v2) { printf("."); fflush(stdout); } else { \
  65. have_failed = 1; \
  66. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n" \
  67. " ("fmt" == "fmt")\n", \
  68. _SHORT_FILE_, \
  69. __LINE__, \
  70. PRETTY_FUNCTION, \
  71. #expr1, #expr2, \
  72. _test_v1, _test_v2); \
  73. goto done; \
  74. } STMT_END
  75. #define test_neq(expr1, expr2) \
  76. test_neq_type(long, "%ld", expr1, expr2)
  77. #define test_neq_ptr(expr1, expr2) \
  78. test_neq_type(void *, "%p", expr1, expr2)
  79. #define test_streq(expr1, expr2) \
  80. STMT_BEGIN \
  81. const char *_test_v1=(expr1), *_test_v2=(expr2); \
  82. if (!strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
  83. have_failed = 1; \
  84. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
  85. " (\"%s\" != \"%s\")\n", \
  86. _SHORT_FILE_, \
  87. __LINE__, \
  88. PRETTY_FUNCTION, \
  89. #expr1, #expr2, \
  90. _test_v1, _test_v2); \
  91. goto done; \
  92. } STMT_END
  93. #define test_strneq(expr1, expr2) \
  94. STMT_BEGIN \
  95. const char *_test_v1=(expr1), *_test_v2=(expr2); \
  96. if (strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
  97. have_failed = 1; \
  98. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
  99. " (\"%s\" == \"%s\")\n", \
  100. _SHORT_FILE_, \
  101. __LINE__, \
  102. PRETTY_FUNCTION, \
  103. #expr1, #expr2, \
  104. _test_v1, _test_v2); \
  105. goto done; \
  106. } STMT_END
  107. #define test_memeq(expr1, expr2, len) \
  108. STMT_BEGIN \
  109. const void *_test_v1=(expr1), *_test_v2=(expr2); \
  110. char *mem1, *mem2; \
  111. if (!memcmp(_test_v1,_test_v2,(len))) { \
  112. printf("."); fflush(stdout); } else { \
  113. have_failed = 1; \
  114. mem1 = tor_malloc(len*2+1); \
  115. mem2 = tor_malloc(len*2+1); \
  116. base16_encode(mem1, len*2+1, _test_v1, len); \
  117. base16_encode(mem2, len*2+1, _test_v2, len); \
  118. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
  119. " %s != %s\n", \
  120. _SHORT_FILE_, \
  121. __LINE__, \
  122. PRETTY_FUNCTION, \
  123. #expr1, #expr2, mem1, mem2); \
  124. tor_free(mem1); \
  125. tor_free(mem2); \
  126. goto done; \
  127. } STMT_END
  128. #define test_memeq_hex(expr1, hex) \
  129. STMT_BEGIN \
  130. const char *_test_v1 = (char*)(expr1); \
  131. const char *_test_v2 = (hex); \
  132. size_t _len_v2 = strlen(_test_v2); \
  133. char *_mem2 = tor_malloc(_len_v2/2); \
  134. tor_assert((_len_v2 & 1) == 0); \
  135. base16_decode(_mem2, _len_v2/2, _test_v2, _len_v2); \
  136. if (!memcmp(_mem2, _test_v1, _len_v2/2)) { \
  137. printf("."); fflush(stdout); } else { \
  138. char *_mem1 = tor_malloc(_len_v2+1); \
  139. base16_encode(_mem1, _len_v2+1, _test_v1, _len_v2/2); \
  140. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
  141. " %s != %s\n", \
  142. _SHORT_FILE_, \
  143. __LINE__, \
  144. PRETTY_FUNCTION, \
  145. #expr1, _test_v2, _mem1, _test_v2); \
  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