src/test/str.c
author Tero Marttila <terom@fixme.fi>
Mon, 04 May 2009 20:55:04 +0300
branchnew-transport
changeset 168 a58ad50911fc
child 171 b54f393c3df0
permissions -rw-r--r--
refactor test.c into tests/*
/**
 * @file
 *
 * Test functions for the str module.
 */
#include "assert.h"

void assert_str_quote (size_t buf_size, const char *data, ssize_t len, const char *target, size_t out)
{
    char buf[buf_size];
    
    size_t ret = str_quote(buf, buf_size, data, len);

    log_debug("str_quote(%zu, %zd) -> %s:%zu / %s:%zu", buf_size, len, buf, ret, target, out);

    assert_strcmp(buf, target);
    assert(ret == out);
}

void test_str_quote (void)
{
    log_info("testing str_quote()");

    assert_str_quote(5,     NULL,           -1,     "NULL",         4   );
    assert_str_quote(16,    "foo",          -1,     "'foo'",        5   );
    assert_str_quote(16,    "foobar",       3,      "'foo'",        5   );
    assert_str_quote(16,    "\r\n",         -1,     "'\\r\\n'",     6   );
    assert_str_quote(16,    "\x13",         -1,     "'\\x13'",      6   );
    assert_str_quote(16,    "x'y",          -1,     "'x\\'y'",      6   );
    assert_str_quote(7,     "1234567890",   -1,     "'1'...",       12  );
    assert_str_quote(9,     "1234567890",   -1,     "'123'...",     12  );
}

struct str_format_ctx {
    const char *name;

    const char *value;
};

err_t test_str_format_cb (const char *name, const char **value, ssize_t *value_len, void *arg)
{
    struct str_format_ctx *ctx = arg;

    assert_strcmp(name, ctx->name);

    *value = ctx->value;
    *value_len = -1;

    return SUCCESS;
}

void assert_str_format (const char *format, const char *name, const char *value, const char *out)
{
    struct str_format_ctx ctx = { name, value };
    char buf[512];

    assert_success(str_format(buf, sizeof(buf), format, test_str_format_cb, &ctx));
    
    log_debug("str_format(%s), { %s:%s } -> %s / %s", format, name, value, buf, out);

    assert_strcmp(buf, out);
}

void test_str_format (void)
{
    log_info("test str_format()");

    assert_str_format("foo", NULL, NULL, "foo");
    assert_str_format("foo {bar} quux", "bar", "XXX", "foo XXX quux");
}

void test_dump_str (void)
{
    log_info("dumping example strings on stdout:");

    log_debug("normal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