test/string_tests.c

Wed, 10 Oct 2012 14:18:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Oct 2012 14:18:06 +0200
changeset 64
16590c9c497c
parent 47
22fb97e9f58a
child 69
fb59270b1de3
permissions
-rw-r--r--

added autoextend feature to buffer

and we celebrate the 50th test case

/*
 *
 */

#include "string_tests.h"

UCX_TEST_IMPLEMENT(test_sstr) {
    sstr_t s1 = sstr("1234");
    sstr_t s2 = sstrn("ab", 2);
    
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(s1.length == 4, "s1 length must be 4");
    UCX_TEST_ASSERT(s2.length == 2, "s2 length must be 2");
    
    UCX_TEST_END
}

UCX_TEST_IMPLEMENT(test_sstr_len_cat) {
    sstr_t s1 = sstr("1234");
    sstr_t s2 = sstr(".:.:.");
    sstr_t s3 = sstr("X");
    
    sstr_t cat;
    
    size_t len = sstrnlen(3, s1, s2, s3);
    
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(len == s1.length + s2.length + s3.length,
            "sstrnlen returned wrong size");
    
    len = s1.length + s2.length + s3.length;
    
    cat.ptr = malloc(len + 1);
    cat.ptr[len] = 0;
    cat.length = len;
    
    sstrncat(3, cat, s1, s2, s3);
    
    UCX_TEST_ASSERT(cat.ptr[0] == '1', "sstrncat, wrong content");
    UCX_TEST_ASSERT(cat.ptr[1] == '2', "sstrncat, wrong content");
    UCX_TEST_ASSERT(cat.ptr[2] == '3', "sstrncat, wrong content");
    UCX_TEST_ASSERT(cat.ptr[3] == '4', "sstrncat, wrong content");
    UCX_TEST_ASSERT(cat.ptr[4] == '.', "sstrncat, wrong content");
    UCX_TEST_ASSERT(cat.ptr[8] == '.', "sstrncat, wrong content");
    UCX_TEST_ASSERT(cat.ptr[9] == 'X', "sstrncat, wrong content");
    
    UCX_TEST_END
    
    free(cat.ptr);
}

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