Mon, 19 Aug 2013 10:44:11 +0200
added sstrrchr
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "string_tests.h"
31 UCX_TEST(test_sstr) {
32 sstr_t s1 = sstr((char*)"1234");
33 sstr_t s2 = sstrn((char*)"ab", 2);
35 UCX_TEST_BEGIN
37 UCX_TEST_ASSERT(s1.length == 4, "s1 length must be 4");
38 UCX_TEST_ASSERT(s2.length == 2, "s2 length must be 2");
40 UCX_TEST_END
41 }
43 UCX_TEST(test_sstr_len_cat) {
44 sstr_t s1 = ST("1234");
45 sstr_t s2 = ST(".:.:.");
46 sstr_t s3 = ST("X");
48 size_t len = sstrnlen(3, s1, s2, s3);
49 sstr_t cat;
50 cat.ptr = (char*) malloc(16);
51 cat.length = 16;
52 cat = sstrncat(cat, 3, s1, s2, s3);
54 UCX_TEST_BEGIN
56 UCX_TEST_ASSERT(len == 10, "sstrnlen returned wrong size");
58 UCX_TEST_ASSERT(cat.ptr[0] == '1', "sstrncat, wrong content");
59 UCX_TEST_ASSERT(cat.ptr[1] == '2', "sstrncat, wrong content");
60 UCX_TEST_ASSERT(cat.ptr[2] == '3', "sstrncat, wrong content");
61 UCX_TEST_ASSERT(cat.ptr[3] == '4', "sstrncat, wrong content");
62 UCX_TEST_ASSERT(cat.ptr[4] == '.', "sstrncat, wrong content");
63 UCX_TEST_ASSERT(cat.ptr[8] == '.', "sstrncat, wrong content");
64 UCX_TEST_ASSERT(cat.ptr[9] == 'X', "sstrncat, wrong content");
65 UCX_TEST_ASSERT(cat.length == 10, "sstrncat, wrong length");
67 UCX_TEST_END
69 free(cat.ptr);
70 }
72 UCX_TEST(test_sstrchr) {
73 sstr_t str = ST("I will find you - and I will kill you");
74 UCX_TEST_BEGIN
76 sstr_t result = sstrchr(str, 'w');
77 UCX_TEST_ASSERT(result.length == 35, "sstrchr returned wrong length");
78 UCX_TEST_ASSERT(strcmp("will find you - and I will kill you", result.ptr)
79 == 0, "sstrchr did not return the expected string");
81 result = sstrrchr(str, 'w');
82 UCX_TEST_ASSERT(result.length == 13, "sstrrchr returned wrong length");
83 UCX_TEST_ASSERT(strcmp("will kill you", result.ptr)
84 == 0, "sstrrchr did not return the expected string");
86 UCX_TEST_END
87 }
89 UCX_TEST(test_sstrsplit) {
91 const char *original = "this,is,a,csv,string";
92 sstr_t test = ST("this,is,a,csv,string"); /* use copy of original here */
93 size_t n;
94 sstr_t *list;
96 UCX_TEST_BEGIN
98 /* Nullpointer check */
99 n = 0;
100 UCX_TEST_ASSERT(sstrsplit(test, S(""), &n) == NULL,
101 "empty delimiter must return NULL");
103 /* no delimiter occurence (ndo) */
104 n = 0;
105 list = sstrsplit(test, S("z"), &n);
106 UCX_TEST_ASSERT(n == 1, "ndo, list length must be 1");
107 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "ndo, "
108 "original string shall be returned as single list element");
109 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
110 "ndo, original has been modified");
111 for(int i=0;i<n;i++) {
112 free(list[i].ptr);
113 }
114 free(list);
116 /* partially matching delimiter (pmd) */
117 n = 0;
118 list = sstrsplit(test, S("stringbuilder"), &n);
119 UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1");
120 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, "
121 "original string shall be returned as single list element");
122 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
123 "pmd, original has been modified");
124 for(int i=0;i<n;i++) {
125 free(list[i].ptr);
126 }
127 free(list);
129 /* matching single-char delimiter (mscd) */
130 n = 0;
131 list = sstrsplit(test, S(","), &n);
132 UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5");
133 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch");
134 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch");
135 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch");
136 UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch");
137 UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch");
138 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
139 "mscd, original has been modified");
140 for(int i=0;i<n;i++) {
141 free(list[i].ptr);
142 }
143 free(list);
145 /* matching multi-char delimiter (mmcd) */
146 n = 0;
147 list = sstrsplit(test, S("is"), &n);
148 UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3");
149 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch");
150 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch");
151 UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0,
152 "mmcd, item 2 mismatch");
153 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
154 "mmcd, original has been modified");
155 for(int i=0;i<n;i++) {
156 free(list[i].ptr);
157 }
158 free(list);
160 /* bounded list using single-char delimiter (blsc) */
161 n = 3;
162 list = sstrsplit(test, S(","), &n);
163 UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3");
164 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch");
165 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch");
166 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0,
167 "blsc, item 2 mismatch");
168 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
169 "blsc, original has been modified");
170 for(int i=0;i<n;i++) {
171 free(list[i].ptr);
172 }
173 free(list);
175 /* bounded list using multi-char delimiter (blmc) */
176 n = 2;
177 list = sstrsplit(test, S("is"), &n);
178 UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2");
179 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch");
180 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
181 "blmc, item 1 mismatch");
182 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
183 "blmc, original has been modified");
184 for(int i=0;i<n;i++) {
185 free(list[i].ptr);
186 }
187 free(list);
189 /* start with delimiter (swd) */
190 n = 0;
191 list = sstrsplit(test, S("this"), &n);
192 UCX_TEST_ASSERT(n == 2, "swd, list length must be 2");
193 UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty");
194 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
195 "swd, second item corrupt");
196 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
197 "swd, original has been modified");
198 for(int i=0;i<n;i++) {
199 free(list[i].ptr);
200 }
201 free(list);
203 /* end with delimiter (ewd) */
204 n = 0;
205 list = sstrsplit(test, S("string"), &n);
206 UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2");
207 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0,
208 "swd, first item corrupt");
209 UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty");
210 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
211 "ewd, original has been modified");
212 for(int i=0;i<n;i++) {
213 free(list[i].ptr);
214 }
215 free(list);
217 /* exact match (exm) */
218 n = 0;
219 list = sstrsplit(test, S("this,is,a,csv,string"), &n);
220 UCX_TEST_ASSERT(n == 0, "exm, list length must be 0");
221 UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL");
222 for(int i=0;i<n;i++) {
223 free(list[i].ptr);
224 }
225 free(list);
227 /* substring (subs) */
228 n = 0;
229 list = sstrsplit(test, S("this,is,a,csv,string,with,extension"), &n);
230 UCX_TEST_ASSERT(n == 1, "subs, list length must be 1");
231 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0,
232 "subs, single item must be the original string");
233 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
234 "subs, original has been modified");
235 for(int i=0;i<n;i++) {
236 free(list[i].ptr);
237 }
238 free(list);
240 UCX_TEST_END
241 }
243 UCX_TEST(test_sstrtrim) {
244 sstr_t t1 = sstrtrim(sstr((char*)" ein test "));
245 sstr_t t2 = sstrtrim(sstr((char*)"abc"));
246 sstr_t t3 = sstrtrim(sstr((char*)" 123"));
247 sstr_t t4 = sstrtrim(sstr((char*)"xyz "));
248 sstr_t t5 = sstrtrim(sstr((char*)" "));
249 sstr_t empty = sstrtrim(sstr((char*)""));
250 UCX_TEST_BEGIN
251 UCX_TEST_ASSERT(strncmp(t1.ptr, "ein test", t1.length) == 0, "failed");
252 UCX_TEST_ASSERT(strncmp(t2.ptr, "abc", t2.length) == 0, "failed");
253 UCX_TEST_ASSERT(strncmp(t3.ptr, "123", t3.length) == 0, "failed");
254 UCX_TEST_ASSERT(strncmp(t4.ptr, "xyz", t4.length) == 0, "failed");
255 UCX_TEST_ASSERT(t5.length == 0, "string t5 not empty");
256 UCX_TEST_ASSERT(empty.length == 0, "empty string failed");
257 UCX_TEST_END
258 }
260 UCX_TEST(test_sstrprefixsuffix) {
261 sstr_t str = ST("test my prefix and my suffix");
262 sstr_t empty = ST("");
264 UCX_TEST_BEGIN
266 UCX_TEST_ASSERT(!sstrprefix(empty, S("pref")), "prefix empty string fails");
267 UCX_TEST_ASSERT(!sstrsuffix(empty, S("suf")), "suffix empty string fails");
269 UCX_TEST_ASSERT(sstrprefix(str, empty), "empty prefix fails");
270 UCX_TEST_ASSERT(sstrsuffix(str, empty), "empty suffix fails");
272 UCX_TEST_ASSERT(sstrprefix(empty, empty), "string and prefix empty fails");
273 UCX_TEST_ASSERT(sstrsuffix(empty, empty), "string and suffix empty fails");
275 UCX_TEST_ASSERT(sstrprefix(str, S("test ")), "prefix false negative");
276 UCX_TEST_ASSERT(!sstrprefix(str, S("8-) fsck ")), "prefix false positive");
278 UCX_TEST_ASSERT(sstrsuffix(str, S("fix")), "suffix false negative");
279 UCX_TEST_ASSERT(!sstrsuffix(str, S("fox")), "suffix false positive");
282 UCX_TEST_END
283 }