test/string_tests.c

Thu, 04 Oct 2012 14:08:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 04 Oct 2012 14:08:31 +0200
changeset 41
7f90a03e186e
parent 39
bf8ab7bb74ff
child 47
22fb97e9f58a
permissions
-rw-r--r--

simplified UCX_MAP_FOREACH

/*
 *
 */

#include "string_tests.h"

UCX_TEST_IMPLEMENT(test_sstrsplit) {

    const char *original = "this,is,a,csv,string";
    sstr_t test = sstr("this,is,a,csv,string"); /* use copy of original here */
    size_t n;
    sstr_t *list;

    UCX_TEST_BEGIN

    /* Nullpointer check */
    n = 0;
    UCX_TEST_ASSERT(sstrsplit(test, ST(""), &n) == NULL,
            "empty delimiter must return NULL");

    /* no delimiter occurence (ndo) */
    n = 0;
    list = sstrsplit(test, ST("z"), &n);
    UCX_TEST_ASSERT(n == 1, "ndo, list length must be 1");
    UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "ndo, "
            "original string shall be returned as single list element");
    UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
                "ndo, original has been modified");
    free(list);

    /* partially matching delimiter (pmd) */
    n = 0;
    list = sstrsplit(test, ST("stringbuilder"), &n);
    UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1");
    UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, "
            "original string shall be returned as single list element");
    UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
                "pmd, original has been modified");
    free(list);

    /* matching single-char delimiter (mscd) */
    n = 0;
    list = sstrsplit(test, ST(","), &n);
    UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5");
    UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch");
    UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch");
    UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch");
    UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch");
    UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch");
    UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
            "mscd, original has been modified");
    free(list);

    /* matching multi-char delimiter (mmcd) */
    n = 0;
    list = sstrsplit(test, ST("is"), &n);
    UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3");
    UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch");
    UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch");
    UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0,
            "mmcd, item 2 mismatch");
    UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
            "mmcd, original has been modified");
    free(list);

    /* bounded list using single-char delimiter (blsc) */
    n = 3;
    list = sstrsplit(test, ST(","), &n);
    UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3");
    UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch");
    UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch");
    UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0,
            "blsc, item 2 mismatch");
    UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
            "blsc, original has been modified");
    free(list);

    /* bounded list using multi-char delimiter (blmc) */
    n = 2;
    list = sstrsplit(test, ST("is"), &n);
    UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2");
    UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch");
    UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
            "blmc, item 1 mismatch");
    UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
            "blmc, original has been modified");
    free(list);

    /* start with delimiter (swd) */
    n = 0;
    list = sstrsplit(test, ST("this"), &n);
    UCX_TEST_ASSERT(n == 2, "swd, list length must be 2");
    UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty");
    UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
            "swd, second item corrupt");
    UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
            "swd, original has been modified");
    free(list);

    /* end with delimiter (ewd) */
    n = 0;
    list = sstrsplit(test, ST("string"), &n);
    UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2");
    UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0,
            "swd, first item corrupt");
    UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty");
    UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
            "ewd, original has been modified");
    free(list);

    /* exact match (exm) */
    n = 0;
    list = sstrsplit(test, ST("this,is,a,csv,string"), &n);
    UCX_TEST_ASSERT(n == 1, "exm, list length must be 1");
    UCX_TEST_ASSERT(list[0].length == 0, "exm, single item must be empty");
    UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
            "exm, original has been modified");
    free(list);

    /* substring (subs) */
    n = 0;
    list = sstrsplit(test, ST("this,is,a,csv,string,with,extension"), &n);
    UCX_TEST_ASSERT(n == 1, "subs, list length must be 1");
    UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0,
            "subs, single item must be the original string");
    UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
            "subs, original has been modified");
    free(list);

    UCX_TEST_END
}

mercurial