src/test/assert.c
changeset 189 f351facab1f0
parent 168 a58ad50911fc
child 194 808b1b047620
equal deleted inserted replaced
188:6fd4706a4180 189:f351facab1f0
     1 #include "assert.h"
     1 #include "assert.h"
     2 #include "util.h"
     2 #include "util.h"
       
     3 #include "backtrace.h"
     3 
     4 
     4 #include <string.h>
     5 #include <string.h>
     5 
     6 
     6 #define ASSERT_FAIL(...) do { log_fatal(__VA_ARGS__); abort(); } while (0)
     7 #define ASSERT_FAIL(...) do { log_fatal(__VA_ARGS__); test_backtrace(1); abort(); } while (0)
     7 
     8 
     8 void assert_true (bool cond, const char *msg)
     9 void assert_true (bool cond, const char *msg)
     9 {
    10 {
    10     if (!cond)
    11     if (!cond)
    11         ASSERT_FAIL("%s", msg);
    12         ASSERT_FAIL("%s", msg);
    17         ASSERT_FAIL("%p != NULL", ptr);
    18         ASSERT_FAIL("%p != NULL", ptr);
    18 }
    19 }
    19 
    20 
    20 void assert_strcmp (const char *is, const char *should_be)
    21 void assert_strcmp (const char *is, const char *should_be)
    21 {
    22 {
       
    23     if (!should_be && !is)
       
    24         return;
       
    25 
    22     if (!is || strcmp(is, should_be))
    26     if (!is || strcmp(is, should_be))
    23         ASSERT_FAIL("%s != %s", dump_str(is), dump_str(should_be));
    27         ASSERT_FAIL("%s != %s", dump_str(is), dump_str(should_be));
    24 }
    28 }
    25 
    29 
    26 void assert_strncmp (const char *is, const char *should_be, size_t n)
    30 void assert_strncmp (const char *is, const char *should_be, size_t n)
    27 {   
    31 {   
       
    32     if (!should_be && !is)
       
    33         return;
       
    34 
    28     if (!is || strncmp(is, should_be, n))
    35     if (!is || strncmp(is, should_be, n))
    29         ASSERT_FAIL("%s:%u != %s", dump_strn(is, n), (unsigned) n, dump_strn(should_be, n));
    36         ASSERT_FAIL("%s:%u != %s", dump_strn(is, n), (unsigned) n, dump_strn(should_be, n));
    30 }
    37 }
    31 
    38 
    32 void assert_strlen (const char *str, size_t n)
    39 void assert_strlen (const char *str, size_t n)