test/string_tests.c

Mon, 19 Aug 2013 10:44:11 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 19 Aug 2013 10:44:11 +0200
changeset 148
c27c2425c0b1
parent 147
1aa598f36872
child 149
3bf87676d42d
permissions
-rw-r--r--

added sstrrchr

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
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@134 43 UCX_TEST(test_sstr_len_cat) {
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 sstr_t cat;
universe@100 50 cat.ptr = (char*) malloc(16);
universe@100 51 cat.length = 16;
universe@123 52 cat = sstrncat(cat, 3, 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@148 72 UCX_TEST(test_sstrchr) {
universe@148 73 sstr_t str = ST("I will find you - and I will kill you");
universe@148 74 UCX_TEST_BEGIN
universe@148 75
universe@148 76 sstr_t result = sstrchr(str, 'w');
universe@148 77 UCX_TEST_ASSERT(result.length == 35, "sstrchr returned wrong length");
universe@148 78 UCX_TEST_ASSERT(strcmp("will find you - and I will kill you", result.ptr)
universe@148 79 == 0, "sstrchr did not return the expected string");
universe@148 80
universe@148 81 result = sstrrchr(str, 'w');
universe@148 82 UCX_TEST_ASSERT(result.length == 13, "sstrrchr returned wrong length");
universe@148 83 UCX_TEST_ASSERT(strcmp("will kill you", result.ptr)
universe@148 84 == 0, "sstrrchr did not return the expected string");
universe@148 85
universe@148 86 UCX_TEST_END
universe@148 87 }
universe@148 88
universe@134 89 UCX_TEST(test_sstrsplit) {
universe@39 90
universe@39 91 const char *original = "this,is,a,csv,string";
universe@116 92 sstr_t test = ST("this,is,a,csv,string"); /* use copy of original here */
universe@39 93 size_t n;
universe@39 94 sstr_t *list;
universe@39 95
universe@39 96 UCX_TEST_BEGIN
universe@39 97
universe@39 98 /* Nullpointer check */
universe@39 99 n = 0;
universe@116 100 UCX_TEST_ASSERT(sstrsplit(test, S(""), &n) == NULL,
universe@39 101 "empty delimiter must return NULL");
universe@39 102
universe@39 103 /* no delimiter occurence (ndo) */
universe@39 104 n = 0;
universe@116 105 list = sstrsplit(test, S("z"), &n);
universe@39 106 UCX_TEST_ASSERT(n == 1, "ndo, list length must be 1");
universe@39 107 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "ndo, "
universe@39 108 "original string shall be returned as single list element");
universe@39 109 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 110 "ndo, original has been modified");
olaf@147 111 for(int i=0;i<n;i++) {
olaf@147 112 free(list[i].ptr);
olaf@147 113 }
universe@39 114 free(list);
universe@39 115
universe@39 116 /* partially matching delimiter (pmd) */
universe@39 117 n = 0;
universe@116 118 list = sstrsplit(test, S("stringbuilder"), &n);
universe@39 119 UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1");
universe@39 120 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, "
universe@39 121 "original string shall be returned as single list element");
universe@39 122 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 123 "pmd, original has been modified");
olaf@147 124 for(int i=0;i<n;i++) {
olaf@147 125 free(list[i].ptr);
olaf@147 126 }
universe@39 127 free(list);
universe@39 128
universe@39 129 /* matching single-char delimiter (mscd) */
universe@39 130 n = 0;
universe@116 131 list = sstrsplit(test, S(","), &n);
universe@39 132 UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5");
universe@39 133 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch");
universe@39 134 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch");
universe@39 135 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch");
universe@39 136 UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch");
universe@39 137 UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch");
universe@39 138 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 139 "mscd, original has been modified");
olaf@147 140 for(int i=0;i<n;i++) {
olaf@147 141 free(list[i].ptr);
olaf@147 142 }
universe@39 143 free(list);
universe@39 144
universe@39 145 /* matching multi-char delimiter (mmcd) */
universe@39 146 n = 0;
universe@116 147 list = sstrsplit(test, S("is"), &n);
universe@39 148 UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3");
universe@39 149 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch");
universe@39 150 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch");
universe@39 151 UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0,
universe@39 152 "mmcd, item 2 mismatch");
universe@39 153 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 154 "mmcd, original has been modified");
olaf@147 155 for(int i=0;i<n;i++) {
olaf@147 156 free(list[i].ptr);
olaf@147 157 }
universe@39 158 free(list);
universe@39 159
universe@39 160 /* bounded list using single-char delimiter (blsc) */
universe@39 161 n = 3;
universe@116 162 list = sstrsplit(test, S(","), &n);
universe@39 163 UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3");
universe@39 164 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch");
universe@39 165 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch");
universe@39 166 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0,
universe@39 167 "blsc, item 2 mismatch");
universe@39 168 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 169 "blsc, original has been modified");
olaf@147 170 for(int i=0;i<n;i++) {
olaf@147 171 free(list[i].ptr);
olaf@147 172 }
universe@39 173 free(list);
universe@39 174
universe@39 175 /* bounded list using multi-char delimiter (blmc) */
universe@39 176 n = 2;
universe@116 177 list = sstrsplit(test, S("is"), &n);
universe@39 178 UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2");
universe@39 179 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch");
universe@39 180 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
universe@39 181 "blmc, item 1 mismatch");
universe@39 182 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 183 "blmc, 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 /* start with delimiter (swd) */
universe@39 190 n = 0;
universe@116 191 list = sstrsplit(test, S("this"), &n);
universe@39 192 UCX_TEST_ASSERT(n == 2, "swd, list length must be 2");
universe@39 193 UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty");
universe@39 194 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
universe@39 195 "swd, second item corrupt");
universe@39 196 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 197 "swd, 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 /* end with delimiter (ewd) */
universe@39 204 n = 0;
universe@116 205 list = sstrsplit(test, S("string"), &n);
universe@39 206 UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2");
universe@39 207 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0,
universe@39 208 "swd, first item corrupt");
universe@39 209 UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty");
universe@39 210 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 211 "ewd, 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 /* exact match (exm) */
universe@39 218 n = 0;
universe@116 219 list = sstrsplit(test, S("this,is,a,csv,string"), &n);
universe@71 220 UCX_TEST_ASSERT(n == 0, "exm, list length must be 0");
universe@71 221 UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL");
olaf@147 222 for(int i=0;i<n;i++) {
olaf@147 223 free(list[i].ptr);
olaf@147 224 }
universe@39 225 free(list);
universe@39 226
universe@39 227 /* substring (subs) */
universe@39 228 n = 0;
universe@116 229 list = sstrsplit(test, S("this,is,a,csv,string,with,extension"), &n);
universe@39 230 UCX_TEST_ASSERT(n == 1, "subs, list length must be 1");
universe@39 231 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0,
universe@39 232 "subs, single item must be the original string");
universe@39 233 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 234 "subs, original has been modified");
olaf@147 235 for(int i=0;i<n;i++) {
olaf@147 236 free(list[i].ptr);
olaf@147 237 }
universe@39 238 free(list);
universe@39 239
universe@39 240 UCX_TEST_END
universe@39 241 }
universe@97 242
universe@134 243 UCX_TEST(test_sstrtrim) {
olaf@104 244 sstr_t t1 = sstrtrim(sstr((char*)" ein test "));
olaf@104 245 sstr_t t2 = sstrtrim(sstr((char*)"abc"));
olaf@104 246 sstr_t t3 = sstrtrim(sstr((char*)" 123"));
olaf@104 247 sstr_t t4 = sstrtrim(sstr((char*)"xyz "));
olaf@104 248 sstr_t t5 = sstrtrim(sstr((char*)" "));
universe@100 249 sstr_t empty = sstrtrim(sstr((char*)""));
universe@97 250 UCX_TEST_BEGIN
olaf@104 251 UCX_TEST_ASSERT(strncmp(t1.ptr, "ein test", t1.length) == 0, "failed");
olaf@104 252 UCX_TEST_ASSERT(strncmp(t2.ptr, "abc", t2.length) == 0, "failed");
olaf@104 253 UCX_TEST_ASSERT(strncmp(t3.ptr, "123", t3.length) == 0, "failed");
olaf@104 254 UCX_TEST_ASSERT(strncmp(t4.ptr, "xyz", t4.length) == 0, "failed");
olaf@104 255 UCX_TEST_ASSERT(t5.length == 0, "string t5 not empty");
universe@98 256 UCX_TEST_ASSERT(empty.length == 0, "empty string failed");
universe@97 257 UCX_TEST_END
universe@97 258 }
universe@146 259
universe@146 260 UCX_TEST(test_sstrprefixsuffix) {
universe@146 261 sstr_t str = ST("test my prefix and my suffix");
universe@146 262 sstr_t empty = ST("");
universe@146 263
universe@146 264 UCX_TEST_BEGIN
universe@146 265
universe@146 266 UCX_TEST_ASSERT(!sstrprefix(empty, S("pref")), "prefix empty string fails");
universe@146 267 UCX_TEST_ASSERT(!sstrsuffix(empty, S("suf")), "suffix empty string fails");
universe@146 268
universe@146 269 UCX_TEST_ASSERT(sstrprefix(str, empty), "empty prefix fails");
universe@146 270 UCX_TEST_ASSERT(sstrsuffix(str, empty), "empty suffix fails");
universe@146 271
universe@146 272 UCX_TEST_ASSERT(sstrprefix(empty, empty), "string and prefix empty fails");
universe@146 273 UCX_TEST_ASSERT(sstrsuffix(empty, empty), "string and suffix empty fails");
universe@146 274
universe@146 275 UCX_TEST_ASSERT(sstrprefix(str, S("test ")), "prefix false negative");
universe@146 276 UCX_TEST_ASSERT(!sstrprefix(str, S("8-) fsck ")), "prefix false positive");
universe@146 277
universe@146 278 UCX_TEST_ASSERT(sstrsuffix(str, S("fix")), "suffix false negative");
universe@146 279 UCX_TEST_ASSERT(!sstrsuffix(str, S("fox")), "suffix false positive");
universe@146 280
universe@146 281
universe@146 282 UCX_TEST_END
universe@146 283 }

mercurial