test/string_tests.c

Fri, 18 Nov 2016 15:17:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 18 Nov 2016 15:17:04 +0100
changeset 228
9f385abc72fb
parent 225
a1a068c2c4ef
child 232
5f2d650eade7
permissions
-rw-r--r--

adds ucx_list_append_once() and ucx_list_prepend_once()

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2016 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(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(test_sstr_len) {
    sstr_t s1 = ST("1234");
    sstr_t s2 = ST(".:.:.");
    sstr_t s3 = ST("X");
    
    size_t len1 = sstrnlen(1, s1);
    size_t len2 = sstrnlen(2, s1, s2);
    size_t len3 = sstrnlen(3, s1, s2, s3);
    
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(len1 == 4, "sstrnlen returned wrong size");
    UCX_TEST_ASSERT(len2 == 9, "sstrnlen returned wrong size");
    UCX_TEST_ASSERT(len3 == 10, "sstrnlen returned wrong size");

    UCX_TEST_END
}


UCX_TEST(test_sstrchr_sstrrchr) {
    sstr_t str = ST("I will find you - and I will kill you");
    UCX_TEST_BEGIN
    
    sstr_t notfound = sstrchr(str, 'x');
    UCX_TEST_ASSERT(notfound.length == 0, "string length not 0");
    
    sstr_t result = sstrchr(str, 'w');
    UCX_TEST_ASSERT(result.length == 35, "sstrchr returned wrong length");
    UCX_TEST_ASSERT(strcmp("will find you - and I will kill you", result.ptr)
        == 0, "sstrchr did not return the expected string");
    
    result = sstrrchr(str, 'w');
    UCX_TEST_ASSERT(result.length == 13, "sstrrchr returned wrong length");
    UCX_TEST_ASSERT(strcmp("will kill you", result.ptr)
        == 0, "sstrrchr did not return the expected string");
    
    UCX_TEST_END
}

UCX_TEST(test_sstrstr) {
    sstr_t str = ST("find the match in this string");
    UCX_TEST_BEGIN
    
    sstr_t notfound = sstrstr(str, S("no match"));
    UCX_TEST_ASSERT(notfound.length == 0, "no match must return empty string");
    
    sstr_t result = sstrstr(str, S("match"));
    UCX_TEST_ASSERT(result.length == 20, "sstrstr returned wrong length");
    UCX_TEST_ASSERT(!strcmp("match in this string", result.ptr),
        "sstrstr did not return the expected string");
    
    result = sstrstr(str, S(""));
    UCX_TEST_ASSERT(result.length == str.length,
        "sstrstr with empty match string returned wrong length");
    UCX_TEST_ASSERT(!strcmp(str.ptr, result.ptr),
        "sstrstr with empty match string did not return the original string");
    
    UCX_TEST_END
}

UCX_TEST(test_sstrcmp) {
    sstr_t str = ST("compare this");
    
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(sstrcmp(str, S("")) == 1, "empty cmp failed");
    UCX_TEST_ASSERT(sstrcmp(S(""), S("")) == 0, "empty cmp false negative");
    UCX_TEST_ASSERT(sstrcmp(str, S("compare this")) == 0, "false negative");
    UCX_TEST_ASSERT(sstrcmp(str, S("Compare This")) != 0, "false positive");
    UCX_TEST_ASSERT(sstrcmp(str, S("compare tool")) < 0, "memcmp < 0 failed");
    UCX_TEST_ASSERT(sstrcmp(str, S("compare shit")) > 0, "memcmp > 0 failed");
    UCX_TEST_ASSERT(sstrcmp(str, S("compare this not")) < 0, "len < 0 failed");
    UCX_TEST_ASSERT(sstrcmp(str, S("compare")) > 0, "len > 0 failed");
    UCX_TEST_END
}

UCX_TEST(test_sstrcasecmp) {
    
    sstr_t str = ST("compare this");
    
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(sstrcasecmp(str, S("")) == 1, "empty cmp failed");
    UCX_TEST_ASSERT(sstrcasecmp(S(""), S("")) == 0, "empty cmp false negative");
    UCX_TEST_ASSERT(sstrcasecmp(str, S("compare this")) == 0, "false negative");
    UCX_TEST_ASSERT(sstrcasecmp(str, S("Compare This")) == 0,
        "not ignoring case");
    UCX_TEST_ASSERT(sstrcasecmp(str, S("compare tool")) < 0, "< 0 failed");
    UCX_TEST_ASSERT(sstrcasecmp(str, S("compare shit")) > 0, "> 0 failed");
    UCX_TEST_ASSERT(sstrcasecmp(str, S("compare this not")) < 0,
        "len < 0 failed");
    UCX_TEST_ASSERT(sstrcasecmp(str, S("compare")) > 0, "len > 0 failed");
    UCX_TEST_END
}


UCX_TEST(test_sstrcat) {
    sstr_t s1 = S("12");
    sstr_t s2 = S("34");
    sstr_t s3 = S("56");
    sstr_t sn = { NULL, 0 };
    
    UCX_TEST_BEGIN
    
    sstr_t t1 = sstrcat(2, s1, s2);
    UCX_TEST_ASSERT(!sstrcmp(t1, S("1234")), "t1: wrong content");
    free(t1.ptr);
    
    sstr_t t2 = sstrcat(3, s1, s2, s3);
    UCX_TEST_ASSERT(!sstrcmp(t2, S("123456")), "t2: wrong content");
    free(t2.ptr);
    
    sstr_t t3 = sstrcat(6, s1, sn, s2, sn, s3, sn);
    UCX_TEST_ASSERT(!sstrcmp(t3, S("123456")), "t3: wrong content");
    free(t3.ptr);
    
    sstr_t t4 = sstrcat(2, sn, sn);
    UCX_TEST_ASSERT(t4.ptr, "t4.ptr is NULL");
    UCX_TEST_ASSERT(t4.length == 0, "t4 has wrong length");
    free(t4.ptr);
    
            
    UCX_TEST_END
    
}

UCX_TEST(test_sstrsplit) {

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

    UCX_TEST_BEGIN

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

    /* no delimiter occurence (ndo) */
    n = 0;
    list = sstrsplit(test, S("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");
    for(int i=0;i<n;i++) {
        free(list[i].ptr);
    }
    free(list);

    /* partially matching delimiter (pmd) */
    n = 0;
    list = sstrsplit(test, S("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");
    for(int i=0;i<n;i++) {
        free(list[i].ptr);
    }
    free(list);

    /* matching single-char delimiter (mscd) */
    n = 0;
    list = sstrsplit(test, S(","), &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");
    for(int i=0;i<n;i++) {
        free(list[i].ptr);
    }
    free(list);

    /* matching multi-char delimiter (mmcd) */
    n = 0;
    list = sstrsplit(test, S("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");
    for(int i=0;i<n;i++) {
        free(list[i].ptr);
    }
    free(list);

    /* bounded list using single-char delimiter (blsc) */
    n = 3;
    list = sstrsplit(test, S(","), &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");
    for(int i=0;i<n;i++) {
        free(list[i].ptr);
    }
    free(list);

    /* bounded list using multi-char delimiter (blmc) */
    n = 2;
    list = sstrsplit(test, S("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");
    for(int i=0;i<n;i++) {
        free(list[i].ptr);
    }
    free(list);

    /* start with delimiter (swd) */
    n = 0;
    list = sstrsplit(test, S("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");
    for(int i=0;i<n;i++) {
        free(list[i].ptr);
    }
    free(list);

    /* end with delimiter (ewd) */
    n = 0;
    list = sstrsplit(test, S("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");
    for(int i=0;i<n;i++) {
        free(list[i].ptr);
    }
    free(list);

    /* exact match (exm) */
    n = 0;
    list = sstrsplit(test, S("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");
    for(int i=0;i<n;i++) {
        free(list[i].ptr);
    }
    free(list);

    /* substring (subs) */
    n = 0;
    list = sstrsplit(test, S("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");
    for(int i=0;i<n;i++) {
        free(list[i].ptr);
    }
    free(list);
    
    /* double encounter delimiter (ded) */
    n = 0;
    list = sstrsplit(test, S("is,"), &n);
    UCX_TEST_ASSERT(n == 3, "ded, list length must be 3");
    UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "ded, item 0 mismatch");
    UCX_TEST_ASSERT(list[1].length == 0, "ded, item 1 not empty!");
    UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0,
        "ded, item 2 mismatch");
    for(int i=0;i<n;i++) {
        free(list[i].ptr);
    }
    free(list);

    UCX_TEST_END
}

UCX_TEST(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
}

UCX_TEST(test_sstrprefixsuffix) {
    sstr_t str = ST("test my prefix and my suffix");
    sstr_t empty = ST("");
    
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(!sstrprefix(empty, S("pref")), "prefix empty string fails");
    UCX_TEST_ASSERT(!sstrsuffix(empty, S("suf")), "suffix empty string fails");
    
    UCX_TEST_ASSERT(sstrprefix(str, empty), "empty prefix fails");
    UCX_TEST_ASSERT(sstrsuffix(str, empty), "empty suffix fails");
    
    UCX_TEST_ASSERT(sstrprefix(empty, empty), "string and prefix empty fails");
    UCX_TEST_ASSERT(sstrsuffix(empty, empty), "string and suffix empty fails");
    
    UCX_TEST_ASSERT(sstrprefix(str, S("test ")), "prefix false negative");
    UCX_TEST_ASSERT(!sstrprefix(str, S("8-) fsck ")), "prefix false positive");
    
    UCX_TEST_ASSERT(sstrsuffix(str, S("fix")), "suffix false negative");
    UCX_TEST_ASSERT(!sstrsuffix(str, S("fox")), "suffix false positive");

    
    UCX_TEST_END
}

mercurial