test.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <string.h>
  13. #include <stdio.h>
  14. #include "compat.h"
  15. #define STMT_BEGIN do {
  16. #define STMT_END } while (0)
  17. #ifdef __GNUC__
  18. #define PRETTY_FUNCTION __PRETTY_FUNCTION__
  19. #else
  20. #define PRETTY_FUNCTION ""
  21. #endif
  22. extern int have_failed;
  23. #define test_fail() \
  24. STMT_BEGIN \
  25. have_failed = 1; \
  26. printf("\nFile %s: line %d (%s): assertion failed.", \
  27. _SHORT_FILE_, \
  28. __LINE__, \
  29. PRETTY_FUNCTION); \
  30. return; \
  31. STMT_END
  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. return; \
  42. } STMT_END
  43. #define test_eq_type(tp, fmt, expr1, expr2) \
  44. STMT_BEGIN \
  45. tp v1=(tp)(expr1); \
  46. tp v2=(tp)(expr2); \
  47. if (v1==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. v1, v2); \
  56. return; \
  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(expr1, expr2) \
  63. STMT_BEGIN \
  64. long v1=(long)(expr1), v2=(long)(expr2); \
  65. if (v1!=v2) { printf("."); fflush(stdout); } else { \
  66. have_failed = 1; \
  67. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
  68. " (%ld == %ld)\n", \
  69. _SHORT_FILE_, \
  70. __LINE__, \
  71. PRETTY_FUNCTION, \
  72. #expr1, #expr2, \
  73. v1, v2); \
  74. return; \
  75. } STMT_END
  76. #define test_streq(expr1, expr2) \
  77. STMT_BEGIN \
  78. const char *v1=(expr1), *v2=(expr2); \
  79. if (!strcmp(v1,v2)) { printf("."); fflush(stdout); } else { \
  80. have_failed = 1; \
  81. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
  82. " (\"%s\" != \"%s\")\n", \
  83. _SHORT_FILE_, \
  84. __LINE__, \
  85. PRETTY_FUNCTION, \
  86. #expr1, #expr2, \
  87. v1, v2); \
  88. return; \
  89. } STMT_END
  90. #define test_strneq(expr1, expr2) \
  91. STMT_BEGIN \
  92. const char *v1=(expr1), *v2=(expr2); \
  93. if (strcmp(v1,v2)) { printf("."); fflush(stdout); } else { \
  94. have_failed = 1; \
  95. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
  96. " (\"%s\" == \"%s\")\n", \
  97. _SHORT_FILE_, \
  98. __LINE__, \
  99. PRETTY_FUNCTION, \
  100. #expr1, #expr2, \
  101. v1, v2); \
  102. return; \
  103. } STMT_END
  104. #define test_memeq(expr1, expr2, len) \
  105. STMT_BEGIN \
  106. const void *v1=(expr1), *v2=(expr2); \
  107. if (!memcmp(v1,v2,(len))) { printf("."); fflush(stdout); } else {\
  108. have_failed = 1; \
  109. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \
  110. _SHORT_FILE_, \
  111. __LINE__, \
  112. PRETTY_FUNCTION, \
  113. #expr1, #expr2); \
  114. return; \
  115. } STMT_END
  116. #define test_memneq(expr1, expr2, len) \
  117. STMT_BEGIN \
  118. void *v1=(expr1), *v2=(expr2); \
  119. if (memcmp(v1,v2,(len))) { printf("."); fflush(stdout); } else {\
  120. have_failed = 1; \
  121. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
  122. _SHORT_FILE_, \
  123. __LINE__, \
  124. PRETTY_FUNCTION, \
  125. #expr1, #expr2); \
  126. return; \
  127. } STMT_END
  128. #endif