curlcheck.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "test.h"
  23. /* The fail macros mark the current test step as failed, and continue */
  24. #define fail_if(expr, msg) \
  25. do { \
  26. if(expr) { \
  27. fprintf(stderr, "%s:%d Assertion '%s' met: %s\n", \
  28. __FILE__, __LINE__, #expr, msg); \
  29. unitfail++; \
  30. } \
  31. } while(0)
  32. #define fail_unless(expr, msg) \
  33. do { \
  34. if(!(expr)) { \
  35. fprintf(stderr, "%s:%d Assertion '%s' failed: %s\n", \
  36. __FILE__, __LINE__, #expr, msg); \
  37. unitfail++; \
  38. } \
  39. } while(0)
  40. #define verify_memory(dynamic, check, len) \
  41. do { \
  42. if(dynamic && memcmp(dynamic, check, len)) { \
  43. fprintf(stderr, "%s:%d Memory buffer mismatch size %d. '%s' is not\n", \
  44. __FILE__, __LINE__, len, \
  45. hexdump((const unsigned char *)check, len)); \
  46. fprintf(stderr, "%s:%d the same as '%s'\n", __FILE__, __LINE__, \
  47. hexdump((const unsigned char *)dynamic, len)); \
  48. unitfail++; \
  49. } \
  50. } while(0)
  51. /* fail() is for when the test case figured out by itself that a check
  52. proved a failure */
  53. #define fail(msg) do { \
  54. fprintf(stderr, "%s:%d test failed: '%s'\n", \
  55. __FILE__, __LINE__, msg); \
  56. unitfail++; \
  57. } while(0)
  58. /* The abort macros mark the current test step as failed, and exit the test */
  59. #define abort_if(expr, msg) \
  60. do { \
  61. if(expr) { \
  62. fprintf(stderr, "%s:%d Abort assertion '%s' met: %s\n", \
  63. __FILE__, __LINE__, #expr, msg); \
  64. unitfail++; \
  65. goto unit_test_abort; \
  66. } \
  67. } while(0)
  68. #define abort_unless(expr, msg) \
  69. do { \
  70. if(!(expr)) { \
  71. fprintf(stderr, "%s:%d Abort assertion '%s' failed: %s\n", \
  72. __FILE__, __LINE__, #expr, msg); \
  73. unitfail++; \
  74. goto unit_test_abort; \
  75. } \
  76. } while(0)
  77. #define abort_test(msg) \
  78. do { \
  79. fprintf(stderr, "%s:%d test aborted: '%s'\n", \
  80. __FILE__, __LINE__, msg); \
  81. unitfail++; \
  82. goto unit_test_abort; \
  83. } while(0)
  84. extern int unitfail;
  85. #define UNITTEST_START \
  86. int test(char *arg) \
  87. { \
  88. (void)arg; \
  89. if(unit_setup()) { \
  90. fail("unit_setup() failure"); \
  91. } \
  92. else {
  93. #define UNITTEST_STOP \
  94. goto unit_test_abort; /* avoid warning */ \
  95. unit_test_abort: \
  96. unit_stop(); \
  97. } \
  98. return unitfail; \
  99. }