unitcheck.h (5972B)
1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 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 "first.h" 25 26 /* The fail macros mark the current test step as failed, and continue */ 27 #define fail_if(expr, msg) \ 28 do { \ 29 if(expr) { \ 30 curl_mfprintf(stderr, "%s:%d FAILED Assertion '%s' met: %s\n", \ 31 __FILE__, __LINE__, #expr, msg); \ 32 unitfail++; \ 33 } \ 34 } while(0) 35 36 #define fail_unless(expr, msg) \ 37 do { \ 38 if(!(expr)) { \ 39 curl_mfprintf(stderr, "%s:%d Assertion '%s' FAILED: %s\n", \ 40 __FILE__, __LINE__, #expr, msg); \ 41 unitfail++; \ 42 } \ 43 } while(0) 44 45 #define verify_memory(dynamic, check, len) \ 46 do { \ 47 if(dynamic && memcmp(dynamic, check, len)) { \ 48 curl_mfprintf(stderr, "%s:%d Memory buffer FAILED match size %d. " \ 49 "'%s' is not\n", __FILE__, __LINE__, len, \ 50 hexdump((const unsigned char *)check, len)); \ 51 curl_mfprintf(stderr, "%s:%d the same as '%s'\n", __FILE__, __LINE__, \ 52 hexdump((const unsigned char *)dynamic, len)); \ 53 unitfail++; \ 54 } \ 55 } while(0) 56 57 /* fail() is for when the test case figured out by itself that a check 58 proved a failure */ 59 #define fail(msg) do { \ 60 curl_mfprintf(stderr, "%s:%d test FAILED: '%s'\n", \ 61 __FILE__, __LINE__, msg); \ 62 unitfail++; \ 63 } while(0) 64 65 /* The abort macros mark the current test step as failed, and exit the test */ 66 #define abort_if(expr, msg) \ 67 do { \ 68 if(expr) { \ 69 curl_mfprintf(stderr, "%s:%d ABORT assertion '%s' met: %s\n", \ 70 __FILE__, __LINE__, #expr, msg); \ 71 unitfail++; \ 72 goto unit_test_abort; \ 73 } \ 74 } while(0) 75 76 #define abort_unless(expr, msg) \ 77 do { \ 78 if(!(expr)) { \ 79 curl_mfprintf(stderr, "%s:%d ABORT assertion '%s' failed: %s\n", \ 80 __FILE__, __LINE__, #expr, msg); \ 81 unitfail++; \ 82 goto unit_test_abort; \ 83 } \ 84 } while(0) 85 86 #define unittest_abort(msg) \ 87 do { \ 88 curl_mfprintf(stderr, "%s:%d test ABORTED: '%s'\n", \ 89 __FILE__, __LINE__, msg); \ 90 unitfail++; \ 91 goto unit_test_abort; \ 92 } while(0) 93 94 95 #define UNITTEST_BEGIN_SIMPLE \ 96 (void)arg; \ 97 { 98 99 #define UNITTEST_END_SIMPLE \ 100 goto unit_test_abort; /* avoid warning */ \ 101 } \ 102 unit_test_abort: \ 103 return (CURLcode)unitfail; 104 105 #define UNITTEST_BEGIN(setupfunc) \ 106 (void)arg; \ 107 if(setupfunc) { \ 108 fail("unit_setup() FAILURE"); \ 109 return (CURLcode)unitfail; \ 110 } \ 111 { 112 113 #define UNITTEST_END(stopfunc) \ 114 goto unit_test_abort; /* avoid warning */ \ 115 } \ 116 unit_test_abort: \ 117 stopfunc; \ 118 return (CURLcode)unitfail;