Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
21.string.find.cpp
Go to the documentation of this file.
1
29#include <string> // for string
30#include <cstddef> // size_t
31#include <exception> // for exception
32
33#include <rw_strings.h> // for StringMembers
34#include <rw_driver.h> // for rw_assert()
35#include <rw_allocator.h> // for UserAlloc
36#include <rw_char.h> // for rw_expand()
37
38
40// for convenience and brevity
41#define Find(sig) StringIds::find_ ## sig
42
43static const char* const exceptions[] = {
44 "unknown exception", "out_of_range", "length_error",
45 "bad_alloc", "exception"
46};
47
48
50// exercises:
51// find (const value_type*)
52static const StringTestCase
53cptr_test_cases [] = {
54
55#undef TEST
56#define TEST(str, arg, res) { \
57 __LINE__, -1, -1, -1, -1, -1, \
58 str, sizeof str - 1, \
59 arg, sizeof arg - 1, 0, res, 0 \
60 }
61
62 // +----------------------------------- controlled sequence
63 // | +--------------------- sequence to be found
64 // | | +---- expected result
65 // | | |
66 // | | |
67 // V V V
68 TEST ("", "", 0),
69 TEST ("", "a", NPOS),
70 TEST ("", "\0", 0),
71
72 TEST ("ab", "a", 0),
73
74 TEST ("\0", "", 0),
75 TEST ("\0", "\0", 0),
76 TEST ("\0", "a", NPOS),
77
78 TEST ("aababcabcd", "", 0),
79 TEST ("aababcabcd", "a", 0),
80 TEST ("aababcabcd", "ab", 1),
81 TEST ("aababcabcd", "abc", 3),
82 TEST ("aababcabcd", "abcd", 6),
83 TEST ("aababcabcd", "abcde", NPOS),
84
85 TEST ("bbcdefghij", "a", NPOS),
86 TEST ("abcdefghij", "a", 0),
87 TEST ("abcdefghij", "f", 5),
88 TEST ("abcdefghij", "j", 9),
89
90 TEST ("edfcbbhjig", "cba", NPOS),
91 TEST ("edfcbahjig", "cba", 3),
92 TEST ("edfcbahcba", "cba", 3),
93 TEST ("cbacbahjig", "cba", 0),
94 TEST ("abcbcbd", "bcbd", 3),
95
96 TEST ("e\0cb\0\0g", "b\0\0g", 3),
97 TEST ("e\0cb\0\0g", "ecb", NPOS),
98 TEST ("\0cb\0\0ge", "\0\0ge", 0),
99 TEST ("\0cb\0\0ge", "cb\0", 1),
100 TEST ("e\0cbg\0\0", "bg", 3),
101 TEST ("e\0cbg\0\0", "cba", NPOS),
102
103 TEST ("bcbedfbjih", "a", NPOS),
104 TEST ("bcaedfajih", "a", 2),
105 TEST ("bcedfaajih", "a", 5),
106 TEST ("bcaaedfaji", "a", 2),
107
108 TEST ("aaaaaaaaaa", "aaaaaaaaaa", 0),
109 TEST ("aaaaabaaaa", "aaaaaaaaaa", NPOS),
110 TEST ("aaaabaaaaa", "aaaaa", 5),
111 TEST ("aaaaaaaaaa", "aaaaaaaaa", 0),
112 TEST ("aaaaaaaaa", "aaaaaaaaaa", NPOS),
113
114 TEST ("x@4096", "", 0),
115 TEST ("x@4096", "a", NPOS),
116 TEST ("x@4096", "x", 0),
117 TEST ("x@4096", "xxx", 0),
118 TEST ("x@4096", "xxa", NPOS),
119 TEST ("abc", "x@4096", NPOS),
120 TEST ("xxxxxxxxxx", "x@4096", NPOS),
121
122 TEST ("abcdefghij", 0, 0),
123 TEST ("\0cb\0\0ge", 0, 0),
124 TEST ("x@4096", 0, 0),
125
126 TEST ("last test", "test", 5)
127};
128
129
131// exercises:
132// find (const basic_string&)
133static const StringTestCase
134cstr_test_cases [] = {
135
136#undef TEST
137#define TEST(str, arg, res) { \
138 __LINE__, -1, -1, -1, -1, -1, \
139 str, sizeof str - 1, \
140 arg, sizeof arg - 1, 0, res, 0 \
141 }
142
143 // +------------------------------------ controlled sequence
144 // | +---------------------- sequence to be found
145 // | | +----- expected result
146 // | | |
147 // | | |
148 // V V V
149 TEST ("ab", "a", 0),
150
151 TEST ("", "", 0),
152 TEST ("", "\0", NPOS),
153 TEST ("", "a", NPOS),
154
155 TEST ("\0", "", 0),
156 TEST ("\0", "\0", 0),
157 TEST ("\0", "\0\0", NPOS),
158 TEST ("\0", "a", NPOS),
159 TEST ("\0a", "a\0", NPOS),
160
161 TEST ("aababcabcd", "", 0),
162 TEST ("aababcabcd", "a", 0),
163 TEST ("aababcabcd", "ab", 1),
164 TEST ("aababcabcd", "abc", 3),
165 TEST ("aababcabcd", "abcd", 6),
166 TEST ("aababcabcd", "abcde", NPOS),
167
168 TEST ("bbcdefghij", "a", NPOS),
169 TEST ("abcdefghij", "a", 0),
170 TEST ("abcdefghij", "f", 5),
171 TEST ("abcdefghij", "j", 9),
172
173 TEST ("edfcbbhjig", "cba", NPOS),
174 TEST ("edfcbahjig", "cba", 3),
175 TEST ("edfcbahcba", "cba", 3),
176 TEST ("cbacbahjig", "cba", 0),
177 TEST ("abcbcbd", "bcbd", 3),
178
179 TEST ("e\0cb\0\0g", "b\0\0g", 3),
180 TEST ("e\0cb\0\0g", "ecb", NPOS),
181 TEST ("\0cb\0\0ge", "\0\0ge", 3),
182 TEST ("\0cb\0\0ge", "cb\0", 1),
183 TEST ("\0cb\0\0ge", "cb\0a", NPOS),
184 TEST ("e\0cbg\0\0", "bg", 3),
185 TEST ("e\0cbg\0\0", "\0\0", 5),
186 TEST ("e\0cbg\0\0", "\0\0a", NPOS),
187 TEST ("e\0cbg\0\0", "cba", NPOS),
188
189 TEST ("bcbedfbjih", "a", NPOS),
190 TEST ("bcaedfajih", "a", 2),
191 TEST ("bcedfaajih", "a", 5),
192 TEST ("bcaaedfaji", "a", 2),
193
194 TEST ("aaaaaaaaaa", "aaaaaaaaaa", 0),
195 TEST ("aaaaabaaaa", "aaaaaaaaaa", NPOS),
196 TEST ("aaaabaaaaa", "aaaaa", 5),
197 TEST ("aaaaaaaaaa", "aaaaaaaaa", 0),
198 TEST ("aaaaaaaaa", "aaaaaaaaaa", NPOS),
199
200 TEST ("x@4096", "", 0),
201 TEST ("x@4096", "a", NPOS),
202 TEST ("x@4096", "x", 0),
203 TEST ("x@4096", "xxx", 0),
204 TEST ("x@4096", "xxa", NPOS),
205 TEST ("abc", "x@4096", NPOS),
206 TEST ("xxxxxxxxxx", "x@4096", NPOS),
207
208 TEST ("abcdefghij", 0, 0),
209 TEST ("\0cb\0\0ge", 0, 0),
210 TEST ("x@4096", 0, 0),
211
212 TEST ("last test", "test", 5)
213};
214
215
217// exercises:
218// find (const value_type*, size_type)
219static const StringTestCase
220cptr_size_test_cases [] = {
221
222#undef TEST
223#define TEST(str, arg, off, res) { \
224 __LINE__, off, -1, -1, -1, -1, \
225 str, sizeof str - 1, \
226 arg, sizeof arg - 1, 0, res, 0 \
227 }
228
229 // +-------------------------------------- controlled sequence
230 // | +------------------------- sequence to be found
231 // | | +--------- find() off argument
232 // | | | +-- expected result
233 // | | | |
234 // | | | |
235 // V V V V
236 TEST ("ab", "a", 0, 0),
237
238 TEST ("", "", 0, 0),
239 TEST ("", "\0", 0, 0),
240 TEST ("", "a", 0, NPOS),
241
242 TEST ("\0", "", 0, 0),
243 TEST ("\0", "\0", 0, 0),
244 TEST ("\0", "\0", 1, 1),
245 TEST ("\0", "a", 0, NPOS),
246
247 TEST ("aababcabcd", "", 0, 0),
248 TEST ("aababcabcd", "a", 0, 0),
249 TEST ("aababcabcd", "ab", 0, 1),
250 TEST ("aababcabcd", "abc", 0, 3),
251 TEST ("aababcabcd", "abcd", 0, 6),
252 TEST ("aababcabcd", "abcde", 0, NPOS),
253
254 TEST ("bbcdefghij", "a", 0, NPOS),
255 TEST ("abcdefghij", "a", 0, 0),
256 TEST ("abcdefghij", "a", 2, NPOS),
257 TEST ("abcdefghij", "f", 2, 5),
258 TEST ("abcdefghij", "f", 7, NPOS),
259 TEST ("abcdefghij", "j", 9, 9),
260
261 TEST ("edfcbbhjig", "cba", 0, NPOS),
262 TEST ("edfcbahjig", "cba", 1, 3),
263 TEST ("edfcbahjig", "cba", 4, NPOS),
264 TEST ("edfcbahcba", "cba", 1, 3),
265 TEST ("edfcbahcba", "cba", 5, 7),
266 TEST ("cbacbahjig", "cba", 5, NPOS),
267 TEST ("abcbcbd", "bcbd", 0, 3),
268
269 TEST ("e\0cb\0\0g", "b\0\0g", 0, 3),
270 TEST ("e\0cb\0\0g", "b\0\0g", 4, NPOS),
271 TEST ("e\0cb\0\0g", "ecb", 0, NPOS),
272 TEST ("\0cb\0\0ge", "\0\0ge", 6, 6),
273 TEST ("\0cb\0\0ge", "cb\0", 1, 1),
274 TEST ("e\0cbg\0\0", "bg", 1, 3),
275 TEST ("e\0cbg\0\0", "cba", 0, NPOS),
276
277 TEST ("bcbedfbjih", "a", 0, NPOS),
278 TEST ("bcaedfajih", "a", 1, 2),
279 TEST ("bcedfaajih", "a", 6, 6),
280 TEST ("bcaaedfaji", "a", 5, 7),
281
282 TEST ("aaaaaaaaaa", "aaaaaaaaaa", 0, 0),
283 TEST ("aaaaaaaaaa", "aaaaaaaaaa", 1, NPOS),
284 TEST ("aaaaabaaaa", "aaaaaaaaaa", 0, NPOS),
285 TEST ("aaaabaaaaa", "aaaaa", 0, 5),
286 TEST ("aaaabaaaaa", "aaaaa", 6, NPOS),
287 TEST ("aaaaaaaaaa", "aaaaaaaaa", 0, 0),
288 TEST ("aaaaaaaaaa", "aaaaaaaaa", 1, 1),
289 TEST ("aaaaaaaaaa", "aaaaaaaaa", 2, NPOS),
290 TEST ("aaaaaaaaa", "aaaaaaaaaa", 0, NPOS),
291
292 TEST ("x@4096", "", 0, 0),
293 TEST ("x@4096", "a", 0, NPOS),
294 TEST ("x@4096", "x", 0, 0),
295 TEST ("x@4096", "xxx", 10, 10),
296 TEST ("x@4096", "xxa", 10, NPOS),
297 TEST ("abc", "x@4096", 2, NPOS),
298 TEST ("xxxxxxxxxx", "x@4096", 0, NPOS),
299
300 TEST ("x@4096", "xxx", 4092, 4092),
301 TEST ("x@4096", "xxx", 4094, NPOS),
302
303 TEST ("abcdefghij", 0, 0, 0),
304 TEST ("abcdefghij", 0, 1, NPOS),
305 TEST ("\0cb\0\0ge", 0, 5, 5),
306 TEST ("x@4096", 0, 0, 0),
307 TEST ("x@4096", 0, 1, NPOS),
308
309 TEST ("", "", 1, NPOS),
310 TEST ("abcdefghij", "abc", 10, NPOS),
311 TEST ("abcdefghij", "cba", 10, NPOS),
312
313 TEST ("last test", "test", 0, 5)
314};
315
316
318// exercises:
319// find (const value_type*, size_type, size_type)
320static const StringTestCase
321cptr_size_size_test_cases [] = {
322
323#undef TEST
324#define TEST(str, arg, off, size, res) { \
325 __LINE__, off, size, -1, -1, -1, \
326 str, sizeof str - 1, \
327 arg, sizeof arg - 1, 0, res, 0 \
328 }
329
330 // +--------------------------------------- controlled sequence
331 // | +-------------------------- sequence to be found
332 // | | +---------- find() off argument
333 // | | | +------ find() n argument
334 // | | | | +-- expected result
335 // | | | | |
336 // | | | | |
337 // | | | | |
338 // | | | | |
339 // | | | | |
340 // V V V V V
341 TEST ("ab", "a", 0, 1, 0),
342
343 TEST ("", "", 0, 0, 0),
344 TEST ("", "\0", 0, 0, 0),
345 TEST ("", "\0", 0, 1, NPOS),
346 TEST ("", "a", 0, 0, 0),
347 TEST ("", "a", 0, 1, NPOS),
348
349 TEST ("aababcabcd", "", 0, 0, 0),
350 TEST ("aababcabcd", "a", 0, 1, 0),
351 TEST ("aababcabcd", "ab", 0, 2, 1),
352 TEST ("aababcabcd", "abc", 0, 3, 3),
353 TEST ("aababcabcd", "abcd", 0, 4, 6),
354 TEST ("aababcabcd", "abcde", 0, 5, NPOS),
355 TEST ("aababcabcd", "aababcabcde", 0, 11, NPOS),
356
357 TEST ("\0", "", 0, 0, 0),
358 TEST ("\0", "\0", 0, 1, 0),
359 TEST ("\0", "\0", 1, 1, NPOS),
360 TEST ("\0\0", "\0\0", 1, 1, 1),
361 TEST ("\0", "a", 0, 1, NPOS),
362
363 TEST ("edfcbbhjig", "cba", 0, 3, NPOS),
364 TEST ("edfcbbhjig", "cba", 0, 2, 3),
365 TEST ("edfcbahjig", "cba", 1, 3, 3),
366 TEST ("edfcbahjig", "cba", 4, 3, NPOS),
367 TEST ("edfcbahjig", "cba", 4, 1, NPOS),
368 TEST ("edfcbahcba", "cba", 1, 3, 3),
369 TEST ("edfcbehcba", "cba", 1, 2, 3),
370 TEST ("edfcbahcba", "cba", 5, 3, 7),
371 TEST ("cbacbahjig", "cba", 5, 3, NPOS),
372 TEST ("cbacbahjcg", "cba", 5, 1, 8),
373 TEST ("abcbcbd", "bcbd", 0, 4, 3),
374
375 TEST ("e\0cb\0\0g", "b\0\0g", 0, 4, 3),
376 TEST ("e\0cb\0\0g", "b\0\0g", 4, 4, NPOS),
377 TEST ("e\0cb\0\0b", "b\0\0g", 4, 1, 6),
378 TEST ("\0b\0\0gb\0","b\0\0g", 2, 2, 5),
379 TEST ("\0b\0\0gb\0","b\0\0g", 0, 2, 1),
380 TEST ("\0b\0\0gb\0","b\0\0g", 0, 3, 1),
381 TEST ("e\0cb\0\0g", "ecb", 0, 2, NPOS),
382 TEST ("\0cb\0\0ge", "\0\0ge", 6, 4, NPOS),
383 TEST ("\0cb\0\0ge", "\0\0ge", 6, 0, 6),
384 TEST ("\0cb\0\0ge", "cb\0", 1, 3, 1),
385 TEST ("e\0cbg\0\0", "bg", 1, 2, 3),
386 TEST ("e\0cbg\0\0", "cba", 0, 3, NPOS),
387 TEST ("e\0cbg\0\0", "cba", 0, 2, 2),
388
389 TEST ("e\0a\0", "e\0a\0\0", 0, 4, 0),
390 TEST ("e\0a\0", "e\0a\0\0", 0, 5, NPOS),
391 TEST ("ee\0a\0", "e\0a\0\0", 1, 4, 1),
392 TEST ("ee\0a\0", "e\0a\0\0", 1, 5, NPOS),
393
394 TEST ("aaaaaaaaaa", "aaaaaaaaaa", 0, 10, 0),
395 TEST ("aaaaaaaaaa", "aaaaaaaaaa", 1, 10, NPOS),
396 TEST ("aaaaaaaaaa", "aaaaaaaaaa", 1, 9, 1),
397 TEST ("aaaaabaaaa", "aaaaaaaaaa", 0, 10, NPOS),
398 TEST ("aaaaabaaaa", "aaaaaaaaaa", 1, 4, 1),
399 TEST ("aaaaabaaaa", "aaaaaaaaaa", 2, 4, 6),
400 TEST ("aaaabaaaaa", "aaaaa", 0, 5, 5),
401 TEST ("aaaabaaaaa", "aaaaa", 0, 4, 0),
402 TEST ("aaaabaaaaa", "aaaaa", 6, 5, NPOS),
403 TEST ("aaaaaaaaaa", "aaaaaaaaa", 0, 9, 0),
404 TEST ("aaaaaaaaaa", "aaaaaaaaa", 1, 9, 1),
405 TEST ("aaaaaaaaaa", "aaaaaaaaa", 2, 9, NPOS),
406 TEST ("aaaaaaaaaa", "aaaaaaaaa", 2, 8, 2),
407 TEST ("aaaaaaaaa", "aaaaaaaaaa", 0, 10, NPOS),
408 TEST ("aaaaaaaaa", "aaaaaaaaaa", 0, 7, 0),
409
410 TEST ("x@4096", "", 0, 0, 0),
411 TEST ("x@4096", "a", 0, 1, NPOS),
412 TEST ("x@4096", "x", 0, 1, 0),
413 TEST ("x@4096", "xxx", 10, 3, 10),
414 TEST ("x@4096", "xxa", 10, 3, NPOS),
415 TEST ("x@4096", "xxa", 10, 2, 10),
416 TEST ("abc", "x@4096", 2, 10, NPOS),
417 TEST ("xxxxxxxxxx", "x@4096", 0, 4096, NPOS),
418 TEST ("xxxxxxxxxx", "x@4096", 2, 4, 2),
419
420 TEST ("x@4096", "xxx", 4093, 3, 4093),
421 TEST ("x@4096", "xxx", 4094, 3, NPOS),
422 TEST ("x@4096", "xxx", 4094, 2, 4094),
423
424 TEST ("abcdefghij", 0, 0, 9, 0),
425 TEST ("abcdefghij", 0, 1, 9, NPOS),
426 TEST ("\0cb\0\0ge", 0, 5, 7, NPOS),
427 TEST ("\0cb\0ge\0", 0, 6, 1, 6),
428 TEST ("x@4096", 0, 0, 4096, 0),
429 TEST ("x@4096", 0, 1, 4096, NPOS),
430 TEST ("x@4096", 0, 5, 4091, 5),
431
432 TEST ("", "", 1, 0, NPOS),
433 TEST ("abcdefghij", "abc", 10, 3, NPOS),
434 TEST ("abcdefghij", "cba", 10, 1, NPOS),
435
436 // excercise strictly undefined behavior
437 TEST ("", "cba", 0, -1, NPOS),
438 TEST ("abcdefghij", "cba", 0, -1, NPOS),
439 TEST ("x@4096", "xxx", 0, -1, NPOS),
440 TEST ("abcdefghij", "x@4096", 0, -1, NPOS),
441
442 TEST ("last test", "test", 0, 4, 5)
443};
444
445
447// exercises:
448// find (const basic_string&, size_type)
449static const StringTestCase
450cstr_size_test_cases [] = {
451
452#undef TEST
453#define TEST(str, arg, off, res) { \
454 __LINE__, off, -1, -1, -1, -1, \
455 str, sizeof str - 1, \
456 arg, sizeof arg - 1, 0, res, 0 \
457 }
458
459 // +--------------------------------------- controlled sequence
460 // | +------------------------- sequence to be found
461 // | | +---------- find() off argument
462 // | | | +---- expected result
463 // | | | |
464 // | | | |
465 // V V V V
466 TEST ("ab", "a", 0, 0),
467
468 TEST ("", "", 0, 0),
469 TEST ("", "\0", 0, NPOS),
470 TEST ("", "a", 0, NPOS),
471
472 TEST ("\0", "", 0, 0),
473 TEST ("\0", "\0", 0, 0),
474 TEST ("\0", "\0", 1, NPOS),
475 TEST ("\0\0", "\0", 1, 1),
476 TEST ("\0", "a", 0, NPOS),
477
478 TEST ("bbcdefghij", "a", 0, NPOS),
479 TEST ("abcdefghij", "a", 0, 0),
480 TEST ("abcdefghij", "a", 2, NPOS),
481 TEST ("abcdefghij", "f", 2, 5),
482 TEST ("abcdefghij", "f", 7, NPOS),
483 TEST ("abcdefghij", "j", 9, 9),
484
485 TEST ("edfcbbhjig", "cba", 0, NPOS),
486 TEST ("edfcbahjig", "cba", 1, 3),
487 TEST ("edfcbahjig", "cba", 4, NPOS),
488 TEST ("edfcbahcba", "cba", 1, 3),
489 TEST ("edfcbahcba", "cba", 5, 7),
490 TEST ("cbacbahjig", "cba", 5, NPOS),
491 TEST ("abcbcbd", "bcbd", 0, 3),
492
493 TEST ("e\0cb\0\0g", "b\0\0g", 0, 3),
494 TEST ("e\0cb\0\0g", "b\0\0g", 4, NPOS),
495 TEST ("e\0cb\0\0g", "ecb", 0, NPOS),
496 TEST ("\0cb\0\0ge", "\0\0ge", 6, NPOS),
497 TEST ("\0cb\0\0ge", "\0\0ge", 1, 3),
498 TEST ("\0cb\0\0ge", "cb\0", 1, 1),
499 TEST ("\0cbg\0\0e", "cb\0", 1, NPOS),
500 TEST ("e\0cbg\0\0", "bg", 1, 3),
501 TEST ("e\0cbg\0\0", "cba", 0, NPOS),
502
503 TEST ("bcbedfbjih", "a", 0, NPOS),
504 TEST ("bcaedfajih", "a", 1, 2),
505 TEST ("bcedfaajih", "a", 6, 6),
506 TEST ("bcaaedfaji", "a", 5, 7),
507
508 TEST ("aaaaaaaaaa", "aaaaaaaaaa", 0, 0),
509 TEST ("aaaaaaaaaa", "aaaaaaaaaa", 1, NPOS),
510 TEST ("aaaaabaaaa", "aaaaaaaaaa", 0, NPOS),
511 TEST ("aaaabaaaaa", "aaaaa", 0, 5),
512 TEST ("aaaabaaaaa", "aaaaa", 6, NPOS),
513 TEST ("aaaaaaaaaa", "aaaaaaaaa", 0, 0),
514 TEST ("aaaaaaaaaa", "aaaaaaaaa", 1, 1),
515 TEST ("aaaaaaaaaa", "aaaaaaaaa", 2, NPOS),
516 TEST ("aaaaaaaaa", "aaaaaaaaaa", 0, NPOS),
517
518 TEST ("x@4096", "", 0, 0),
519 TEST ("x@4096", "a", 0, NPOS),
520 TEST ("x@4096", "x", 0, 0),
521 TEST ("x@4096", "xxx", 10, 10),
522 TEST ("x@4096", "xxa", 10, NPOS),
523 TEST ("abc", "x@4096", 2, NPOS),
524 TEST ("xxxxxxxxxx", "x@4096", 0, NPOS),
525
526 TEST ("x@4096", "xxx", 4093, 4093),
527 TEST ("x@4096", "xxx", 4094, NPOS),
528
529 TEST ("abcdefghij", 0, 0, 0),
530 TEST ("abcdefghij", 0, 1, NPOS),
531 TEST ("\0cb\0\0ge", 0, 5, NPOS),
532 TEST ("\0cb\0\0ge", 0, 0, 0),
533 TEST ("x@4096", 0, 0, 0),
534 TEST ("x@4096", 0, 1, NPOS),
535
536 TEST ("", "", 1, NPOS),
537 TEST ("abcdefghij", "abc", 10, NPOS),
538 TEST ("abcdefghij", "cba", 10, NPOS),
539
540 TEST ("last test", "test", 0, 5)
541};
542
543
545// exercises:
546// find (value_type)
547static const StringTestCase
548val_test_cases [] = {
549
550#undef TEST
551#define TEST(str, val, res) { \
552 __LINE__, -1, -1, -1, -1, val, \
553 str, sizeof str - 1, 0, 0, 0, res, 0 \
554 }
555
556 // +----------------------------- controlled sequence
557 // | +-------------- character to be found
558 // | | +------ expected result
559 // | | |
560 // | | |
561 // V V V
562 TEST ("ab", 'a', 0),
563
564 TEST ("", 'a', NPOS),
565 TEST ("", '\0', NPOS),
566
567 TEST ("\0", '\0', 0),
568 TEST ("\0\0", '\0', 0),
569 TEST ("\0", 'a', NPOS),
570
571 TEST ("e\0cb\0\0g", '\0', 1),
572 TEST ("e\0cb\0\0g", 'b', 3),
573 TEST ("e\0cb\0\0g", 'a', NPOS),
574 TEST ("\0cbge\0\0", '\0', 0),
575 TEST ("\0cbge\0\0", 'b', 2),
576 TEST ("\0cbge\0\0", 'a', NPOS),
577
578 TEST ("x@4096", 'x', 0),
579 TEST ("x@4096", '\0', NPOS),
580 TEST ("x@4096", 'a', NPOS),
581
582 TEST ("last test", 't', 3)
583};
584
585
587// exercises:
588// find (value_type, size_type)
589static const StringTestCase
590val_size_test_cases [] = {
591
592#undef TEST
593#define TEST(str, val, off, res) { \
594 __LINE__, off, -1, -1, -1, val, \
595 str, sizeof str - 1, 0, 0, 0, res, 0 \
596 }
597
598 // +------------------------------ controlled sequence
599 // | +--------------- character to be found
600 // | | +--------- find() off argument
601 // | | | +--- expected result
602 // | | | |
603 // | | | |
604 // V V V V
605 TEST ("ab", 'a', 0, 0),
606
607 TEST ("", 'a', 0, NPOS),
608 TEST ("", '\0', 0, NPOS),
609
610 TEST ("\0", '\0', 1, NPOS),
611 TEST ("\0", 'a', 0, NPOS),
612 TEST ("\0\0", '\0', 1, 1),
613 TEST ("\0\0", 'a', 3, NPOS),
614 TEST ("\0\0", '\0', 3, NPOS),
615
616 TEST ("e\0cb\0\0g", '\0', 1, 1),
617 TEST ("e\0cb\0\0g", '\0', 2, 4),
618 TEST ("e\0cb\0\0g", '\0', 6, NPOS),
619 TEST ("e\0cb\0\0g", 'b', 1, 3),
620 TEST ("e\0cb\0\0g", 'b', 4, NPOS),
621 TEST ("e\0cb\0\0g", 'a', 0, NPOS),
622 TEST ("\0cbge\0\0", '\0', 0, 0),
623 TEST ("\0cbge\0\0", '\0', 1, 5),
624 TEST ("\0cbge\0\0", '\0', 9, NPOS),
625 TEST ("\0cbge\0\0", 'b', 0, 2),
626 TEST ("\0bgeb\0\0", 'b', 2, 4),
627 TEST ("\0cbge\0\0", 'a', 1, NPOS),
628
629 TEST ("x@4096", 'x', 0, 0),
630 TEST ("x@4096", 'x', 5, 5),
631 TEST ("x@4096", '\0', 0, NPOS),
632 TEST ("x@2048\0xxx",'\0', 0, 2048),
633 TEST ("x@4096", 'a', 3, NPOS),
634 TEST ("x@4096", 'x', 4096, NPOS),
635 TEST ("x@4096", 'x', 4095, 4095),
636
637 TEST ("last test", 't', 0, 3)
638};
639
640
642template <class charT, class Traits, class Allocator>
644 const StringFunc &func,
645 const StringTestCase &tcase)
646{
647 typedef std::basic_string <charT, Traits, Allocator> String;
648
649 static const std::size_t BUFSIZE = 256;
650
651 static charT wstr_buf [BUFSIZE];
652 static charT warg_buf [BUFSIZE];
653
654 std::size_t str_len = sizeof wstr_buf / sizeof *wstr_buf;
655 std::size_t arg_len = sizeof warg_buf / sizeof *warg_buf;
656
657 charT* wstr = rw_expand (wstr_buf, tcase.str, tcase.str_len, &str_len);
658 charT* warg = rw_expand (warg_buf, tcase.arg, tcase.arg_len, &arg_len);
659
660 // construct the string object and the argument string
661 const String s_str (wstr, str_len);
662 const String s_arg (warg, arg_len);
663
664 if (wstr != wstr_buf)
665 delete[] wstr;
666
667 if (warg != warg_buf)
668 delete[] warg;
669
670 wstr = 0;
671 warg = 0;
672
673 // save the state of the string object before the call
674 // to detect wxception safety violations (changes to
675 // the state of the object after an exception)
676 const StringState str_state (rw_get_string_state (s_str));
677
678 // string function argument
679 const charT* const arg_ptr = tcase.arg ? s_arg.c_str () : s_str.c_str ();
680 const String& arg_str = tcase.arg ? s_arg : s_str;
681 const charT arg_val = make_char (char (tcase.val), (charT*)0);
682
683 std::size_t size = tcase.size >= 0 ? tcase.size : s_arg.max_size () + 1;
684
685#ifndef _RWSTD_NO_EXCEPTIONS
686
687 // no exceptions expected
688 const char* expected = 0;
689 const char* caught = 0;
690
691#else // if defined (_RWSTD_NO_EXCEPTIONS)
692
693 if (tcase.bthrow)
694 return;
695
696#endif // _RWSTD_NO_EXCEPTIONS
697
698 try {
699 std::size_t res = 0;
700
701 switch (func.which_) {
702
703 case Find (cptr):
704 res = s_str.find (arg_ptr);
705 break;
706
707 case Find (cstr):
708 res = s_str.find (arg_str);
709 break;
710
711 case Find (cptr_size):
712 res = s_str.find (arg_ptr, tcase.off);
713 break;
714
715 case Find (cptr_size_size):
716 res = s_str.find (arg_ptr, tcase.off, size);
717 break;
718
719 case Find (cstr_size):
720 res = s_str.find (arg_str, tcase.off);
721 break;
722
723 case Find (val):
724 res = s_str.find (arg_val);
725 break;
726
727 case Find (val_size):
728 res = s_str.find (arg_val, tcase.off);
729 break;
730
731 default:
732 RW_ASSERT (!"logic error: unknown find overload");
733 return;
734 }
735
736 const std::size_t exp_res =
737 NPOS != tcase.nres ? tcase.nres : String::npos;
738
739 // verify the returned value
740 rw_assert (exp_res == res, 0, tcase.line,
741 "line %d. %{$FUNCALL} == %{?}%zu%{;}%{?}npos%{;}, "
742 "got %{?}%zu%{;}%{?}npos%{;}",
743 __LINE__, NPOS != tcase.nres, exp_res, NPOS == tcase.nres,
744 String::npos != res, res, String::npos == res);
745 }
746
747#ifndef _RWSTD_NO_EXCEPTIONS
748
749 catch (const std::exception &ex) {
750 caught = exceptions [4];
751 rw_assert (0, 0, tcase.line,
752 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
753 "unexpectedly%{;} caught std::%s(%#s)",
754 __LINE__, 0 != expected, expected, caught, ex.what ());
755 }
756 catch (...) {
757 caught = exceptions [0];
758 rw_assert (0, 0, tcase.line,
759 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
760 "unexpectedly%{;} caught %s",
761 __LINE__, 0 != expected, expected, caught);
762 }
763
764 if (caught) {
765 // verify that an exception thrown during allocation
766 // didn't cause a change in the state of the object
767 str_state.assert_equal (rw_get_string_state (s_str),
768 __LINE__, tcase.line, caught);
769 }
770 else if (-1 != tcase.bthrow) {
771 rw_assert (caught == expected, 0, tcase.line,
772 "line %d. %{$FUNCALL} %{?}expected %s, caught %s"
773 "%{:}unexpectedly caught %s%{;}",
774 __LINE__, 0 != expected, expected, caught, caught);
775 }
776
777#endif // _RWSTD_NO_EXCEPTIONS
778}
779
780
783
784int main (int argc, char** argv)
785{
786 static const StringTest
787 tests [] = {
788
789#undef TEST
790#define TEST(sig) { \
791 Find (sig), sig ## _test_cases, \
792 sizeof sig ## _test_cases / sizeof *sig ## _test_cases \
793 }
794
795 TEST (cptr),
796 TEST (cstr),
797 TEST (cptr_size),
798 TEST (cptr_size_size),
799 TEST (cstr_size),
800 TEST (val),
801 TEST (val_size)
802 };
803
804 const std::size_t test_count = sizeof tests / sizeof *tests;
805
806 return rw_run_string_test (argc, argv, __FILE__,
807 "lib.string.find",
808 test_find, tests, test_count);
809}
#define res
Definition 17.names.cpp:531
#define charT
Definition 17.names.cpp:456
#define val
Definition 17.names.cpp:570
#define Find(sig)
#define TEST(str, arg, res)
void test_find(charT, Traits *, Allocator *, const StringFunc &func, const StringTestCase &tcase)
std::char_traits< S > Traits
#define NPOS
#define main
int caught
UserChar make_char(char c, UserChar *)
Definition rw_char.h:74
_TEST_EXPORT char * rw_expand(char *, const char *, _RWSTD_SIZE_T=_RWSTD_SIZE_MAX, _RWSTD_SIZE_T *=0)
_TEST_EXPORT int rw_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630
StringState rw_get_string_state(const String &str)
Definition rw_strings.h:752
#define DEFINE_STRING_TEST_DISPATCH(fname)
Definition rw_strings.h:885
_TEST_EXPORT int rw_run_string_test(int, char **, const char *, const char *, StringTestFunc *, const StringTest *, _RWSTD_SIZE_T)
#define RW_ASSERT(expr)
int assert_equal(const StringState &, int, int, const char *) const
_RWSTD_SIZE_T arg_len
Definition rw_strings.h:600
_RWSTD_SIZE_T str_len
Definition rw_strings.h:597
const char * str
Definition rw_strings.h:596
const char * arg
Definition rw_strings.h:599
_RWSTD_SIZE_T nres
Definition rw_strings.h:603