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@149: UCX_TEST(test_sstrchr_sstrrchr) { universe@148: sstr_t str = ST("I will find you - and I will kill you"); universe@148: UCX_TEST_BEGIN universe@148: universe@148: sstr_t result = sstrchr(str, 'w'); universe@148: UCX_TEST_ASSERT(result.length == 35, "sstrchr returned wrong length"); universe@148: UCX_TEST_ASSERT(strcmp("will find you - and I will kill you", result.ptr) universe@148: == 0, "sstrchr did not return the expected string"); universe@148: universe@148: result = sstrrchr(str, 'w'); universe@148: UCX_TEST_ASSERT(result.length == 13, "sstrrchr returned wrong length"); universe@148: UCX_TEST_ASSERT(strcmp("will kill you", result.ptr) universe@148: == 0, "sstrrchr did not return the expected string"); universe@148: universe@148: UCX_TEST_END universe@148: } universe@148: universe@149: UCX_TEST(test_sstrcmp) { universe@149: sstr_t str = ST("compare this"); universe@149: universe@149: UCX_TEST_BEGIN universe@149: UCX_TEST_ASSERT(sstrcmp(str, S("compare this")) == 0, "false negative"); universe@149: UCX_TEST_ASSERT(sstrcmp(str, S("Compare This")) != 0, "false positive"); universe@149: UCX_TEST_ASSERT(sstrcmp(str, S("compare tool")) < 0, "memcmp < 0 failed"); universe@149: UCX_TEST_ASSERT(sstrcmp(str, S("compare shit")) > 0, "memcmp > 0 failed"); universe@149: UCX_TEST_ASSERT(sstrcmp(str, S("compare this not")) < 0, "len < 0 failed"); universe@149: UCX_TEST_ASSERT(sstrcmp(str, S("compare")) > 0, "len > 0 failed"); universe@149: UCX_TEST_END universe@149: } universe@149: universe@149: UCX_TEST(test_sstrcasecmp) { universe@149: universe@149: sstr_t str = ST("compare this"); universe@149: universe@149: UCX_TEST_BEGIN universe@149: UCX_TEST_ASSERT(sstrcasecmp(str, S("compare this")) == 0, "false negative"); universe@149: UCX_TEST_ASSERT(sstrcasecmp(str, S("Compare This")) == 0, universe@149: "not ignoring case"); universe@149: UCX_TEST_ASSERT(sstrcasecmp(str, S("compare tool")) < 0, "< 0 failed"); universe@149: UCX_TEST_ASSERT(sstrcasecmp(str, S("compare shit")) > 0, "> 0 failed"); universe@149: UCX_TEST_ASSERT(sstrcasecmp(str, S("compare this not")) < 0, universe@149: "len < 0 failed"); universe@149: UCX_TEST_ASSERT(sstrcasecmp(str, S("compare")) > 0, "len > 0 failed"); universe@149: UCX_TEST_END universe@149: } universe@149: 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@173: ssize_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"); olaf@147: for(int i=0;i