test/string_tests.c

Fri, 12 Jul 2013 20:50:18 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 12 Jul 2013 20:50:18 +0200
changeset 108
d2b1e67b2b48
parent 104
9d3dea320d8e
child 116
234920008754
permissions
-rw-r--r--

new properties parser

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "string_tests.h"

UCX_TEST_IMPLEMENT(test_sstr) {
    sstr_t s1 = sstr((char*)"1234");
    sstr_t s2 = sstrn((char*)"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 = S("1234");
    sstr_t s2 = S(".:.:.");
    sstr_t s3 = S("X");
    
    size_t len = sstrnlen(3, s1, s2, s3);
    sstr_t cat;
    cat.ptr = (char*) malloc(16);
    cat.length = 16;
    cat = sstrncat(3, cat, s1, s2, s3);
    
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(len == 10, "sstrnlen returned wrong size");
    
    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_ASSERT(cat.length == 10, "sstrncat, wrong length");
    
    UCX_TEST_END
    
    free(cat.ptr);
}

UCX_TEST_IMPLEMENT(test_sstrsplit) {

    const char *original = "this,is,a,csv,string";
    sstr_t test = S("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 == 0, "exm, list length must be 0");
    UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL");
    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
}

UCX_TEST_IMPLEMENT(test_sstrtrim) {
    sstr_t t1 = sstrtrim(sstr((char*)"  ein test   "));
    sstr_t t2 = sstrtrim(sstr((char*)"abc"));
    sstr_t t3 = sstrtrim(sstr((char*)" 123"));
    sstr_t t4 = sstrtrim(sstr((char*)"xyz "));
    sstr_t t5 = sstrtrim(sstr((char*)"   "));
    sstr_t empty = sstrtrim(sstr((char*)""));
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(strncmp(t1.ptr, "ein test", t1.length) == 0, "failed");
    UCX_TEST_ASSERT(strncmp(t2.ptr, "abc", t2.length) == 0, "failed");
    UCX_TEST_ASSERT(strncmp(t3.ptr, "123", t3.length) == 0, "failed");
    UCX_TEST_ASSERT(strncmp(t4.ptr, "xyz", t4.length) == 0, "failed");
    UCX_TEST_ASSERT(t5.length == 0, "string t5 not empty");
    UCX_TEST_ASSERT(empty.length == 0, "empty string failed");
    UCX_TEST_END
}

mercurial