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