44#define OpEqual(which) StringIds::op_equal_ ## which
45#define OpNotEqual(which) StringIds::op_not_equal_ ## which
46#define OpLess(which) StringIds::op_less_ ## which
47#define OpLessEqual(which) StringIds::op_less_equal_ ## which
48#define OpGreater(which) StringIds::op_greater_ ## which
49#define OpGreaterEqual(which) StringIds::op_greater_equal_ ## which
51static const char*
const exceptions[] = {
52 "unknown exception",
"out_of_range",
"length_error",
53 "bad_alloc",
"exception"
66cptr_cstr_test_cases [] = {
69#define TEST(str, arg, res) \
70 { __LINE__, -1, -1, -1, -1, -1, \
71 str, sizeof str - 1, arg, sizeof arg - 1, \
72 0, (std::size_t) res, 0 \
92 TEST (
"",
"<U0>", -1),
93 TEST (
"<U0>",
"<U0>", -1),
95 TEST (
"abcdefgjih",
"abcdefgji", 1),
96 TEST (
"abcdefgjih",
"abcdefgjih", 0),
97 TEST (
"abcdefgji",
"abcdefgjih", -1),
98 TEST (
"abcefgjih",
"abcdefgjih", 1),
99 TEST (
"abcdefgjih",
"abcefgjih", -1),
101 TEST (
"<U0>ab<U0>@2c",
"<U0>ab<U0>@2c", -1),
102 TEST (
"ab<U0>@2c<U0>",
"ab", 0),
103 TEST (
"e<U0>",
"ab<U0>@2c<U0>", 1),
105 TEST (
"x@2048",
"x@2048", 0),
106 TEST (
"x@2048",
"x@2049", -1),
107 TEST (
"x@2048",
"x@2047", 1),
108 TEST (
"a@2048",
"b@2048", -1),
109 TEST (
"b@2048",
"a@2048", 1),
110 TEST (
"a@2048<U0>",
"a@2048", 0),
111 TEST (
"a@2048",
"a@2048<U0>", -1),
113 TEST (
"x@128",
"x@207", -1),
114 TEST (
"x@334",
"x@334", 0),
115 TEST (
"x@873",
"x@540", 1),
116 TEST (
"x@1412",
"x@2284", -1),
117 TEST (
"x@3695",
"x@3695", 0),
118 TEST (
"x@4096",
"x@3695", 1),
121 TEST (0,
"<U0>ab<U0>", -1),
122 TEST (0,
"x@2048", 0),
124 TEST (
"last",
"last", 0)
137cstr_cstr_test_cases [] = {
140#define TEST(str, arg, res) \
141 { __LINE__, -1, -1, -1, -1, -1, \
142 str, sizeof str - 1, arg, sizeof arg - 1, \
143 0, (std::size_t) res, 0 \
155 TEST (
"ab",
"c", -1),
162 TEST (
"<U0>",
"", 1),
163 TEST (
"",
"<U0>", -1),
164 TEST (
"<U0>",
"<U0>", 0),
166 TEST (
"abcdefgjih",
"abcdefgji", 1),
167 TEST (
"abcdefgjih",
"abcdefgjih", 0),
168 TEST (
"abcdefgji",
"abcdefgjih", -1),
169 TEST (
"abcefgjih",
"abcdefgjih", 1),
170 TEST (
"abcdefgjih",
"abcefgjih", -1),
172 TEST (
"<U0>ab<U0>@2c",
"<U0>ab<U0>@2c", 0),
173 TEST (
"ab<U0>@2c<U0>",
"<U0>ab<U0>@2c", 1),
174 TEST (
"ab<U0>@2c<U0>",
"ab<U0>@2c<U0>", 0),
175 TEST (
"ab<U0>@2c<U0>",
"ab", 1),
176 TEST (
"ab",
"ab<U0>@2c<U0>", -1),
177 TEST (
"ab<U0>@3",
"ab<U0>@2c<U0>", -1),
178 TEST (
"ab<U0>@3",
"ab<U0>@3", 0),
179 TEST (
"e<U0>",
"ab<U0>@2c<U0>", 1),
180 TEST (
"ab<U0>@2c<U0>",
"e<U0>", -1),
182 TEST (
"x@2048",
"x@2048", 0),
183 TEST (
"x@2048",
"x@2049", -1),
184 TEST (
"x@2048",
"x@2047", 1),
185 TEST (
"a@2048",
"b@2048", -1),
186 TEST (
"b@2048",
"a@2048", 1),
187 TEST (
"<U0>a@2048",
"a@2048<U0>", -1),
188 TEST (
"a@2048<U0>",
"<U0>a@2048", 1),
189 TEST (
"a@2048<U0>",
"a@2048", 1),
190 TEST (
"a@2048",
"a@2048<U0>", -1),
191 TEST (
"a@2048<U0>",
"a@2048<U0>", 0),
193 TEST (
"x@128",
"x@207", -1),
194 TEST (
"x@334",
"x@334", 0),
195 TEST (
"x@873",
"x@540", 1),
196 TEST (
"x@1412",
"x@2284", -1),
197 TEST (
"x@3695",
"x@3695", 0),
198 TEST (
"x@4096",
"x@3695", 1),
201 TEST (0,
"<U0>ab<U0>", 0),
202 TEST (0,
"x@2048", 0),
204 TEST (
"<U0>ab<U0>", 0, 0),
205 TEST (
"x@2048", 0, 0),
207 TEST (
"last",
"last", 0)
220cstr_cptr_test_cases [] = {
223#define TEST(str, arg, res) \
224 { __LINE__, -1, -1, -1, -1, -1, \
225 str, sizeof str - 1, arg, sizeof arg - 1, \
226 0, (std::size_t) res, 0 \
238 TEST (
"ab",
"c", -1),
245 TEST (
"<U0>",
"", 1),
246 TEST (
"",
"<U0>", 0),
247 TEST (
"<U0>",
"<U0>", 1),
249 TEST (
"abcdefgjih",
"abcdefgji", 1),
250 TEST (
"abcdefgjih",
"abcdefgjih", 0),
251 TEST (
"abcdefgji",
"abcdefgjih", -1),
252 TEST (
"abcefgjih",
"abcdefgjih", 1),
253 TEST (
"abcdefgjih",
"abcefgjih", -1),
255 TEST (
"<U0>ab<U0>@2c",
"<U0>ab<U0>@2c", 1),
256 TEST (
"ab",
"ab<U0>@2c<U0>", 0),
257 TEST (
"e<U0>",
"ab<U0>@2c<U0>", 1),
258 TEST (
"ab<U0>@2c<U0>",
"e<U0>", -1),
260 TEST (
"x@2048",
"x@2048", 0),
261 TEST (
"x@2048",
"x@2049", -1),
262 TEST (
"x@2048",
"x@2047", 1),
263 TEST (
"a@2048",
"b@2048", -1),
264 TEST (
"b@2048",
"a@2048", 1),
265 TEST (
"a@2048",
"a@2048<U0>", 0),
266 TEST (
"a@2048<U0>",
"a@2048<U0>", 1),
268 TEST (
"x@128",
"x@207", -1),
269 TEST (
"x@334",
"x@334", 0),
270 TEST (
"x@873",
"x@540", 1),
271 TEST (
"x@1412",
"x@2284", -1),
272 TEST (
"x@3695",
"x@3695", 0),
273 TEST (
"x@4096",
"x@3695", 1),
276 TEST (
"<U0>ab<U0>", 0, 1),
277 TEST (
"x@2048", 0, 0),
279 TEST (
"last",
"last", 0)
284template <
class charT,
class Traits,
class Allocator>
289 typedef std::basic_string <charT, Traits, Allocator>
String;
292 static const std::size_t BUFSIZE = 256;
294 static charT wstr_buf [BUFSIZE];
295 static charT warg_buf [BUFSIZE];
297 std::size_t str_len =
sizeof wstr_buf /
sizeof *wstr_buf;
298 std::size_t arg_len =
sizeof warg_buf /
sizeof *warg_buf;
305 String s_str (wstr, str_len);
306 const String s_arg (warg, arg_len);
308 if (wstr != wstr_buf)
311 if (warg != warg_buf)
317 const charT*
const arg1_ptr = tcase.
str ? s_str.c_str () : s_arg.c_str ();
318 const String& arg1_str = tcase.
str ? s_str : s_arg;
319 const charT*
const arg2_ptr = tcase.
arg ? s_arg.c_str () : s_str.c_str ();
320 const String& arg2_str = tcase.
arg ? s_arg : s_str;
322 std::size_t total_length_calls = 0;
323 std::size_t n_length_calls = 0;
327 total_length_calls = rg_calls [UTMemFun::length];
335 std::size_t throw_count;
336 for (throw_count = 0; ; ++throw_count) {
339 const char* expected = 0;
342#ifndef _RWSTD_NO_EXCEPTIONS
348 expected = exceptions [3];
360 if (wres != wres_buf)
372 switch (
func.which_) {
375 res = arg1_ptr == arg2_str;
377 n_length_calls = rg_calls [UTMemFun::length];
381 res = arg1_ptr != arg2_str;
383 n_length_calls = rg_calls [UTMemFun::length];
387 res = arg1_ptr < arg2_str;
389 n_length_calls = rg_calls [UTMemFun::length];
393 res = arg1_ptr <= arg2_str;
395 n_length_calls = rg_calls [UTMemFun::length];
399 res = arg1_ptr > arg2_str;
401 n_length_calls = rg_calls [UTMemFun::length];
405 res = arg1_ptr >= arg2_str;
407 n_length_calls = rg_calls [UTMemFun::length];
413 res = arg1_str == arg2_str;
417 res = arg1_str != arg2_str;
421 res = arg1_str < arg2_str;
425 res = arg1_str <= arg2_str;
429 res = arg1_str > arg2_str;
433 res = arg1_str >= arg2_str;
439 res = arg1_str == arg2_ptr;
441 n_length_calls = rg_calls [UTMemFun::length];
445 res = arg1_str != arg2_ptr;
447 n_length_calls = rg_calls [UTMemFun::length];
451 res = arg1_str < arg2_ptr;
453 n_length_calls = rg_calls [UTMemFun::length];
457 res = arg1_str <= arg2_ptr;
459 n_length_calls = rg_calls [UTMemFun::length];
463 res = arg1_str > arg2_ptr;
465 n_length_calls = rg_calls [UTMemFun::length];
469 res = arg1_str >= arg2_ptr;
471 n_length_calls = rg_calls [UTMemFun::length];
475 RW_ASSERT (
"test logic error: unknown operator overload");
480 bool exp_res =
false;
483 exp_res = 0 == tcase.
nres;
487 exp_res = 0 != tcase.
nres;
495 exp_res = 1 != tcase.
nres;
499 exp_res = 1 == tcase.
nres;
509 "line %d. %{$FUNCALL} expected %b, got %b",
510 __LINE__, exp_res,
res);
513 std::size_t exp_len_used =
521 if (verlen && rg_calls) {
522 rw_assert (n_length_calls - total_length_calls > 0,
523 0, tcase.
line,
"line %d. %{$FUNCALL} doesn't "
524 "use traits::length()", __LINE__);
528#ifndef _RWSTD_NO_EXCEPTIONS
533 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
534 "unexpectedly%{;} caught std::%s(%#s)",
535 __LINE__, 0 != expected, expected,
caught, ex.what ());
540 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
541 "unexpectedly%{;} caught std::%s(%#s)",
542 __LINE__, 0 != expected, expected,
caught, ex.what ());
547 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
548 "unexpectedly%{;} caught %s",
549 __LINE__, 0 != expected, expected,
caught);
563 else if (0 < tcase.
bthrow) {
565 "line %d. %{$FUNCALL} %{?}expected %s, caught %s"
566 "%{:}unexpectedly caught %s%{;}",
573 const std::size_t expect_throws = 0;
576 "line %d: %{$FUNCALL}: expected no %s exceptions, got %zu",
577 __LINE__, exceptions [3], throw_count);
588int main (
int argc,
char** argv)
594#define TEST(oper, tag) { \
595 StringIds::op_ ## oper ## _ ## tag, tag ## _test_cases, \
596 sizeof tag ## _test_cases / sizeof *tag ## _test_cases \
603 TEST (not_equal, cptr_cstr),
604 TEST (not_equal, cstr_cptr),
605 TEST (not_equal, cstr_cstr),
607 TEST (less, cptr_cstr),
608 TEST (less, cstr_cptr),
609 TEST (less, cstr_cstr),
611 TEST (less_equal, cptr_cstr),
612 TEST (less_equal, cstr_cptr),
613 TEST (less_equal, cstr_cstr),
615 TEST (greater, cptr_cstr),
616 TEST (greater, cstr_cptr),
617 TEST (greater, cstr_cstr),
619 TEST (greater_equal, cptr_cstr),
620 TEST (greater_equal, cstr_cptr),
621 TEST (greater_equal, cstr_cstr)
625 const std::size_t test_count =
sizeof tests /
sizeof *tests;
628 "lib.string.nonmembers",
bool equal(const T &x, const U &y)
#define OpLessEqual(which)
#define OpNotEqual(which)
void test_operators(charT, Traits *, Allocator *, const StringFunc &func, const StringTestCase &tcase)
#define TEST(str, arg, res)
#define OpGreaterEqual(which)
std::char_traits< S > Traits
_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 int rw_assert(int, const char *, int, const char *,...)
_TEST_EXPORT rwt_free_store * rwt_get_free_store(rwt_free_store *)
#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]
_RWSTD_SIZE_T * throw_at_calls_[2]
_RWSTD_SIZE_T new_calls_[2]