test/string_tests.c

Sat, 20 Jul 2013 11:13:26 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 20 Jul 2013 11:13:26 +0200
changeset 120
8170f658f017
parent 116
234920008754
child 123
7fb0f74517c5
permissions
-rw-r--r--

some fixes

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

mercurial