test/string_tests.c

changeset 116
234920008754
parent 104
9d3dea320d8e
child 123
7fb0f74517c5
equal deleted inserted replaced
115:965fd17ed9cf 116:234920008754
39 39
40 UCX_TEST_END 40 UCX_TEST_END
41 } 41 }
42 42
43 UCX_TEST_IMPLEMENT(test_sstr_len_cat) { 43 UCX_TEST_IMPLEMENT(test_sstr_len_cat) {
44 sstr_t s1 = S("1234"); 44 sstr_t s1 = ST("1234");
45 sstr_t s2 = S(".:.:."); 45 sstr_t s2 = ST(".:.:.");
46 sstr_t s3 = S("X"); 46 sstr_t s3 = ST("X");
47 47
48 size_t len = sstrnlen(3, s1, s2, s3); 48 size_t len = sstrnlen(3, s1, s2, s3);
49 sstr_t cat; 49 sstr_t cat;
50 cat.ptr = (char*) malloc(16); 50 cat.ptr = (char*) malloc(16);
51 cat.length = 16; 51 cat.length = 16;
70 } 70 }
71 71
72 UCX_TEST_IMPLEMENT(test_sstrsplit) { 72 UCX_TEST_IMPLEMENT(test_sstrsplit) {
73 73
74 const char *original = "this,is,a,csv,string"; 74 const char *original = "this,is,a,csv,string";
75 sstr_t test = S("this,is,a,csv,string"); /* use copy of original here */ 75 sstr_t test = ST("this,is,a,csv,string"); /* use copy of original here */
76 size_t n; 76 size_t n;
77 sstr_t *list; 77 sstr_t *list;
78 78
79 UCX_TEST_BEGIN 79 UCX_TEST_BEGIN
80 80
81 /* Nullpointer check */ 81 /* Nullpointer check */
82 n = 0; 82 n = 0;
83 UCX_TEST_ASSERT(sstrsplit(test, ST(""), &n) == NULL, 83 UCX_TEST_ASSERT(sstrsplit(test, S(""), &n) == NULL,
84 "empty delimiter must return NULL"); 84 "empty delimiter must return NULL");
85 85
86 /* no delimiter occurence (ndo) */ 86 /* no delimiter occurence (ndo) */
87 n = 0; 87 n = 0;
88 list = sstrsplit(test, ST("z"), &n); 88 list = sstrsplit(test, S("z"), &n);
89 UCX_TEST_ASSERT(n == 1, "ndo, list length must be 1"); 89 UCX_TEST_ASSERT(n == 1, "ndo, list length must be 1");
90 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "ndo, " 90 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "ndo, "
91 "original string shall be returned as single list element"); 91 "original string shall be returned as single list element");
92 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, 92 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
93 "ndo, original has been modified"); 93 "ndo, original has been modified");
94 free(list); 94 free(list);
95 95
96 /* partially matching delimiter (pmd) */ 96 /* partially matching delimiter (pmd) */
97 n = 0; 97 n = 0;
98 list = sstrsplit(test, ST("stringbuilder"), &n); 98 list = sstrsplit(test, S("stringbuilder"), &n);
99 UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1"); 99 UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1");
100 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, " 100 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, "
101 "original string shall be returned as single list element"); 101 "original string shall be returned as single list element");
102 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, 102 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
103 "pmd, original has been modified"); 103 "pmd, original has been modified");
104 free(list); 104 free(list);
105 105
106 /* matching single-char delimiter (mscd) */ 106 /* matching single-char delimiter (mscd) */
107 n = 0; 107 n = 0;
108 list = sstrsplit(test, ST(","), &n); 108 list = sstrsplit(test, S(","), &n);
109 UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5"); 109 UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5");
110 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch"); 110 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch");
111 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch"); 111 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch");
112 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch"); 112 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch");
113 UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch"); 113 UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch");
116 "mscd, original has been modified"); 116 "mscd, original has been modified");
117 free(list); 117 free(list);
118 118
119 /* matching multi-char delimiter (mmcd) */ 119 /* matching multi-char delimiter (mmcd) */
120 n = 0; 120 n = 0;
121 list = sstrsplit(test, ST("is"), &n); 121 list = sstrsplit(test, S("is"), &n);
122 UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3"); 122 UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3");
123 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch"); 123 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch");
124 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch"); 124 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch");
125 UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0, 125 UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0,
126 "mmcd, item 2 mismatch"); 126 "mmcd, item 2 mismatch");
128 "mmcd, original has been modified"); 128 "mmcd, original has been modified");
129 free(list); 129 free(list);
130 130
131 /* bounded list using single-char delimiter (blsc) */ 131 /* bounded list using single-char delimiter (blsc) */
132 n = 3; 132 n = 3;
133 list = sstrsplit(test, ST(","), &n); 133 list = sstrsplit(test, S(","), &n);
134 UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3"); 134 UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3");
135 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch"); 135 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch");
136 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch"); 136 UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch");
137 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0, 137 UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0,
138 "blsc, item 2 mismatch"); 138 "blsc, item 2 mismatch");
140 "blsc, original has been modified"); 140 "blsc, original has been modified");
141 free(list); 141 free(list);
142 142
143 /* bounded list using multi-char delimiter (blmc) */ 143 /* bounded list using multi-char delimiter (blmc) */
144 n = 2; 144 n = 2;
145 list = sstrsplit(test, ST("is"), &n); 145 list = sstrsplit(test, S("is"), &n);
146 UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2"); 146 UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2");
147 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch"); 147 UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch");
148 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, 148 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
149 "blmc, item 1 mismatch"); 149 "blmc, item 1 mismatch");
150 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, 150 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
151 "blmc, original has been modified"); 151 "blmc, original has been modified");
152 free(list); 152 free(list);
153 153
154 /* start with delimiter (swd) */ 154 /* start with delimiter (swd) */
155 n = 0; 155 n = 0;
156 list = sstrsplit(test, ST("this"), &n); 156 list = sstrsplit(test, S("this"), &n);
157 UCX_TEST_ASSERT(n == 2, "swd, list length must be 2"); 157 UCX_TEST_ASSERT(n == 2, "swd, list length must be 2");
158 UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty"); 158 UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty");
159 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, 159 UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0,
160 "swd, second item corrupt"); 160 "swd, second item corrupt");
161 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, 161 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
162 "swd, original has been modified"); 162 "swd, original has been modified");
163 free(list); 163 free(list);
164 164
165 /* end with delimiter (ewd) */ 165 /* end with delimiter (ewd) */
166 n = 0; 166 n = 0;
167 list = sstrsplit(test, ST("string"), &n); 167 list = sstrsplit(test, S("string"), &n);
168 UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2"); 168 UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2");
169 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0, 169 UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0,
170 "swd, first item corrupt"); 170 "swd, first item corrupt");
171 UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty"); 171 UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty");
172 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, 172 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
173 "ewd, original has been modified"); 173 "ewd, original has been modified");
174 free(list); 174 free(list);
175 175
176 /* exact match (exm) */ 176 /* exact match (exm) */
177 n = 0; 177 n = 0;
178 list = sstrsplit(test, ST("this,is,a,csv,string"), &n); 178 list = sstrsplit(test, S("this,is,a,csv,string"), &n);
179 UCX_TEST_ASSERT(n == 0, "exm, list length must be 0"); 179 UCX_TEST_ASSERT(n == 0, "exm, list length must be 0");
180 UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL"); 180 UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL");
181 free(list); 181 free(list);
182 182
183 /* substring (subs) */ 183 /* substring (subs) */
184 n = 0; 184 n = 0;
185 list = sstrsplit(test, ST("this,is,a,csv,string,with,extension"), &n); 185 list = sstrsplit(test, S("this,is,a,csv,string,with,extension"), &n);
186 UCX_TEST_ASSERT(n == 1, "subs, list length must be 1"); 186 UCX_TEST_ASSERT(n == 1, "subs, list length must be 1");
187 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, 187 UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0,
188 "subs, single item must be the original string"); 188 "subs, single item must be the original string");
189 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, 189 UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
190 "subs, original has been modified"); 190 "subs, original has been modified");

mercurial