test.h 6.5 KB

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