test.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Copyright 2001,2002,2003 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #ifndef __TEST_H
  5. #define __TEST_H
  6. #include <string.h>
  7. #define STMT_BEGIN do {
  8. #define STMT_END } while (0)
  9. #ifdef __GNUC__
  10. #define PRETTY_FUNCTION __PRETTY_FUNCTION__
  11. #else
  12. #define PRETTY_FUNCTION ""
  13. #endif
  14. extern int have_failed;
  15. #define test_fail() \
  16. STMT_BEGIN \
  17. have_failed = 1; \
  18. printf("\nFile %s: line %d (%s): assertion failed.", \
  19. __FILE__, \
  20. __LINE__, \
  21. PRETTY_FUNCTION); \
  22. return; \
  23. STMT_END
  24. #define test_assert(expr) \
  25. STMT_BEGIN \
  26. if(expr) { printf("."); } else { \
  27. have_failed = 1; \
  28. printf("\nFile %s: line %d (%s): assertion failed: (%s)\n", \
  29. __FILE__, \
  30. __LINE__, \
  31. PRETTY_FUNCTION, \
  32. #expr); \
  33. return; \
  34. } STMT_END
  35. #define test_eq(expr1, expr2) \
  36. STMT_BEGIN \
  37. long v1=(long)(expr1), v2=(long)(expr2); \
  38. if(v1==v2) { printf("."); } else { \
  39. have_failed = 1; \
  40. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
  41. " (%ld != %ld)\n", \
  42. __FILE__, \
  43. __LINE__, \
  44. PRETTY_FUNCTION, \
  45. #expr1, #expr2, \
  46. v1, v2); \
  47. return; \
  48. } STMT_END
  49. #define test_neq(expr1, expr2) \
  50. STMT_BEGIN \
  51. long v1=(long)(expr1), v2=(long)(expr2); \
  52. if(v1!=v2) { printf("."); } else { \
  53. have_failed = 1; \
  54. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
  55. " (%ld == %ld)\n", \
  56. __FILE__, \
  57. __LINE__, \
  58. PRETTY_FUNCTION, \
  59. #expr1, #expr2, \
  60. v1, v2); \
  61. return; \
  62. } STMT_END
  63. #define test_streq(expr1, expr2) \
  64. STMT_BEGIN \
  65. const char *v1=(expr1), *v2=(expr2); \
  66. if(!strcmp(v1,v2)) { printf("."); } else { \
  67. have_failed = 1; \
  68. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
  69. " (\"%s\" != \"%s\")\n", \
  70. __FILE__, \
  71. __LINE__, \
  72. PRETTY_FUNCTION, \
  73. #expr1, #expr2, \
  74. v1, v2); \
  75. return; \
  76. } STMT_END
  77. #define test_strneq(expr1, expr2) \
  78. STMT_BEGIN \
  79. const char *v1=(expr1), *v2=(expr2); \
  80. if(strcmp(v1,v2)) { printf("."); } else { \
  81. have_failed = 1; \
  82. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
  83. " (\"%s\" == \"%s\")\n", \
  84. __FILE__, \
  85. __LINE__, \
  86. PRETTY_FUNCTION, \
  87. #expr1, #expr2, \
  88. v1, v2); \
  89. return; \
  90. } STMT_END
  91. #define test_memeq(expr1, expr2, len) \
  92. STMT_BEGIN \
  93. void *v1=(expr1), *v2=(expr2); \
  94. if(!memcmp(v1,v2,(len))) { printf("."); } else { \
  95. have_failed = 1; \
  96. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \
  97. __FILE__, \
  98. __LINE__, \
  99. PRETTY_FUNCTION, \
  100. #expr1, #expr2); \
  101. return; \
  102. } STMT_END
  103. #define test_memneq(expr1, expr2, len) \
  104. STMT_BEGIN \
  105. void *v1=(expr1), *v2=(expr2); \
  106. if(memcmp(v1,v2,(len))) { printf("."); } else { \
  107. have_failed = 1; \
  108. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
  109. __FILE__, \
  110. __LINE__, \
  111. PRETTY_FUNCTION, \
  112. #expr1, #expr2); \
  113. return; \
  114. } STMT_END
  115. #endif
  116. /*
  117. Local Variables:
  118. mode:c
  119. indent-tabs-mode:nil
  120. c-basic-offset:2
  121. End:
  122. */