41#define Copy(which) StringIds::copy_ ## which
43static const char*
const exceptions[] = {
44 "unknown exception",
"out_of_range",
"length_error",
45 "bad_alloc",
"exception"
53ptr_size_test_cases [] = {
56#define TEST(str, size, res) { \
57 __LINE__, -1, size, -1, -1, -1, \
58 str, sizeof str - 1, 0, 0, \
59 res, sizeof res - 1, 0 \
72 TEST (
"<U0>", 1,
"<U0>"),
73 TEST (
"<U0>@2", 2,
"<U0>@2"),
77 TEST (
"abc", 2,
"ab"),
78 TEST (
"abc", 3,
"abc"),
79 TEST (
"abc", 5,
"abc"),
81 TEST (
"a<U0>b<U0>@2c", 10,
"a<U0>b<U0>@2c"),
82 TEST (
"<U0>ab<U0>@3c<U0>", 1,
"<U0>"),
83 TEST (
"<U0>ab<U0>@3c<U0>", 8,
"<U0>ab<U0>@3c<U0>"),
84 TEST (
"<U0>ab<U0>@3c<U0>", 5,
"<U0>ab<U0>@2"),
85 TEST (
"<U0>@2ab<U0>@2c<U0>", 6,
"<U0>@2ab<U0>@2"),
87 TEST (
"x@4096", 4096,
"x@4096"),
89 TEST (
"last", 4,
"last")
97ptr_size_size_test_cases [] = {
100#define TEST(str, size, off, res, bthrow) { \
101 __LINE__, off, size, -1, -1, -1, \
102 str, sizeof str - 1, 0, 0, \
103 res, sizeof res - 1, bthrow \
116 TEST (
"ab", 2, 0,
"ab", 0),
118 TEST (
"", 0, 0,
"", 0),
119 TEST (
"", 10, 0,
"", 0),
121 TEST (
"<U0>", 1, 0,
"<U0>", 0),
122 TEST (
"<U0>@2", 1, 1,
"<U0>", 0),
123 TEST (
"<U0>@2", 2, 0,
"<U0>@2", 0),
125 TEST (
"abc", 1, 0,
"a", 0),
126 TEST (
"abc", 1, 1,
"b", 0),
127 TEST (
"abc", 1, 2,
"c", 0),
129 TEST (
"abc", 0, 0,
"", 0),
130 TEST (
"abc", 2, 0,
"ab", 0),
131 TEST (
"abc", 2, 1,
"bc", 0),
132 TEST (
"abc", 3, 0,
"abc", 0),
133 TEST (
"abc", 10, 1,
"bc", 0),
134 TEST (
"abc", 3, 2,
"c", 0),
136 TEST (
"a<U0>b<U0>@2c", 10, 1,
"<U0>b<U0>@2c", 0),
137 TEST (
"a<U0>b<U0>@2c", 10, 0,
"a<U0>b<U0>@2c", 0),
138 TEST (
"a<U0>b<U0>@2c", 1, 1,
"<U0>", 0),
140 TEST (
"<U0>ab<U0>@3c<U0>", 10, 1,
"ab<U0>@3c<U0>", 0),
141 TEST (
"<U0>ab<U0>@3c<U0>", 5, 0,
"<U0>ab<U0>@2", 0),
142 TEST (
"<U0>ab<U0>@3c<U0>", 3, 3,
"<U0>@3", 0),
144 TEST (
"<U0>@2ab<U0>@2c<U0>", 6, 0,
"<U0>@2ab<U0>@2", 0),
145 TEST (
"<U0>@2ab<U0>@2c<U0>", 4, 1,
"<U0>ab<U0>", 0),
147 TEST (
"x@4096", 4096, 0,
"x@4096", 0),
148 TEST (
"x@4096", 2, 1,
"xx", 0),
149 TEST (
"x@4096", 4096, 4095,
"x", 0),
151 TEST (
"<U0>", 0, 2,
"", 1),
152 TEST (
"a", 0, 10,
"", 1),
153 TEST (
"x@4096", 0, 4106,
"", 1),
155 TEST (
"last", 4, 0,
"last", 0)
160template <
class charT,
class Traits,
class Allocator>
165 typedef std::basic_string <charT, Traits, Allocator>
String;
167 static const std::size_t BUFSIZE = 256;
169 static charT wstr_buf [BUFSIZE];
170 std::size_t str_len =
sizeof wstr_buf /
sizeof *wstr_buf;
173 static charT wres_buf [BUFSIZE];
174 std::size_t res_len =
sizeof wres_buf /
sizeof *wres_buf;
180 if (wstr != wstr_buf)
185 const std::size_t min_len =
186 str_len < std::size_t (tcase.
size) ? str_len : tcase.
size;
193 const char cgb [2] =
"#";
195 Traits::assign (s_res, min_len + 1, wcgb);
196 s_res [min_len + 1] =
charT ();
203#ifndef _RWSTD_NO_EXCEPTIONS
206 const char*
const expected = tcase.
bthrow ? exceptions [1] : 0;
212 if (wres != wres_buf)
221 switch (
func.which_) {
222 case Copy (ptr_size): {
227 case Copy (ptr_size_size): {
232 RW_ASSERT (!
"logic error: unknown copy overload");
238 "line %d. %{$FUNCALL} == %zu, got %zu",
239 __LINE__, res_len,
res);
243 const std::size_t match =
rw_match (tcase.
res, s_res, res_len);
244 bool success = match == res_len;
247 "line %d. %{$FUNCALL} expected %{#*s}, "
248 "got %{/*.*Gs}, differ at pos %zu",
249 __LINE__, int (tcase.
nres), tcase.
res,
250 int (
sizeof (
charT)),
int (
res), s_res, match);
252 success = 1 ==
rw_match (cgb, s_res + min_len, 1);
254 "line %d. %{$FUNCALL} detected writing past the end of "
255 "the provided buffer", __LINE__);
259#ifndef _RWSTD_NO_EXCEPTIONS
261 catch (
const std::out_of_range &ex) {
264 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
265 "unexpectedly%{;} caught std::%s(%#s)",
266 __LINE__, 0 != expected, expected,
caught, ex.what ());
271 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
272 "unexpectedly%{;} caught std::%s(%#s)",
273 __LINE__, 0 != expected, expected,
caught, ex.what ());
278 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
279 "unexpectedly%{;} caught %s",
280 __LINE__, 0 != expected, expected,
caught);
289 else if (-1 != tcase.
bthrow) {
291 "line %d. %{$FUNCALL} %{?}expected %s, caught %s"
292 "%{:}unexpectedly caught %s%{;}",
298 if (wres != wres_buf)
308int main (
int argc,
char** argv)
314#define TEST(which) { \
315 Copy (which), which ## _test_cases, \
316 sizeof which ## _test_cases / sizeof *which ## _test_cases \
323 const std::size_t test_count =
sizeof tests /
sizeof *tests;
#define TEST(str, size, res)
void test_copy(std::size_t N, const InputIterator &input_iter, const OutputIterator &output_iter, const T *)
std::char_traits< S > Traits
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 *,...)
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)
int assert_equal(const StringState &, int, int, const char *) const