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