curlcheck.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. /* The fail macros mark the current test step as failed, and continue */
  26. #define fail_if(expr, msg) \
  27. do { \
  28. if(expr) { \
  29. fprintf(stderr, "%s:%d Assertion '%s' met: %s\n", \
  30. __FILE__, __LINE__, #expr, msg); \
  31. unitfail++; \
  32. } \
  33. } while(0)
  34. #define fail_unless(expr, msg) \
  35. do { \
  36. if(!(expr)) { \
  37. fprintf(stderr, "%s:%d Assertion '%s' failed: %s\n", \
  38. __FILE__, __LINE__, #expr, msg); \
  39. unitfail++; \
  40. } \
  41. } while(0)
  42. #define verify_memory(dynamic, check, len) \
  43. do { \
  44. if(dynamic && memcmp(dynamic, check, len)) { \
  45. fprintf(stderr, "%s:%d Memory buffer mismatch size %d. '%s' is not\n", \
  46. __FILE__, __LINE__, len, \
  47. hexdump((const unsigned char *)check, len)); \
  48. fprintf(stderr, "%s:%d the same as '%s'\n", __FILE__, __LINE__, \
  49. hexdump((const unsigned char *)dynamic, len)); \
  50. unitfail++; \
  51. } \
  52. } while(0)
  53. /* fail() is for when the test case figured out by itself that a check
  54. proved a failure */
  55. #define fail(msg) do { \
  56. fprintf(stderr, "%s:%d test failed: '%s'\n", \
  57. __FILE__, __LINE__, msg); \
  58. unitfail++; \
  59. } while(0)
  60. /* The abort macros mark the current test step as failed, and exit the test */
  61. #define abort_if(expr, msg) \
  62. do { \
  63. if(expr) { \
  64. fprintf(stderr, "%s:%d Abort assertion '%s' met: %s\n", \
  65. __FILE__, __LINE__, #expr, msg); \
  66. unitfail++; \
  67. goto unit_test_abort; \
  68. } \
  69. } while(0)
  70. #define abort_unless(expr, msg) \
  71. do { \
  72. if(!(expr)) { \
  73. fprintf(stderr, "%s:%d Abort assertion '%s' failed: %s\n", \
  74. __FILE__, __LINE__, #expr, msg); \
  75. unitfail++; \
  76. goto unit_test_abort; \
  77. } \
  78. } while(0)
  79. #define abort_test(msg) \
  80. do { \
  81. fprintf(stderr, "%s:%d test aborted: '%s'\n", \
  82. __FILE__, __LINE__, msg); \
  83. unitfail++; \
  84. goto unit_test_abort; \
  85. } while(0)
  86. extern int unitfail;
  87. #define UNITTEST_START \
  88. int test(char *arg) \
  89. { \
  90. (void)arg; \
  91. if(unit_setup()) { \
  92. fail("unit_setup() failure"); \
  93. } \
  94. else {
  95. #define UNITTEST_STOP \
  96. goto unit_test_abort; /* avoid warning */ \
  97. unit_test_abort: \
  98. unit_stop(); \
  99. } \
  100. return unitfail; \
  101. }