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