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