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