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