test/string_tests.c

Fri, 16 Aug 2013 14:48:58 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 16 Aug 2013 14:48:58 +0200
changeset 147
1aa598f36872
parent 146
aa376dba1ba8
child 148
c27c2425c0b1
permissions
-rw-r--r--

added printf for UcxBuffer + fixed memory leaks

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@134 72 UCX_TEST(test_sstrsplit) {
universe@39 73
universe@39 74 const char *original = "this,is,a,csv,string";
universe@116 75 sstr_t test = ST("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@116 83 UCX_TEST_ASSERT(sstrsplit(test, S(""), &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@116 88 list = sstrsplit(test, S("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");
olaf@147 94 for(int i=0;i<n;i++) {
olaf@147 95 free(list[i].ptr);
olaf@147 96 }
universe@39 97 free(list);
universe@39 98
universe@39 99 /* partially matching delimiter (pmd) */
universe@39 100 n = 0;
universe@116 101 list = sstrsplit(test, S("stringbuilder"), &n);
universe@39 102 UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1");
universe@39 103 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, "
universe@39 104 "original string shall be returned as single list element");
universe@39 105 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 106 "pmd, original has been modified");
olaf@147 107 for(int i=0;i<n;i++) {
olaf@147 108 free(list[i].ptr);
olaf@147 109 }
universe@39 110 free(list);
universe@39 111
universe@39 112 /* matching single-char delimiter (mscd) */
universe@39 113 n = 0;
universe@116 114 list = sstrsplit(test, S(","), &n);
universe@39 115 UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5");
universe@39 116 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch");
universe@39 117 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch");
universe@39 118 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch");
universe@39 119 UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch");
universe@39 120 UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch");
universe@39 121 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 122 "mscd, original has been modified");
olaf@147 123 for(int i=0;i<n;i++) {
olaf@147 124 free(list[i].ptr);
olaf@147 125 }
universe@39 126 free(list);
universe@39 127
universe@39 128 /* matching multi-char delimiter (mmcd) */
universe@39 129 n = 0;
universe@116 130 list = sstrsplit(test, S("is"), &n);
universe@39 131 UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3");
universe@39 132 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch");
universe@39 133 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch");
universe@39 134 UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0,
universe@39 135 "mmcd, item 2 mismatch");
universe@39 136 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 137 "mmcd, 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 /* bounded list using single-char delimiter (blsc) */
universe@39 144 n = 3;
universe@116 145 list = sstrsplit(test, S(","), &n);
universe@39 146 UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3");
universe@39 147 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch");
universe@39 148 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch");
universe@39 149 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0,
universe@39 150 "blsc, item 2 mismatch");
universe@39 151 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 152 "blsc, original has been modified");
olaf@147 153 for(int i=0;i<n;i++) {
olaf@147 154 free(list[i].ptr);
olaf@147 155 }
universe@39 156 free(list);
universe@39 157
universe@39 158 /* bounded list using multi-char delimiter (blmc) */
universe@39 159 n = 2;
universe@116 160 list = sstrsplit(test, S("is"), &n);
universe@39 161 UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2");
universe@39 162 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch");
universe@39 163 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
universe@39 164 "blmc, item 1 mismatch");
universe@39 165 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 166 "blmc, original has been modified");
olaf@147 167 for(int i=0;i<n;i++) {
olaf@147 168 free(list[i].ptr);
olaf@147 169 }
universe@39 170 free(list);
universe@39 171
universe@39 172 /* start with delimiter (swd) */
universe@39 173 n = 0;
universe@116 174 list = sstrsplit(test, S("this"), &n);
universe@39 175 UCX_TEST_ASSERT(n == 2, "swd, list length must be 2");
universe@39 176 UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty");
universe@39 177 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
universe@39 178 "swd, second item corrupt");
universe@39 179 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 180 "swd, original has been modified");
olaf@147 181 for(int i=0;i<n;i++) {
olaf@147 182 free(list[i].ptr);
olaf@147 183 }
universe@39 184 free(list);
universe@39 185
universe@39 186 /* end with delimiter (ewd) */
universe@39 187 n = 0;
universe@116 188 list = sstrsplit(test, S("string"), &n);
universe@39 189 UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2");
universe@39 190 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0,
universe@39 191 "swd, first item corrupt");
universe@39 192 UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty");
universe@39 193 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 194 "ewd, original has been modified");
olaf@147 195 for(int i=0;i<n;i++) {
olaf@147 196 free(list[i].ptr);
olaf@147 197 }
universe@39 198 free(list);
universe@39 199
universe@39 200 /* exact match (exm) */
universe@39 201 n = 0;
universe@116 202 list = sstrsplit(test, S("this,is,a,csv,string"), &n);
universe@71 203 UCX_TEST_ASSERT(n == 0, "exm, list length must be 0");
universe@71 204 UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL");
olaf@147 205 for(int i=0;i<n;i++) {
olaf@147 206 free(list[i].ptr);
olaf@147 207 }
universe@39 208 free(list);
universe@39 209
universe@39 210 /* substring (subs) */
universe@39 211 n = 0;
universe@116 212 list = sstrsplit(test, S("this,is,a,csv,string,with,extension"), &n);
universe@39 213 UCX_TEST_ASSERT(n == 1, "subs, list length must be 1");
universe@39 214 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0,
universe@39 215 "subs, single item must be the original string");
universe@39 216 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
universe@39 217 "subs, original has been modified");
olaf@147 218 for(int i=0;i<n;i++) {
olaf@147 219 free(list[i].ptr);
olaf@147 220 }
universe@39 221 free(list);
universe@39 222
universe@39 223 UCX_TEST_END
universe@39 224 }
universe@97 225
universe@134 226 UCX_TEST(test_sstrtrim) {
olaf@104 227 sstr_t t1 = sstrtrim(sstr((char*)" ein test "));
olaf@104 228 sstr_t t2 = sstrtrim(sstr((char*)"abc"));
olaf@104 229 sstr_t t3 = sstrtrim(sstr((char*)" 123"));
olaf@104 230 sstr_t t4 = sstrtrim(sstr((char*)"xyz "));
olaf@104 231 sstr_t t5 = sstrtrim(sstr((char*)" "));
universe@100 232 sstr_t empty = sstrtrim(sstr((char*)""));
universe@97 233 UCX_TEST_BEGIN
olaf@104 234 UCX_TEST_ASSERT(strncmp(t1.ptr, "ein test", t1.length) == 0, "failed");
olaf@104 235 UCX_TEST_ASSERT(strncmp(t2.ptr, "abc", t2.length) == 0, "failed");
olaf@104 236 UCX_TEST_ASSERT(strncmp(t3.ptr, "123", t3.length) == 0, "failed");
olaf@104 237 UCX_TEST_ASSERT(strncmp(t4.ptr, "xyz", t4.length) == 0, "failed");
olaf@104 238 UCX_TEST_ASSERT(t5.length == 0, "string t5 not empty");
universe@98 239 UCX_TEST_ASSERT(empty.length == 0, "empty string failed");
universe@97 240 UCX_TEST_END
universe@97 241 }
universe@146 242
universe@146 243 UCX_TEST(test_sstrprefixsuffix) {
universe@146 244 sstr_t str = ST("test my prefix and my suffix");
universe@146 245 sstr_t empty = ST("");
universe@146 246
universe@146 247 UCX_TEST_BEGIN
universe@146 248
universe@146 249 UCX_TEST_ASSERT(!sstrprefix(empty, S("pref")), "prefix empty string fails");
universe@146 250 UCX_TEST_ASSERT(!sstrsuffix(empty, S("suf")), "suffix empty string fails");
universe@146 251
universe@146 252 UCX_TEST_ASSERT(sstrprefix(str, empty), "empty prefix fails");
universe@146 253 UCX_TEST_ASSERT(sstrsuffix(str, empty), "empty suffix fails");
universe@146 254
universe@146 255 UCX_TEST_ASSERT(sstrprefix(empty, empty), "string and prefix empty fails");
universe@146 256 UCX_TEST_ASSERT(sstrsuffix(empty, empty), "string and suffix empty fails");
universe@146 257
universe@146 258 UCX_TEST_ASSERT(sstrprefix(str, S("test ")), "prefix false negative");
universe@146 259 UCX_TEST_ASSERT(!sstrprefix(str, S("8-) fsck ")), "prefix false positive");
universe@146 260
universe@146 261 UCX_TEST_ASSERT(sstrsuffix(str, S("fix")), "suffix false negative");
universe@146 262 UCX_TEST_ASSERT(!sstrsuffix(str, S("fox")), "suffix false positive");
universe@146 263
universe@146 264
universe@146 265 UCX_TEST_END
universe@146 266 }

mercurial