xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 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 | |
| 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 | fprintf(stderr, "%s:%d 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 | fprintf(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 | fprintf(stderr, "%s:%d Memory buffer mismatch size %d. '%s' is not\n", \ |
| 49 | __FILE__, __LINE__, len, \ |
| 50 | hexdump((const unsigned char *)check, len)); \ |
| 51 | fprintf(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 | fprintf(stderr, "%s:%d test failed: '%s'\n", \ |
| 61 | __FILE__, __LINE__, msg); \ |
| 62 | unitfail++; \ |
| 63 | } while(0) |
| 64 | |
| 65 | |
| 66 | /* The abort macros mark the current test step as failed, and exit the test */ |
| 67 | #define abort_if(expr, msg) \ |
| 68 | do { \ |
| 69 | if(expr) { \ |
| 70 | fprintf(stderr, "%s:%d Abort assertion '%s' met: %s\n", \ |
| 71 | __FILE__, __LINE__, #expr, msg); \ |
| 72 | unitfail++; \ |
| 73 | goto unit_test_abort; \ |
| 74 | } \ |
| 75 | } while(0) |
| 76 | |
| 77 | #define abort_unless(expr, msg) \ |
| 78 | do { \ |
| 79 | if(!(expr)) { \ |
| 80 | fprintf(stderr, "%s:%d Abort assertion '%s' failed: %s\n", \ |
| 81 | __FILE__, __LINE__, #expr, msg); \ |
| 82 | unitfail++; \ |
| 83 | goto unit_test_abort; \ |
| 84 | } \ |
| 85 | } while(0) |
| 86 | |
| 87 | #define abort_test(msg) \ |
| 88 | do { \ |
| 89 | fprintf(stderr, "%s:%d test aborted: '%s'\n", \ |
| 90 | __FILE__, __LINE__, msg); \ |
| 91 | unitfail++; \ |
| 92 | goto unit_test_abort; \ |
| 93 | } while(0) |
| 94 | |
| 95 | |
| 96 | extern int unitfail; |
| 97 | |
| 98 | #define UNITTEST_START \ |
| 99 | int test(char *arg) \ |
| 100 | { \ |
| 101 | (void)arg; \ |
| 102 | if(unit_setup()) { \ |
| 103 | fail("unit_setup() failure"); \ |
| 104 | } \ |
| 105 | else { |
| 106 | |
| 107 | #define UNITTEST_STOP \ |
| 108 | goto unit_test_abort; /* avoid warning */ \ |
| 109 | unit_test_abort: \ |
| 110 | unit_stop(); \ |
| 111 | } \ |
| 112 | return unitfail; \ |
| 113 | } |