Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
25.replace.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for replace(), replace_copy()
30#include <cstring> // for size_t, strlen()
31
32#include <rw_alg_test.h>
33#include <rw_value.h> // for UserClass
34#include <rw_driver.h> // for rw_test()
35
36
39
40// disable explicit instantiation for compilers (like MSVC)
41// that can't handle it
42#ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION
43
44template
45void replace (FwdIter<eq_comp<assign<base<> > > >,
47 const eq_comp<assign<base<> > >&,
48 const eq_comp<assign<base<> > >&);
49
50template
51void replace_if (FwdIter<assign<base<> > > ,
54 const assign<base<> > &);
55
56template
58replace_copy (InputIter<eq_comp<assign<base<> > > >,
61 const eq_comp<assign<base<> > >&,
62 const eq_comp<assign<base<> > >&);
63
64template
66replace_copy_if (InputIter<assign<base<> > > ,
70 const assign<base<> > &);
71
72#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION
73
74} // namespace std
75
76
78// names of tested functions
79const char* const fnames[] = { "replace", "replace_copy" };
80const char* const fnames_if[] = { "replace_if", "replace_copy_if" };
81
82// tags to select replace or replace_copy at compile time
83struct ReplaceTag {
84 enum { fname_inx = 0 };
86};
87
89 enum { fname_inx = 1 };
91};
92
93// used as a dummy template argument to test functions exercising replace()
94// and replace_if() (to differentiate from replace_copy() and replace_copy_if())
95struct NoIterator { };
96const char* type_name (NoIterator, const UserClass*)
97{
98 return 0;
99}
100
101
103// predicate used as an argument to replace_if() and replace_copy_if()
104template <class T>
106{
107 static std::size_t funcalls_;
108
109 EqualityPredicate (const T &val, int /* dummy */)
110 : val_ (val) {
111 funcalls_ = 0;
112 }
113
114 class ConvertibleToBool {
115 bool result_;
116 public:
117 ConvertibleToBool (bool res): result_ (res) { /* empty */ }
118 operator bool() const { return result_; }
119 };
120
121 // return a type other than bool but one that is implicitly
122 // convertible to bool to detect incorrect assumptions
124 ++funcalls_;
125 return ConvertibleToBool (arg == val_);
126 }
127
128private:
129 void operator= (EqualityPredicate&);
130 const T &val_;
131};
132
133template <class T>
135
136
138// exercises std::replace() and std::replace_if()
139template <class Iterator, class T, class Predicate>
141 const char *src, // source sequence
142 char val, // value to replace
143 char new_val, // value to replace it with
144 Iterator dummy, // not used
146 const T*,
147 const Predicate*,
148 ReplaceTag tag) // replace() or replace_if()
149{
150 static const char* const itname = type_name (Iterator (), (T*)0);
151 static const char* const fname = tag.use_predicate ?
152 fnames_if [tag.fname_inx] : fnames [tag.fname_inx];
153
154 // compute the length of the source sequence
155 const std::size_t nsrc = std::strlen (src);
156
157 // construct a sequence of `nsrc' elements to pass to replace
158 // (make sure there's always at least 1 element, even if the
159 // sequence is otherwise empty, to avoid undefined behavior
160 // when de-referencing xsrc[0] in inactive diagnostic messages)
161 T* const xsrc = T::from_char (src, nsrc + 1);
162 T* const xsrc_end = xsrc + nsrc;
163
164 // construct an element to replace
165 T to_replace;
166 to_replace.data_.val_ = val;
167
168 // construct an element to replace it with
169 T replace_with;
170 replace_with.data_.val_ = new_val;
171
172 // construct a predicate object (used with replace_if() only)
173 const Predicate pred (to_replace, 0);
174
175 // construct iterators pointing to the beginning and end
176 // of the source sequence
177 const Iterator first = make_iter (xsrc, xsrc, xsrc_end, dummy);
178 const Iterator last = make_iter (xsrc_end, xsrc_end, xsrc_end, dummy);
179
180 // zero out predicate counters
181 T::n_total_op_eq_ = 0;
182
183 // call replace() or replace_if()
184 if (tag.use_predicate)
185 std::replace_if (first, last, pred, replace_with);
186 else
187 std::replace (first, last, to_replace, replace_with);
188
189 // verify that the value to be replaced has been replaced
190 // with the replacement value
191 bool success = true;
192 std::size_t i = 0;
193 for ( ; i != nsrc; ++i) {
194 success = to_replace.data_.val_ != xsrc [i].data_.val_;
195 if (!success)
196 break;
197 }
198
199 static const int cwidth = sizeof (T);
200
201 rw_assert (success, 0, line,
202 "line %d: %s<%s>(\"%s\", ..., %#c, %#c) ==> "
203 "\"%{X=*.*}\"; unexpected element value %#c",
204 __LINE__, fname, itname, src, val, new_val,
205 cwidth, int (nsrc), int (i), xsrc, src [i]);
206
207 // check the id (not just the value) of the matching element
208 // to make sure it has really been replaced
209 for (i = 0; i != nsrc; ++i) {
210
211 success =
212 val != src [i]
213 || (val == src [i] && replace_with.origin_ == xsrc [i].origin_);
214
215 if (!success)
216 break;
217 }
218
219 rw_assert (success, 0, line,
220 "line %d: %s<%s>(\"%s\", ..., %#c, %#c) ==> "
221 "\"%{X=#*.*}\"; failed to replace element %zu: "
222 "origin %d (%d, %d), expected %d (%d)",
223 __LINE__, fname, itname, src, val, new_val,
224 cwidth, int (nsrc), int (i), xsrc,
225 i, xsrc [i].origin_, xsrc [i].id_, xsrc [i].src_id_,
226 replace_with.origin_, replace_with.id_);
227
228 // verify the number of applications of the predicate: p 25.2.4.3
229 if (tag.use_predicate) {
230 rw_assert (pred.funcalls_ == nsrc, 0, line,
231 "line %d: %s<%s>(\"%s\", ..., %#c) called "
232 "Predicate::operator() %zu times, %zu expected",
233 __LINE__, fname, itname, src, val,
234 pred.funcalls_, nsrc);
235 }
236 else {
237 rw_assert (T::n_total_op_eq_ == nsrc, 0, line,
238 "line %d: %s<%s>(\"%s\", ..., %#c) called "
239 "T::operator< %zu times, %zu expected",
240 __LINE__, fname, itname, src, val,
241 T::n_total_op_eq_, nsrc);
242 }
243
244 delete[] xsrc;
245}
246
247
249// exercises std::replace_copy()
250template <class Iterator, class OutputIterator, class T, class Predicate>
252 const char *src, // source sequence
253 char val, // value to replace
254 char new_val, // value to replace it with
255 Iterator it,
256 OutputIterator dummy,
257 const T*,
258 const Predicate*,
259 ReplaceCopyTag tag) // replace_copy or replace_copy_if
260{
261 static const char* const itname = type_name (it, (T*)0);
262 static const char* const fname = tag.use_predicate ?
263 fnames_if [tag.fname_inx] : fnames [tag.fname_inx];
264
265 const std::size_t nsrc = std::strlen (src);
266
267 // construct a sequence of `nsrc' elements to pass to replace
268 // (make sure there's always at least 1 element, even if the
269 // sequence is otherwise empty, to avoid undefined behavior
270 // when de-referencing xsrc[0] in inactive diagnostic messages)
271 T* const xsrc = T::from_char (src, nsrc + 1);
272 T* const xdst = T::from_char (src, nsrc + 1);
273
274 T* const xsrc_end = xsrc + nsrc;
275 T* const xdst_end = xdst + nsrc;
276
277 T to_replace;
278 to_replace.data_.val_ = val;
279
280 T replace_with;
281 replace_with.data_.val_ = new_val;
282
283 const Predicate pred (to_replace, 0);
284
285 const Iterator first = make_iter (xsrc, xsrc, xsrc_end, it);
286 const Iterator last = make_iter (xsrc_end, xsrc_end, xsrc_end, it);
287
288 const OutputIterator result = make_iter (xdst, xdst, xdst_end, dummy);
289
290 // zero out predicate counters
291 T::n_total_op_eq_ = 0;
292
293 const OutputIterator end = tag.use_predicate
294 ? std::replace_copy_if (first, last, result, pred, replace_with)
295 : std::replace_copy (first, last, result, to_replace, replace_with);
296
297 // verify that the returned iterator is set as expected
298 rw_assert (end.cur_ == result.cur_ + nsrc, 0, line,
299 "line %d: %s<%s>(\"%s\", ..., %#c) == result + %zu, got %td",
300 __LINE__, fname, itname, src, val, nsrc, end.cur_ - xdst);
301
302 // verify that the value to be replaced does not appear anywhere
303 // in the range [result, end)
304 bool success = true;
305 std::size_t i = 0;
306 for ( ; i != nsrc; ++i) {
307 typedef unsigned char UChar;
308 success = UChar (val) != xdst [i].data_.val_;
309 if (!success)
310 break;
311 }
312
313 static const int cwidth = sizeof (T);
314
315 rw_assert (success, 0, line,
316 "line %d: %s<%s>(\"%s\", ..., %#c) ==> "
317 "\"%{X=*.*}\"; unexpected element value %#c",
318 __LINE__, fname, itname, src, val,
319 cwidth, int (nsrc), int (i), xdst, src [i]);
320
321 // check the id (not just the value) of the matching element
322 // to make sure it has really been copied
323 for (i = 0; i != nsrc; ++i) {
324 success =
325 val != src [i]
326 || (val == src [i] && replace_with.origin_ == xdst [i].origin_);
327
328 if (!success)
329 break;
330 }
331
332 rw_assert (success, 0, line,
333 "line %d: %s<%s>(\"%s\", ..., %#c, %#c) ==> "
334 "\"%{X=*.*}\"; failed to copy and replace element %zu: "
335 "origin %d (%d, %d), expected %d (%d)",
336 __LINE__, fname, itname, src, val, new_val,
337 cwidth, int (nsrc), int (i), xdst,
338 i, xdst [i].origin_, xdst [i].id_, xdst [i].src_id_,
339 replace_with.origin_, replace_with.id_);
340
341 // verify the number of applications of the predicate: p 25.2.4.7
342 if (tag.use_predicate) {
343 rw_assert (pred.funcalls_ == nsrc, 0, line,
344 "line %d: %s<%s>(\"%s\", ..., %#c) called "
345 "Predicate::operator() %zu times, %zu expected",
346 __LINE__, fname, itname, src, val,
347 pred.funcalls_, nsrc);
348 }
349 else {
350 rw_assert (T::n_total_op_eq_ == nsrc, 0, line,
351 "line %d: %s<%s>(\"%s\", ..., %#c) called "
352 "T::operator< %zu times, %zu expected",
353 __LINE__, fname, itname, src, val,
354 T::n_total_op_eq_, nsrc);
355 }
356
357 delete[] xsrc;
358 delete[] xdst;
359}
360
361
363// exercises all four function templates
364template <class Iterator1, class Iterator2, class T,
365 class Predicate, class Tag>
366void test_replace (Iterator1 it1, Iterator2 it2, const T*,
367 const Predicate* pred, Tag tag)
368{
369 static const char* const it1name = type_name (it1, (T*)0);
370 static const char* const it2name = type_name (it2, (T*)0);
371
372 if (tag.fname_inx) { // replace_copy(), replace_copy_if()
373 rw_info (0, 0, 0,
374 "std::%s (%s, %s, %s, %s, const T&)",
375 tag.use_predicate ? "replace_copy_if" : "replace_copy",
376 it1name, it1name, it2name,
377 tag.use_predicate ? "UnaryPredicate" : "const T&");
378 }
379 else { // replace, replace_if()
380 rw_info (0, 0, 0,
381 "std::%s (%s, %s, %s, const T&)",
382 tag.use_predicate ? "replace_if" : "replace",
383 it1name, it1name,
384 tag.use_predicate ? "UnaryPredicate" : "const T&");
385 }
386
387
388#define TEST(src, val, new_val) \
389 test_replace (__LINE__, src, val, new_val, it1, it2, (T*)0, pred, tag)
390
391 // +----------------------- original sequence
392 // | +------- value to replace
393 // | | +- value to replace it with
394 // | | |
395 // v v v
396 TEST ("", 'a', 'b');
397 TEST ("a", 'a', 'b');
398 TEST ("aa", 'a', 'b');
399 TEST ("aaa", 'a', 'b');
400 TEST ("aaaa", 'a', 'b');
401 TEST ("aaaaa", 'a', 'b');
402 TEST ("aaaaaa", 'a', 'b');
403 TEST ("aaaaaaa", 'a', 'b');
404 TEST ("aaaaaaaa", 'a', 'b');
405 TEST ("aaaaaaaaa", 'a', 'b');
406 TEST ("aaaaaaaaaa", 'a', 'b');
407
408 TEST ("b", 'a', 'b');
409 TEST ("bb", 'a', 'b');
410 TEST ("ba", 'a', 'b');
411 TEST ("ab", 'a', 'b');
412 TEST ("abc", 'a', 'b');
413 TEST ("bac", 'a', 'b');
414 TEST ("bca", 'a', 'b');
415 TEST ("aba", 'a', 'b');
416 TEST ("abab", 'a', 'b');
417 TEST ("ababa", 'a', 'b');
418 TEST ("bababa", 'a', 'b');
419 TEST ("bababab", 'a', 'b');
420 TEST ("babababa", 'a', 'b');
421 TEST ("bbbbbbbbb", 'a', 'b');
422}
423
424
426/* extern */ int rw_opt_no_replace; // --no-replace
427/* extern */ int rw_opt_no_replace_if; // --no-replace_if
428/* extern */ int rw_opt_no_replace_copy; // --no-replace_copy
429/* extern */ int rw_opt_no_replace_copy_if; // --no-replace_copy_if
430/* extern */ int rw_opt_no_input_iter; // --no-InputIterator
431/* extern */ int rw_opt_no_output_iter; // --no-OutputIterator
432/* extern */ int rw_opt_no_fwd_iter; // --no-ForwardIterator
433/* extern */ int rw_opt_no_bidir_iter; // --no-BidirectionalIterator
434/* extern */ int rw_opt_no_rnd_iter; // --no-RandomAccessIterator
435
436template <class T, class Predicate, class Tag>
437void test_replace (const T* , const Predicate* pred, Tag tag)
438{
439 rw_info (0, 0, 0,
440 "template <class ForwardIterator, class T> "
441 "std::%s (ForwardIterator, ForwardIterator, "
442 "%s, const T&)",
443 tag.use_predicate ? "replace_if" : "replace",
444 tag.use_predicate ? "Predicate" : "const T&");
445
446 if (rw_opt_no_fwd_iter) {
447 rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled");
448 }
449 else {
450 test_replace (FwdIter<T>(), NoIterator (), (T*)0, pred, tag);
451 }
452
454 rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled");
455 }
456 else {
457 test_replace (BidirIter<T>(), NoIterator (), (T*)0, pred, tag);
458 }
459
460 if (rw_opt_no_rnd_iter) {
461 rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled");
462 }
463 else {
464 test_replace (RandomAccessIter<T>(), NoIterator (), (T*)0, pred, tag);
465 }
466}
467
468
470template <class T>
471void test_replace (const T* )
472{
473 const ReplaceTag replace_tag = { false };
474 const ReplaceTag replace_if_tag = { true };
475
476 if (rw_opt_no_replace) {
477 rw_note (0, __FILE__, __LINE__, "std::replace test disabled");
478 }
479 else {
480 const EqualityPredicate<T>* const pred = (EqualityPredicate<T>*) 0;
481 test_replace((T*)0, pred, replace_tag);
482 }
483
485 rw_note (0, __FILE__, __LINE__, "std::replace_if test disabled");
486 }
487 else {
488 const EqualityPredicate<T>* const pred = (EqualityPredicate<T>*) 1;
489 test_replace((T*)0, pred, replace_if_tag);
490 }
491}
492
493
495template <class InputIterator, class T, class Predicate, class Tag>
496void test_replace_copy (const InputIterator& iter, const T*,
497 const Predicate* pred, Tag tag)
498{
500 rw_note (0, __FILE__, __LINE__, "OutputIterator test disabled");
501 }
502 else {
503 test_replace (iter, OutputIter<T>(0, 0, 0), (T*)0, pred, tag);
504 }
505
506 if (rw_opt_no_fwd_iter) {
507 rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled");
508 }
509 else {
510 test_replace (iter, FwdIter<T>(), (T*)0, pred, tag);
511 }
512
514 rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled");
515 }
516 else {
517 test_replace (iter, BidirIter<T>(), (T*)0, pred, tag);
518 }
519
520 if (rw_opt_no_rnd_iter) {
521 rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled");
522 }
523 else {
524 test_replace (iter, RandomAccessIter<T>(), (T*)0, pred, tag);
525 }
526}
527
528
530template <class T, class Predicate, class Tag>
531void test_replace_copy (const T*, const Predicate* pred, Tag tag)
532{
533 rw_info (0, 0, 0,
534 "template "
535 "<class InputIterator, class OutputIterator, class T> "
536 "std::%s (InputIterator, InputIterator, "
537 "OutputIterator, %s, const T&)",
538 tag.use_predicate ? "replace_copy_if" : "replace_copy",
539 tag.use_predicate ? "Predicate" : "const T&");
540
542 rw_note (0, __FILE__, __LINE__, "InputIterator test disabled");
543 }
544 else {
545 test_replace_copy (InputIter<T>(0, 0, 0), (T*)0, pred, tag);
546 }
547
548 if (rw_opt_no_fwd_iter) {
549 rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled");
550 }
551 else {
552 test_replace_copy (FwdIter<T>(), (T*)0, pred, tag);
553 }
554
556 rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled");
557 }
558 else {
559 test_replace_copy (BidirIter<T>(), (T*)0, pred, tag);
560 }
561
562 if (rw_opt_no_rnd_iter) {
563 rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled");
564 }
565 else {
567 }
568}
569
570
572template <class T>
573void test_replace_copy (const T* )
574{
575 const ReplaceCopyTag replace_copy_tag = { false };
576 const ReplaceCopyTag replace_copy_if_tag = { true };
577
579 rw_note (0, __FILE__, __LINE__, "std::replace_copy test disabled");
580 }
581 else {
582 const EqualityPredicate<T>* const pred = (EqualityPredicate<T>*) 0;
583 test_replace_copy ((T*)0, pred, replace_copy_tag);
584 }
585
587 rw_note (0, __FILE__, __LINE__, "std::replace_copy_if test disabled");
588 }
589 else {
590 const EqualityPredicate<T>* const pred = (EqualityPredicate<T>*)1;
591 test_replace_copy ((T*)0, pred, replace_copy_if_tag);
592 }
593}
594
595
597static int
598run_test (int, char*[])
599{
601
603
604 return 0;
605}
606
607
609int main (int argc, char *argv[])
610{
611 return rw_test (argc, argv, __FILE__,
612 "lib.alg.replace",
613 0 /* no comment */,
614 run_test,
615 "|-no-replace# "
616 "|-no-replace_if# "
617 "|-no-replace_copy# "
618 "|-no-replace_copy_if# "
619 "|-no-InputIterator# "
620 "|-no-OutputIterator# "
621 "|-no-ForwardIterator# "
622 "|-no-BidirectionalIterator# "
623 "|-no-RandomAccessIterator#",
633}
#define i
Definition 17.names.cpp:126
#define T
Definition 17.names.cpp:163
#define line
Definition 17.names.cpp:490
#define res
Definition 17.names.cpp:531
#define last
Definition 17.names.cpp:487
#define Predicate
Definition 17.names.cpp:522
#define val
Definition 17.names.cpp:570
#define OutputIterator
Definition 17.names.cpp:515
#define tag
Definition 17.names.cpp:551
volatile int result
const MyBuffer< char > nsrc[]
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
void test_replace(int line, const char *src, char val, char new_val, Iterator dummy, NoIterator, const T *, const Predicate *, ReplaceTag tag)
int rw_opt_no_fwd_iter
const char *const fnames[]
int rw_opt_no_rnd_iter
const char *const fnames_if[]
int rw_opt_no_bidir_iter
int rw_opt_no_replace_copy_if
int rw_opt_no_replace
int rw_opt_no_input_iter
int rw_opt_no_replace_if
void test_replace_copy(const InputIterator &iter, const T *, const Predicate *pred, Tag tag)
int rw_opt_no_output_iter
int rw_opt_no_replace_copy
#define TEST(src, val, new_val)
const char * type_name()
const int end
#define main
#define _RWSTD_NAMESPACE(name)
Definition _defs.h:567
Definition json.h:5363
T * make_iter(T *cur, const T *, const T *, T *)
_TEST_EXPORT int rw_test(int argc, char *argv[], const char *filename, const char *clause, const char *comment, int(*testfun)(int, char **), const char *optspec,...)
_TEST_EXPORT int rw_info(int, const char *, int, const char *,...)
Definition driver.cpp:1681
_TEST_EXPORT int rw_note(int, const char *, int, const char *,...)
Definition driver.cpp:1664
_TEST_EXPORT int rw_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630
unsigned char UChar
Definition collate.cpp:122
#define bool
Definition stdbool.h:24
ConvertibleToBool operator()(const T &x, const U &y)
EqualityPredicate(const T &val, int)
static std::size_t funcalls_
static std::size_t funcalls_
Definition 25.find.cpp:62
int use_predicate
int arg
Definition locale.cpp:2806