Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
21.string.capacity.cpp
Go to the documentation of this file.
1
29#include <string> // for string
30#include <cstddef> // for ptrdiff_t, size_t
31#include <stdexcept> // for out_of_range
32
33#include <rw_strings.h> // for StringMembers
34#include <rw_driver.h> // for rw_test()
35#include <rw_allocator.h> // for UserAlloc
36#include <rw_char.h> // for rw_expand()
37#include <rw_new.h> // for bad_alloc, replacement operator new
38
39
41// for convenience and brevity
42#define Size(sig) StringIds::size_ ## sig
43#define Length(sig) StringIds::length_ ## sig
44#define MaxSize(sig) StringIds::max_size_ ## sig
45#define Resize(sig) StringIds::resize_ ## sig
46#define Capacity(sig) StringIds::capacity_ ## sig
47#define Reserve(sig) StringIds::reserve_ ## sig
48#define Clear(sig) StringIds::clear_ ## sig
49#define Empty(sig) StringIds::empty_ ## sig
50
51static const char* const exceptions[] = {
52 "unknown exception", "out_of_range", "length_error",
53 "bad_alloc", "exception"
54};
55
56
58// used to exercise
59// size ()
60static const StringTestCase
61size_void_test_cases [] = {
62
63#define length_void_test_cases size_void_test_cases
64
65#undef TEST
66#define TEST(str, res) { \
67 __LINE__, -1, -1, -1, -1, -1, \
68 str, sizeof str - 1, 0, 0, \
69 0, res, -1 \
70 }
71
72 // +--------------------------------------- controlled sequence
73 // | +------------------ expected result
74 // | |
75 // | |
76 // V V
77 TEST (0, 0),
78 TEST ("", 0),
79
80 TEST ("<U0>", 1),
81 TEST ("a", 1),
82 TEST (" ", 1),
83 TEST ("ab", 2),
84 TEST ("bc", 2),
85
86 TEST ("test string", 11),
87 TEST ("Test String", 11),
88
89 TEST ("t<U0> s", 4),
90 TEST ("Test<U0>string", 11),
91
92 TEST ("<U0>a<U0>b", 4),
93 TEST ("a<U0>@2b", 4),
94 TEST ("a<U0>@3b", 5),
95 TEST ("a<U0>@2b<U0>", 5),
96 TEST ("a<U0>b<U0>@2c", 6),
97 TEST ("a<U0>b<U0>c<U0>@2", 7),
98
99 TEST ("x@128", 128),
100 TEST ("x@207", 207),
101 TEST ("x@334", 334),
102 TEST ("x@540", 540),
103 TEST ("x@873", 873),
104 TEST ("x@1412", 1412),
105 TEST ("x@2284", 2284),
106 TEST ("x@3695", 3695),
107 TEST ("x@4096", 4096),
108
109 TEST ("last", 4)
110
111};
112
113
115// used to exercise
116// max_size ()
117static const StringTestCase
118max_size_void_test_cases [] = {
119
120#undef TEST
121#define TEST(str) { \
122 __LINE__, -1, -1, -1, -1, -1, \
123 str, sizeof str - 1, 0, 0, \
124 0, 0, -1 \
125 }
126
127 // +------------------------------ controlled sequence
128 // |
129 // |
130 // |
131 // V
132 TEST (0),
133 TEST (""),
134
135 TEST ("<U0>"),
136 TEST ("a"),
137
138 TEST ("test string"),
139 TEST ("a<U0>b<U0>c<U0>@2"),
140
141 TEST ("x@128"),
142 TEST ("x@207"),
143 TEST ("x@2284"),
144 TEST ("x@3695"),
145
146 TEST ("last")
147
148};
149
150
152// used to exercise
153// resize (size_type, value_type)
154static const StringTestCase
155resize_size_val_test_cases [] = {
156
157#undef TEST
158#define TEST(str, size, val, res, bthrow) { \
159 __LINE__, -1, size, -1, -1, val, \
160 str, sizeof str - 1, 0, 0, \
161 res, sizeof res - 1, bthrow \
162 }
163
164 // +------------------------------------------ controlled sequence
165 // | +------------------- new size
166 // | | +------------- value_type argument
167 // | | | +-------- expected result sequence
168 // | | | | +- exception info
169 // | | | | | 0 - no exception
170 // | | | | | 1 - length_error
171 // | | | | |
172 // | | | | +-----------+
173 // V V V V V
174 TEST ("", 0, 'a', "", 0),
175 TEST ("", 5, '\0', "<U0>@3<U0>@2", 0),
176 TEST ("", 334, 'x', "x@334", 0),
177
178 TEST ("<U0>", 0, 'a', "", 0),
179 TEST ("<U0>", 2, 'a', "<U0>a", 0),
180 TEST ("<U0>", 1, 'a', "<U0>", 0),
181 TEST ("<U0>", 128, 'a', "<U0>a@127", 0),
182
183 TEST ("a", 0, 'a', "", 0),
184 TEST ("a", 2, '\0', "a<U0>", 0),
185 TEST ("a", 540, 'a', "a@540", 0),
186
187 TEST ("a<U0>@3b", 10, 'a', "a<U0>@3baaaaa", 0),
188 TEST ("ab<U0>@3", 10, 'a', "ab<U0>@3aaaaa", 0),
189 TEST ("<U0>@3ab", 10, 'a', "<U0>@3abaaaaa", 0),
190 TEST ("a<U0>@3b", 7, '\0', "a<U0>@3b<U0>@2", 0),
191 TEST ("ab<U0>@3", 7, '\0', "ab<U0>@3<U0>@2", 0),
192 TEST ("<U0>@3ba", 7, '\0', "<U0>@3ba<U0>@2", 0),
193
194 TEST ("a<U0>b<U0>c<U0>@2", 6, 'a', "a<U0>b<U0>c<U0>", 0),
195 TEST ("a<U0>b<U0>c<U0>@2", 5, '\0', "a<U0>b<U0>c", 0),
196 TEST ("<U0>ba<U0>c<U0>@2", 1, '\0', "<U0>", 0),
197 TEST ("<U0>ba<U0>c<U0>@2", 0, '\0', "", 0),
198
199 TEST ("x@540", 127, 'a', "x@127", 0),
200 TEST ("x@873", 127, 'a', "x@127", 0),
201 TEST ("x@1412", 127, 'a', "x@127", 0),
202 TEST ("x@2284", 127, 'a', "x@127", 0),
203
204 TEST ("x@127", 128, 'a', "x@127a", 0),
205 TEST ("x@128", 207, 'a', "x@128a@79", 0),
206 TEST ("x@207", 334, 'a', "x@207a@127", 0),
207 TEST ("x@334", 540, 'a', "x@334a@206", 0),
208 TEST ("x@540", 873, 'a', "x@540a@333", 0),
209 TEST ("x@873", 1412, 'a', "x@873a@539", 0),
210 TEST ("x@1412", 2284, 'a', "x@1412a@872", 0),
211 TEST ("x@2284", 3695, 'a', "x@2284a@1411", 0),
212
213 TEST ("", NPOS, 'a', "", 1),
214 TEST ("abc", NPOS, 'a', "abc", 1),
215 TEST ("x@3695", NPOS, 'a', "x@3695", 1),
216
217 TEST ("last", 4, 't', "last", 0)
218
219};
220
221
223// used to exercise
224// resize (size_type)
225static const StringTestCase
226resize_size_test_cases [] = {
227
228#undef TEST
229#define TEST(str, size, res, bthrow) { \
230 __LINE__, -1, size, -1, -1, -1, \
231 str, sizeof str - 1, 0, 0, \
232 res, sizeof res - 1, bthrow \
233 }
234
235 // +------------------------------------------ controlled sequence
236 // | +------------------- new size
237 // | | +-------------- expected result sequence
238 // | | | +-- exception info
239 // | | | | 0 - no exception
240 // | | | | 1 - length_error
241 // | | | |
242 // V V V V
243 TEST ("", 0, "", 0),
244 TEST ("", 5, "", 0),
245 TEST ("", 334, "", 0),
246
247 TEST ("<U0>", 0, "", 0),
248 TEST ("<U0>", 2, "<U0>", 0),
249 TEST ("<U0>", 1, "<U0>", 0),
250 TEST ("<U0>", 127, "<U0>", 0),
251
252 TEST ("a", 0, "", 0),
253 TEST ("a", 2, "a", 0),
254 TEST ("a", 539, "a", 0),
255
256 TEST ("a<U0>@3b", 10, "a<U0>@3b", 0),
257 TEST ("ab<U0>@3", 10, "ab<U0>@3", 0),
258 TEST ("<U0>@3ab", 10, "<U0>@3ab", 0),
259 TEST ("a<U0>@3b", 7, "a<U0>@3b", 0),
260 TEST ("ab<U0>@3", 7, "ab<U0>@3", 0),
261 TEST ("<U0>@3ba", 7, "<U0>@3ba", 0),
262
263 TEST ("a<U0>b<U0>c<U0>@2", 5, "a<U0>b<U0>c", 0),
264 TEST ("a<U0>b<U0>c<U0>@2", 4, "a<U0>b<U0>", 0),
265 TEST ("<U0>ba<U0>c<U0>@2", 1, "<U0>", 0),
266 TEST ("<U0>ba<U0>c<U0>@2", 0, "", 0),
267
268 TEST ("x@540", 127, "x@127", 0),
269 TEST ("x@873", 127, "x@127", 0),
270 TEST ("x@1412", 127, "x@127", 0),
271 TEST ("x@2284", 127, "x@127", 0),
272
273 TEST ("x@127", 128, "x@127", 0),
274 TEST ("x@128", 207, "x@128", 0),
275 TEST ("x@207", 334, "x@207", 0),
276 TEST ("x@334", 540, "x@334", 0),
277 TEST ("x@540", 873, "x@540", 0),
278 TEST ("x@873", 1412, "x@873", 0),
279 TEST ("x@1412", 2284, "x@1412", 0),
280 TEST ("x@2284", 3695, "x@2284", 0),
281
282 TEST ("", NPOS, "", 1),
283 TEST ("abc", NPOS, "abc", 1),
284 TEST ("x@3695", NPOS, "x@3695", 1),
285
286 TEST ("last", 4, "last", 0)
287
288};
289
290
292// used to exercise
293// capacity ()
294static const StringTestCase
295capacity_void_test_cases [] = {
296
297#undef TEST
298#define TEST(str) { \
299 __LINE__, -1, -1, -1, -1, -1, \
300 str, sizeof str - 1, 0, 0, \
301 0, 0, -1 \
302 }
303
304 // +------------------------------ controlled sequence
305 // |
306 // |
307 // |
308 // V
309 TEST (0),
310 TEST (""),
311
312 TEST ("<U0>"),
313 TEST ("a"),
314
315 TEST ("test string"),
316 TEST ("a<U0>b<U0>c<U0>@2"),
317
318 TEST ("x@128"),
319 TEST ("x@207"),
320 TEST ("x@334"),
321 TEST ("x@540"),
322 TEST ("x@873"),
323 TEST ("x@1412"),
324 TEST ("x@2284"),
325 TEST ("x@3695"),
326 TEST ("x@4096"),
327
328 TEST ("last")
329};
330
331
333// used to exercise
334// reserve (size_type)
335static const StringTestCase
336reserve_size_test_cases [] = {
337
338#undef TEST
339#define TEST(str, size, bthrow) { \
340 __LINE__, -1, size, -1, -1, -1, \
341 str, sizeof str - 1, 0, 0, \
342 0, 0, bthrow \
343 }
344
345 // +--------------------------------------- controlled sequence
346 // | +---------------- requested capacity
347 // | | +----------- exception info
348 // | | | 0 - no exception
349 // | | | 1 - length_error
350 // | | |
351 // V V V
352 TEST ("", 0, 0),
353 TEST ("", 5, 0),
354 TEST ("", 334, 0),
355
356 TEST ("<U0>", 0, 0),
357 TEST ("<U0>", 2, 0),
358 TEST ("<U0>", 1, 0),
359 TEST ("<U0>", 127, 0),
360
361 TEST ("a", 0, 0),
362 TEST ("a", 2, 0),
363 TEST ("a", 539, 0),
364
365 TEST ("a<U0>@3b", 10, 0),
366 TEST ("ab<U0>@3", 10, 0),
367 TEST ("<U0>@3ab", 10, 0),
368 TEST ("a<U0>b<U0>c<U0>@2", 5, 0),
369 TEST ("a<U0>b<U0>c<U0>@2", 4, 0),
370 TEST ("<U0>ba<U0>c<U0>@2", 1, 0),
371 TEST ("<U0>ba<U0>c<U0>@2", 0, 0),
372
373 TEST ("x@540", 127, 0),
374 TEST ("x@873", 127, 0),
375 TEST ("x@1412", 127, 0),
376 TEST ("x@2284", 127, 0),
377
378 TEST ("x@127", 1412, 0),
379 TEST ("x@128", 1412, 0),
380 TEST ("x@206", 207, 0),
381 TEST ("x@333", 334, 0),
382 TEST ("x@540", 540, 0),
383 TEST ("x@873", 873, 0),
384 TEST ("x@1412", 2284, 0),
385 TEST ("x@2284", 3695, 0),
386
387 TEST ("", NPOS, 1),
388 TEST ("abc", NPOS, 1),
389 TEST ("x@3695", NPOS, 1),
390
391 TEST ("last", 4, 0)
392};
393
394
396// used to exercise
397// reserve ()
398static const StringTestCase
399reserve_void_test_cases [] = {
400
401#undef TEST
402#define TEST(str) { \
403 __LINE__, -1, 0, -1, -1, -1, \
404 str, sizeof str - 1, 0, 0, \
405 0, 0, -1 \
406 }
407
408 // +----------------------------- controlled sequence
409 // |
410 // |
411 // V
412 TEST (""),
413 TEST ("<U0>"),
414 TEST ("a"),
415
416 TEST ("a<U0>@3b"),
417 TEST ("a<U0>b<U0>c<U0>@2"),
418
419 TEST ("x@127"),
420 TEST ("x@128"),
421 TEST ("x@206"),
422 TEST ("x@333"),
423 TEST ("x@540"),
424 TEST ("x@873"),
425 TEST ("x@1412"),
426 TEST ("x@2284"),
427
428 TEST ("last")
429};
430
431
433// used to exercise
434// clear ()
435static const StringTestCase
436clear_void_test_cases [] = {
437
438#undef TEST
439#define TEST(str) { \
440 __LINE__, -1, -1, -1, -1, -1, \
441 str, sizeof str - 1, 0, 0, \
442 "", 0, -1 \
443 }
444
445 // +------------------------------ controlled sequence
446 // |
447 // |
448 // |
449 // V
450 TEST (0),
451 TEST (""),
452
453 TEST ("<U0>"),
454 TEST ("a"),
455
456 TEST ("test string"),
457 TEST ("a<U0>b<U0>c<U0>@2"),
458
459 TEST ("x@128"),
460 TEST ("x@207"),
461 TEST ("x@334"),
462 TEST ("x@540"),
463 TEST ("x@873"),
464 TEST ("x@1412"),
465 TEST ("x@2284"),
466 TEST ("x@3695"),
467 TEST ("x@4096"),
468
469 TEST ("last")
470};
471
472
474// used to exercise
475// empty ()
476static const StringTestCase
477empty_void_test_cases [] = {
478
479#undef TEST
480#define TEST(str, res) { \
481 __LINE__, -1, -1, -1, -1, -1, \
482 str, sizeof str - 1, 0, 0, \
483 0, res, -1 \
484 }
485
486 // +------------------------------ controlled sequence
487 // | +--------------- expected result
488 // | |
489 // | |
490 // V V
491 TEST (0, 1),
492 TEST ("", 1),
493
494 TEST ("<U0>", 0),
495 TEST ("a", 0),
496 TEST ("<U0>ab<U0>c", 0),
497
498 TEST ("x@128", 0),
499 TEST ("x@3695", 0),
500
501 TEST ("last", 0)
502};
503
504
505template <class charT, class Traits, class Allocator>
507 const StringFunc &func,
508 const StringTestCase &tcase)
509{
510 typedef std::basic_string <charT, Traits, Allocator> String;
511 typedef typename UserTraits<charT>::MemFun UTMemFun;
512
513 static const std::size_t BUFSIZE = 256;
514
515 static charT wstr_buf [BUFSIZE];
516 std::size_t str_len = sizeof wstr_buf / sizeof *wstr_buf;
517 charT* wstr = rw_expand (wstr_buf, tcase.str, tcase.str_len, &str_len);
518
519 static charT wres_buf [BUFSIZE];
520 std::size_t res_len = sizeof wres_buf / sizeof *wres_buf;
521 charT* wres = rw_expand (wres_buf, tcase.res, tcase.nres, &res_len);
522
523 // construct the string object to be modified and constant one
524 String str (wstr, str_len);
525 const String cstr (wstr, str_len);
526
527 if (wstr != wstr_buf)
528 delete[] wstr;
529
530 wstr = 0;
531
532 // save the state of the string object before the call
533 // to detect wxception safety violations (changes to
534 // the state of the object after an exception)
535 const StringState str_state (rw_get_string_state (str));
536
537 const charT arg_val = -1 != tcase.val ?
538 make_char (char (tcase.val), (charT*)0)
539 : make_char (char (), (charT*)0);
540
541 rwt_free_store* const pst = rwt_get_free_store (0);
542 SharedAlloc* const pal = SharedAlloc::instance ();
543
544 // iterate for`throw_after' starting at the next call to operator new,
545 // forcing each call to throw an exception, until the function finally
546 // succeeds (i.e, no exception is thrown)
547 std::size_t throw_count;
548 for (throw_count = 0; ; ++throw_count) {
549
550 // (name of) expected and caught exception
551 const char* expected = 0;
552 const char* caught = 0;
553
554#ifndef _RWSTD_NO_EXCEPTIONS
555
556 if (1 == tcase.bthrow)
557 expected = exceptions [2]; // length_error
558 else if (0 == tcase.bthrow) {
559 // by default excercise the exception safety of the function
560 // by iteratively inducing an exception at each call to operator
561 // new or Allocator::allocate() until the call succeeds
562 expected = exceptions [3]; // bad_alloc
563 *pst->throw_at_calls_ [0] = pst->new_calls_ [0] + throw_count + 1;
564 pal->throw_at_calls_ [pal->m_allocate] =
565 pal->throw_at_calls_ [pal->m_allocate] + throw_count + 1;
566 }
567 else {
568 // exceptions disabled for this test case
569 }
570
571#else // if defined (_RWSTD_NO_EXCEPTIONS)
572
573 if (tcase.bthrow) {
574 if (wres != wres_buf)
575 delete[] wres;
576
577 return;
578 }
579
580#endif // _RWSTD_NO_EXCEPTIONS
581
582 // start checking for memory leaks
583 rw_check_leaks (str.get_allocator ());
584
585 std::size_t res = 0;
586
587 try {
588
589 switch (func.which_) {
590
591 case Size (void):
592 res = cstr.size ();
593 break;
594
595 case Length (void):
596 res = cstr.length ();
597 break;
598
599 case MaxSize (void):
600 res = cstr.max_size ();
601 break;
602
603 case Resize (size_val):
604 str.resize (tcase.size, arg_val);
605 break;
606
607 case Resize (size):
608 str.resize (tcase.size);
609 break;
610
611 case Capacity (void):
612 res = cstr.capacity ();
613 break;
614
615 case Reserve (size):
616 str.reserve (tcase.size);
617 break;
618
619 case Reserve (void):
620 str.reserve ();
621 break;
622
623 case Clear (void):
624 str.clear ();
625 break;
626
627 case Empty (void):
628 res = cstr.empty () ? 1 : 0;
629 break;
630
631 default:
632 RW_ASSERT ("test logic error: unknown capacity overload");
633 return;
634 }
635
636 // verify the returned value
637 if (func.which_ == StringIds::size_void
638 || func.which_ == StringIds::length_void
639 || func.which_ == StringIds::empty_void) {
640 rw_assert (res == tcase.nres, 0, tcase.line,
641 "line %d. %{$FUNCALL} expected %zu, got %zu",
642 __LINE__, tcase.nres, res);
643 }
644
645 if (func.which_ == StringIds::capacity_void) {
646 std::size_t cur_sz = cstr.size ();
647 std::size_t max_sz = cstr.max_size ();
648
649 rw_assert (cur_sz <= res && res <= max_sz, 0, tcase.line,
650 "line %d. %{$FUNCALL} == %zu, "
651 "expected %zu < res < %zu",
652 __LINE__, res, cur_sz, max_sz);
653 }
654
655 if (func.which_ == StringIds::max_size_void) {
656 std::size_t cur_sz = cstr.size ();
657
658 rw_assert (cur_sz <= res, 0, tcase.line,
659 "line %d. %{$FUNCALL} == %zu, expected res > %zu",
660 __LINE__, res, cur_sz);
661 }
662
663 if (func.which_ == StringIds::reserve_size
664 || func.which_ == StringIds::reserve_void) {
665
666 res = str.capacity ();
667 rw_assert (std::size_t (tcase.size) <= res, 0, tcase.line,
668 "line %d. %{$FUNCALL} : capacity() >= "
669 "%zu, got %zu", __LINE__, tcase.size, res);
670 }
671
672 if (func.which_ == StringIds::resize_size_val
673 || func.which_ == StringIds::clear_void) {
674
675 // verfiy that strings length are equal
676 rw_assert (res_len == str.size (), 0, tcase.line,
677 "line %d. %{$FUNCALL} expected %{#*s} "
678 "with length %zu, got %{/*.*Gs} with length %zu",
679 __LINE__, int (tcase.nres), tcase.res, res_len,
680 int (sizeof (charT)), int (str.size ()),
681 str.c_str (), str.size ());
682
683 if (res_len == str.size ()) {
684 // if the result length matches the expected length
685 // (and only then), also verify that the modified
686 // string matches the expected result
687 const std::size_t match =
688 rw_match (tcase.res, str.c_str(), str.size ());
689
690 rw_assert (match == res_len, 0, tcase.line,
691 "line %d. %{$FUNCALL} expected %{#*s}, "
692 "got %{/*.*Gs}, difference at offset %zu",
693 __LINE__, int (tcase.nres), tcase.res,
694 int (sizeof (charT)), int (str.size ()),
695 str.c_str (), match);
696 }
697 }
698
699 if (func.which_ == StringIds::resize_size) {
700
701 std::size_t match =
702 rw_match (tcase.res, str.data (), res_len);
703
704 rw_assert (match == res_len, 0, tcase.line,
705 "line %d. %{$FUNCALL} expected %{#*s}, "
706 "got %{/*.*Gs}, difference at offset %zu",
707 __LINE__, int (tcase.nres), tcase.res,
708 int (sizeof (charT)), int (str.size ()),
709 str.c_str (), match);
710
711 for (std::size_t tmp = res_len; tmp < str.size (); tmp++) {
712
713 char c = char ();
714 match = rw_match (&c, str.data () + tmp, 1);
715 rw_assert (1 == match, 0, tcase.line,
716 "line %d. %{$FUNCALL} expected %c "
717 "at offset %zu, got %{#c}",
718 __LINE__, c, tmp, str.data () + tmp);
719 }
720 }
721 }
722
723#ifndef _RWSTD_NO_EXCEPTIONS
724
725 catch (const std::length_error &ex) {
726 caught = exceptions [2];
727 rw_assert (caught == expected, 0, tcase.line,
728 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
729 "unexpectedly%{;} caught std::%s(%#s)",
730 __LINE__, 0 != expected, expected, caught, ex.what ());
731 }
732 catch (const std::bad_alloc &ex) {
733 caught = exceptions [3];
734 rw_assert (0 == tcase.bthrow, 0, tcase.line,
735 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
736 "unexpectedly%{;} caught std::%s(%#s)",
737 __LINE__, 0 != expected, expected, caught, ex.what ());
738 }
739 catch (const std::exception &ex) {
740 caught = exceptions [4];
741 rw_assert (0, 0, tcase.line,
742 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
743 "unexpectedly%{;} caught std::%s(%#s)",
744 __LINE__, 0 != expected, expected, caught, ex.what ());
745 }
746 catch (...) {
747 caught = exceptions [0];
748 rw_assert (0, 0, tcase.line,
749 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
750 "unexpectedly%{;} caught %s",
751 __LINE__, 0 != expected, expected, caught);
752 }
753
754#endif // _RWSTD_NO_EXCEPTIONS
755
756 // FIXME: verify the number of blocks the function call
757 // is expected to allocate and detect any memory leaks
758 rw_check_leaks (str.get_allocator (), tcase.line,
759 std::size_t (-1), std::size_t (-1));
760
761 if (caught) {
762 // verify that an exception thrown during allocation
763 // didn't cause a change in the state of the object
765 __LINE__, tcase.line, caught);
766
767 if (0 == tcase.bthrow) {
768 // allow this call to operator new to succeed and try
769 // to make the next one to fail during the next call
770 // to the same function again
771 continue;
772 }
773 }
774 else if (0 < tcase.bthrow) {
775 rw_assert (caught == expected, 0, tcase.line,
776 "line %d. %{$FUNCALL} %{?}expected %s, caught %s"
777 "%{:}unexpectedly caught %s%{;}",
778 __LINE__, 0 != expected, expected, caught, caught);
779 }
780
781 break;
782 }
783
784#ifndef _RWSTD_NO_REPLACEABLE_NEW_DELETE
785
786 // verify that if exceptions are enabled and when capacity changes
787 // at least one exception is thrown
788 const std::size_t expect_throws = str_state.capacity_ < str.capacity ();
789
790#else // if defined (_RWSTD_NO_REPLACEABLE_NEW_DELETE)
791
792 const std::size_t expect_throws =
793 (StringIds::UserAlloc == func.alloc_id_)
794 ? str_state.capacity_ < str.capacity () : 0;
795
796#endif // _RWSTD_NO_REPLACEABLE_NEW_DELETE
797
798 rw_assert (expect_throws == throw_count, 0, tcase.line,
799 "line %d: %{$FUNCALL}: expected exactly 1 %s exception "
800 "while changing capacity from %zu to %zu, got %zu",
801 __LINE__, exceptions [3],
802 str_state.capacity_, str.capacity (), throw_count);
803
804 // disable bad_alloc exceptions
805 *pst->throw_at_calls_ [0] = 0;
806 pal->throw_at_calls_ [pal->m_allocate] = 0;
807
808 if (wres != wres_buf)
809 delete[] wres;
810}
811
812
815
816int main (int argc, char** argv)
817{
818 static const StringTest
819 tests [] = {
820
821#undef TEST
822#define TEST(gsig, sig) { \
823 gsig, sig ## _test_cases, \
824 sizeof sig ## _test_cases / sizeof *sig ## _test_cases \
825 }
826
827 TEST (StringIds::size_void, size_void),
828 TEST (StringIds::length_void, length_void),
829 TEST (StringIds::max_size_void, max_size_void),
830 TEST (StringIds::resize_size_val, resize_size_val),
831 TEST (StringIds::resize_size, resize_size),
832 TEST (StringIds::capacity_void, capacity_void),
833 TEST (StringIds::reserve_size, reserve_size),
834 TEST (StringIds::reserve_void, reserve_void),
835 TEST (StringIds::clear_void, clear_void),
836 TEST (StringIds::empty_void, empty_void)
837 };
838
839 const std::size_t test_count = sizeof tests / sizeof *tests;
840
841 return rw_run_string_test (argc, argv, __FILE__,
842 "lib.string.capacity",
843 test_capacity, tests, test_count);
844}
845
846
#define res
Definition 17.names.cpp:531
#define c
Definition 17.names.cpp:111
#define charT
Definition 17.names.cpp:456
#define tmp
Definition 17.names.cpp:553
#define Size(sig)
#define TEST(str, res)
#define Clear(sig)
#define MaxSize(sig)
#define Length(sig)
#define Resize(sig)
#define Reserve(sig)
void test_capacity(charT, Traits *, Allocator *, const StringFunc &func, const StringTestCase &tcase)
#define Empty(sig)
#define Capacity(sig)
std::char_traits< S > Traits
#define NPOS
#define main
int caught
const char * str
Definition exec.cpp:170
_TEST_EXPORT void rw_check_leaks(const SharedAlloc *=0, int=0, _RWSTD_SIZE_T=0, _RWSTD_SIZE_T=_RWSTD_SIZE_MAX)
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 _RWSTD_SIZE_T rw_match(const char *, const char *, _RWSTD_SIZE_T=_RWSTD_SIZE_MAX)
_TEST_EXPORT int rw_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630
_TEST_EXPORT rwt_free_store * rwt_get_free_store(rwt_free_store *)
Definition new.cpp:597
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)
static SharedAlloc * instance(SharedAlloc *=0)
_RWSTD_SIZE_T throw_at_calls_[n_funs]
_RWSTD_SIZE_T capacity_
Definition rw_strings.h:741
int assert_equal(const StringState &, int, int, const char *) const
_RWSTD_SIZE_T str_len
Definition rw_strings.h:597
const char * str
Definition rw_strings.h:596
_RWSTD_SIZE_T nres
Definition rw_strings.h:603
const char * res
Definition rw_strings.h:602
_RWSTD_SIZE_T * throw_at_calls_[2]
Definition rw_new.h:83
_RWSTD_SIZE_T new_calls_[2]
Definition rw_new.h:47