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