xref: /curl/tests/unit/curlcheck.h (revision 25cbc2f7)
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 "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 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       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 FAILED match size %d. "      \
49               "'%s' is not\n", __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 #define UNITTEST_START                          \
97   CURLcode test(char *arg)                      \
98   {                                             \
99     (void)arg;                                  \
100     if(unit_setup()) {                          \
101       fail("unit_setup() FAILURE");             \
102     }                                           \
103     else {
104 
105 #define UNITTEST_STOP                           \
106     goto unit_test_abort; /* avoid warning */   \
107 unit_test_abort:                                \
108     unit_stop();                                \
109   }                                             \
110   return (CURLcode)unitfail;                    \
111   }
112