view tests.c @ 261:f1decaee6157

Improve tests.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 17 Feb 2016 12:59:55 +0200
parents 33652234f162
children 423771158575
line wrap: on
line source

#include "th_types.h"
#include "th_util.h"
#include "th_string.h"
#include "th_crypto.h"

#define SET_BUF_SIZE 128

char *test_str_header = NULL,
     *test_str_res = NULL;

int tests_failed, tests_passed, tests_total;

char buf1[SET_BUF_SIZE+2], buf2[SET_BUF_SIZE+2];


void test_start_v(const char *fmt, va_list ap)
{
    tests_total++;

    th_free_r(&test_str_header);
    th_free_r(&test_str_res);

    test_str_header = th_strdup_vprintf(fmt, ap);
}


void test_start(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    test_start_v(fmt, ap);
    va_end(ap);
}


void test_result_msg_v(BOOL check, const char *fmt, va_list ap)
{
    if (check)
    {
        THPRINT(1, "%s: OK\n", test_str_header);
        tests_passed++;
    }
    else
    {
        THPRINT(0, "%s: ", test_str_header);
        if (fmt != NULL)
            THPRINT_V(0, fmt, ap);
        THPRINT(0, "\n%s\n", test_str_res);
        tests_failed++;
    }
}


void test_result_msg(BOOL check, const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    test_result_msg_v(check, fmt, ap);
    va_end(ap);
}


void test_result(BOOL check)
{
    test_result_msg_v(check, NULL, NULL);
}


#define SET_SENTINEL_BYTE 0x0e5

void test_snprintf_do(size_t len, const char *fmt, va_list ap, const char *msg)
{
    int ret1, ret2;
    va_list tmp;

    test_start("th_vsnprintf('%s', %s)", fmt, msg);

    memset(buf1, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf1[SET_BUF_SIZE+1] = 0;
    memset(buf2, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf2[SET_BUF_SIZE+1] = 0;

    va_copy(tmp, ap);

    ret1 = th_vsnprintf(buf1, len, fmt, ap);
    ret2 = vsnprintf(buf2, len, fmt, tmp);

    test_result_msg(ret1 == ret2, "retval mismatch %d != %d", ret1, ret2);
    test_result_msg(strcmp(buf1, buf2) == 0, "result mismatch '%s' != '%s'", buf1, buf2);
    
    test_result_msg((unsigned char) buf1[len] == SET_SENTINEL_BYTE, "buffer #1 overflow, sentinel 0x%02x", buf1[len]);
    test_result_msg((unsigned char) buf2[len] == SET_SENTINEL_BYTE, "buffer #2 overflow, sentinel 0x%02x", buf2[len]);

    test_start("th_strdup_vprintf('%s')", fmt);
    char *str = th_strdup_vprintf(fmt, ap);
    test_result_msg(str != NULL, "result NULL");
    th_free(str);
}


void test_snprintf(const char *fmt, ...)
{
    va_list ap, tmp;
    THPRINT(1, "----------------------------------------------\n");
    va_start(ap, fmt);
    va_copy(tmp, ap); test_snprintf_do(0, fmt, tmp, "0");
    va_copy(tmp, ap); test_snprintf_do(1, fmt, tmp, "1");
    va_copy(tmp, ap); test_snprintf_do(2, fmt, tmp, "2");
    va_copy(tmp, ap); test_snprintf_do(16, fmt, tmp, "16");
    va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, fmt, tmp, "SET_BUF_SIZE");
    va_end(ap); 
    THPRINT(1, "----------------------------------------------\n");
}


int main(int argc, char *argv[])
{
    (void) argc;
    (void) argv;

    //
    // Initialization
    //
    th_init("th-test", "th-libs unit tests", "0.0.1", NULL, NULL);
    th_verbosityLevel = 0;

    if (sizeof(char) != sizeof(unsigned char))
    {
        THERR("sizeof(char) != sizeof(unsigned char)???\n");
        return -1;
    }

    tests_failed = tests_passed = tests_total = 0;

    //
    // Test series #1
    //
    THPRINT(0, "printf() family function tests.\n");
    int i_vals[] = { 2, 612342, -2, -612342, 0x1fff, 0x8000000, };
    char *i_fmts[] = { "%d", "%x", "%05d", "%5d", "%-5d", "%05x", "%5x", "", };
    size_t i1, i2;

    for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++)
    for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++)
        test_snprintf(i_fmts[i2], i_vals);

    char *s_vals[] = { "", "asdf", "xxx yyy zzz ppp fff", NULL, "X", "abcde", };
    char *s_fmts[] = { "%s", "%2s", "%-2s", "%5s", "%-5s", "%16s", "%-16s", "%1s", "%-1s", };

    for (i1 = 0; i1 < sizeof(s_vals) / sizeof(s_vals[0]); i1++)
    for (i2 = 0; i2 < sizeof(s_fmts) / sizeof(s_fmts[0]); i2++)
        test_snprintf(s_fmts[i2], s_vals);

    test_snprintf("a%cBC", 'x');
    test_snprintf("%c", 'x');
    test_snprintf("", 'x');

    THPRINT(0, "printf() family function tests.\n");

    //
    // Print summary and exit
    //
    THPRINT(0, "%d tests failed, %d passed (%d main tests).\n",
        tests_failed, tests_passed, tests_total);

    return 0;
}