41#define Substr(sig) StringIds::substr_ ## sig
43static const char*
const exceptions[] = {
44 "unknown exception",
"out_of_range",
"length_error",
45 "bad_alloc",
"exception"
56#define TEST(str, res) \
57 { __LINE__, -1, -1, -1, -1, -1, str, sizeof str - 1, 0, 0, \
58 res, sizeof res - 1, 0 }
67 TEST (
"<U0>",
"<U0>"),
68 TEST (
"a<U0>",
"a<U0>"),
69 TEST (
"<U0>a",
"<U0>a"),
71 TEST (
"a<U0>@2bcd<U0>",
"a<U0>@2bcd<U0>"),
72 TEST (
"<U0>@2abcd<U0>",
"<U0>@2abcd<U0>"),
73 TEST (
"<U0>@2ab<U0>@2",
"<U0>@2ab<U0>@2"),
75 TEST (
"x@4096",
"x@4096"),
89#define TEST(str, off, res, bthrow) \
90 { __LINE__, off, -1, -1, -1, -1, str, sizeof str - 1, 0, 0, \
91 res, sizeof res - 1, bthrow }
100 TEST (
"abc", 0,
"abc", 0),
103 TEST (
"<U0>", 0,
"<U0>", 0),
104 TEST (
"<U0>", 1,
"", 0),
105 TEST (
"a<U0>", 0,
"a<U0>", 0),
106 TEST (
"a<U0>", 1,
"<U0>", 0),
107 TEST (
"<U0>a", 0,
"<U0>a", 0),
108 TEST (
"<U0>a", 1,
"a", 0),
110 TEST (
"a<U0>@2bcd<U0>", 0,
"a<U0>@2bcd<U0>", 0),
111 TEST (
"a<U0>@2bcd<U0>", 1,
"<U0>@2bcd<U0>", 0),
112 TEST (
"a<U0>@2bcd<U0>", 6,
"<U0>", 0),
113 TEST (
"a<U0>@2bcd<U0>", 7,
"", 0),
114 TEST (
"<U0>@2abcd<U0>", 0,
"<U0>@2abcd<U0>", 0),
115 TEST (
"<U0>@2abcd<U0>", 1,
"<U0>abcd<U0>", 0),
116 TEST (
"<U0>@2abcd<U0>", 2,
"abcd<U0>", 0),
117 TEST (
"<U0>@2ab<U0>@2", 0,
"<U0>@2ab<U0>@2", 0),
118 TEST (
"<U0>@2ab<U0>@2", 1,
"<U0>ab<U0>@2", 0),
119 TEST (
"<U0>@2ab<U0>@2", 4,
"<U0>@2", 0),
120 TEST (
"<U0>@2ab<U0>@2", 6,
"", 0),
122 TEST (
"x@4096", 0,
"x@4096", 0),
123 TEST (
"x@4096", 4094,
"xx", 0),
124 TEST (
"x@4096", 4088,
"xxxxxxxx", 0),
125 TEST (
"x@4096", 4096,
"", 0),
126 TEST (
"abx@4096", 2,
"x@4096", 0),
127 TEST (
"x@4096ab", 2048,
"x@2048ab", 0),
129 TEST (
"<U0>", 2,
"", 1),
130 TEST (
"a", 10,
"", 1),
131 TEST (
"x@4096", 4106,
"", 1),
133 TEST (
"last", 0,
"last", 0)
141size_size_test_cases [] = {
144#define TEST(str, off, size, res, bthrow) \
145 { __LINE__, off, size, -1, -1, -1, str, sizeof str - 1, 0, 0, \
146 res, sizeof res - 1, bthrow }
158 TEST (
"abc", 0, 3,
"abc", 0),
160 TEST (
"", 0, 0,
"", 0),
161 TEST (
"<U0>", 0, 1,
"<U0>", 0),
162 TEST (
"<U0>", 0, 0,
"", 0),
163 TEST (
"<U0>", 1, 1,
"", 0),
164 TEST (
"a<U0>", 0, 2,
"a<U0>", 0),
165 TEST (
"a<U0>", 1, 2,
"<U0>", 0),
166 TEST (
"a<U0>", 0, 1,
"a", 0),
167 TEST (
"a<U0>", 1, 1,
"<U0>", 0),
168 TEST (
"a<U0>", 1, 0,
"", 0),
169 TEST (
"<U0>a", 0, 2,
"<U0>a", 0),
170 TEST (
"<U0>a", 0, 1,
"<U0>", 0),
171 TEST (
"<U0>a", 1, 2,
"a", 0),
173 TEST (
"a<U0>@2bcd<U0>", 0, 7,
"a<U0>@2bcd<U0>", 0),
174 TEST (
"a<U0>@2bcd<U0>", 1, 7,
"<U0>@2bcd<U0>", 0),
175 TEST (
"a<U0>@2bcd<U0>", 0, 3,
"a<U0>@2", 0),
176 TEST (
"a<U0>@2bcd<U0>", 1, 2,
"<U0>@2", 0),
177 TEST (
"a<U0>@2bcd<U0>", 6, 7,
"<U0>", 0),
178 TEST (
"a<U0>@2bcd<U0>", 7, 7,
"", 0),
179 TEST (
"<U0>@2abcd<U0>", 0, 7,
"<U0>@2abcd<U0>", 0),
180 TEST (
"<U0>@2abcd<U0>", 1, 7,
"<U0>abcd<U0>", 0),
181 TEST (
"<U0>@2abcd<U0>", 0, 3,
"<U0>@2a", 0),
182 TEST (
"<U0>@2abcd<U0>", 1, 1,
"<U0>", 0),
183 TEST (
"<U0>@2abcd<U0>", 6, 3,
"<U0>", 0),
184 TEST (
"<U0>@2abcd<U0>", 1, 6,
"<U0>abcd<U0>", 0),
185 TEST (
"<U0>@2abcd<U0>", 0, 0,
"", 0),
186 TEST (
"<U0>@2abcd<U0>", 2, 7,
"abcd<U0>", 0),
187 TEST (
"<U0>@2ab<U0>@2", 0, 6,
"<U0>@2ab<U0>@2", 0),
188 TEST (
"<U0>@2ab<U0>@2", 1, 6,
"<U0>ab<U0>@2", 0),
189 TEST (
"<U0>@2ab<U0>@2", 0, 2,
"<U0>@2", 0),
190 TEST (
"<U0>@2ab<U0>@2", 4, 6,
"<U0>@2", 0),
191 TEST (
"<U0>@2ab<U0>@2", 2, 3,
"ab<U0>", 0),
192 TEST (
"<U0>@2ab<U0>@2", 6, 6,
"", 0),
194 TEST (
"x@4096", 0, 4096,
"x@4096", 0),
195 TEST (
"x@4096", 4095, 4096,
"x", 0),
196 TEST (
"x@4096", 4088, 2,
"xx", 0),
197 TEST (
"x@4096", 4093, 5,
"xxx", 0),
198 TEST (
"x@4096", 4088, 8,
"xxxxxxxx", 0),
199 TEST (
"x@4096", 4096, 1,
"", 0),
200 TEST (
"ax@4096b", 1, 4094,
"x@4094", 0),
202 TEST (
"<U0>", 2, 0,
"", 1),
203 TEST (
"a", 10, 0,
"", 1),
204 TEST (
"x@4096", 4106, 0,
"", 1),
206 TEST (
"last", 0, 4,
"last", 0)
211template <
class charT,
class Traits,
class Allocator>
216 typedef std::basic_string <charT, Traits, Allocator>
String;
218 static const std::size_t BUFSIZE = 256;
220 static charT wstr_buf [BUFSIZE];
221 std::size_t str_len =
sizeof wstr_buf /
sizeof *wstr_buf;
224 static charT wres_buf [BUFSIZE];
225 std::size_t res_len =
sizeof wres_buf /
sizeof *wres_buf;
231 if (wstr != wstr_buf)
243#ifndef _RWSTD_NO_EXCEPTIONS
246 const char*
const expected = tcase.
bthrow ? exceptions [1] : 0;
252 if (wres != wres_buf)
261 switch (
func.which_) {
264 s_res =
str.substr ();
268 s_res =
str.substr (tcase.
off);
276 RW_ASSERT (!
"logic error: unknown substr overload");
282 "line %d. %{$FUNCALL} expected %{#*s} with length "
283 "%zu, got %{/*.*Gs} with length %zu",
284 __LINE__, int (tcase.
nres), tcase.
res,
285 res_len, int (
sizeof (
charT)),
286 int (s_res.size ()), s_res.c_str (), s_res.size ());
288 if (res_len == s_res.size ()) {
292 const std::size_t match =
293 rw_match (tcase.
res, s_res.c_str(), s_res.size ());
296 "line %d. %{$FUNCALL} expected %{#*s}, "
297 "got %{/*.*Gs}, difference at offset %zu",
298 __LINE__, int (tcase.
nres), tcase.
res,
299 int (
sizeof (
charT)),
int (s_res.size ()),
300 s_res.c_str (), match);
304#ifndef _RWSTD_NO_EXCEPTIONS
306 catch (
const std::out_of_range &ex) {
309 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
310 "unexpectedly%{;} caught std::%s(%#s)",
311 __LINE__, 0 != expected, expected,
caught, ex.what ());
316 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
317 "unexpectedly%{;} caught std::%s(%#s)",
318 __LINE__, 0 != expected, expected,
caught, ex.what ());
323 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
324 "unexpectedly%{;} caught %s",
325 __LINE__, 0 != expected, expected,
caught);
337 if (wres != wres_buf)
345int main (
int argc,
char** argv)
352 Substr (sig), sig ## _test_cases, \
353 sizeof sig ## _test_cases / sizeof *sig ## _test_cases \
361 const std::size_t test_count =
sizeof tests /
sizeof *tests;
void test_substr(charT, Traits *, Allocator *, const StringFunc &func, const StringTestCase &tcase)
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)
int assert_equal(const StringState &, int, int, const char *) const