test/string_tests.c

Mon, 14 Jul 2014 12:45:48 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 Jul 2014 12:45:48 +0200
changeset 179
ee25d79a4187
parent 177
11ad03783baf
child 180
2185f19dcc45
permissions
-rw-r--r--

removed old sstrncat

universe@39 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@39 3 *
universe@177 4 * Copyright 2014 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
universe@134 31 UCX_TEST(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
universe@179 43 UCX_TEST(test_sstr_len) {
universe@116 44 sstr_t s1 = ST("1234");
universe@116 45 sstr_t s2 = ST(".:.:.");
universe@116 46 sstr_t s3 = ST("X");
olaf@47 47
universe@100 48 size_t len = sstrnlen(3, s1, s2, s3);
olaf@47 49
olaf@47 50 UCX_TEST_BEGIN
olaf@47 51
universe@100 52 UCX_TEST_ASSERT(len == 10, "sstrnlen returned wrong size");
universe@179 53
olaf@47 54 UCX_TEST_END
olaf@47 55 }
olaf@47 56
universe@149 57 UCX_TEST(test_sstrchr_sstrrchr) {
universe@148 58 sstr_t str = ST("I will find you - and I will kill you");
universe@148 59 UCX_TEST_BEGIN
universe@148 60
universe@148 61 sstr_t result = sstrchr(str, 'w');
universe@148 62 UCX_TEST_ASSERT(result.length == 35, "sstrchr returned wrong length");
universe@148 63 UCX_TEST_ASSERT(strcmp("will find you - and I will kill you", result.ptr)
universe@148 64 == 0, "sstrchr did not return the expected string");
universe@148 65
universe@148 66 result = sstrrchr(str, 'w');
universe@148 67 UCX_TEST_ASSERT(result.length == 13, "sstrrchr returned wrong length");
universe@148 68 UCX_TEST_ASSERT(strcmp("will kill you", result.ptr)
universe@148 69 == 0, "sstrrchr did not return the expected string");
universe@148 70
universe@148 71 UCX_TEST_END
universe@148 72 }
universe@148 73
universe@149 74 UCX_TEST(test_sstrcmp) {
universe@149 75 sstr_t str = ST("compare this");
universe@149 76
universe@149 77 UCX_TEST_BEGIN
universe@149 78 UCX_TEST_ASSERT(sstrcmp(str, S("compare this")) == 0, "false negative");
universe@149 79 UCX_TEST_ASSERT(sstrcmp(str, S("Compare This")) != 0, "false positive");
universe@149 80 UCX_TEST_ASSERT(sstrcmp(str, S("compare tool")) < 0, "memcmp < 0 failed");
universe@149 81 UCX_TEST_ASSERT(sstrcmp(str, S("compare shit")) > 0, "memcmp > 0 failed");
universe@149 82 UCX_TEST_ASSERT(sstrcmp(str, S("compare this not")) < 0, "len < 0 failed");
universe@149 83 UCX_TEST_ASSERT(sstrcmp(str, S("compare")) > 0, "len > 0 failed");
universe@149 84 UCX_TEST_END
universe@149 85 }
universe@149 86
universe@149 87 UCX_TEST(test_sstrcasecmp) {
universe@149 88
universe@149 89 sstr_t str = ST("compare this");
universe@149 90
universe@149 91 UCX_TEST_BEGIN
universe@149 92 UCX_TEST_ASSERT(sstrcasecmp(str, S("compare this")) == 0, "false negative");
universe@149 93 UCX_TEST_ASSERT(sstrcasecmp(str, S("Compare This")) == 0,
universe@149 94 "not ignoring case");
universe@149 95 UCX_TEST_ASSERT(sstrcasecmp(str, S("compare tool")) < 0, "< 0 failed");
universe@149 96 UCX_TEST_ASSERT(sstrcasecmp(str, S("compare shit")) > 0, "> 0 failed");
universe@149 97 UCX_TEST_ASSERT(sstrcasecmp(str, S("compare this not")) < 0,
universe@149 98 "len < 0 failed");
universe@149 99 UCX_TEST_ASSERT(sstrcasecmp(str, S("compare")) > 0, "len > 0 failed");
universe@149 100 UCX_TEST_END
universe@149 101 }
universe@149 102
universe@134 103 UCX_TEST(test_sstrsplit) {
universe@39 104
universe@39 105 const char *original = "this,is,a,csv,string";
universe@116 106 sstr_t test = ST("this,is,a,csv,string"); /* use copy of original here */
universe@173 107 ssize_t n;
universe@39 108 sstr_t *list;
universe@39 109
universe@39 110 UCX_TEST_BEGIN
universe@39 111
universe@39 112 /* Nullpointer check */
universe@39 113 n = 0;
universe@116 114 UCX_TEST_ASSERT(sstrsplit(test, S(""), &n) == NULL,
universe@39 115 "empty delimiter must return NULL");
universe@39 116
universe@39 117 /* no delimiter occurence (ndo) */
universe@39 118 n = 0;
universe@116 119 list = sstrsplit(test, S("z"), &n);
universe@39 120 UCX_TEST_ASSERT(n == 1, "ndo, list length must be 1");
universe@39 121 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "ndo, "
universe@39 122 "original string shall be returned as single list element");
universe@39 123 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 124 "ndo, original has been modified");
olaf@147 125 for(int i=0;i<n;i++) {
olaf@147 126 free(list[i].ptr);
olaf@147 127 }
universe@39 128 free(list);
universe@39 129
universe@39 130 /* partially matching delimiter (pmd) */
universe@39 131 n = 0;
universe@116 132 list = sstrsplit(test, S("stringbuilder"), &n);
universe@39 133 UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1");
universe@39 134 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, "
universe@39 135 "original string shall be returned as single list element");
universe@39 136 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 137 "pmd, original has been modified");
olaf@147 138 for(int i=0;i<n;i++) {
olaf@147 139 free(list[i].ptr);
olaf@147 140 }
universe@39 141 free(list);
universe@39 142
universe@39 143 /* matching single-char delimiter (mscd) */
universe@39 144 n = 0;
universe@116 145 list = sstrsplit(test, S(","), &n);
universe@39 146 UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5");
universe@39 147 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch");
universe@39 148 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch");
universe@39 149 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch");
universe@39 150 UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch");
universe@39 151 UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch");
universe@39 152 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 153 "mscd, original has been modified");
olaf@147 154 for(int i=0;i<n;i++) {
olaf@147 155 free(list[i].ptr);
olaf@147 156 }
universe@39 157 free(list);
universe@39 158
universe@39 159 /* matching multi-char delimiter (mmcd) */
universe@39 160 n = 0;
universe@116 161 list = sstrsplit(test, S("is"), &n);
universe@39 162 UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3");
universe@39 163 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch");
universe@39 164 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch");
universe@39 165 UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0,
universe@39 166 "mmcd, item 2 mismatch");
universe@39 167 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 168 "mmcd, original has been modified");
olaf@147 169 for(int i=0;i<n;i++) {
olaf@147 170 free(list[i].ptr);
olaf@147 171 }
universe@39 172 free(list);
universe@39 173
universe@39 174 /* bounded list using single-char delimiter (blsc) */
universe@39 175 n = 3;
universe@116 176 list = sstrsplit(test, S(","), &n);
universe@39 177 UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3");
universe@39 178 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch");
universe@39 179 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch");
universe@39 180 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0,
universe@39 181 "blsc, item 2 mismatch");
universe@39 182 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 183 "blsc, original has been modified");
olaf@147 184 for(int i=0;i<n;i++) {
olaf@147 185 free(list[i].ptr);
olaf@147 186 }
universe@39 187 free(list);
universe@39 188
universe@39 189 /* bounded list using multi-char delimiter (blmc) */
universe@39 190 n = 2;
universe@116 191 list = sstrsplit(test, S("is"), &n);
universe@39 192 UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2");
universe@39 193 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch");
universe@39 194 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
universe@39 195 "blmc, item 1 mismatch");
universe@39 196 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 197 "blmc, original has been modified");
olaf@147 198 for(int i=0;i<n;i++) {
olaf@147 199 free(list[i].ptr);
olaf@147 200 }
universe@39 201 free(list);
universe@39 202
universe@39 203 /* start with delimiter (swd) */
universe@39 204 n = 0;
universe@116 205 list = sstrsplit(test, S("this"), &n);
universe@39 206 UCX_TEST_ASSERT(n == 2, "swd, list length must be 2");
universe@39 207 UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty");
universe@39 208 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
universe@39 209 "swd, second item corrupt");
universe@39 210 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 211 "swd, original has been modified");
olaf@147 212 for(int i=0;i<n;i++) {
olaf@147 213 free(list[i].ptr);
olaf@147 214 }
universe@39 215 free(list);
universe@39 216
universe@39 217 /* end with delimiter (ewd) */
universe@39 218 n = 0;
universe@116 219 list = sstrsplit(test, S("string"), &n);
universe@39 220 UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2");
universe@39 221 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0,
universe@39 222 "swd, first item corrupt");
universe@39 223 UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty");
universe@39 224 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 225 "ewd, original has been modified");
olaf@147 226 for(int i=0;i<n;i++) {
olaf@147 227 free(list[i].ptr);
olaf@147 228 }
universe@39 229 free(list);
universe@39 230
universe@39 231 /* exact match (exm) */
universe@39 232 n = 0;
universe@116 233 list = sstrsplit(test, S("this,is,a,csv,string"), &n);
universe@71 234 UCX_TEST_ASSERT(n == 0, "exm, list length must be 0");
universe@71 235 UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL");
olaf@147 236 for(int i=0;i<n;i++) {
olaf@147 237 free(list[i].ptr);
olaf@147 238 }
universe@39 239 free(list);
universe@39 240
universe@39 241 /* substring (subs) */
universe@39 242 n = 0;
universe@116 243 list = sstrsplit(test, S("this,is,a,csv,string,with,extension"), &n);
universe@39 244 UCX_TEST_ASSERT(n == 1, "subs, list length must be 1");
universe@39 245 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0,
universe@39 246 "subs, single item must be the original string");
universe@39 247 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 248 "subs, original has been modified");
olaf@147 249 for(int i=0;i<n;i++) {
olaf@147 250 free(list[i].ptr);
olaf@147 251 }
universe@39 252 free(list);
universe@39 253
universe@39 254 UCX_TEST_END
universe@39 255 }
universe@97 256
universe@134 257 UCX_TEST(test_sstrtrim) {
olaf@104 258 sstr_t t1 = sstrtrim(sstr((char*)" ein test "));
olaf@104 259 sstr_t t2 = sstrtrim(sstr((char*)"abc"));
olaf@104 260 sstr_t t3 = sstrtrim(sstr((char*)" 123"));
olaf@104 261 sstr_t t4 = sstrtrim(sstr((char*)"xyz "));
olaf@104 262 sstr_t t5 = sstrtrim(sstr((char*)" "));
universe@100 263 sstr_t empty = sstrtrim(sstr((char*)""));
universe@97 264 UCX_TEST_BEGIN
olaf@104 265 UCX_TEST_ASSERT(strncmp(t1.ptr, "ein test", t1.length) == 0, "failed");
olaf@104 266 UCX_TEST_ASSERT(strncmp(t2.ptr, "abc", t2.length) == 0, "failed");
olaf@104 267 UCX_TEST_ASSERT(strncmp(t3.ptr, "123", t3.length) == 0, "failed");
olaf@104 268 UCX_TEST_ASSERT(strncmp(t4.ptr, "xyz", t4.length) == 0, "failed");
olaf@104 269 UCX_TEST_ASSERT(t5.length == 0, "string t5 not empty");
universe@98 270 UCX_TEST_ASSERT(empty.length == 0, "empty string failed");
universe@97 271 UCX_TEST_END
universe@97 272 }
universe@146 273
universe@146 274 UCX_TEST(test_sstrprefixsuffix) {
universe@146 275 sstr_t str = ST("test my prefix and my suffix");
universe@146 276 sstr_t empty = ST("");
universe@146 277
universe@146 278 UCX_TEST_BEGIN
universe@146 279
universe@146 280 UCX_TEST_ASSERT(!sstrprefix(empty, S("pref")), "prefix empty string fails");
universe@146 281 UCX_TEST_ASSERT(!sstrsuffix(empty, S("suf")), "suffix empty string fails");
universe@146 282
universe@146 283 UCX_TEST_ASSERT(sstrprefix(str, empty), "empty prefix fails");
universe@146 284 UCX_TEST_ASSERT(sstrsuffix(str, empty), "empty suffix fails");
universe@146 285
universe@146 286 UCX_TEST_ASSERT(sstrprefix(empty, empty), "string and prefix empty fails");
universe@146 287 UCX_TEST_ASSERT(sstrsuffix(empty, empty), "string and suffix empty fails");
universe@146 288
universe@146 289 UCX_TEST_ASSERT(sstrprefix(str, S("test ")), "prefix false negative");
universe@146 290 UCX_TEST_ASSERT(!sstrprefix(str, S("8-) fsck ")), "prefix false positive");
universe@146 291
universe@146 292 UCX_TEST_ASSERT(sstrsuffix(str, S("fix")), "suffix false negative");
universe@146 293 UCX_TEST_ASSERT(!sstrsuffix(str, S("fox")), "suffix false positive");
universe@146 294
universe@146 295
universe@146 296 UCX_TEST_END
universe@146 297 }

mercurial