42#define Erase(sig) StringIds::erase_ ## sig
44static const char*
const exceptions[] = {
45 "unknown exception",
"out_of_range",
"length_error",
46 "bad_alloc",
"exception"
57#define TEST(str, res) { \
58 __LINE__, -1, -1, -1, -1, -1, str, sizeof str - 1, \
59 0, 0, res, sizeof res - 1, 0 }
71 TEST (
"<U0>ab<U0>@2c",
""),
72 TEST (
"a<U0>b<U0>@2c",
""),
73 TEST (
"a<U0>bc<U0>@2",
""),
88#define TEST(str, off, res, bthrow) { \
89 __LINE__, off, -1, -1, -1, -1, str, sizeof str - 1, \
90 0, 0, res, sizeof res - 1, bthrow }
101 TEST (
"<U0>", 0,
"", 0),
102 TEST (
"<U0>", 1,
"<U0>", 0),
103 TEST (
"<U0>@2", 1,
"<U0>", 0),
105 TEST (
"a", 0,
"", 0),
106 TEST (
"a", 1,
"a", 0),
108 TEST (
"abc", 0,
"", 0),
109 TEST (
"abc", 1,
"a", 0),
110 TEST (
"abc", 2,
"ab", 0),
111 TEST (
"abc", 3,
"abc", 0),
113 TEST (
"t<U0> s", 0,
"", 0),
114 TEST (
"t<U0> s", 1,
"t", 0),
115 TEST (
"t<U0> s", 2,
"t<U0>", 0),
116 TEST (
"t<U0> s", 3,
"t<U0> ", 0),
117 TEST (
"t<U0> s", 4,
"t<U0> s", 0),
119 TEST (
"a<U0>@3b", 2,
"a<U0>", 0),
120 TEST (
"a<U0>@3b", 1,
"a", 0),
121 TEST (
"a<U0>@3b", 0,
"", 0),
122 TEST (
"a<U0>b<U0>@2c", 0,
"", 0),
123 TEST (
"a<U0>b<U0>@2c", 4,
"a<U0>b<U0>", 0),
124 TEST (
"<U0>ab<U0>@2c", 2,
"<U0>a", 0),
125 TEST (
"ab<U0>c<U0>@2", 5,
"ab<U0>c<U0>", 0),
127 TEST (
"a", 3,
"a", 1),
128 TEST (
"t<U0> s", 5,
"t<U0> s", 1),
129 TEST (
"ab<U0>c<U0>@2", 10,
"ab<U0>c<U0>@2", 1),
130 TEST (
"x@4096", 4106,
"x@4096", 1),
132 TEST (
"x@4096", 0,
"", 0),
133 TEST (
"x@4096", 1,
"x", 0),
134 TEST (
"x@4096", 4,
"xxxx", 0),
135 TEST (
"x@4096", 4096,
"x@4096", 0),
136 TEST (
"x@2048y@2048", 2048,
"x@2048", 0),
138 TEST (
"last test", 4,
"last", 0)
148size_size_test_cases [] = {
151#define iter_iter_test_cases size_size_test_cases
154#define TEST(str, off, size, res, bthrow) { \
155 __LINE__, off, size, -1, -1, -1, str, sizeof str - 1, \
156 0, 0, res, sizeof res - 1, bthrow }
168 TEST (
"", 0, 0,
"", 0),
169 TEST (
"<U0>", 0, 1,
"", 0),
170 TEST (
"<U0>", 0, 0,
"<U0>", 0),
171 TEST (
"<U0>", 1, 1,
"<U0>", 0),
172 TEST (
"<U0>@2", 1, 1,
"<U0>", 0),
173 TEST (
"<U0>@2", 0, 1,
"<U0>", 0),
175 TEST (
"a", 0, 1,
"", 0),
176 TEST (
"a", 0, 0,
"a", 0),
177 TEST (
"a", 1, 1,
"a", 0),
179 TEST (
"abc", 0, 3,
"", 0),
180 TEST (
"abc", 0, 2,
"c", 0),
181 TEST (
"abc", 1, 2,
"a", 0),
182 TEST (
"abc", 1, 1,
"ac", 0),
183 TEST (
"abc", 2, 1,
"ab", 0),
184 TEST (
"abc", 3, 0,
"abc", 0),
186 TEST (
"t<U0> s", 0, 3,
"s", 0),
187 TEST (
"t<U0> s", 0, 4,
"", 0),
188 TEST (
"t<U0> s", 0, 1,
"<U0> s", 0),
189 TEST (
"t<U0> s", 1, 3,
"t", 0),
190 TEST (
"t<U0> s", 1, 2,
"ts", 0),
191 TEST (
"t<U0> s", 2, 2,
"t<U0>", 0),
192 TEST (
"t<U0> s", 2, 1,
"t<U0>s", 0),
193 TEST (
"t<U0> s", 3, 2,
"t<U0> ", 0),
194 TEST (
"t<U0> s", 4, 0,
"t<U0> s", 0),
196 TEST (
"a<U0>@3b", 2, 0,
"a<U0>@3b", 0),
197 TEST (
"a<U0>@3b", 2, 3,
"a<U0>", 0),
198 TEST (
"a<U0>@3b", 2, 2,
"a<U0>b", 0),
199 TEST (
"a<U0>@3b", 1, 4,
"a", 0),
200 TEST (
"a<U0>@3b", 0, 5,
"", 0),
201 TEST (
"a<U0>@3b", 0, 2,
"<U0>@2b", 0),
202 TEST (
"a<U0>b<U0>@2c", 0, 6,
"", 0),
203 TEST (
"a<U0>b<U0>@2c", 4, 2,
"a<U0>b<U0>", 0),
204 TEST (
"a<U0>b<U0>@2c", 4, 1,
"a<U0>b<U0>c", 0),
205 TEST (
"<U0>ab<U0>@2c", 2, 5,
"<U0>a", 0),
206 TEST (
"<U0>ab<U0>@2c", 0, 4,
"<U0>c", 0),
207 TEST (
"ab<U0>c<U0>@2", 5, 1,
"ab<U0>c<U0>", 0),
209 TEST (
"a", 0, 3,
"", 0),
210 TEST (
"t<U0> s", 0, 9,
"", 0),
211 TEST (
"ab<U0>c<U0>@2", 0, 10,
"", 0),
212 TEST (
"x@4096", 0, 4105,
"", 0),
214 TEST (
"a", 3, 1,
"a", 1),
215 TEST (
"t<U0> s", 5, 1,
"t<U0> s", 1),
216 TEST (
"ab<U0>c<U0>@2", 10, 1,
"ab<U0>c<U0>@2", 1),
217 TEST (
"x@4096", 4106, 1,
"x@4096", 1),
219 TEST (
"x@4096", 0, 4096,
"", 0),
220 TEST (
"x@4096", 1, 4095,
"x", 0),
221 TEST (
"x@4096", 4, 4092,
"xxxx", 0),
222 TEST (
"x@4096", 4, 4090,
"xxxxxx", 0),
223 TEST (
"x@4096", 4096, 4096,
"x@4096", 0),
224 TEST (
"x@2048y@2048", 1, 4094,
"xy", 0),
226 TEST (
"last test", 4, 1,
"lasttest", 0)
235iter_test_cases [] = {
238#define TEST(str, off, res) { \
239 __LINE__, off, -1, -1, -1, -1, str, sizeof str - 1, \
240 0, 0, res, sizeof res - 1, 0 }
249 TEST (
"<U0>", 0,
""),
250 TEST (
"<U0>@2", 0,
"<U0>"),
251 TEST (
"<U0>@2", 1,
"<U0>"),
253 TEST (
"abc", 0,
"bc"),
254 TEST (
"abc", 1,
"ac"),
255 TEST (
"abc", 2,
"ab"),
257 TEST (
"t<U0> s", 0,
"<U0> s"),
258 TEST (
"t<U0> s", 1,
"t s"),
259 TEST (
"t<U0> s", 2,
"t<U0>s"),
260 TEST (
"t<U0> s", 3,
"t<U0> "),
262 TEST (
"a<U0>@3b", 4,
"a<U0>@3"),
263 TEST (
"a<U0>@3b", 2,
"a<U0>@2b"),
264 TEST (
"a<U0>@3b", 1,
"a<U0>@2b"),
265 TEST (
"a<U0>@3b", 0,
"<U0>@3b"),
266 TEST (
"a<U0>b<U0>@2c", 4,
"a<U0>b<U0>c"),
267 TEST (
"<U0>ab<U0>@2c", 0,
"ab<U0>@2c"),
268 TEST (
"<U0>ab<U0>@2c", 2,
"<U0>a<U0>@2c"),
269 TEST (
"ab<U0>c<U0>@2", 5,
"ab<U0>c<U0>"),
271 TEST (
"x@4096y", 4096,
"x@4096"),
272 TEST (
"x@4096", 4088,
"x@4095"),
273 TEST (
"ax@4096", 0,
"x@4096"),
274 TEST (
"x@4096", 9,
"x@4095"),
276 TEST (
"last test", 4,
"lasttest")
282template <
class charT,
class Traits,
class Allocator>
287 typedef std::basic_string <charT, Traits, Allocator>
String;
288 typedef typename String::iterator StringIter;
289 typedef typename String::const_iterator ConstStringIter;
291 const bool use_iters =
294 if (use_iters && tcase.
bthrow)
297 static const std::size_t BUFSIZE = 256;
299 static charT wstr_buf [BUFSIZE];
300 std::size_t str_len =
sizeof wstr_buf /
sizeof *wstr_buf;
303 static charT wres_buf [BUFSIZE];
304 std::size_t res_len =
sizeof wres_buf /
sizeof *wres_buf;
307 static char nres_buf [BUFSIZE];
310 std::size_t nreslen =
sizeof nres_buf /
sizeof *nres_buf;
318 if (wstr != wstr_buf)
328 StringIter res_iter =
str.begin ();
332 const std::size_t size1 = str_len;
333 const std::size_t off1 =
334 std::size_t (tcase.
off) < size1 ? std::size_t (tcase.
off) : size1;
335 const std::size_t ext1 =
336 off1 + tcase.
size < size1 ? tcase.
size : size1 - off1;
340 const StringIter it_first (
str.begin () + off1);
341 const StringIter it_last (it_first + ext1);
343#ifndef _RWSTD_NO_EXCEPTIONS
346 const char* expected = 0;
348 expected = exceptions [1];
355 if (wres != wres_buf)
364 const String* ret_ptr = 0;
366 typedef typename String::size_type size_type;
369 switch (
func.which_) {
372 ret_ptr = &
str.erase ();
376 ret_ptr = &
str.erase (size_type (tcase.
off));
379 case Erase (size_size):
380 ret_ptr = &
str.erase (size_type (tcase.
off),
381 size_type (tcase.
size));
385 res_iter =
str.erase (it_first);
388 case Erase (iter_iter):
389 res_iter =
str.erase (it_first, it_last);
393 RW_ASSERT (!
"test logic error: unknown erase overload");
399 const ConstStringIter begin =
str.begin ();
400 const ConstStringIter
end =
str.end ();
402 const bool success = begin <= res_iter && res_iter <=
end;
404 "line %d. %{$FUNCALL} returned invalid iterator, "
405 "difference with begin is %td",
406 __LINE__, res_iter - begin);
408 if (std::size_t (tcase.
off) >= res_len) {
410 "line %d. %{$FUNCALL} != end()", __LINE__);
413 const std::size_t match =
417 "line %d. %{$FUNCALL} == %{#c}, got %{#c}",
418 __LINE__, nres[tcase.
off], *res_iter);
425 const std::ptrdiff_t ret_off = ret_ptr - &
str;
429 "line %d. %{$FUNCALL} returned invalid reference, "
430 "offset is %td", __LINE__, ret_off);
435 "line %d. %{$FUNCALL} expected %{#*s} with length "
436 "%zu, got %{/*.*Gs} with length %zu",
437 __LINE__, int (tcase.
nres), tcase.
res,
438 res_len, int (
sizeof (
charT)),
439 int (
str.size ()),
str.c_str (),
str.size ());
441 if (res_len ==
str.size ()) {
445 const std::size_t match =
449 "line %d. %{$FUNCALL} expected %{#*s}, "
450 "got %{/*.*Gs}, difference at offset %zu",
451 __LINE__, int (tcase.
nres), tcase.
res,
452 int (
sizeof (
charT)),
int (
str.size ()),
453 str.c_str (), match);
457#ifndef _RWSTD_NO_EXCEPTIONS
459 catch (
const std::out_of_range &ex) {
462 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
463 "unexpectedly%{;} caught std::%s(%#s)",
464 __LINE__, 0 != expected, expected,
caught, ex.what ());
469 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
470 "unexpectedly%{;} caught std::%s(%#s)",
471 __LINE__, 0 != expected, expected,
caught, ex.what ());
476 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
477 "unexpectedly%{;} caught %s",
478 __LINE__, 0 != expected, expected,
caught);
490 if (nres != nres_buf)
493 if (wres != wres_buf)
501int main (
int argc,
char** argv)
508 Erase (sig), sig ## _test_cases, \
509 sizeof sig ## _test_cases / sizeof *sig ## _test_cases \
519 const std::size_t test_count =
sizeof tests /
sizeof *tests;
std::char_traits< S > Traits
_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 *,...)
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 ArgId arg_type(OverloadId id, int argno)
int assert_equal(const StringState &, int, int, const char *) const