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
51static const char*
const exceptions[] = {
52 "unknown exception",
"out_of_range",
"length_error",
53 "bad_alloc",
"exception"
61size_void_test_cases [] = {
63#define length_void_test_cases size_void_test_cases
66#define TEST(str, res) { \
67 __LINE__, -1, -1, -1, -1, -1, \
68 str, sizeof str - 1, 0, 0, \
86 TEST (
"test string", 11),
87 TEST (
"Test String", 11),
90 TEST (
"Test<U0>string", 11),
92 TEST (
"<U0>a<U0>b", 4),
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),
104 TEST (
"x@1412", 1412),
105 TEST (
"x@2284", 2284),
106 TEST (
"x@3695", 3695),
107 TEST (
"x@4096", 4096),
118max_size_void_test_cases [] = {
122 __LINE__, -1, -1, -1, -1, -1, \
123 str, sizeof str - 1, 0, 0, \
138 TEST (
"test string"),
139 TEST (
"a<U0>b<U0>c<U0>@2"),
155resize_size_val_test_cases [] = {
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 \
174 TEST (
"", 0,
'a',
"", 0),
175 TEST (
"", 5,
'\0',
"<U0>@3<U0>@2", 0),
176 TEST (
"", 334,
'x',
"x@334", 0),
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),
183 TEST (
"a", 0,
'a',
"", 0),
184 TEST (
"a", 2,
'\0',
"a<U0>", 0),
185 TEST (
"a", 540,
'a',
"a@540", 0),
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),
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),
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),
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),
215 TEST (
"x@3695",
NPOS,
'a',
"x@3695", 1),
217 TEST (
"last", 4,
't',
"last", 0)
226resize_size_test_cases [] = {
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 \
245 TEST (
"", 334,
"", 0),
247 TEST (
"<U0>", 0,
"", 0),
248 TEST (
"<U0>", 2,
"<U0>", 0),
249 TEST (
"<U0>", 1,
"<U0>", 0),
250 TEST (
"<U0>", 127,
"<U0>", 0),
252 TEST (
"a", 0,
"", 0),
253 TEST (
"a", 2,
"a", 0),
254 TEST (
"a", 539,
"a", 0),
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),
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),
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),
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),
286 TEST (
"last", 4,
"last", 0)
295capacity_void_test_cases [] = {
299 __LINE__, -1, -1, -1, -1, -1, \
300 str, sizeof str - 1, 0, 0, \
315 TEST (
"test string"),
316 TEST (
"a<U0>b<U0>c<U0>@2"),
336reserve_size_test_cases [] = {
339#define TEST(str, size, bthrow) { \
340 __LINE__, -1, size, -1, -1, -1, \
341 str, sizeof str - 1, 0, 0, \
359 TEST (
"<U0>", 127, 0),
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),
373 TEST (
"x@540", 127, 0),
374 TEST (
"x@873", 127, 0),
375 TEST (
"x@1412", 127, 0),
376 TEST (
"x@2284", 127, 0),
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),
399reserve_void_test_cases [] = {
403 __LINE__, -1, 0, -1, -1, -1, \
404 str, sizeof str - 1, 0, 0, \
417 TEST (
"a<U0>b<U0>c<U0>@2"),
436clear_void_test_cases [] = {
440 __LINE__, -1, -1, -1, -1, -1, \
441 str, sizeof str - 1, 0, 0, \
456 TEST (
"test string"),
457 TEST (
"a<U0>b<U0>c<U0>@2"),
477empty_void_test_cases [] = {
480#define TEST(str, res) { \
481 __LINE__, -1, -1, -1, -1, -1, \
482 str, sizeof str - 1, 0, 0, \
496 TEST (
"<U0>ab<U0>c", 0),
505template <
class charT,
class Traits,
class Allocator>
510 typedef std::basic_string <charT, Traits, Allocator>
String;
513 static const std::size_t BUFSIZE = 256;
515 static charT wstr_buf [BUFSIZE];
516 std::size_t str_len =
sizeof wstr_buf /
sizeof *wstr_buf;
519 static charT wres_buf [BUFSIZE];
520 std::size_t res_len =
sizeof wres_buf /
sizeof *wres_buf;
525 const String cstr (wstr, str_len);
527 if (wstr != wstr_buf)
537 const charT arg_val = -1 != tcase.
val ?
547 std::size_t throw_count;
548 for (throw_count = 0; ; ++throw_count) {
551 const char* expected = 0;
554#ifndef _RWSTD_NO_EXCEPTIONS
557 expected = exceptions [2];
558 else if (0 == tcase.
bthrow) {
562 expected = exceptions [3];
574 if (wres != wres_buf)
589 switch (
func.which_) {
596 res = cstr.length ();
600 res = cstr.max_size ();
604 str.resize (tcase.
size, arg_val);
612 res = cstr.capacity ();
628 res = cstr.empty () ? 1 : 0;
632 RW_ASSERT (
"test logic error: unknown capacity overload");
637 if (
func.which_ == StringIds::size_void
638 ||
func.which_ == StringIds::length_void
639 ||
func.which_ == StringIds::empty_void) {
641 "line %d. %{$FUNCALL} expected %zu, got %zu",
645 if (
func.which_ == StringIds::capacity_void) {
646 std::size_t cur_sz = cstr.size ();
647 std::size_t max_sz = cstr.max_size ();
650 "line %d. %{$FUNCALL} == %zu, "
651 "expected %zu < res < %zu",
652 __LINE__,
res, cur_sz, max_sz);
655 if (
func.which_ == StringIds::max_size_void) {
656 std::size_t cur_sz = cstr.size ();
659 "line %d. %{$FUNCALL} == %zu, expected res > %zu",
660 __LINE__,
res, cur_sz);
663 if (
func.which_ == StringIds::reserve_size
664 ||
func.which_ == StringIds::reserve_void) {
668 "line %d. %{$FUNCALL} : capacity() >= "
669 "%zu, got %zu", __LINE__, tcase.
size,
res);
672 if (
func.which_ == StringIds::resize_size_val
673 ||
func.which_ == StringIds::clear_void) {
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 ());
683 if (res_len ==
str.size ()) {
687 const std::size_t match =
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);
699 if (
func.which_ == StringIds::resize_size) {
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);
711 for (std::size_t
tmp = res_len;
tmp <
str.size ();
tmp++) {
716 "line %d. %{$FUNCALL} expected %c "
717 "at offset %zu, got %{#c}",
723#ifndef _RWSTD_NO_EXCEPTIONS
725 catch (
const std::length_error &ex) {
728 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
729 "unexpectedly%{;} caught std::%s(%#s)",
730 __LINE__, 0 != expected, expected,
caught, ex.what ());
735 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
736 "unexpectedly%{;} caught std::%s(%#s)",
737 __LINE__, 0 != expected, expected,
caught, ex.what ());
742 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
743 "unexpectedly%{;} caught std::%s(%#s)",
744 __LINE__, 0 != expected, expected,
caught, ex.what ());
749 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
750 "unexpectedly%{;} caught %s",
751 __LINE__, 0 != expected, expected,
caught);
759 std::size_t (-1), std::size_t (-1));
774 else if (0 < tcase.
bthrow) {
776 "line %d. %{$FUNCALL} %{?}expected %s, caught %s"
777 "%{:}unexpectedly caught %s%{;}",
784#ifndef _RWSTD_NO_REPLACEABLE_NEW_DELETE
788 const std::size_t expect_throws = str_state.
capacity_ <
str.capacity ();
792 const std::size_t expect_throws =
799 "line %d: %{$FUNCALL}: expected exactly 1 %s exception "
800 "while changing capacity from %zu to %zu, got %zu",
801 __LINE__, exceptions [3],
808 if (wres != wres_buf)
816int main (
int argc,
char** argv)
822#define TEST(gsig, sig) { \
823 gsig, sig ## _test_cases, \
824 sizeof sig ## _test_cases / sizeof *sig ## _test_cases \
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)
839 const std::size_t test_count =
sizeof tests /
sizeof *tests;
842 "lib.string.capacity",
void test_capacity(charT, Traits *, Allocator *, const StringFunc &func, const StringTestCase &tcase)
std::char_traits< S > Traits
_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 *)
_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 *,...)
_TEST_EXPORT rwt_free_store * rwt_get_free_store(rwt_free_store *)
StringState rw_get_string_state(const String &str)
#define DEFINE_STRING_TEST_DISPATCH(fname)
_TEST_EXPORT int rw_run_string_test(int, char **, const char *, const char *, StringTestFunc *, const StringTest *, _RWSTD_SIZE_T)
static SharedAlloc * instance(SharedAlloc *=0)
_RWSTD_SIZE_T throw_at_calls_[n_funs]
int assert_equal(const StringState &, int, int, const char *) const
_RWSTD_SIZE_T * throw_at_calls_[2]
_RWSTD_SIZE_T new_calls_[2]