Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
21.string.substr.cpp
Go to the documentation of this file.
1
29#include <string> // for string
30#include <cstddef> // for size_t
31#include <stdexcept> // for out_of_range
32
33#include <rw_strings.h> // for StringIds
34#include <rw_driver.h> // for rw_test()
35#include <rw_allocator.h> // for UserAlloc
36#include <rw_char.h> // for rw_expand()
37
38
40// for convenience and brevity
41#define Substr(sig) StringIds::substr_ ## sig
42
43static const char* const exceptions[] = {
44 "unknown exception", "out_of_range", "length_error",
45 "bad_alloc", "exception"
46};
47
48
50// used to exercise
51// substr ()
52static const StringTestCase
53void_test_cases [] = {
54
55#undef TEST
56#define TEST(str, res) \
57 { __LINE__, -1, -1, -1, -1, -1, str, sizeof str - 1, 0, 0, \
58 res, sizeof res - 1, 0 }
59
60 // +------------------------------------- controlled sequence
61 // | +----------------- expected result sequence
62 // | |
63 // V V
64 TEST ("abc", "abc"),
65
66 TEST ("", ""),
67 TEST ("<U0>", "<U0>"),
68 TEST ("a<U0>", "a<U0>"),
69 TEST ("<U0>a", "<U0>a"),
70
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"),
74
75 TEST ("x@4096", "x@4096"),
76
77 TEST ("last", "last")
78};
79
80
81
83// used to exercise
84// substr (size_type)
85static const StringTestCase
86size_test_cases [] = {
87
88#undef TEST
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 }
92
93 // +--------------------------------------- controlled sequence
94 // | +----------------- substr() off argument
95 // | | +------------- expected result sequence
96 // | | | +--- exception info:
97 // | | | | 0 - no exception
98 // | | | | 1 - out_of_range
99 // V V V V
100 TEST ("abc", 0, "abc", 0),
101
102 TEST ("", 0, "", 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),
109
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),
121
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),
128
129 TEST ("<U0>", 2, "", 1),
130 TEST ("a", 10, "", 1),
131 TEST ("x@4096", 4106, "", 1),
132
133 TEST ("last", 0, "last", 0)
134};
135
136
138// used to exercise
139// substr (size_type, size_type)
140static const StringTestCase
141size_size_test_cases [] = {
142
143#undef TEST
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 }
147
148 // +------------------------------------------ controlled sequence
149 // | +-------------------- substr() off argument
150 // | | +---------------- substr() n argument
151 // | | | +------------- expected result sequence
152 // | | | | +--- exception info:
153 // | | | | | 0 - no exception
154 // | | | | | 1 - out_of_range
155 // | | | | |
156 // | | | | +-------+
157 // V V V V V
158 TEST ("abc", 0, 3, "abc", 0),
159
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),
172
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),
193
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),
201
202 TEST ("<U0>", 2, 0, "", 1),
203 TEST ("a", 10, 0, "", 1),
204 TEST ("x@4096", 4106, 0, "", 1),
205
206 TEST ("last", 0, 4, "last", 0)
207};
208
209
211template <class charT, class Traits, class Allocator>
213 const StringFunc &func,
214 const StringTestCase &tcase)
215{
216 typedef std::basic_string <charT, Traits, Allocator> String;
217
218 static const std::size_t BUFSIZE = 256;
219
220 static charT wstr_buf [BUFSIZE];
221 std::size_t str_len = sizeof wstr_buf / sizeof *wstr_buf;
222 charT* wstr = rw_expand (wstr_buf, tcase.str, tcase.str_len, &str_len);
223
224 static charT wres_buf [BUFSIZE];
225 std::size_t res_len = sizeof wres_buf / sizeof *wres_buf;
226 charT* wres = rw_expand (wres_buf, tcase.res, tcase.nres, &res_len);
227
228 // construct the string object
229 const String str (wstr, str_len);
230
231 if (wstr != wstr_buf)
232 delete[] wstr;
233
234 wstr = 0;
235
236 String s_res;
237
238 // save the state of the string object before the call
239 // to detect wxception safety violations (changes to
240 // the state of the object after an exception)
241 const StringState str_state (rw_get_string_state (str));
242
243#ifndef _RWSTD_NO_EXCEPTIONS
244
245 // is some exception expected?
246 const char* const expected = tcase.bthrow ? exceptions [1] : 0;
247 const char* caught = 0;
248
249#else
250
251 if (tcase.bthrow) {
252 if (wres != wres_buf)
253 delete[] wres;
254
255 return;
256 }
257
258#endif // _RWSTD_NO_EXCEPTIONS
259
260 try {
261 switch (func.which_) {
262
263 case Substr (void):
264 s_res = str.substr ();
265 break;
266
267 case Substr (size):
268 s_res = str.substr (tcase.off);
269 break;
270
271 case Substr (size_size):
272 s_res = str.substr (tcase.off, tcase.size);
273 break;
274
275 default:
276 RW_ASSERT (!"logic error: unknown substr overload");
277 return;
278 }
279
280 // verfiy that strings length are equal
281 rw_assert (res_len == s_res.size (), 0, tcase.line,
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 ());
287
288 if (res_len == s_res.size ()) {
289 // if the result length matches the expected length
290 // (and only then), also verify that the resulted
291 // string matches the expected result
292 const std::size_t match =
293 rw_match (tcase.res, s_res.c_str(), s_res.size ());
294
295 rw_assert (match == res_len, 0, tcase.line,
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);
301 }
302 }
303
304#ifndef _RWSTD_NO_EXCEPTIONS
305
306 catch (const std::out_of_range &ex) {
307 caught = exceptions [1];
308 rw_assert (caught == expected, 0, tcase.line,
309 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
310 "unexpectedly%{;} caught std::%s(%#s)",
311 __LINE__, 0 != expected, expected, caught, ex.what ());
312 }
313 catch (const std::exception &ex) {
314 caught = exceptions [4];
315 rw_assert (0, 0, tcase.line,
316 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
317 "unexpectedly%{;} caught std::%s(%#s)",
318 __LINE__, 0 != expected, expected, caught, ex.what ());
319 }
320 catch (...) {
321 caught = exceptions [0];
322 rw_assert (0, 0, tcase.line,
323 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
324 "unexpectedly%{;} caught %s",
325 __LINE__, 0 != expected, expected, caught);
326 }
327
328 if (caught) {
329 // verify that an exception thrown during allocation
330 // didn't cause a change in the state of the object
332 __LINE__, tcase.line, caught);
333 }
334
335#endif // _RWSTD_NO_EXCEPTIONS
336
337 if (wres != wres_buf)
338 delete[] wres;
339}
340
341
344
345int main (int argc, char** argv)
346{
347 static const StringTest
348 tests [] = {
349
350#undef TEST
351#define TEST(sig) { \
352 Substr (sig), sig ## _test_cases, \
353 sizeof sig ## _test_cases / sizeof *sig ## _test_cases \
354 }
355
356 TEST (void),
357 TEST (size),
358 TEST (size_size)
359 };
360
361 const std::size_t test_count = sizeof tests / sizeof *tests;
362
363 return rw_run_string_test (argc, argv, __FILE__,
364 "lib.string.substr",
365 test_substr, tests, test_count);
366}
#define charT
Definition 17.names.cpp:456
#define TEST(str, res)
void test_substr(charT, Traits *, Allocator *, const StringFunc &func, const StringTestCase &tcase)
#define Substr(sig)
std::char_traits< S > Traits
#define main
int caught
const char * str
Definition exec.cpp:170
_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 *,...)
Definition driver.cpp:1630
StringState rw_get_string_state(const String &str)
Definition rw_strings.h:752
#define DEFINE_STRING_TEST_DISPATCH(fname)
Definition rw_strings.h:885
_TEST_EXPORT int rw_run_string_test(int, char **, const char *, const char *, StringTestFunc *, const StringTest *, _RWSTD_SIZE_T)
#define RW_ASSERT(expr)
int assert_equal(const StringState &, int, int, const char *) const
_RWSTD_SIZE_T str_len
Definition rw_strings.h:597
const char * str
Definition rw_strings.h:596
_RWSTD_SIZE_T nres
Definition rw_strings.h:603
const char * res
Definition rw_strings.h:602