Fri, 21 Jun 2013 10:27:03 +0200
fixed sstrtrim and some warnings
universe@39 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@39 | 3 | * |
universe@103 | 4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@39 | 27 | */ |
universe@39 | 28 | |
universe@39 | 29 | #include "string_tests.h" |
universe@39 | 30 | |
olaf@47 | 31 | UCX_TEST_IMPLEMENT(test_sstr) { |
universe@74 | 32 | sstr_t s1 = sstr((char*)"1234"); |
universe@74 | 33 | sstr_t s2 = sstrn((char*)"ab", 2); |
olaf@47 | 34 | |
olaf@47 | 35 | UCX_TEST_BEGIN |
olaf@47 | 36 | |
olaf@47 | 37 | UCX_TEST_ASSERT(s1.length == 4, "s1 length must be 4"); |
olaf@47 | 38 | UCX_TEST_ASSERT(s2.length == 2, "s2 length must be 2"); |
olaf@47 | 39 | |
olaf@47 | 40 | UCX_TEST_END |
olaf@47 | 41 | } |
olaf@47 | 42 | |
olaf@47 | 43 | UCX_TEST_IMPLEMENT(test_sstr_len_cat) { |
universe@74 | 44 | sstr_t s1 = S("1234"); |
universe@74 | 45 | sstr_t s2 = S(".:.:."); |
universe@74 | 46 | sstr_t s3 = S("X"); |
olaf@47 | 47 | |
universe@100 | 48 | size_t len = sstrnlen(3, s1, s2, s3); |
olaf@47 | 49 | sstr_t cat; |
universe@100 | 50 | cat.ptr = (char*) malloc(16); |
universe@100 | 51 | cat.length = 16; |
universe@100 | 52 | cat = sstrncat(3, cat, s1, s2, s3); |
olaf@47 | 53 | |
olaf@47 | 54 | UCX_TEST_BEGIN |
olaf@47 | 55 | |
universe@100 | 56 | UCX_TEST_ASSERT(len == 10, "sstrnlen returned wrong size"); |
olaf@47 | 57 | |
olaf@47 | 58 | UCX_TEST_ASSERT(cat.ptr[0] == '1', "sstrncat, wrong content"); |
olaf@47 | 59 | UCX_TEST_ASSERT(cat.ptr[1] == '2', "sstrncat, wrong content"); |
olaf@47 | 60 | UCX_TEST_ASSERT(cat.ptr[2] == '3', "sstrncat, wrong content"); |
olaf@47 | 61 | UCX_TEST_ASSERT(cat.ptr[3] == '4', "sstrncat, wrong content"); |
olaf@47 | 62 | UCX_TEST_ASSERT(cat.ptr[4] == '.', "sstrncat, wrong content"); |
olaf@47 | 63 | UCX_TEST_ASSERT(cat.ptr[8] == '.', "sstrncat, wrong content"); |
olaf@47 | 64 | UCX_TEST_ASSERT(cat.ptr[9] == 'X', "sstrncat, wrong content"); |
universe@100 | 65 | UCX_TEST_ASSERT(cat.length == 10, "sstrncat, wrong length"); |
olaf@47 | 66 | |
olaf@47 | 67 | UCX_TEST_END |
olaf@47 | 68 | |
olaf@47 | 69 | free(cat.ptr); |
olaf@47 | 70 | } |
olaf@47 | 71 | |
universe@39 | 72 | UCX_TEST_IMPLEMENT(test_sstrsplit) { |
universe@39 | 73 | |
universe@39 | 74 | const char *original = "this,is,a,csv,string"; |
universe@74 | 75 | sstr_t test = S("this,is,a,csv,string"); /* use copy of original here */ |
universe@39 | 76 | size_t n; |
universe@39 | 77 | sstr_t *list; |
universe@39 | 78 | |
universe@39 | 79 | UCX_TEST_BEGIN |
universe@39 | 80 | |
universe@39 | 81 | /* Nullpointer check */ |
universe@39 | 82 | n = 0; |
universe@39 | 83 | UCX_TEST_ASSERT(sstrsplit(test, ST(""), &n) == NULL, |
universe@39 | 84 | "empty delimiter must return NULL"); |
universe@39 | 85 | |
universe@39 | 86 | /* no delimiter occurence (ndo) */ |
universe@39 | 87 | n = 0; |
universe@39 | 88 | list = sstrsplit(test, ST("z"), &n); |
universe@39 | 89 | UCX_TEST_ASSERT(n == 1, "ndo, list length must be 1"); |
universe@39 | 90 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "ndo, " |
universe@39 | 91 | "original string shall be returned as single list element"); |
universe@39 | 92 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, |
universe@39 | 93 | "ndo, original has been modified"); |
universe@39 | 94 | free(list); |
universe@39 | 95 | |
universe@39 | 96 | /* partially matching delimiter (pmd) */ |
universe@39 | 97 | n = 0; |
universe@39 | 98 | list = sstrsplit(test, ST("stringbuilder"), &n); |
universe@39 | 99 | UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1"); |
universe@39 | 100 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, " |
universe@39 | 101 | "original string shall be returned as single list element"); |
universe@39 | 102 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, |
universe@39 | 103 | "pmd, original has been modified"); |
universe@39 | 104 | free(list); |
universe@39 | 105 | |
universe@39 | 106 | /* matching single-char delimiter (mscd) */ |
universe@39 | 107 | n = 0; |
universe@39 | 108 | list = sstrsplit(test, ST(","), &n); |
universe@39 | 109 | UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5"); |
universe@39 | 110 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch"); |
universe@39 | 111 | UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch"); |
universe@39 | 112 | UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch"); |
universe@39 | 113 | UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch"); |
universe@39 | 114 | UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch"); |
universe@39 | 115 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, |
universe@39 | 116 | "mscd, original has been modified"); |
universe@39 | 117 | free(list); |
universe@39 | 118 | |
universe@39 | 119 | /* matching multi-char delimiter (mmcd) */ |
universe@39 | 120 | n = 0; |
universe@39 | 121 | list = sstrsplit(test, ST("is"), &n); |
universe@39 | 122 | UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3"); |
universe@39 | 123 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch"); |
universe@39 | 124 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch"); |
universe@39 | 125 | UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0, |
universe@39 | 126 | "mmcd, item 2 mismatch"); |
universe@39 | 127 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, |
universe@39 | 128 | "mmcd, original has been modified"); |
universe@39 | 129 | free(list); |
universe@39 | 130 | |
universe@39 | 131 | /* bounded list using single-char delimiter (blsc) */ |
universe@39 | 132 | n = 3; |
universe@39 | 133 | list = sstrsplit(test, ST(","), &n); |
universe@39 | 134 | UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3"); |
universe@39 | 135 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch"); |
universe@39 | 136 | UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch"); |
universe@39 | 137 | UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0, |
universe@39 | 138 | "blsc, item 2 mismatch"); |
universe@39 | 139 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, |
universe@39 | 140 | "blsc, original has been modified"); |
universe@39 | 141 | free(list); |
universe@39 | 142 | |
universe@39 | 143 | /* bounded list using multi-char delimiter (blmc) */ |
universe@39 | 144 | n = 2; |
universe@39 | 145 | list = sstrsplit(test, ST("is"), &n); |
universe@39 | 146 | UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2"); |
universe@39 | 147 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch"); |
universe@39 | 148 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, |
universe@39 | 149 | "blmc, item 1 mismatch"); |
universe@39 | 150 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, |
universe@39 | 151 | "blmc, original has been modified"); |
universe@39 | 152 | free(list); |
universe@39 | 153 | |
universe@39 | 154 | /* start with delimiter (swd) */ |
universe@39 | 155 | n = 0; |
universe@39 | 156 | list = sstrsplit(test, ST("this"), &n); |
universe@39 | 157 | UCX_TEST_ASSERT(n == 2, "swd, list length must be 2"); |
universe@39 | 158 | UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty"); |
universe@39 | 159 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, |
universe@39 | 160 | "swd, second item corrupt"); |
universe@39 | 161 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, |
universe@39 | 162 | "swd, original has been modified"); |
universe@39 | 163 | free(list); |
universe@39 | 164 | |
universe@39 | 165 | /* end with delimiter (ewd) */ |
universe@39 | 166 | n = 0; |
universe@39 | 167 | list = sstrsplit(test, ST("string"), &n); |
universe@39 | 168 | UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2"); |
universe@39 | 169 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0, |
universe@39 | 170 | "swd, first item corrupt"); |
universe@39 | 171 | UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty"); |
universe@39 | 172 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, |
universe@39 | 173 | "ewd, original has been modified"); |
universe@39 | 174 | free(list); |
universe@39 | 175 | |
universe@39 | 176 | /* exact match (exm) */ |
universe@39 | 177 | n = 0; |
universe@39 | 178 | list = sstrsplit(test, ST("this,is,a,csv,string"), &n); |
universe@71 | 179 | UCX_TEST_ASSERT(n == 0, "exm, list length must be 0"); |
universe@71 | 180 | UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL"); |
universe@39 | 181 | free(list); |
universe@39 | 182 | |
universe@39 | 183 | /* substring (subs) */ |
universe@39 | 184 | n = 0; |
universe@39 | 185 | list = sstrsplit(test, ST("this,is,a,csv,string,with,extension"), &n); |
universe@39 | 186 | UCX_TEST_ASSERT(n == 1, "subs, list length must be 1"); |
universe@39 | 187 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, |
universe@39 | 188 | "subs, single item must be the original string"); |
universe@39 | 189 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, |
universe@39 | 190 | "subs, original has been modified"); |
universe@39 | 191 | free(list); |
universe@39 | 192 | |
universe@39 | 193 | UCX_TEST_END |
universe@39 | 194 | } |
universe@97 | 195 | |
universe@97 | 196 | UCX_TEST_IMPLEMENT(test_sstrtrim) { |
olaf@104 | 197 | sstr_t t1 = sstrtrim(sstr((char*)" ein test ")); |
olaf@104 | 198 | sstr_t t2 = sstrtrim(sstr((char*)"abc")); |
olaf@104 | 199 | sstr_t t3 = sstrtrim(sstr((char*)" 123")); |
olaf@104 | 200 | sstr_t t4 = sstrtrim(sstr((char*)"xyz ")); |
olaf@104 | 201 | sstr_t t5 = sstrtrim(sstr((char*)" ")); |
universe@100 | 202 | sstr_t empty = sstrtrim(sstr((char*)"")); |
universe@97 | 203 | UCX_TEST_BEGIN |
olaf@104 | 204 | UCX_TEST_ASSERT(strncmp(t1.ptr, "ein test", t1.length) == 0, "failed"); |
olaf@104 | 205 | UCX_TEST_ASSERT(strncmp(t2.ptr, "abc", t2.length) == 0, "failed"); |
olaf@104 | 206 | UCX_TEST_ASSERT(strncmp(t3.ptr, "123", t3.length) == 0, "failed"); |
olaf@104 | 207 | UCX_TEST_ASSERT(strncmp(t4.ptr, "xyz", t4.length) == 0, "failed"); |
olaf@104 | 208 | UCX_TEST_ASSERT(t5.length == 0, "string t5 not empty"); |
universe@98 | 209 | UCX_TEST_ASSERT(empty.length == 0, "empty string failed"); |
universe@97 | 210 | UCX_TEST_END |
universe@97 | 211 | } |