test.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright 2001,2002,2003 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #ifndef __TEST_H
  5. #define __TEST_H
  6. /**
  7. * \file test.h
  8. * \brief Headers for test.c
  9. */
  10. #include <string.h>
  11. #include <stdio.h>
  12. #define STMT_BEGIN do {
  13. #define STMT_END } while (0)
  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. __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. __FILE__, \
  35. __LINE__, \
  36. PRETTY_FUNCTION, \
  37. #expr); \
  38. return; \
  39. } STMT_END
  40. #define test_eq(expr1, expr2) \
  41. STMT_BEGIN \
  42. long v1=(long)(expr1), v2=(long)(expr2); \
  43. if(v1==v2) { printf("."); fflush(stdout); } else { \
  44. have_failed = 1; \
  45. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
  46. " (%ld != %ld)\n", \
  47. __FILE__, \
  48. __LINE__, \
  49. PRETTY_FUNCTION, \
  50. #expr1, #expr2, \
  51. v1, v2); \
  52. return; \
  53. } STMT_END
  54. #define test_neq(expr1, expr2) \
  55. STMT_BEGIN \
  56. long v1=(long)(expr1), v2=(long)(expr2); \
  57. if(v1!=v2) { printf("."); fflush(stdout); } else { \
  58. have_failed = 1; \
  59. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
  60. " (%ld == %ld)\n", \
  61. __FILE__, \
  62. __LINE__, \
  63. PRETTY_FUNCTION, \
  64. #expr1, #expr2, \
  65. v1, v2); \
  66. return; \
  67. } STMT_END
  68. #define test_streq(expr1, expr2) \
  69. STMT_BEGIN \
  70. const char *v1=(expr1), *v2=(expr2); \
  71. if(!strcmp(v1,v2)) { printf("."); fflush(stdout); } else { \
  72. have_failed = 1; \
  73. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
  74. " (\"%s\" != \"%s\")\n", \
  75. __FILE__, \
  76. __LINE__, \
  77. PRETTY_FUNCTION, \
  78. #expr1, #expr2, \
  79. v1, v2); \
  80. return; \
  81. } STMT_END
  82. #define test_strneq(expr1, expr2) \
  83. STMT_BEGIN \
  84. const char *v1=(expr1), *v2=(expr2); \
  85. if(strcmp(v1,v2)) { printf("."); fflush(stdout); } else { \
  86. have_failed = 1; \
  87. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
  88. " (\"%s\" == \"%s\")\n", \
  89. __FILE__, \
  90. __LINE__, \
  91. PRETTY_FUNCTION, \
  92. #expr1, #expr2, \
  93. v1, v2); \
  94. return; \
  95. } STMT_END
  96. #define test_memeq(expr1, expr2, len) \
  97. STMT_BEGIN \
  98. const void *v1=(expr1), *v2=(expr2); \
  99. if(!memcmp(v1,v2,(len))) { printf("."); fflush(stdout); } else { \
  100. have_failed = 1; \
  101. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \
  102. __FILE__, \
  103. __LINE__, \
  104. PRETTY_FUNCTION, \
  105. #expr1, #expr2); \
  106. return; \
  107. } STMT_END
  108. #define test_memneq(expr1, expr2, len) \
  109. STMT_BEGIN \
  110. void *v1=(expr1), *v2=(expr2); \
  111. if(memcmp(v1,v2,(len))) { printf("."); fflush(stdout); } else { \
  112. have_failed = 1; \
  113. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
  114. __FILE__, \
  115. __LINE__, \
  116. PRETTY_FUNCTION, \
  117. #expr1, #expr2); \
  118. return; \
  119. } STMT_END
  120. #endif