test.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright 2001,2002,2003 Roger Dingledine.
  2. * Copyright 2004-2007 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. #ifndef __TEST_H
  6. #define __TEST_H
  7. #define TEST_H_ID "$Id$"
  8. /**
  9. * \file test.h
  10. * \brief Macros used by unit tests.
  11. */
  12. #include "compat.h"
  13. #ifdef __GNUC__
  14. #define PRETTY_FUNCTION __PRETTY_FUNCTION__
  15. #else
  16. #define PRETTY_FUNCTION ""
  17. #endif
  18. extern int have_failed;
  19. #define test_fail() \
  20. STMT_BEGIN \
  21. have_failed = 1; \
  22. printf("\nFile %s: line %d (%s): assertion failed.", \
  23. _SHORT_FILE_, \
  24. __LINE__, \
  25. PRETTY_FUNCTION); \
  26. return; \
  27. STMT_END
  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. return; \
  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. return; \
  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. return; \
  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. return; \
  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. return; \
  104. } STMT_END
  105. #define test_memeq(expr1, expr2, len) \
  106. STMT_BEGIN \
  107. const void *_test_v1=(expr1), *_test_v2=(expr2); \
  108. if (!memcmp(_test_v1,_test_v2,(len))) { \
  109. printf("."); fflush(stdout); } else { \
  110. have_failed = 1; \
  111. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \
  112. _SHORT_FILE_, \
  113. __LINE__, \
  114. PRETTY_FUNCTION, \
  115. #expr1, #expr2); \
  116. return; \
  117. } STMT_END
  118. #define test_memneq(expr1, expr2, len) \
  119. STMT_BEGIN \
  120. void *_test_v1=(expr1), *_test_v2=(expr2); \
  121. if (memcmp(_test_v1,_test_v2,(len))) { \
  122. printf("."); fflush(stdout); \
  123. } else { \
  124. have_failed = 1; \
  125. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
  126. _SHORT_FILE_, \
  127. __LINE__, \
  128. PRETTY_FUNCTION, \
  129. #expr1, #expr2); \
  130. return; \
  131. } STMT_END
  132. #endif