44#define OpPlus(which) StringIds::op_plus_ ## which
46static const char*
const exceptions[] = {
47 "unknown exception",
"out_of_range",
"length_error",
48 "bad_alloc",
"exception"
56cptr_cstr_test_cases [] = {
59#define TEST(str, src, res, bthrow) \
60 { __LINE__, -1, -1, -1, -1, -1, \
61 str, sizeof str - 1, src, sizeof src - 1, \
62 res, sizeof res - 1, bthrow \
73 TEST (
"ab",
"c",
"abc", 0),
76 TEST (
"",
"a",
"a", 0),
77 TEST (
"a",
"",
"a", 0),
79 TEST (
"<U0>",
"",
"", 0),
80 TEST (
"",
"<U0>",
"<U0>", 0),
81 TEST (
"<U0>",
"<U0>",
"<U0>", 0),
83 TEST (
"abcdefg",
"jih",
"abcdefgjih", 0),
84 TEST (
"jih",
"abcdefg",
"jihabcdefg", 0),
86 TEST (
"<U0>ab<U0>@2c",
"e<U0>",
"e<U0>", 0),
87 TEST (
"e<U0>",
"<U0>ab<U0>@2c",
"e<U0>ab<U0>@2c", 0),
88 TEST (
"ab<U0>@2c<U0>",
"e<U0>",
"abe<U0>", 0),
89 TEST (
"e<U0>",
"ab<U0>@2c<U0>",
"eab<U0>@2c<U0>", 0),
91 TEST (
"x@2048",
"x@2048",
"x@4096", 0),
92 TEST (
"a@2048",
"b@2048",
"a@2048b@2048", 0),
93 TEST (
"a@2048<U0>",
"b@2048",
"a@2048b@2048", 0),
94 TEST (
"a@2048",
"b@2048<U0>",
"a@2048b@2048<U0>", 0),
96 TEST (
"x@10",
"x@118",
"x@128", 0),
97 TEST (
"x@128",
"x@79",
"x@207", 0),
98 TEST (
"x@207",
"x@127",
"x@334", 0),
99 TEST (
"x@334",
"x@206",
"x@540", 0),
100 TEST (
"x@540",
"x@333",
"x@873", 0),
101 TEST (
"x@539",
"x@873",
"x@1412", 0),
102 TEST (
"x@872",
"x@1412",
"x@2284", 0),
103 TEST (
"x@1411",
"x@2284",
"x@3695", 0),
104 TEST (
"x@1412",
"x@2284",
"x@3696", 0),
106 TEST (0,
"abc",
"abcabc", 0),
107 TEST (0,
"<U0>ab<U0>",
"<U0>ab<U0>", 0),
108 TEST (0,
"x@2048",
"x@4096", 0),
110 TEST (
"last",
"test",
"lasttest", 0)
119cstr_cstr_test_cases [] = {
122#define TEST(str, src, res, bthrow) \
123 { __LINE__, -1, -1, -1, -1, -1, \
124 str, sizeof str - 1, src, sizeof src - 1, \
125 res, sizeof res - 1, bthrow \
136 TEST (
"ab",
"c",
"abc", 0),
138 TEST (
"",
"",
"", 0),
139 TEST (
"",
"a",
"a", 0),
140 TEST (
"a",
"",
"a", 0),
142 TEST (
"<U0>",
"",
"<U0>", 0),
143 TEST (
"",
"<U0>",
"<U0>", 0),
144 TEST (
"<U0>",
"<U0>",
"<U0>@2", 0),
146 TEST (
"abcdefg",
"jih",
"abcdefgjih", 0),
147 TEST (
"jih",
"abcdefg",
"jihabcdefg", 0),
149 TEST (
"<U0>ab<U0>@2c",
"e<U0>",
"<U0>ab<U0>@2ce<U0>", 0),
150 TEST (
"e<U0>",
"<U0>ab<U0>@2c",
"e<U0>@2ab<U0>@2c", 0),
151 TEST (
"ab<U0>@2c<U0>",
"e<U0>",
"ab<U0>@2c<U0>e<U0>", 0),
152 TEST (
"e<U0>",
"ab<U0>@2c<U0>",
"e<U0>ab<U0>@2c<U0>", 0),
154 TEST (
"x@2048",
"x@2048",
"x@4096", 0),
155 TEST (
"a@2048",
"b@2048",
"a@2048b@2048", 0),
156 TEST (
"a@2048<U0>",
"b@2048",
"a@2048<U0>b@2048", 0),
157 TEST (
"a@2048",
"b@2048<U0>",
"a@2048b@2048<U0>", 0),
159 TEST (
"x@10",
"x@118",
"x@128", 0),
160 TEST (
"x@128",
"x@79",
"x@207", 0),
161 TEST (
"x@207",
"x@127",
"x@334", 0),
162 TEST (
"x@334",
"x@206",
"x@540", 0),
163 TEST (
"x@540",
"x@333",
"x@873", 0),
164 TEST (
"x@539",
"x@873",
"x@1412", 0),
165 TEST (
"x@872",
"x@1412",
"x@2284", 0),
166 TEST (
"x@1411",
"x@2284",
"x@3695", 0),
167 TEST (
"x@1412",
"x@2284",
"x@3696", 0),
169 TEST (0,
"abc",
"abcabc", 0),
170 TEST (0,
"<U0>ab<U0>",
"<U0>ab<U0>@2ab<U0>", 0),
171 TEST (0,
"x@2048",
"x@4096", 0),
173 TEST (
"abc", 0,
"abcabc", 0),
174 TEST (
"<U0>ab<U0>", 0,
"<U0>ab<U0>@2ab<U0>", 0),
175 TEST (
"x@2048", 0,
"x@4096", 0),
177 TEST (
"last",
"test",
"lasttest", 0)
185cstr_cptr_test_cases [] = {
188#define TEST(str, src, res, bthrow) \
189 { __LINE__, -1, -1, -1, -1, -1, \
190 str, sizeof str - 1, src, sizeof src - 1, \
191 res, sizeof res - 1, bthrow \
202 TEST (
"ab",
"c",
"abc", 0),
204 TEST (
"",
"",
"", 0),
205 TEST (
"",
"a",
"a", 0),
206 TEST (
"a",
"",
"a", 0),
208 TEST (
"<U0>",
"",
"<U0>", 0),
209 TEST (
"",
"<U0>",
"", 0),
210 TEST (
"<U0>",
"<U0>",
"<U0>", 0),
212 TEST (
"abcdefg",
"jih",
"abcdefgjih", 0),
213 TEST (
"jih",
"abcdefg",
"jihabcdefg", 0),
215 TEST (
"<U0>ab<U0>@2c",
"e<U0>",
"<U0>ab<U0>@2ce", 0),
216 TEST (
"e<U0>",
"<U0>ab<U0>@2c",
"e<U0>", 0),
217 TEST (
"ab<U0>@2c<U0>",
"e<U0>",
"ab<U0>@2c<U0>e", 0),
218 TEST (
"e<U0>",
"ab<U0>@2c<U0>",
"e<U0>ab", 0),
220 TEST (
"x@2048",
"x@2048",
"x@4096", 0),
221 TEST (
"a@2048",
"b@2048",
"a@2048b@2048", 0),
222 TEST (
"a@2048<U0>",
"b@2048",
"a@2048<U0>b@2048", 0),
223 TEST (
"a@2048",
"b@2048<U0>",
"a@2048b@2048", 0),
225 TEST (
"x@10",
"x@118",
"x@128", 0),
226 TEST (
"x@128",
"x@79",
"x@207", 0),
227 TEST (
"x@207",
"x@127",
"x@334", 0),
228 TEST (
"x@334",
"x@206",
"x@540", 0),
229 TEST (
"x@540",
"x@333",
"x@873", 0),
230 TEST (
"x@539",
"x@873",
"x@1412", 0),
231 TEST (
"x@872",
"x@1412",
"x@2284", 0),
232 TEST (
"x@1411",
"x@2284",
"x@3695", 0),
233 TEST (
"x@1412",
"x@2284",
"x@3696", 0),
235 TEST (
"abc", 0,
"abcabc", 0),
236 TEST (
"<U0>ab<U0>", 0,
"<U0>ab<U0>", 0),
237 TEST (
"x@2048", 0,
"x@4096", 0),
239 TEST (
"last",
"test",
"lasttest", 0)
247cstr_val_test_cases [] = {
250#define TEST(arg, val, res) \
251 { __LINE__, -1, -1, -1, -1, val, \
252 0, 0, arg, sizeof arg - 1, \
253 res, sizeof res - 1, 0 \
262 TEST (
"ab",
'c',
"abc"),
265 TEST (
"",
'\0',
"<U0>"),
267 TEST (
"<U0>",
'a',
"<U0>a"),
268 TEST (
"<U0>",
'\0',
"<U0>@2"),
270 TEST (
"<U0>ab<U0>@2c",
'e',
"<U0>ab<U0>@2ce"),
271 TEST (
"<U0>ab<U0>@2c",
'\0',
"<U0>ab<U0>@2c<U0>"),
272 TEST (
"ab<U0>@2c<U0>",
'e',
"ab<U0>@2c<U0>e"),
273 TEST (
"ab<U0>@2c<U0>",
'\0',
"ab<U0>@2c<U0>@2"),
275 TEST (
"abcdefghj",
'i',
"abcdefghji"),
277 TEST (
"x@4095",
'x',
"x@4096"),
278 TEST (
"x@4095",
'\0',
"x@4095<U0>"),
280 TEST (
"x@127",
'x',
"x@128"),
281 TEST (
"x@206",
'x',
"x@207"),
282 TEST (
"x@333",
'x',
"x@334"),
283 TEST (
"x@539",
'x',
"x@540"),
284 TEST (
"x@1411",
'x',
"x@1412"),
285 TEST (
"x@2283",
'x',
"x@2284"),
286 TEST (
"x@3694",
'x',
"x@3695"),
287 TEST (
"x@540",
'x',
"x@541"),
289 TEST (
"last",
't',
"lastt")
297val_cstr_test_cases [] = {
300#define TEST(val, arg, res) \
301 { __LINE__, -1, -1, -1, -1, val, \
302 "", 0, arg, sizeof arg - 1, \
303 res, sizeof res - 1, 0 \
312 TEST (
'c',
"ab",
"cab"),
315 TEST (
'\0',
"",
"<U0>"),
317 TEST (
'a',
"<U0>",
"a<U0>"),
318 TEST (
'\0',
"<U0>",
"<U0>@2"),
320 TEST (
'e',
"<U0>ab<U0>@2c",
"e<U0>ab<U0>@2c"),
321 TEST (
'\0',
"<U0>ab<U0>@2c",
"<U0>@2ab<U0>@2c"),
322 TEST (
'e',
"ab<U0>@2c<U0>",
"eab<U0>@2c<U0>"),
323 TEST (
'\0',
"ab<U0>@2c<U0>",
"<U0>ab<U0>@2c<U0>"),
325 TEST (
'i',
"abcdefghj",
"iabcdefghj"),
327 TEST (
'x',
"x@4095",
"x@4096"),
328 TEST (
'\0',
"x@4095",
"<U0>x@4095"),
330 TEST (
'x',
"x@127",
"x@128"),
331 TEST (
'x',
"x@206",
"x@207"),
332 TEST (
'x',
"x@333",
"x@334"),
333 TEST (
'x',
"x@539",
"x@540"),
334 TEST (
'x',
"x@1411",
"x@1412"),
335 TEST (
'x',
"x@2283",
"x@2284"),
336 TEST (
'x',
"x@3694",
"x@3695"),
337 TEST (
'x',
"x@540",
"x@541"),
339 TEST (
't',
"last",
"tlast")
344template <
class charT,
class Traits,
class Allocator>
349 typedef std::basic_string <charT, Traits, Allocator>
String;
352 static const std::size_t BUFSIZE = 256;
354 static charT wstr_buf [BUFSIZE];
355 static charT warg_buf [BUFSIZE];
357 std::size_t str_len =
sizeof wstr_buf /
sizeof *wstr_buf;
358 std::size_t arg_len =
sizeof warg_buf /
sizeof *warg_buf;
363 static charT wres_buf [BUFSIZE];
364 std::size_t res_len =
sizeof wres_buf /
sizeof *wres_buf;
369 String s_str (wstr, str_len);
370 const String s_arg (warg, arg_len);
372 if (wstr != wstr_buf)
375 if (warg != warg_buf)
388 const charT*
const arg1_ptr = tcase.
str ? s_str.c_str () : s_arg.c_str ();
389 const String& arg1_str = tcase.
str ? s_str : s_arg;
390 const charT*
const arg2_ptr = tcase.
arg ? s_arg.c_str () : s_str.c_str ();
391 const String& arg2_str = tcase.
arg ? s_arg : s_str;
394 std::size_t total_length_calls = 0;
395 std::size_t n_length_calls = 0;
399 total_length_calls = rg_calls [UTMemFun::length];
407 std::size_t throw_count;
408 for (throw_count = 0; ; ++throw_count) {
411 const char* expected = 0;
414#ifndef _RWSTD_NO_EXCEPTIONS
420 expected = exceptions [3];
432 if (wres != wres_buf)
445 switch (
func.which_) {
447 s_res = arg1_ptr + arg2_str;
449 n_length_calls = rg_calls [UTMemFun::length];
453 s_res = arg1_str + arg2_str;
457 s_res = arg1_str + arg2_ptr;
459 n_length_calls = rg_calls [UTMemFun::length];
463 s_res = arg1_str + arg_val;
467 s_res = arg_val + arg2_str;
471 RW_ASSERT (
"test logic error: unknown operator+ overload");
477 "line %d. %{$FUNCALL} expected %{#*s} "
478 "with length %zu, got %{/*.*Gs} with length %zu",
479 __LINE__, int (tcase.
nres), tcase.
res, res_len,
480 int (
sizeof (
charT)),
int (s_res.size ()),
481 s_res.c_str (), s_res.size ());
483 if (res_len == s_res.size ()) {
487 const std::size_t match =
488 rw_match (tcase.
res, s_res.c_str(), s_res.size ());
491 "line %d. %{$FUNCALL} expected %{#*s}, "
492 "got %{/*.*Gs}, difference at offset %zu",
493 __LINE__, int (tcase.
nres), tcase.
res,
494 int (
sizeof (
charT)),
int (s_res.size ()),
495 s_res.c_str (), match);
500 ||
OpPlus (cstr_cptr) ==
func.which_) && rg_calls) {
501 rw_assert (n_length_calls - total_length_calls > 0,
502 0, tcase.
line,
"line %d. %{$FUNCALL} doesn't "
503 "use traits::length()", __LINE__);
507#ifndef _RWSTD_NO_EXCEPTIONS
512 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
513 "unexpectedly%{;} caught std::%s(%#s)",
514 __LINE__, 0 != expected, expected,
caught, ex.what ());
519 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
520 "unexpectedly%{;} caught std::%s(%#s)",
521 __LINE__, 0 != expected, expected,
caught, ex.what ());
526 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
527 "unexpectedly%{;} caught %s",
528 __LINE__, 0 != expected, expected,
caught);
536 std::size_t (-1), std::size_t (-1));
551 else if (0 < tcase.
bthrow) {
553 "line %d. %{$FUNCALL} %{?}expected %s, caught %s"
554 "%{:}unexpectedly caught %s%{;}",
561#ifndef _RWSTD_NO_REPLACEABLE_NEW_DELETE
565 const std::size_t expect_throws = str_state.
capacity_ < s_res.capacity ();
569 const std::size_t expect_throws =
571 ? str_state.
capacity_ < s_res.capacity () : 0;
576 rw_assert (expect_throws <= throw_count && throw_count <= 2, 0, tcase.
line,
577 "line %d: %{$FUNCALL}: expected at least 1 %s exception "
578 "while changing capacity from %zu to %zu, got %zu",
579 __LINE__, exceptions [3],
580 str_state.
capacity_, s_res.capacity (), throw_count);
586 if (wres != wres_buf)
594int main (
int argc,
char** argv)
601 StringIds::op_plus_ ## tag, tag ## _test_cases, \
602 sizeof tag ## _test_cases / sizeof *tag ## _test_cases \
612 const std::size_t test_count =
sizeof tests /
sizeof *tests;
void test_op_plus(charT, Traits *, Allocator *, const StringFunc &func, const StringTestCase &tcase)
#define TEST(str, src, res, bthrow)
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)
_RWSTD_SIZE_T * rw_get_call_counters(std::char_traits< charT > *, charT *)
_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]