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