src/test/assert.c
author Tero Marttila <terom@fixme.fi>
Mon, 04 May 2009 20:55:04 +0300
branchnew-transport
changeset 168 a58ad50911fc
child 189 f351facab1f0
permissions -rw-r--r--
refactor test.c into tests/*
#include "assert.h"
#include "util.h"

#include <string.h>

#define ASSERT_FAIL(...) do { log_fatal(__VA_ARGS__); abort(); } while (0)

void assert_true (bool cond, const char *msg)
{
    if (!cond)
        ASSERT_FAIL("%s", msg);
}

void assert_null (const void *ptr)
{
    if (ptr)
        ASSERT_FAIL("%p != NULL", ptr);
}

void assert_strcmp (const char *is, const char *should_be)
{
    if (!is || strcmp(is, should_be))
        ASSERT_FAIL("%s != %s", dump_str(is), dump_str(should_be));
}

void assert_strncmp (const char *is, const char *should_be, size_t n)
{   
    if (!is || strncmp(is, should_be, n))
        ASSERT_FAIL("%s:%u != %s", dump_strn(is, n), (unsigned) n, dump_strn(should_be, n));
}

void assert_strlen (const char *str, size_t n)
{
    if (!str || strlen(str) != n)
        ASSERT_FAIL("strlen(%s) != %u", dump_str(str), (unsigned) n);
}

void assert_strnull (const char *str)
{
    if (str != NULL)
        ASSERT_FAIL("%s != NULL", dump_str(str));
}

void assert_success (err_t err)
{
    if (err != SUCCESS)
        ASSERT_FAIL("error: %s", error_name(err));
}

void assert_err (err_t err, err_t target)
{
    if (err != target)
        ASSERT_FAIL("error: <%s> != <%s>", error_name(err), error_name(target));
}

void assert_error (error_t *is, error_t *should_be)
{
    if (ERROR_CODE(is) != ERROR_CODE(should_be) || ERROR_EXTRA(is) != ERROR_EXTRA(should_be))
        // XXX: dual use of error_msg
        ASSERT_FAIL("error: <%s> != <%s>", error_msg(is), error_msg(should_be));
}