universe@39: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@39: * universe@103: * Copyright 2013 Olaf Wintermann. All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. universe@39: */ universe@39: universe@39: #include "string_tests.h" universe@39: universe@134: UCX_TEST(test_sstr) { universe@74: sstr_t s1 = sstr((char*)"1234"); universe@74: sstr_t s2 = sstrn((char*)"ab", 2); olaf@47: olaf@47: UCX_TEST_BEGIN olaf@47: olaf@47: UCX_TEST_ASSERT(s1.length == 4, "s1 length must be 4"); olaf@47: UCX_TEST_ASSERT(s2.length == 2, "s2 length must be 2"); olaf@47: olaf@47: UCX_TEST_END olaf@47: } olaf@47: universe@134: UCX_TEST(test_sstr_len_cat) { universe@116: sstr_t s1 = ST("1234"); universe@116: sstr_t s2 = ST(".:.:."); universe@116: sstr_t s3 = ST("X"); olaf@47: universe@100: size_t len = sstrnlen(3, s1, s2, s3); olaf@47: sstr_t cat; universe@100: cat.ptr = (char*) malloc(16); universe@100: cat.length = 16; universe@123: cat = sstrncat(cat, 3, s1, s2, s3); olaf@47: olaf@47: UCX_TEST_BEGIN olaf@47: universe@100: UCX_TEST_ASSERT(len == 10, "sstrnlen returned wrong size"); olaf@47: olaf@47: UCX_TEST_ASSERT(cat.ptr[0] == '1', "sstrncat, wrong content"); olaf@47: UCX_TEST_ASSERT(cat.ptr[1] == '2', "sstrncat, wrong content"); olaf@47: UCX_TEST_ASSERT(cat.ptr[2] == '3', "sstrncat, wrong content"); olaf@47: UCX_TEST_ASSERT(cat.ptr[3] == '4', "sstrncat, wrong content"); olaf@47: UCX_TEST_ASSERT(cat.ptr[4] == '.', "sstrncat, wrong content"); olaf@47: UCX_TEST_ASSERT(cat.ptr[8] == '.', "sstrncat, wrong content"); olaf@47: UCX_TEST_ASSERT(cat.ptr[9] == 'X', "sstrncat, wrong content"); universe@100: UCX_TEST_ASSERT(cat.length == 10, "sstrncat, wrong length"); olaf@47: olaf@47: UCX_TEST_END olaf@47: olaf@47: free(cat.ptr); olaf@47: } olaf@47: universe@134: UCX_TEST(test_sstrsplit) { universe@39: universe@39: const char *original = "this,is,a,csv,string"; universe@116: sstr_t test = ST("this,is,a,csv,string"); /* use copy of original here */ universe@39: size_t n; universe@39: sstr_t *list; universe@39: universe@39: UCX_TEST_BEGIN universe@39: universe@39: /* Nullpointer check */ universe@39: n = 0; universe@116: UCX_TEST_ASSERT(sstrsplit(test, S(""), &n) == NULL, universe@39: "empty delimiter must return NULL"); universe@39: universe@39: /* no delimiter occurence (ndo) */ universe@39: n = 0; universe@116: list = sstrsplit(test, S("z"), &n); universe@39: UCX_TEST_ASSERT(n == 1, "ndo, list length must be 1"); universe@39: UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "ndo, " universe@39: "original string shall be returned as single list element"); universe@39: UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, universe@39: "ndo, original has been modified"); universe@39: free(list); universe@39: universe@39: /* partially matching delimiter (pmd) */ universe@39: n = 0; universe@116: list = sstrsplit(test, S("stringbuilder"), &n); universe@39: UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1"); universe@39: UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, " universe@39: "original string shall be returned as single list element"); universe@39: UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, universe@39: "pmd, original has been modified"); universe@39: free(list); universe@39: universe@39: /* matching single-char delimiter (mscd) */ universe@39: n = 0; universe@116: list = sstrsplit(test, S(","), &n); universe@39: UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5"); universe@39: UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, universe@39: "mscd, original has been modified"); universe@39: free(list); universe@39: universe@39: /* matching multi-char delimiter (mmcd) */ universe@39: n = 0; universe@116: list = sstrsplit(test, S("is"), &n); universe@39: UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3"); universe@39: UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0, universe@39: "mmcd, item 2 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, universe@39: "mmcd, original has been modified"); universe@39: free(list); universe@39: universe@39: /* bounded list using single-char delimiter (blsc) */ universe@39: n = 3; universe@116: list = sstrsplit(test, S(","), &n); universe@39: UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3"); universe@39: UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0, universe@39: "blsc, item 2 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, universe@39: "blsc, original has been modified"); universe@39: free(list); universe@39: universe@39: /* bounded list using multi-char delimiter (blmc) */ universe@39: n = 2; universe@116: list = sstrsplit(test, S("is"), &n); universe@39: UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2"); universe@39: UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, universe@39: "blmc, item 1 mismatch"); universe@39: UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, universe@39: "blmc, original has been modified"); universe@39: free(list); universe@39: universe@39: /* start with delimiter (swd) */ universe@39: n = 0; universe@116: list = sstrsplit(test, S("this"), &n); universe@39: UCX_TEST_ASSERT(n == 2, "swd, list length must be 2"); universe@39: UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty"); universe@39: UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, universe@39: "swd, second item corrupt"); universe@39: UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, universe@39: "swd, original has been modified"); universe@39: free(list); universe@39: universe@39: /* end with delimiter (ewd) */ universe@39: n = 0; universe@116: list = sstrsplit(test, S("string"), &n); universe@39: UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2"); universe@39: UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0, universe@39: "swd, first item corrupt"); universe@39: UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty"); universe@39: UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, universe@39: "ewd, original has been modified"); universe@39: free(list); universe@39: universe@39: /* exact match (exm) */ universe@39: n = 0; universe@116: list = sstrsplit(test, S("this,is,a,csv,string"), &n); universe@71: UCX_TEST_ASSERT(n == 0, "exm, list length must be 0"); universe@71: UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL"); universe@39: free(list); universe@39: universe@39: /* substring (subs) */ universe@39: n = 0; universe@116: list = sstrsplit(test, S("this,is,a,csv,string,with,extension"), &n); universe@39: UCX_TEST_ASSERT(n == 1, "subs, list length must be 1"); universe@39: UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, universe@39: "subs, single item must be the original string"); universe@39: UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, universe@39: "subs, original has been modified"); universe@39: free(list); universe@39: universe@39: UCX_TEST_END universe@39: } universe@97: universe@134: UCX_TEST(test_sstrtrim) { olaf@104: sstr_t t1 = sstrtrim(sstr((char*)" ein test ")); olaf@104: sstr_t t2 = sstrtrim(sstr((char*)"abc")); olaf@104: sstr_t t3 = sstrtrim(sstr((char*)" 123")); olaf@104: sstr_t t4 = sstrtrim(sstr((char*)"xyz ")); olaf@104: sstr_t t5 = sstrtrim(sstr((char*)" ")); universe@100: sstr_t empty = sstrtrim(sstr((char*)"")); universe@97: UCX_TEST_BEGIN olaf@104: UCX_TEST_ASSERT(strncmp(t1.ptr, "ein test", t1.length) == 0, "failed"); olaf@104: UCX_TEST_ASSERT(strncmp(t2.ptr, "abc", t2.length) == 0, "failed"); olaf@104: UCX_TEST_ASSERT(strncmp(t3.ptr, "123", t3.length) == 0, "failed"); olaf@104: UCX_TEST_ASSERT(strncmp(t4.ptr, "xyz", t4.length) == 0, "failed"); olaf@104: UCX_TEST_ASSERT(t5.length == 0, "string t5 not empty"); universe@98: UCX_TEST_ASSERT(empty.length == 0, "empty string failed"); universe@97: UCX_TEST_END universe@97: } universe@146: universe@146: UCX_TEST(test_sstrprefixsuffix) { universe@146: sstr_t str = ST("test my prefix and my suffix"); universe@146: sstr_t empty = ST(""); universe@146: universe@146: UCX_TEST_BEGIN universe@146: universe@146: UCX_TEST_ASSERT(!sstrprefix(empty, S("pref")), "prefix empty string fails"); universe@146: UCX_TEST_ASSERT(!sstrsuffix(empty, S("suf")), "suffix empty string fails"); universe@146: universe@146: UCX_TEST_ASSERT(sstrprefix(str, empty), "empty prefix fails"); universe@146: UCX_TEST_ASSERT(sstrsuffix(str, empty), "empty suffix fails"); universe@146: universe@146: UCX_TEST_ASSERT(sstrprefix(empty, empty), "string and prefix empty fails"); universe@146: UCX_TEST_ASSERT(sstrsuffix(empty, empty), "string and suffix empty fails"); universe@146: universe@146: UCX_TEST_ASSERT(sstrprefix(str, S("test ")), "prefix false negative"); universe@146: UCX_TEST_ASSERT(!sstrprefix(str, S("8-) fsck ")), "prefix false positive"); universe@146: universe@146: UCX_TEST_ASSERT(sstrsuffix(str, S("fix")), "suffix false negative"); universe@146: UCX_TEST_ASSERT(!sstrsuffix(str, S("fox")), "suffix false positive"); universe@146: universe@146: universe@146: UCX_TEST_END universe@146: }