tests/test_string.c

changeset 806
e06249e09f99
parent 777
e5b29e6f0615
child 809
b7e2e1d7ed22
equal deleted inserted replaced
805:26500fc24058 806:e06249e09f99
173 } 173 }
174 } 174 }
175 175
176 CX_TEST(test_strstr) { 176 CX_TEST(test_strstr) {
177 cxstring str = CX_STR("find the match in this string"); 177 cxstring str = CX_STR("find the match in this string");
178 cxstring longstr = CX_STR( 178
179 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl" 179 size_t const longstrpatternlen = 64 + cx_strstr_sbo_size;
180 "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx" 180 size_t const longstrlen = 320 + longstrpatternlen + 14;
181 "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij" 181
182 "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv" 182 char *longstrc = malloc(longstrlen+1);
183 "abababababababababababababababababababababababababababababababab" 183 char *longstrpatternc = malloc(longstrpatternlen+1);
184 "abababababababababababababababababababababababababababababababab" 184
185 "abababababababababababababababababababababababababababababababab" 185 memcpy(longstrc,
186 "abababababababababababababababababababababababababababababababab" 186 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"
187 "abababababababababababababababababababababababababababababababab" 187 "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
188 "abababababababababababababababababababababababababababababababab" 188 "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij"
189 "wxyz1234567890"); 189 "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv"
190 cxstring longstrpattern = CX_STR( 190 "abababababababababababababababababababababababababababababababab",
191 "abababababababababababababababababababababababababababababababab" 191 320
192 "abababababababababababababababababababababababababababababababab"
193 "abababababababababababababababababababababababababababababababab"
194 "abababababababababababababababababababababababababababababababab"
195 "abababababababababababababababababababababababababababababababab"
196 ); 192 );
197 cxstring longstrresult = CX_STR( 193 memcpy(longstrpatternc,
198 "abababababababababababababababababababababababababababababababab" 194 "abababababababababababababababababababababababababababababababab",
199 "abababababababababababababababababababababababababababababababab" 195 64
200 "abababababababababababababababababababababababababababababababab"
201 "abababababababababababababababababababababababababababababababab"
202 "abababababababababababababababababababababababababababababababab"
203 "abababababababababababababababababababababababababababababababab"
204 "wxyz1234567890"
205 ); 196 );
197 char x = 'a', y='b', z;
198 for (size_t i = 0; i < cx_strstr_sbo_size ; i++) {
199 longstrpatternc[64+i] = x;
200 longstrc[320+i] = x;
201 z=x; x=y; y=z;
202 }
203 longstrpatternc[longstrpatternlen] = '\0';
204 memcpy(longstrc+longstrlen-14, "wxyz1234567890", 15);
205
206 cxmutstr longstr = cx_mutstrn(longstrc, longstrlen);
207 cxstring longstrpattern = cx_strn(longstrpatternc, longstrpatternlen);
208 cxmutstr longstrresult = cx_mutstrn(longstrc+256, longstrlen-256);
206 209
207 CX_TEST_DO { 210 CX_TEST_DO {
208 cxstring notfound = cx_strstr(str, CX_STR("no match")); 211 cxstring notfound = cx_strstr(str, CX_STR("no match"));
209 CX_TEST_ASSERT(notfound.length == 0); 212 CX_TEST_ASSERT(notfound.length == 0);
210 213
214 217
215 result = cx_strstr(str, CX_STR("")); 218 result = cx_strstr(str, CX_STR(""));
216 CX_TEST_ASSERT(result.length == str.length); 219 CX_TEST_ASSERT(result.length == str.length);
217 CX_TEST_ASSERT(0 == strcmp(result.ptr, str.ptr)); 220 CX_TEST_ASSERT(0 == strcmp(result.ptr, str.ptr));
218 221
219 result = cx_strstr(longstr, longstrpattern); 222 cxmutstr resultm = cx_strstr_m(longstr, longstrpattern);
220 CX_TEST_ASSERT(result.length == longstrresult.length); 223 CX_TEST_ASSERT(resultm.length == longstrresult.length);
221 CX_TEST_ASSERT(0 == strcmp(result.ptr, longstrresult.ptr)); 224 CX_TEST_ASSERT(0 == strcmp(resultm.ptr, longstrresult.ptr));
222 225 }
223 // just for coverage, call the _m variant 226
224 cxmutstr mstr = cx_strdup(longstr); 227 free(longstrc);
225 cxmutstr m = cx_strstr_m(mstr, longstrpattern); 228 free(longstrpatternc);
226 CX_TEST_ASSERT(m.length == longstrresult.length);
227 CX_TEST_ASSERT(0 == strcmp(m.ptr, longstrresult.ptr));
228 cx_strfree(&mstr);
229 }
230 } 229 }
231 230
232 CX_TEST(test_strcmp) { 231 CX_TEST(test_strcmp) {
233 cxstring str = CX_STR("compare this"); 232 cxstring str = CX_STR("compare this");
234 CX_TEST_DO { 233 CX_TEST_DO {

mercurial