curlcheck.h 4.5 KB

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