test.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (c) 2001-2003, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007, 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() \
  21. STMT_BEGIN \
  22. have_failed = 1; \
  23. printf("\nFile %s: line %d (%s): assertion failed.", \
  24. _SHORT_FILE_, \
  25. __LINE__, \
  26. PRETTY_FUNCTION); \
  27. return; \
  28. STMT_END
  29. #define test_assert(expr) \
  30. STMT_BEGIN \
  31. if (expr) { printf("."); fflush(stdout); } else { \
  32. have_failed = 1; \
  33. printf("\nFile %s: line %d (%s): assertion failed: (%s)\n", \
  34. _SHORT_FILE_, \
  35. __LINE__, \
  36. PRETTY_FUNCTION, \
  37. #expr); \
  38. return; \
  39. } STMT_END
  40. #define test_eq_type(tp, fmt, expr1, expr2) \
  41. STMT_BEGIN \
  42. tp _test_v1=(tp)(expr1); \
  43. tp _test_v2=(tp)(expr2); \
  44. if (_test_v1==_test_v2) { printf("."); fflush(stdout); } else { \
  45. have_failed = 1; \
  46. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
  47. " "fmt "!="fmt"\n", \
  48. _SHORT_FILE_, \
  49. __LINE__, \
  50. PRETTY_FUNCTION, \
  51. #expr1, #expr2, \
  52. _test_v1, _test_v2); \
  53. return; \
  54. } STMT_END
  55. #define test_eq(expr1, expr2) \
  56. test_eq_type(long, "%ld", expr1, expr2)
  57. #define test_eq_ptr(expr1, expr2) \
  58. test_eq_type(void*, "%p", expr1, expr2)
  59. #define test_neq_type(tp, fmt, expr1, expr2) \
  60. STMT_BEGIN \
  61. tp _test_v1=(tp)(expr1); \
  62. tp _test_v2=(tp)(expr2); \
  63. if (_test_v1!=_test_v2) { printf("."); fflush(stdout); } else { \
  64. have_failed = 1; \
  65. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n" \
  66. " ("fmt" == "fmt")\n", \
  67. _SHORT_FILE_, \
  68. __LINE__, \
  69. PRETTY_FUNCTION, \
  70. #expr1, #expr2, \
  71. _test_v1, _test_v2); \
  72. return; \
  73. } STMT_END
  74. #define test_neq(expr1, expr2) \
  75. test_neq_type(long, "%ld", expr1, expr2)
  76. #define test_neq_ptr(expr1, expr2) \
  77. test_neq_type(void *, "%p", expr1, expr2)
  78. #define test_streq(expr1, expr2) \
  79. STMT_BEGIN \
  80. const char *_test_v1=(expr1), *_test_v2=(expr2); \
  81. if (!strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
  82. have_failed = 1; \
  83. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
  84. " (\"%s\" != \"%s\")\n", \
  85. _SHORT_FILE_, \
  86. __LINE__, \
  87. PRETTY_FUNCTION, \
  88. #expr1, #expr2, \
  89. _test_v1, _test_v2); \
  90. return; \
  91. } STMT_END
  92. #define test_strneq(expr1, expr2) \
  93. STMT_BEGIN \
  94. const char *_test_v1=(expr1), *_test_v2=(expr2); \
  95. if (strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
  96. have_failed = 1; \
  97. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
  98. " (\"%s\" == \"%s\")\n", \
  99. _SHORT_FILE_, \
  100. __LINE__, \
  101. PRETTY_FUNCTION, \
  102. #expr1, #expr2, \
  103. _test_v1, _test_v2); \
  104. return; \
  105. } STMT_END
  106. #define test_memeq(expr1, expr2, len) \
  107. STMT_BEGIN \
  108. const void *_test_v1=(expr1), *_test_v2=(expr2); \
  109. if (!memcmp(_test_v1,_test_v2,(len))) { \
  110. printf("."); fflush(stdout); } else { \
  111. have_failed = 1; \
  112. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \
  113. _SHORT_FILE_, \
  114. __LINE__, \
  115. PRETTY_FUNCTION, \
  116. #expr1, #expr2); \
  117. return; \
  118. } STMT_END
  119. #define test_memneq(expr1, expr2, len) \
  120. STMT_BEGIN \
  121. void *_test_v1=(expr1), *_test_v2=(expr2); \
  122. if (memcmp(_test_v1,_test_v2,(len))) { \
  123. printf("."); fflush(stdout); \
  124. } else { \
  125. have_failed = 1; \
  126. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
  127. _SHORT_FILE_, \
  128. __LINE__, \
  129. PRETTY_FUNCTION, \
  130. #expr1, #expr2); \
  131. return; \
  132. } STMT_END
  133. #endif