src/test/assert.c
branchnew-transport
changeset 168 a58ad50911fc
child 189 f351facab1f0
equal deleted inserted replaced
167:0d2d8ca879d8 168:a58ad50911fc
       
     1 #include "assert.h"
       
     2 #include "util.h"
       
     3 
       
     4 #include <string.h>
       
     5 
       
     6 #define ASSERT_FAIL(...) do { log_fatal(__VA_ARGS__); abort(); } while (0)
       
     7 
       
     8 void assert_true (bool cond, const char *msg)
       
     9 {
       
    10     if (!cond)
       
    11         ASSERT_FAIL("%s", msg);
       
    12 }
       
    13 
       
    14 void assert_null (const void *ptr)
       
    15 {
       
    16     if (ptr)
       
    17         ASSERT_FAIL("%p != NULL", ptr);
       
    18 }
       
    19 
       
    20 void assert_strcmp (const char *is, const char *should_be)
       
    21 {
       
    22     if (!is || strcmp(is, should_be))
       
    23         ASSERT_FAIL("%s != %s", dump_str(is), dump_str(should_be));
       
    24 }
       
    25 
       
    26 void assert_strncmp (const char *is, const char *should_be, size_t n)
       
    27 {   
       
    28     if (!is || strncmp(is, should_be, n))
       
    29         ASSERT_FAIL("%s:%u != %s", dump_strn(is, n), (unsigned) n, dump_strn(should_be, n));
       
    30 }
       
    31 
       
    32 void assert_strlen (const char *str, size_t n)
       
    33 {
       
    34     if (!str || strlen(str) != n)
       
    35         ASSERT_FAIL("strlen(%s) != %u", dump_str(str), (unsigned) n);
       
    36 }
       
    37 
       
    38 void assert_strnull (const char *str)
       
    39 {
       
    40     if (str != NULL)
       
    41         ASSERT_FAIL("%s != NULL", dump_str(str));
       
    42 }
       
    43 
       
    44 void assert_success (err_t err)
       
    45 {
       
    46     if (err != SUCCESS)
       
    47         ASSERT_FAIL("error: %s", error_name(err));
       
    48 }
       
    49 
       
    50 void assert_err (err_t err, err_t target)
       
    51 {
       
    52     if (err != target)
       
    53         ASSERT_FAIL("error: <%s> != <%s>", error_name(err), error_name(target));
       
    54 }
       
    55 
       
    56 void assert_error (error_t *is, error_t *should_be)
       
    57 {
       
    58     if (ERROR_CODE(is) != ERROR_CODE(should_be) || ERROR_EXTRA(is) != ERROR_EXTRA(should_be))
       
    59         // XXX: dual use of error_msg
       
    60         ASSERT_FAIL("error: <%s> != <%s>", error_msg(is), error_msg(should_be));
       
    61 }
       
    62 
       
    63