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