Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
21.string.compare.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, length_error
32
33#include <rw_strings.h> // for StringMembers
34#include <rw_driver.h> // for rw_assert()
35#include <rw_allocator.h> // for UserAlloc
36#include <rw_char.h> // for rw_expand()
37
38
40// for convenience and brevity
41#define Compare(sig) StringIds::compare_ ## sig
42
43static const char* const exceptions[] = {
44 "unknown exception", "out_of_range", "length_error",
45 "bad_alloc", "exception"
46};
47
48
50// exercises:
51// compare (const value_type*)
52static const StringTestCase
53cptr_test_cases [] = {
54
55#undef TEST
56#define TEST(str, arg, res, bthrow) { \
57 __LINE__, -1, -1, -1, -1, -1, \
58 str, sizeof str - 1, \
59 arg, sizeof arg - 1, 0, res, bthrow \
60 }
61
62 // +------------------------------------------ controlled sequence
63 // | +----------------------- sequence to be inserted
64 // | | +------ expected result
65 // | | | +-- exception info
66 // | | | | 0 - no exception
67 // | | | | 1,2 - out_of_range
68 // | | | | 3 - length_error
69 // | | | |
70 // | | | |
71 // V V V V
72 TEST ("ab", "ab", 0, 0),
73
74 TEST ("", "", 0, 0),
75 TEST ("", "\0", 0, 0),
76 TEST ("", "a", NPOS, 0),
77
78 TEST ("\0", "", 1, 0),
79 TEST ("\0", "\0", 1, 0),
80 TEST ("\0", "a", NPOS, 0),
81
82 TEST ("abcdefghij", "abcdefghij", 0, 0),
83 TEST ("abcdefghij", "abcdefghi", 1, 0),
84 TEST ("abcdefghi", "abcdefghij", NPOS, 0),
85
86 TEST ("abbdefghij", "abcdefghij", NPOS, 0),
87 TEST ("abcdeeghij", "abcdefghij", NPOS, 0),
88 TEST ("abcdefghii", "abcdefghij", NPOS, 0),
89
90 TEST ("bbcdefghij", "a", 1, 0),
91 TEST ("eeeeeeghij", "aeeee", 1, 0),
92 TEST ("a", "bbcdefghij", NPOS, 0),
93 TEST ("aeeee", "eeeeeeghij", NPOS, 0),
94
95 TEST ("bbcdefghij", "abcdefghij", 1, 0),
96 TEST ("abcdffghij", "abcdefghij", 1, 0),
97 TEST ("abcdefghjj", "abcdefghij", 1, 0),
98
99 TEST ("a\0b\0\0c", "a\0b\0\0c", 1, 0),
100 TEST ("abc\0\0\0", "abc\0\0\0", 1, 0),
101 TEST ("\0ab\0\0c", "e\0ab\0\0c", NPOS, 0),
102
103 TEST ("a\0b", "a\0b\0\0c", 1, 0),
104 TEST ("ab\0", "abc\0\0\0", NPOS, 0),
105 TEST ("\0ab", "e\0ab\0\0c", NPOS, 0),
106
107 TEST ("x@4096", "x@4096", 0, 0),
108 TEST ("xx", "x@4096", NPOS, 0),
109 TEST ("x@4096", "xxxxxx", 1, 0),
110 TEST ("a\0b", "x@4096", NPOS, 0),
111 TEST ("x@4096", "a\0b", 1, 0),
112
113 TEST ("a@2048b@2048", "a@2048b@2047", 1, 0),
114 TEST ("a@2048b@2048", "a@2048b@2047c", NPOS, 0),
115 TEST ("a@2048cb@2047", "a@2048cb@2047", 0, 0),
116
117 TEST ("last", "last", 0, 0)
118};
119
120
122// exercises:
123// compare (const basic_string&)
124static const StringTestCase
125cstr_test_cases [] = {
126
127#undef TEST
128#define TEST(str, arg, res, bthrow) { \
129 __LINE__, -1, -1, -1, -1, -1, \
130 str, sizeof str - 1, \
131 arg, sizeof arg - 1, 0, res, bthrow \
132 }
133
134 // +------------------------------------------- controlled sequence
135 // | +------------------------ sequence to be compared
136 // | | +------- expected result
137 // | | | +- exception info
138 // | | | | 0 - no exception
139 // | | | | 1,2 - out_of_range
140 // | | | | 3 - length_error
141 // | | | |
142 // | | | |
143 // V V V V
144 TEST ("ab", "ab", 0, 0),
145
146 TEST ("", "", 0, 0),
147 TEST ("", "\0", NPOS, 0),
148 TEST ("", "a", NPOS, 0),
149
150 TEST ("\0", "", 1, 0),
151 TEST ("\0", "\0", 0, 0),
152 TEST ("\0", "a", NPOS, 0),
153
154 TEST ("abcdefghij", "abcdefghij", 0, 0),
155 TEST ("abcdefghij", "abcdefghi", 1, 0),
156 TEST ("abcdefghi", "abcdefghij", NPOS, 0),
157
158 TEST ("abbdefghij", "abcdefghij", NPOS, 0),
159 TEST ("abcdeeghij", "abcdefghij", NPOS, 0),
160 TEST ("abcdefghii", "abcdefghij", NPOS, 0),
161
162 TEST ("bbcdefghij", "a", 1, 0),
163 TEST ("eeeeeeghij", "aeeee", 1, 0),
164 TEST ("a", "bbcdefghij", NPOS, 0),
165 TEST ("aeeee", "eeeeeeghij", NPOS, 0),
166
167 TEST ("bbcdefghij", "abcdefghij", 1, 0),
168 TEST ("abcdffghij", "abcdefghij", 1, 0),
169 TEST ("abcdefghjj", "abcdefghij", 1, 0),
170
171 TEST ("a\0b\0\0c", "a\0b\0\0c", 0, 0),
172 TEST ("abc\0\0\0", "bac\0\0\0", NPOS, 0),
173 TEST ("\0ab\0\0c", "e\0ab\0\0c", NPOS, 0),
174 TEST ("\0ab\0\0c", "\0ab\0\0b", 1, 0),
175
176 TEST ("a\0b", "a\0b\0\0c", NPOS, 0),
177 TEST ("ab\0", "abc\0\0\0", NPOS, 0),
178 TEST ("\0ab", "e\0ab\0\0c", NPOS, 0),
179 TEST ("bb\0", "ab\0\0\0c", 1, 0),
180 TEST ("\0ac", "\0abe\0\0c", 1, 0),
181
182 TEST ("x@4096", "x@4096", 0, 0),
183 TEST ("xx", "x@4096", NPOS, 0),
184 TEST ("x@4096", "xxxxxx", 1, 0),
185 TEST ("a\0b", "x@4096", NPOS, 0),
186 TEST ("x@4096", "a\0b", 1, 0),
187
188 TEST ("a@2048b@2048", "a@2048b@2047", 1, 0),
189 TEST ("a@2048b@2048", "a@2048b@2047c", NPOS, 0),
190 TEST ("a@2048cb@2047", "a@2048cb@2047", 0, 0),
191
192 TEST ("last", "last", 0, 0)
193};
194
195
197// exercises:
198// compare (size_type, size_type, const value_type*)
199static const StringTestCase
201
202#undef TEST
203#define TEST(str, off, size, arg, res, bthrow) { \
204 __LINE__, off, size, -1, -1, -1, \
205 str, sizeof str - 1, \
206 arg, sizeof arg - 1, 0, res, bthrow \
207 }
208
209 // +------------------------------------------ controlled sequence
210 // | +--------------------------- compare() off argument
211 // | | +----------------------- compare() n1 argument
212 // | | | +-------------------- sequence to be compared
213 // | | | | +--- expected result
214 // | | | | | +- exception info
215 // | | | | | | 0 - no exception
216 // | | | | | | 1,2 - out_of_range
217 // | | | | | | 3 - length_error
218 // | | | | | |
219 // | | | | | |
220 // V V V V V V
221 TEST ("ab", 0, 2, "ab", 0, 0),
222
223 TEST ("", 0, 0, "", 0, 0),
224 TEST ("", 0, 0, "\0", 0, 0),
225 TEST ("", 0, 0, "a", NPOS, 0),
226
227 TEST ("\0", 0, 1, "", 1, 0),
228 TEST ("\0", 0, 1, "\0", 1, 0),
229 TEST ("\0", 0, 1, "a", NPOS, 0),
230
231 TEST ("abcdefghij", 0, 10, "abcdefghij", 0, 0),
232 TEST ("abcdefghij", 0, 10, "abcdefghi", 1, 0),
233 TEST ("abcdefghij", 1, 9, "abcdefghi", 1, 0),
234 TEST ("abcdefghij", 1, 8, "abcdefghi", 1, 0),
235 TEST ("abcdefghij", 2, 5, "abcdefghi", 1, 0),
236 TEST ("abcdefghi", 0, 9, "abcdefghij", NPOS, 0),
237
238 TEST ("abbdefghij", 0, 10, "abcdefghij", NPOS, 0),
239 TEST ("abbdefghij", 1, 9, "abcdefghij", 1, 0),
240 TEST ("abcdeeghij", 0, 10, "abcdefghij", NPOS, 0),
241 TEST ("abcdeeghij", 5, 10, "abcdefghij", 1, 0),
242 TEST ("abcdeeghij", 0, 5, "abcdefghij", NPOS, 0),
243 TEST ("abcdefghii", 0, 10, "abcdefghij", NPOS, 0),
244 TEST ("abcdefghii", 9, 10, "abcdefghij", 1, 0),
245
246 TEST ("abcdefghij", 1, 1, "b", 0, 0),
247 TEST ("abcdefghij", 0, 1, "b", NPOS, 0),
248 TEST ("abcdefghij", 1, 4, "bcde", 0, 0),
249 TEST ("abcdefghij", 1, 3, "bcde", NPOS, 0),
250
251 TEST ("bbcdefghij", 2, 4, "a", 1, 0),
252 TEST ("bbcdefghij", 2, 0, "a", NPOS, 0),
253 TEST ("aeeeeeghij", 6, 1, "aeeee", 1, 0),
254 TEST ("aeeeeeghij", 0, 2, "aeeee", NPOS, 0),
255 TEST ("a", 0, 2, "bbcdefghij", NPOS, 0),
256 TEST ("jeeee", 1, 2, "eeeeeeghij", NPOS, 0),
257
258 TEST ("a\0b\0\0c", 0, 6, "a\0b\0\0c", 1, 0),
259 TEST ("a\0b\0\0c", 0, 1, "a\0b\0\0c", 0, 0),
260 TEST ("abc\0\0\0", 0, 4, "abc\0\0\0", 1, 0),
261 TEST ("abc\0\0\0", 0, 3, "abc\0\0\0", 0, 0),
262 TEST ("\0af\0\0c", 0, 6, "e\0ab\0\0c", NPOS, 0),
263 TEST ("\0af\0\0c", 2, 2, "e\0ab\0\0c", 1, 0),
264
265 TEST ("a\0b", 0, 3, "a\0b\0\0c", 1, 0),
266 TEST ("a\0b", 0, 1, "a\0b\0\0c", 0, 0),
267 TEST ("ab\0", 0, 5, "abc\0\0\0", NPOS, 0),
268 TEST ("\0eb", 0, 5, "e\0ab\0\0c", NPOS, 0),
269 TEST ("\0eb", 1, 5, "e\0ab\0\0c", 1, 0),
270
271 TEST ("x@4096", 0, 4096, "x@4096", 0, 0),
272 TEST ("x@4096", 1, 4096, "x@4096", NPOS, 0),
273 TEST ("xx", 0, 2, "x@4096", NPOS, 0),
274 TEST ("x@4096", 0, 4096, "xxxxx", 1, 0),
275 TEST ("x@4096", 2, 5, "xxxxx", 0, 0),
276 TEST ("x@4096", 5, 2, "xxxxx", NPOS, 0),
277 TEST ("a\0x", 0, 3, "x@4096", NPOS, 0),
278 TEST ("x@4096", 0, 10, "x\0b", 1, 0),
279 TEST ("x@4096", 0, 1, "x\0b", 0, 0),
280
281 TEST ("\0", 2, 0, "", 1, 1),
282 TEST ("a", 10, 0, "", 1, 1),
283 TEST ("x@4096", 4106, 0, "", 1, 1),
284
285 TEST ("last", 0, 4, "last", 0, 0)
286};
287
288
290// exercises:
291// compare (size_type, size_type, basic_string&)
292static const StringTestCase
294
295#undef TEST
296#define TEST(str, off, size, arg, res, bthrow) { \
297 __LINE__, off, size, -1, -1, -1, \
298 str, sizeof str - 1, \
299 arg, sizeof arg - 1, 0, res, bthrow \
300 }
301
302 // +------------------------------------------ controlled sequence
303 // | +--------------------------- compare() off argument
304 // | | +----------------------- compare() n1 argument
305 // | | | +-------------------- sequence to be compared
306 // | | | | +--- expected result
307 // | | | | | +- exception info
308 // | | | | | | 0 - no exception
309 // | | | | | | 1,2 - out_of_range
310 // | | | | | | 3 - length_error
311 // | | | | | |
312 // | | | | | |
313 // V V V V V V
314 TEST ("ab", 0, 2, "ab", 0, 0),
315
316 TEST ("", 0, 0, "", 0, 0),
317 TEST ("", 0, 0, "\0", NPOS, 0),
318 TEST ("", 0, 0, "a", NPOS, 0),
319
320 TEST ("\0", 0, 1, "", 1, 0),
321 TEST ("\0", 0, 1, "\0", 0, 0),
322 TEST ("\0", 0, 1, "\0\0", NPOS, 0),
323 TEST ("\0", 0, 1, "a", NPOS, 0),
324
325 TEST ("abcdefghij", 0, 10, "abcdefghij", 0, 0),
326 TEST ("abcdefghij", 0, 10, "abcdefghi", 1, 0),
327 TEST ("abcdefghij", 1, 9, "abcdefghi", 1, 0),
328 TEST ("abcdefghij", 1, 8, "abcdefghi", 1, 0),
329 TEST ("abcdefghij", 2, 5, "abcdefghi", 1, 0),
330 TEST ("abcdefghi", 0, 9, "abcdefghij", NPOS, 0),
331
332 TEST ("abbdefghij", 0, 10, "abcdefghij", NPOS, 0),
333 TEST ("abbdefghij", 1, 9, "abcdefghij", 1, 0),
334 TEST ("abcdeeghij", 0, 10, "abcdefghij", NPOS, 0),
335 TEST ("abcdeeghij", 5, 10, "abcdefghij", 1, 0),
336 TEST ("abcdeeghij", 0, 5, "abcdefghij", NPOS, 0),
337 TEST ("abcdefghii", 0, 10, "abcdefghij", NPOS, 0),
338 TEST ("abcdefghii", 9, 10, "abcdefghij", 1, 0),
339
340 TEST ("abcdefghij", 1, 1, "b", 0, 0),
341 TEST ("abcdefghij", 0, 1, "b", NPOS, 0),
342 TEST ("abcdefghij", 1, 4, "bcde", 0, 0),
343 TEST ("abcdefghij", 1, 3, "bcde", NPOS, 0),
344
345 TEST ("bbcdefghij", 2, 4, "a", 1, 0),
346 TEST ("bbcdefghij", 2, 0, "a", NPOS, 0),
347 TEST ("aeeeeeghij", 6, 1, "aeeee", 1, 0),
348 TEST ("aeeeeeghij", 0, 2, "aeeee", NPOS, 0),
349 TEST ("a", 0, 2, "bbcdefghij", NPOS, 0),
350 TEST ("jeeee", 1, 2, "eeeeeeghij", NPOS, 0),
351
352 TEST ("a\0b\0\0c", 0, 6, "a\0b\0\0c", 0, 0),
353 TEST ("a\0b\0\0c", 2, 4, "a\0b\0\0c", 1, 0),
354 TEST ("e\0b\0\0c", 0, 1, "a\0b\0\0c", 1, 0),
355 TEST ("abc\0\0\0", 0, 4, "abc\0\0\0", NPOS, 0),
356 TEST ("ab\0c\0\0", 2, 2, "ab\0b\0\0", NPOS, 0),
357 TEST ("abc\0\0\0", 3, 3, "abc\0\0\0", NPOS, 0),
358 TEST ("\0af\0\0c", 0, 6, "e\0ab\0\0c", NPOS, 0),
359 TEST ("\0af\0\0c", 0, 1, "\0ab\0\0ce", NPOS, 0),
360 TEST ("\0af\0\0c", 2, 2, "e\0ab\0\0c", 1, 0),
361 TEST ("\0ae\0\0c", 2, 4, "e\0\0b\0ca", 1, 0),
362
363 TEST ("a\0b", 0, 3, "a\0b\0\0c", NPOS, 0),
364 TEST ("a\0b", 0, 1, "a\0b\0\0c", NPOS, 0),
365 TEST ("ab\0", 0, 5, "abc\0\0\0", NPOS, 0),
366 TEST ("\0eb", 0, 5, "e\0ab\0\0c", NPOS, 0),
367 TEST ("a\0b\0\0c", 0, 3, "a\0b", 0, 0),
368 TEST ("a\0b\0\0c", 0, 1, "a", 0, 0),
369 TEST ("abc\0\0\0", 0, 5, "abc\0\0", 0, 0),
370 TEST ("e\0ab\0\0c", 0, 7, "e\0ab\0\0c", 0, 0),
371 TEST ("a\0b\0\0c", 2, 3, "a\0b", 1, 0),
372 TEST ("a\0b\0\0c", 2, 1, "a", 1, 0),
373 TEST ("abc\0\0\0", 2, 5, "abc\0\0", 1, 0),
374 TEST ("e\0ag\0\0c", 3, 6, "e\0ab\0\0c", 1, 0),
375
376 TEST ("x@4096", 0, 4096, "x@4096", 0, 0),
377 TEST ("x@4096", 1, 4096, "x@4096", NPOS, 0),
378 TEST ("xx", 0, 2, "x@4096", NPOS, 0),
379 TEST ("x@4096", 0, 4096, "xxxxx", 1, 0),
380 TEST ("x@4096", 2, 5, "xxxxx", 0, 0),
381 TEST ("x@4096", 5, 2, "xxxxx", NPOS, 0),
382 TEST ("a\0x", 0, 3, "x@4096", NPOS, 0),
383 TEST ("x@4096", 0, 10, "x\0b", 1, 0),
384 TEST ("x@4096", 0, 1, "x\0b", NPOS, 0),
385 TEST ("a@4096\0", 4096, 1, "\0", 0, 0),
386
387 TEST ("\0", 2, 0, "", 1, 1),
388 TEST ("a", 10, 0, "", 1, 1),
389 TEST ("x@4096", 4106, 0, "", 1, 1),
390
391 TEST ("last", 0, 4, "last", 0, 0)
392};
393
394
396// exercises:
397// compare (size_type, size_type, const value_type*, size_type)
398static const StringTestCase
400
401#undef TEST
402#define TEST(str, off, size, arg, size2, res, bthrow) { \
403 __LINE__, off, size, -1, size2, -1, \
404 str, sizeof str - 1, \
405 arg, sizeof arg - 1, 0, res, bthrow \
406 }
407
408 // +------------------------------------------- controlled sequence
409 // | +---------------------------- compare() off argument
410 // | | +------------------------ compare() n1 argument
411 // | | | +--------------------- sequence to be compared
412 // | | | | +------ compare() n2 argument
413 // | | | | | +----- expected result
414 // | | | | | | +- exception info
415 // | | | | | | | 0 - no exception
416 // | | | | | | | 1,2 - out_of_range
417 // | | | | | | | 3 - length_error
418 // | | | | | | |
419 // | | | | | | |
420 // V V V V V V V
421 TEST ("ab", 0, 2, "ab", 2, 0, 0),
422
423 TEST ("", 0, 0, "", 0, 0, 0),
424 TEST ("", 0, 0, "\0", 1, NPOS, 0),
425 TEST ("", 0, 0, "a", 0, 0, 0),
426 TEST ("", 0, 0, "a", 1, NPOS, 0),
427
428 TEST ("\0", 0, 1, "", 0, 1, 0),
429 TEST ("\0", 0, 1, "\0", 1, 0, 0),
430 TEST ("\0", 0, 1, "\0\0", 2, NPOS, 0),
431 TEST ("\0", 0, 1, "\0\0", 1, 0, 0),
432 TEST ("\0\0", 1, 1, "\0", 1, 0, 0),
433 TEST ("\0", 0, 1, "a", 1, NPOS, 0),
434
435 TEST ("abcdefghij", 0, 10, "abcdefghij", 10, 0, 0),
436 TEST ("abcdefghij", 0, 10, "abcdefghi", 9, 1, 0),
437 TEST ("abcdefghij", 1, 9, "abcdefghi", 9, 1, 0),
438 TEST ("abcdefghij", 1, 8, "abcdefghi", 9, 1, 0),
439 TEST ("abcdefghij", 2, 5, "abcdefghi", 9, 1, 0),
440 TEST ("abcdefghi", 0, 9, "abcdefghij", 10, NPOS, 0),
441 TEST ("abcdefghi", 0, 4, "abcdefghij", 4, 0, 0),
442 TEST ("abcdefghi", 0, 5, "abcdefghij", 4, 1, 0),
443
444 TEST ("abbdefghij", 0, 10, "abcdefghij", 10, NPOS, 0),
445 TEST ("abbdefghij", 1, 9, "abcdefghij", 10, 1, 0),
446 TEST ("abcdeeghij", 0, 10, "abcdefghij", 10, NPOS, 0),
447 TEST ("abcdeeghij", 5, 10, "abcdefghij", 10, 1, 0),
448 TEST ("abcdeeghij", 0, 3, "abcdefghij", 3, 0, 0),
449 TEST ("abcdeeghij", 0, 5, "abcdefghij", 10, NPOS, 0),
450 TEST ("abcdefghii", 0, 10, "abcdefghij", 10, NPOS, 0),
451 TEST ("abcdefghii", 0, 9, "abcdefghij", 9, 0, 0),
452 TEST ("abcdefghii", 9, 10, "abcdefghij", 10, 1, 0),
453
454 TEST ("abcdefghij", 1, 1, "b", 1, 0, 0),
455 TEST ("abcdefghij", 1, 1, "g", 1, NPOS, 0),
456 TEST ("abcdefghij", 1, 1, "g", 0, 1, 0),
457 TEST ("abcdefghij", 1, 1, "bfg", 1, 0, 0),
458 TEST ("abcdefghij", 0, 1, "b", 1, NPOS, 0),
459 TEST ("abcdefghij", 1, 4, "bcde", 4, 0, 0),
460 TEST ("abcdefghij", 1, 3, "bcde", 4, NPOS, 0),
461 TEST ("abcdefghij", 1, 3, "bcde", 2, 1, 0),
462
463 TEST ("bbcdefghij", 2, 4, "a", 1, 1, 0),
464 TEST ("bbcdefghij", 2, 0, "a", 1, NPOS, 0),
465 TEST ("bbcaefghij", 3, 1, "age", 1, 0, 0),
466 TEST ("aeeeeeghij", 6, 1, "aeeee", 2, 1, 0),
467 TEST ("aeeeeeghij", 0, 2, "aeeee", 3, NPOS, 0),
468 TEST ("aeeeeeghij", 2, 3, "aeeee", 5, 1, 0),
469 TEST ("a", 0, 2, "bbcdefghij", 10, NPOS, 0),
470 TEST ("jeeee", 1, 2, "eeeeeeghij", 10, NPOS, 0),
471 TEST ("jeeee", 1, 4, "eeeeeeghij", 4, 0, 0),
472
473 TEST ("a\0b\0\0c", 0, 6, "a\0b\0\0c", 6, 0, 0),
474 TEST ("a\0b\0\0c", 2, 4, "a\0b\0\0c", 6, 1, 0),
475 TEST ("e\0b\0\0c", 0, 1, "a\0b\0\0c", 6, 1, 0),
476 TEST ("e\0b\0\0c", 3, 2, "\0\0a\0bc", 2, 0, 0),
477 TEST ("e\0b\0\0c", 3, 3, "\0\0c\0ba", 4, NPOS, 0),
478 TEST ("abc\0\0\0", 0, 4, "abc\0\0\0", 5, NPOS, 0),
479 TEST ("ab\0c\0\0", 2, 2, "ab\0b\0\0", 4, NPOS, 0),
480 TEST ("abc\0\0\0", 3, 3, "abc\0\0\0", 3, NPOS, 0),
481 TEST ("\0af\0\0c", 0, 6, "e\0ab\0\0c", 6, NPOS, 0),
482 TEST ("\0af\0\0c", 0, 1, "\0ab\0\0ce", 2, NPOS, 0),
483 TEST ("\0\0c\0af", 3, 3, "\0ab\0\0ce", 3, 1, 0),
484 TEST ("\0af\0\0c", 2, 2, "e\0ab\0\0c", 2, 1, 0),
485 TEST ("\0ae\0\0c", 2, 4, "e\0\0b\0ca", 4, 1, 0),
486
487 TEST ("a\0b", 0, 3, "a\0b\0\0c", 4, NPOS, 0),
488 TEST ("a\0b", 0, 1, "a\0b\0\0c", 2, NPOS, 0),
489 TEST ("ab\0", 0, 5, "abc\0\0\0", 3, NPOS, 0),
490 TEST ("\0eb", 0, 5, "e\0ab\0\0c", 1, NPOS, 0),
491 TEST ("a\0b\0\0c", 0, 3, "a\0b", 3, 0, 0),
492 TEST ("a\0b\0\0c", 0, 1, "a", 1, 0, 0),
493 TEST ("abc\0\0\0", 0, 5, "abc\0\0", 5, 0, 0),
494 TEST ("e\0ab\0\0c", 0, 7, "e\0ab\0\0c", 7, 0, 0),
495 TEST ("a\0b\0\0c", 2, 3, "a\0b", 2, 1, 0),
496 TEST ("a\0b\0\0c", 2, 1, "a", 0, 1, 0),
497 TEST ("abc\0\0\0", 2, 5, "c\0\0ab", 3, 1, 0),
498 TEST ("e\0ag\0\0c", 3, 6, "e\0ab\0\0c", 7, 1, 0),
499 TEST ("abc\0\0\0", 2, 5, "c\0\0ab", 4, NPOS, 0),
500
501 TEST ("x@4096", 0, 4096, "x@4096", 4096, 0, 0),
502 TEST ("x@4096", 1, 4096, "x@4096", 4096, NPOS, 0),
503 TEST ("x@4096", 1, 4096, "x@4096", 4092, 1, 0),
504 TEST ("xx", 0, 2, "x@4096", 4092, NPOS, 0),
505 TEST ("x@4096", 0, 4096, "xxxxx", 5, 1, 0),
506 TEST ("x@4096", 4090, 5, "xxxxx", 5, 0, 0),
507 TEST ("x@4096", 2, 5, "xxxxx", 5, 0, 0),
508 TEST ("x@4096", 5, 2, "xxxxx", 3, NPOS, 0),
509 TEST ("a\0x", 0, 3, "x@4096", 10, NPOS, 0),
510 TEST ("x@4096", 0, 10, "x\0b", 2, 1, 0),
511 TEST ("x@4096", 0, 1, "x\0b", 2, NPOS, 0),
512
513 TEST ("a@4096\0", 4096, 1, "\0b", 1, 0, 0),
514
515 TEST ("\0", 2, 0, "", 0, 1, 1),
516 TEST ("a", 10, 0, "", 0, 1, 1),
517 TEST ("x@4096", 4106, 0, "", 0, 1, 1),
518
519 TEST ("last", 0, 4, "last", 4, 0, 0)
520};
521
522
524// exercises:
525// compare (size_type, size_type, basic_string&, size_type, size_type)
526static const StringTestCase
528
529#undef TEST
530#define TEST(str, off, size, arg, off2, size2, res, bthrow) { \
531 __LINE__, off, size, off2, size2, -1, \
532 str, sizeof str - 1, \
533 arg, sizeof arg - 1, 0, res, bthrow \
534 }
535
536 // +------------------------------------------- controlled sequence
537 // | +---------------------------- compare() off argument
538 // | | +------------------------ compare() n1 argument
539 // | | | +--------------------- sequence to be inserted
540 // | | | | +------ compare() off2 argument
541 // | | | | | +-- compare() n2 argument
542 // | | | | | | +-- expected result
543 // | | | | | | | +- exception info
544 // | | | | | | | | 0-no exception
545 // | | | | | | | | 1-out_of_range
546 // | | | | | | | | 2-out_of_range
547 // | | | | | | | | 3-length_error
548 // | | | | | | | |
549 // | | | | | | | |
550 // V V V V V V V V
551 TEST ("ab", 0, 2, "ab", 0, 2, 0, 0),
552
553 TEST ("", 0, 0, "", 0, 0, 0, 0),
554 TEST ("", 0, 0, "\0", 0, 1, NPOS, 0),
555 TEST ("", 0, 0, "a", 0, 0, 0, 0),
556 TEST ("", 0, 0, "a", 0, 1, NPOS, 0),
557
558 TEST ("\0", 0, 1, "", 0, 0, 1, 0),
559 TEST ("\0", 0, 1, "\0", 0, 1, 0, 0),
560 TEST ("\0", 0, 1, "\0", 0, 0, 1, 0),
561 TEST ("\0", 0, 1, "\0\0", 0, 2, NPOS, 0),
562 TEST ("\0", 0, 1, "\0\0", 1, 1, 0, 0),
563 TEST ("\0", 0, 1, "\0\0", 2, 2, 1, 0),
564 TEST ("\0\0", 1, 1, "\0", 0, 1, 0, 0),
565 TEST ("\0", 0, 1, "a", 0, 1, NPOS, 0),
566
567 TEST ("abcdefghij", 0, 10, "abcdefghij", 0, 10, 0, 0),
568 TEST ("abcdefghij", 0, 10, "abcdefghij", 1, 10, NPOS, 0),
569 TEST ("abcdefghij", 0, 10, "abcdefghi", 0, 9, 1, 0),
570 TEST ("abcdefghij", 5, 4, "abcdefghi", 5, 9, 0, 0),
571 TEST ("abcdefghij", 1, 9, "abcdefghi", 0, 9, 1, 0),
572 TEST ("abcdefghij", 1, 9, "abcdefghi", 1, 8, 1, 0),
573 TEST ("abcdefghij", 1, 8, "abcdefghi", 0, 9, 1, 0),
574 TEST ("abcdefghij", 1, 8, "abcdefghi", 2, 9, NPOS, 0),
575 TEST ("abcdefghij", 2, 5, "abcdefghi", 0, 9, 1, 0),
576 TEST ("abcdefghij", 2, 5, "abcdefghi", 2, 5, 0, 0),
577 TEST ("abcdefghi", 0, 9, "abcdefghij", 0, 10, NPOS, 0),
578 TEST ("abcdefghi", 0, 9, "abcdefghij", 3, 10, NPOS, 0),
579 TEST ("abcdefghi", 0, 4, "abcdefghij", 0, 4, 0, 0),
580 TEST ("abcdefghi", 0, 4, "abcdefghij", 9, 1, NPOS, 0),
581 TEST ("abcdefghi", 0, 5, "abcdefghij", 0, 4, 1, 0),
582
583 TEST ("abbdefghij", 0, 10, "abcdefghij", 0, 10, NPOS, 0),
584 TEST ("abbdefghij", 0, 10, "abcdefghij", 1, 2, NPOS, 0),
585 TEST ("abbdefghij", 1, 9, "abcdefghij", 0, 10, 1, 0),
586 TEST ("abbdefghij", 1, 9, "abcdefghij", 5, 10, NPOS, 0),
587 TEST ("abcdeeghij", 0, 10, "abcdefghij", 0, 10, NPOS, 0),
588 TEST ("abcdeeghij", 0, 10, "abcdefghij", 1, 5, NPOS, 0),
589 TEST ("abcdeeghij", 5, 10, "abcdefghij", 0, 10, 1, 0),
590 TEST ("abcdeeghij", 0, 3, "abcdefghij", 0, 3, 0, 0),
591 TEST ("abcdeeghij", 0, 5, "abcdefghij", 0, 10, NPOS, 0),
592 TEST ("abcdeeghij", 0, 5, "abcdeabcde", 5, 10, 0, 0),
593 TEST ("abcdefghii", 0, 10, "abcdefghij", 0, 10, NPOS, 0),
594 TEST ("abcdefghii", 0, 10, "abcdefghij", 8, 2, NPOS, 0),
595 TEST ("abcdefghii", 0, 9, "abcdefghij", 0, 9, 0, 0),
596 TEST ("abcdefghii", 9, 10, "abcdefghij", 0, 10, 1, 0),
597 TEST ("abcdefghii", 9, 10, "abcdefghij", 9, 10, NPOS, 0),
598
599 TEST ("abcdefghij", 1, 1, "b", 0, 1, 0, 0),
600 TEST ("abcdefghij", 1, 1, "g", 0, 1, NPOS, 0),
601 TEST ("abcdefghij", 1, 1, "g", 0, 0, 1, 0),
602 TEST ("abcdefghij", 1, 1, "bfg", 0, 1, 0, 0),
603 TEST ("abcdefghij", 1, 1, "bfg", 2, 1, NPOS, 0),
604 TEST ("abcdefghij", 0, 1, "b", 0, 1, NPOS, 0),
605 TEST ("abcdefghij", 1, 4, "bcde", 0, 4, 0, 0),
606 TEST ("abcdefghij", 1, 4, "bcde", 2, 4, NPOS, 0),
607 TEST ("abcdefghij", 1, 3, "bcde", 0, 4, NPOS, 0),
608 TEST ("abcdefghij", 1, 3, "aebcd", 2, 3, 0, 0),
609 TEST ("abcdefghij", 1, 3, "bcde", 0, 2, 1, 0),
610 TEST ("abcdefghij", 1, 3, "bcde", 1, 2, NPOS, 0),
611
612 TEST ("bbcdefghij", 2, 4, "a", 0, 1, 1, 0),
613 TEST ("bbcdefghij", 2, 0, "a", 0, 1, NPOS, 0),
614 TEST ("bbcaefghij", 3, 1, "age", 0, 1, 0, 0),
615 TEST ("aeeeeeghij", 6, 1, "aeeee", 0, 2, 1, 0),
616 TEST ("aeeeeeghij", 0, 2, "aeeee", 0, 3, NPOS, 0),
617 TEST ("aeeeeeghij", 2, 3, "aeeee", 0, 5, 1, 0),
618 TEST ("aeeeeeghij", 2, 3, "eeeea", 1, 3, 0, 0),
619 TEST ("a", 0, 2, "bbcdefghij", 0, 10, NPOS, 0),
620 TEST ("jeeee", 1, 2, "eeeeeeghij", 0, 10, NPOS, 0),
621 TEST ("jeeee", 1, 2, "eeeeeeghij", 1, 1, 1, 0),
622 TEST ("jeeee", 1, 4, "eeeeeeghij", 0, 4, 0, 0),
623
624 TEST ("a\0b\0\0c", 0, 6, "a\0b\0\0c", 0, 6, 0, 0),
625 TEST ("a\0b\0\0c", 1, 1, "a\0b\0\0c", 3, 1, 0, 0),
626 TEST ("a\0b\0\0c", 2, 4, "a\0b\0\0c", 0, 6, 1, 0),
627 TEST ("a\0b\0\0c", 2, 4, "ab\0\0c\0", 1, 4, 0, 0),
628 TEST ("e\0b\0\0c", 0, 1, "a\0b\0\0c", 0, 6, 1, 0),
629 TEST ("e\0b\0\0c", 3, 2, "\0\0a\0bc", 0, 2, 0, 0),
630 TEST ("e\0b\0\0c", 3, 3, "\0\0c\0ba", 0, 4, NPOS, 0),
631 TEST ("e\0b\0\0c", 3, 3, "a\0\0c\0b", 1, 3, 0, 0),
632 TEST ("abc\0\0\0", 0, 4, "abc\0\0\0", 0, 5, NPOS, 0),
633 TEST ("abc\0\0\0", 3, 4, "a\0\0\0bc", 1, 3, 0, 0),
634 TEST ("ab\0c\0\0", 2, 2, "ab\0b\0\0", 0, 4, NPOS, 0),
635 TEST ("ab\0c\0\0", 2, 4, "ab\0b\0\0", 2, 4, 1, 0),
636 TEST ("\0af\0\0c", 0, 6, "e\0ab\0\0c", 0, 6, NPOS, 0),
637 TEST ("\0af\0\0c", 2, 6, "e\0ab\0\0c", 2, 6, 1, 0),
638 TEST ("\0af\0\0c", 0, 1, "\0ab\0\0ce", 0, 2, NPOS, 0),
639 TEST ("\0af\0\0c", 3, 1, "\0ab\0\0ce", 0, 1, 0, 0),
640 TEST ("\0\0c\0af", 3, 3, "\0ab\0\0ce", 0, 3, 1, 0),
641 TEST ("\0\0c\0af", 1, 2, "\0ab\0\0ec", 4, 3, NPOS, 0),
642 TEST ("\0af\0\0c", 2, 2, "e\0ab\0\0c", 0, 2, 1, 0),
643 TEST ("\0ae\0\0c", 0, 3, "e\0\0b\0ca", 4, 4, NPOS, 0),
644
645 TEST ("a\0b", 0, 3, "a\0b\0\0c", 0, 4, NPOS, 0),
646 TEST ("a\0b", 1, 3, "a\0b\0\0c", 4, 4, NPOS, 0),
647 TEST ("a\0b", 0, 1, "a\0b\0\0c", 0, 2, NPOS, 0),
648 TEST ("ab\0", 0, 5, "abc\0\0\0", 0, 3, NPOS, 0),
649 TEST ("ab\0", 2, 1, "abc\0\0\0", 3, 3, NPOS, 0),
650 TEST ("\0eb", 0, 5, "e\0ab\0\0c", 0, 1, NPOS, 0),
651 TEST ("a\0b\0\0c", 0, 3, "a\0b", 0, 3, 0, 0),
652 TEST ("a\0b\0\0c", 3, 2, "a\0b", 1, 2, NPOS, 0),
653 TEST ("a\0b\0\0c", 0, 1, "a", 0, 1, 0, 0),
654 TEST ("abc\0\0\0", 0, 5, "abc\0\0", 0, 5, 0, 0),
655 TEST ("e\0ab\0\0c", 0, 7, "e\0ab\0\0c", 0, 7, 0, 0),
656 TEST ("e\0ab\0\0c", 0, 7, "e\0ab\0\0c", 2, 7, 1, 0),
657 TEST ("a\0b\0\0c", 2, 3, "a\0b", 0, 2, 1, 0),
658 TEST ("a\0b\0\0c", 2, 1, "a", 0, 0, 1, 0),
659 TEST ("abc\0\0\0", 2, 5, "c\0\0ab", 0, 3, 1, 0),
660 TEST ("e\0ag\0\0c", 3, 6, "e\0ab\0\0c", 0, 7, 1, 0),
661 TEST ("abc\0\0\0", 2, 5, "c\0\0ab", 0, 4, NPOS, 0),
662
663 TEST ("x@4096", 0, 4096, "x@4096", 0, 4096, 0, 0),
664 TEST ("x@4096", 1, 4096, "x@4096", 0, 4096, NPOS, 0),
665 TEST ("x@4096", 1, 4096, "x@4096", 0, 4092, 1, 0),
666 TEST ("x@4096", 1, 4090, "x@4096", 2, 4090, 0, 0),
667 TEST ("x@4096", 1, 4102, "x@4096", 2, 4102, 1, 0),
668 TEST ("xx", 0, 2, "x@4096", 0, 4092, NPOS, 0),
669 TEST ("xx", 0, 2, "x@4096", 4090, 2, 0, 0),
670 TEST ("x@4096", 0, 4096, "xxxxx", 0, 5, 1, 0),
671 TEST ("x@4096", 4090, 5, "xxxxx", 0, 5, 0, 0),
672 TEST ("x@4096", 2, 5, "xxxxx", 0, 5, 0, 0),
673 TEST ("x@4096", 5, 2, "xxxxx", 0, 3, NPOS, 0),
674 TEST ("a\0x", 0, 3, "x@4096", 0, 10, NPOS, 0),
675 TEST ("a\0x", 2, 1, "x@4096", 4095, 1, 0, 0),
676 TEST ("x@4096", 0, 10, "x\0b", 0, 2, 1, 0),
677 TEST ("x@4096", 0, 1, "x\0b", 0, 2, NPOS, 0),
678
679 TEST ("a@4096\0", 4096, 1, "b\0", 1, 1, 0, 0),
680
681 TEST ("\0", 2, 0, "", 0, 0, 1, 1),
682 TEST ("a", 10, 0, "", 0, 0, 1, 1),
683 TEST ("x@4096", 4106, 0, "", 0, 0, 1, 1),
684
685 TEST ("", 0, 0, "\0", 2, 0, 1, 2),
686 TEST ("", 0, 0, "a", 10, 0, 1, 2),
687 TEST ("", 0, 0, "x@4096", 4106, 0, 1, 2),
688
689 TEST ("last", 0, 4, "last", 0, 4, 0, 0)
690};
691
692
694template <class charT, class Traits, class Allocator>
696 const StringFunc &func,
697 const StringTestCase &tcase)
698{
699 typedef std::basic_string <charT, Traits, Allocator> String;
700 typedef typename UserTraits<charT>::MemFun UTMemFun;
701
702 static const std::size_t BUFSIZE = 256;
703
704 static charT wstr_buf [BUFSIZE];
705 static charT warg_buf [BUFSIZE];
706
707 std::size_t str_len = sizeof wstr_buf / sizeof *wstr_buf;
708 std::size_t arg_len = sizeof warg_buf / sizeof *warg_buf;
709
710 charT* wstr = rw_expand (wstr_buf, tcase.str, tcase.str_len, &str_len);
711 charT* warg = rw_expand (warg_buf, tcase.arg, tcase.arg_len, &arg_len);
712
713 // construct the string object to be modified
714 // and the (possibly unused) argument string
715 const String str (wstr, str_len);
716 const String arg (warg, arg_len);
717
718 if (wstr != wstr_buf)
719 delete[] wstr;
720
721 if (warg != warg_buf)
722 delete[] warg;
723
724 wstr = 0;
725 warg = 0;
726
727 // save the state of the string object before the call
728 // to detect wxception safety violations (changes to
729 // the state of the object after an exception)
730 const StringState str_state (rw_get_string_state (str));
731
732 int res = 0;
733
734 // string function argument
735 const charT* const arg_ptr = tcase.arg ? arg.c_str () : str.c_str ();
736 const String& arg_str = tcase.arg ? arg : str;
737
738 std::size_t total_compare_calls = 0;
739 std::size_t n_compare_calls = 0;
740 std::size_t* const rg_calls =
742
743 if (rg_calls)
744 total_compare_calls = rg_calls[UTMemFun::compare];
745
746 // (name of) expected and caught exception
747 const char* expected = 0;
748 const char* caught = 0;
749
750#ifndef _RWSTD_NO_EXCEPTIONS
751
752 if (1 == tcase.bthrow)
753 expected = exceptions [1];
754 else if ( 2 == tcase.bthrow
755 && Compare (size_size_cstr_size_size) == func.which_)
756 expected = exceptions [1];
757 else if (3 == tcase.bthrow)
758 expected = exceptions [2];
759
760#else // if defined (_RWSTD_NO_EXCEPTIONS)
761
762 if (tcase.bthrow)
763 return;
764
765#endif // _RWSTD_NO_EXCEPTIONS
766
767 try {
768 switch (func.which_) {
769 case Compare (cptr):
770 res = str.compare (arg_ptr);
771 break;
772
773 case Compare (cstr):
774 res = str.compare (arg_str);
775 if (rg_calls)
776 n_compare_calls = rg_calls[UTMemFun::compare];
777 break;
778
779 case Compare (size_size_cptr):
780 res = str.compare (tcase.off, tcase.size, arg_ptr);
781 break;
782
783 case Compare (size_size_cstr):
784 res = str.compare (tcase.off, tcase.size, arg_str);
785 break;
786
787 case Compare (size_size_cptr_size):
788 res = str.compare (tcase.off, tcase.size,
789 arg_ptr, tcase.size2);
790 break;
791
792 case Compare (size_size_cstr_size_size):
793 res = str.compare (tcase.off, tcase.size,
794 arg_str, tcase.off2, tcase.size2);
795 break;
796
797 default:
798 RW_ASSERT ("test logic error: unknown compare overload");
799 return;
800 }
801
802 // verify the returned value
803 const bool success = (res < 0 && tcase.nres == NPOS)
804 || (res > 0 && tcase.nres > 0)
805 || (res == 0 && tcase.nres == 0);
806
807 rw_assert (success, 0, tcase.line,
808 "line %d. %{$FUNCALL} == %d, got %d",
809 __LINE__, tcase.nres == NPOS ? -1 : int (tcase.nres), res);
810
811 // verify that Traits::length was used
812 if (Compare (cstr) == func.which_ && rg_calls) {
813 rw_assert (n_compare_calls - total_compare_calls > 0,
814 0, tcase.line, "line %d. %{$FUNCALL} doesn't "
815 "use traits::compare()", __LINE__);
816 }
817 }
818
819#ifndef _RWSTD_NO_EXCEPTIONS
820
821 catch (const std::out_of_range &ex) {
822 caught = exceptions [1];
823 rw_assert (caught == expected, 0, tcase.line,
824 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
825 "unexpectedly%{;} caught std::%s(%#s)",
826 __LINE__, 0 != expected, expected, caught, ex.what ());
827 }
828 catch (const std::length_error &ex) {
829 caught = exceptions [2];
830 rw_assert (caught == expected, 0, tcase.line,
831 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
832 "unexpectedly%{;} caught std::%s(%#s)",
833 __LINE__, 0 != expected, expected, caught, ex.what ());
834 }
835 catch (const std::exception &ex) {
836 caught = exceptions [4];
837 rw_assert (0, 0, tcase.line,
838 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
839 "unexpectedly%{;} caught std::%s(%#s)",
840 __LINE__, 0 != expected, expected, caught, ex.what ());
841 }
842 catch (...) {
843 caught = exceptions [0];
844 rw_assert (0, 0, tcase.line,
845 "line %d. %{$FUNCALL} %{?}expected %s,%{:}"
846 "unexpectedly%{;} caught %s",
847 __LINE__, 0 != expected, expected, caught);
848 }
849
850 if (caught) {
851 // verify that an exception thrown during allocation
852 // didn't cause a change in the state of the object
854 __LINE__, tcase.line, caught);
855 }
856
857#endif // _RWSTD_NO_EXCEPTIONS
858
859}
860
861
864
865int main (int argc, char** argv)
866{
867 static const StringTest
868 tests [] = {
869
870#undef TEST
871#define TEST(sig) { \
872 Compare (sig), sig ## _test_cases, \
873 sizeof sig ## _test_cases / sizeof *sig ## _test_cases \
874 }
875
876 TEST (cptr),
877 TEST (cstr),
878 TEST (size_size_cptr),
879 TEST (size_size_cstr),
880 TEST (size_size_cptr_size),
881 TEST (size_size_cstr_size_size)
882 };
883
884 const std::size_t test_count = sizeof tests / sizeof *tests;
885
886 return rw_run_string_test (argc, argv, __FILE__,
887 "lib.string.compare",
888 test_compare, tests, test_count);
889}
#define res
Definition 17.names.cpp:531
#define charT
Definition 17.names.cpp:456
#define TEST(str, arg, res, bthrow)
#define Compare(sig)
void test_compare(charT, Traits *, Allocator *, const StringFunc &func, const StringTestCase &tcase)
#define size_size_cptr_test_cases
#define size_size_cstr_test_cases
#define size_size_cptr_size_test_cases
#define size_size_cstr_size_size_test_cases
std::char_traits< S > Traits
#define NPOS
#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)
_RWSTD_SIZE_T * rw_get_call_counters(std::char_traits< charT > *, charT *)
Definition rw_char.h:495
_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 arg_len
Definition rw_strings.h:600
_RWSTD_SIZE_T str_len
Definition rw_strings.h:597
const char * str
Definition rw_strings.h:596
const char * arg
Definition rw_strings.h:599
_RWSTD_SIZE_T nres
Definition rw_strings.h:603
int arg
Definition locale.cpp:2806