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