test.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Copyright 2001,2002,2003 Roger Dingledine.
  2. * Copyright 2004-2005 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 Headers for test.c
  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(expr1, expr2) \
  44. STMT_BEGIN \
  45. long v1=(long)(expr1), v2=(long)(expr2); \
  46. if (v1==v2) { printf("."); fflush(stdout); } else { \
  47. have_failed = 1; \
  48. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
  49. " (%ld != %ld)\n", \
  50. _SHORT_FILE_, \
  51. __LINE__, \
  52. PRETTY_FUNCTION, \
  53. #expr1, #expr2, \
  54. v1, v2); \
  55. return; \
  56. } STMT_END
  57. #define test_neq(expr1, expr2) \
  58. STMT_BEGIN \
  59. long v1=(long)(expr1), v2=(long)(expr2); \
  60. if (v1!=v2) { printf("."); fflush(stdout); } else { \
  61. have_failed = 1; \
  62. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
  63. " (%ld == %ld)\n", \
  64. _SHORT_FILE_, \
  65. __LINE__, \
  66. PRETTY_FUNCTION, \
  67. #expr1, #expr2, \
  68. v1, v2); \
  69. return; \
  70. } STMT_END
  71. #define test_streq(expr1, expr2) \
  72. STMT_BEGIN \
  73. const char *v1=(expr1), *v2=(expr2); \
  74. if (!strcmp(v1,v2)) { printf("."); fflush(stdout); } else { \
  75. have_failed = 1; \
  76. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
  77. " (\"%s\" != \"%s\")\n", \
  78. _SHORT_FILE_, \
  79. __LINE__, \
  80. PRETTY_FUNCTION, \
  81. #expr1, #expr2, \
  82. v1, v2); \
  83. return; \
  84. } STMT_END
  85. #define test_strneq(expr1, expr2) \
  86. STMT_BEGIN \
  87. const char *v1=(expr1), *v2=(expr2); \
  88. if (strcmp(v1,v2)) { printf("."); fflush(stdout); } else { \
  89. have_failed = 1; \
  90. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
  91. " (\"%s\" == \"%s\")\n", \
  92. _SHORT_FILE_, \
  93. __LINE__, \
  94. PRETTY_FUNCTION, \
  95. #expr1, #expr2, \
  96. v1, v2); \
  97. return; \
  98. } STMT_END
  99. #define test_memeq(expr1, expr2, len) \
  100. STMT_BEGIN \
  101. const void *v1=(expr1), *v2=(expr2); \
  102. if (!memcmp(v1,v2,(len))) { printf("."); fflush(stdout); } else {\
  103. have_failed = 1; \
  104. printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \
  105. _SHORT_FILE_, \
  106. __LINE__, \
  107. PRETTY_FUNCTION, \
  108. #expr1, #expr2); \
  109. return; \
  110. } STMT_END
  111. #define test_memneq(expr1, expr2, len) \
  112. STMT_BEGIN \
  113. void *v1=(expr1), *v2=(expr2); \
  114. if (memcmp(v1,v2,(len))) { printf("."); fflush(stdout); } else {\
  115. have_failed = 1; \
  116. printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
  117. _SHORT_FILE_, \
  118. __LINE__, \
  119. PRETTY_FUNCTION, \
  120. #expr1, #expr2); \
  121. return; \
  122. } STMT_END
  123. #endif