Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
20.operators.cpp
Go to the documentation of this file.
1
29// The test exercises the ability to specialize various components of
30// the library (algorithms and containers in particular) on user-defined
31// iterator types in the presence of using directives.
32
33#include <rw/_config.h>
34
35#if defined (__IBMCPP__) && !defined (_RWSTD_NO_IMPLICIT_INCLUSION)
36// Disable implicit inclusion to work around
37// a limitation in IBM's VisualAge 5.0.2.0 (see PR#26959)
38
39# define _RWSTD_NO_IMPLICIT_INCLUSION
40#endif
41
42#if 0 // def _MSC_VER
43// disabled (warnings may be meaningful)
44# pragma warning (disable: 4800)
45# pragma warning (disable: 4805)
46#endif // _MSC_VER
47
48
49#include <algorithm>
50#include <deque>
51#include <functional>
52#include <iterator>
53#include <list>
54#include <map>
55#include <set>
56#include <string>
57#include <vector>
58#include <utility>
59#include <cstddef> // for std::size_t
60
61#include <rw_driver.h>
62
63
65_USING (namespace std);
66_USING (namespace std::rel_ops);
67
68
69#ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION
70
71// explicitly instantiate containers
72template class
73std::deque<int, std::allocator<int> >;
74
75template class
76std::list<int,std::allocator<int> >;
77
78template class
79std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >;
80
81template class
82std::set<int>;
83
84template class
85std::basic_string<int, std::char_traits<int>, std::allocator<int> >;
86
87template class
88std::vector<int, std::allocator<int> >;
89
90#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION
91
92
94#if !defined (__SUNPRO_CC) || __SUNPRO_CC > 0x530
95# define FUN(ignore, result, name, arg_list) do { \
96 result (*pf) arg_list = &name; \
97 _RWSTD_UNUSED (pf); \
98 } while (0)
99#else
100 // working around a SunPro 5.3 bug (see PR #25972) that prevents it
101 // from taking the address of a function template in template code
102# define FUN(T, result, name, arg_list) do { \
103 typedef typename T::iterator Iterator; \
104 typedef typename T::const_iterator ConstIterator; \
105 const Iterator *pi = 0; \
106 const ConstIterator *pci = 0; \
107 name (pi, pi); \
108 name (pci, pci); \
109 } while (0)
110#endif // SunPro 5.3
111
112
113#define TEST_INEQUALITY(T) \
114 FUN (T, bool, std::rel_ops::operator!=, \
115 (const T::iterator&, const T::iterator&)); \
116 FUN (T, bool, std::rel_ops::operator!=, \
117 (const T::const_iterator&, const T::const_iterator&))
118
119#define TEST_OPERATORS(T) \
120 TEST_INEQUALITY (T); \
121 FUN (T, bool, std::rel_ops::operator>, \
122 (const T::iterator&, const T::iterator&)); \
123 FUN (T, bool, std::rel_ops::operator<=, \
124 (const T::iterator&, const T::iterator&)); \
125 FUN (T, bool, std::rel_ops::operator>=, \
126 (const T::iterator&, const T::iterator&)); \
127 FUN (T, bool, std::rel_ops::operator>, \
128 (const T::const_iterator&, const T::const_iterator&)); \
129 FUN (T, bool, std::rel_ops::operator<=, \
130 (const T::const_iterator&, const T::const_iterator&)); \
131 FUN (T, bool, std::rel_ops::operator>=, \
132 (const T::const_iterator&, const T::const_iterator&))
133
134
135template <class Container, class RandomAccessIterator>
137{
138 TEST_OPERATORS (typename Container);
139}
140
141
142template <class Container>
144{
145 // cannot specialize std::rel_ops::operators on native types
146 // or pointers to such things
147}
148
149
151template <class T>
152struct UnaryPredicate
153{
154 bool operator() (const T&) const {
155 return true;
156 }
157};
158
159
160template <class T>
161struct BinaryPredicate
162{
163 bool operator() (const T&, const T&) const {
164 return true;
165 }
166};
167
168
169template <class T>
171{
172 T operator() (T) const {
173 return T ();
174 }
175};
176
177
178template <class T>
179struct Generator
180{
181 T operator() () const {
182 return T ();
183 }
184};
185
186template <class T>
188{
189 T operator() (const T &t) const {
190 return t;
191 }
192};
193
194
195template <class T>
197{
198 T operator() (const T &t, const T&) const {
199 return t;
200 }
201};
202
203
205template <class T, class InputIterator>
207{
208 // do not run (compile only), prevent warnings about unreachable code
209 static int count = 0;
210
211 if (++count)
212 return;
213
214 typedef InputIterator I;
215
216 std::for_each (I (), I (), UnaryFunction<T>());
217 std::find (I (), I (), T ());
218 std::find_if (I (), I (), UnaryPredicate<T>());
219
220#ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
221 std::count (I (), I (), T ());
222 std::count_if (I (), I (), UnaryPredicate<T>());
223#else // if defined (_RWSTD_NO_CLASS_PARTIAL_SPEC)
224 std::size_t n = 0;
225 std::count (I (), I (), T (), n);
226 std::count_if (I (), I (), UnaryPredicate<T>(), n);
227#endif // _RWSTD_NO_CLASS_PARTIAL_SPEC
228
229 std::mismatch (I (), I (), I ());
230 std::mismatch (I (), I (), I (), BinaryPredicate<T>());
231 std::equal (I (), I (), I ());
232 std::equal (I (), I (), I (), BinaryPredicate<T>());
233
234 std::includes (I (), I (), I (), I ());
235 std::includes (I (), I (), I (), I (), BinaryPredicate<T>());
236
237 std::lexicographical_compare (I (), I (), I (), I ());
238 std::lexicographical_compare (I (), I (), I (), I (), BinaryPredicate<T>());
239}
240
241
243template <class T, class OutputIterator>
245{
246 // do not run (compile only), prevent warnings about unreachable code
247 static int count = 0;
248
249 if (++count)
250 return;
251
252 typedef OutputIterator I;
253
254 std::copy (I (), I (), I ());
255 std::copy_backward (I (), I (), I ());
256
257 std::transform (I (), I (), I (), UnaryFunction<T>());
258 std::transform (I (), I (), I (), I (), BinaryFunction<T>());
259
260 std::replace_copy (I (), I (), I (), T (), T ());
261 std::replace_copy_if (I (), I (), I (), UnaryPredicate<T>(), T ());
262
263 std::merge (I (), I (), I (), I (), I ());
264 std::merge (I (), I (), I (), I (), I (), BinaryPredicate<T>());
265
266 std::set_union (I (), I (), I (), I (), I ());
267 std::set_union (I (), I (), I (), I (), I (), BinaryPredicate<T>());
268
269 std::set_intersection (I (), I (), I (), I (), I ());
270 std::set_intersection (I (), I (), I (), I (), I (), BinaryPredicate<T>());
271
272 std::set_difference (I (), I (), I (), I (), I ());
273 std::set_difference (I (), I (), I (), I (), I (), BinaryPredicate<T>());
274
275 std::set_symmetric_difference (I (), I (), I (), I (), I ());
276 std::set_symmetric_difference (I (), I (), I (), I (), I (),
278
279 std::fill_n (I (), 0, T ());
280
281 std::generate_n (I (), 0, Generator<T>());
282
283 std::remove_copy (I (), I (), I (), T ());
284 std::remove_copy_if (I (), I (), I (), UnaryPredicate<T>());
285
286 std::unique_copy (I (), I (), I ());
287 std::unique_copy (I (), I (), I (), BinaryPredicate<T>());
288
289 std::reverse_copy (I (), I (), I ());
290
291 std::rotate_copy (I (), I (), I (), I ());
292}
293
294
296template <class T, class ForwardIterator>
298{
299 // do not run (compile only), prevent warnings about unreachable code
300 static int count = 0;
301
302 if (++count)
303 return;
304
305 typedef ForwardIterator I;
306
307 std::find_end (I (), I (), I (), I ());
308 std::find_end (I (), I (), I (), I (), BinaryPredicate<T>());
309
310 std::find_first_of (I (), I (), I (), I ());
311 std::find_first_of (I (), I (), I (), I (), BinaryPredicate<T>());
312
313 std::adjacent_find (I (), I ());
314 std::adjacent_find (I (), I (), BinaryPredicate<T>());
315
316 std::search (I (), I (), I (), I ());
317 std::search (I (), I (), I (), I (), BinaryPredicate<T>());
318
319 std::search_n (I (), I (), 0, T ());
320 std::search_n (I (), I (), 0, T (), BinaryPredicate<T>());
321
322 std::swap_ranges (I (), I (), I ());
323 std::iter_swap (I (), I ());
324
325 std::replace (I (), I (), T (), T ());
326 std::replace_if (I (), I (), UnaryPredicate<T>(), T ());
327
328 std::equal_range (I (), I (), T ());
329 std::equal_range (I (), I (), T (), BinaryPredicate<T>());
330
331 std::binary_search (I (), I (), T ());
332 std::binary_search (I (), I (), T (), BinaryPredicate<T>());
333
334 std::min_element (I (), I ());
335 std::min_element (I (), I (), BinaryPredicate<T>());
336
337 std::max_element (I (), I ());
338 std::max_element (I (), I (), BinaryPredicate<T>());
339
340 std::fill (I (), I (), T ());
341
342 std::generate (I (), I (), Generator<T>());
343
344 std::remove (I (), I (), T ());
345 std::remove_if (I (), I (), UnaryPredicate<T>());
346
347 std::unique (I (), I ());
348 std::unique (I (), I (), BinaryPredicate<T>());
349
350 std::reverse (I (), I ());
351
352 std::rotate (I (), I (), I ());
353}
354
355
357template <class T, class BidirectionalIterator>
359{
360 // do not run (compile only), prevent warnings about unreachable code
361 static int count = 0;
362
363 if (++count)
364 return;
365
366 typedef BidirectionalIterator I;
367
368 std::partition (I (), I (), UnaryPredicate<T>());
369 std::stable_partition (I (), I (), UnaryPredicate<T>());
370
371 std::lower_bound (I (), I (), T ());
372 std::lower_bound (I (), I (), T (), BinaryPredicate<T>());
373
374 std::upper_bound (I (), I (), T ());
375 std::upper_bound (I (), I (), T (), BinaryPredicate<T>());
376
377 std::inplace_merge (I (), I (), I ());
378 std::inplace_merge (I (), I (), I (), BinaryPredicate<T>());
379
380 std::next_permutation (I (), I ());
381 std::next_permutation (I (), I (), BinaryPredicate<T>());
382
383 std::prev_permutation (I (), I ());
384 std::prev_permutation (I (), I (), BinaryPredicate<T>());
385}
386
387
389template <class T, class RandomAccessIterator>
391{
392 // do not run (compile only), prevent warnings about unreachable code
393 static int count = 0;
394
395 if (++count)
396 return;
397
398 typedef RandomAccessIterator I;
399
400#if !defined _RWSTD_NO_DEBUG_ITER
402#else
404#endif
405
406 std::random_shuffle (I (), I ());
407 std::random_shuffle (I (), I (), rndgen);
408
409 std::sort (I (), I ());
410 std::sort (I (), I (), BinaryPredicate<T>());
411
412 std::stable_sort (I (), I ());
413 std::stable_sort (I (), I (), BinaryPredicate<T>());
414
415 std::partial_sort (I (), I (), I ());
416 std::partial_sort (I (), I (), I (), BinaryPredicate<T>());
417
418 std::partial_sort_copy (I (), I (), I (), I ());
419 std::partial_sort_copy (I (), I (), I (), I (), BinaryPredicate<T>());
420
421 std::nth_element (I (), I (), I ());
422 std::nth_element (I (), I (), I (), BinaryPredicate<T>());
423
424 std::push_heap (I (), I (), BinaryPredicate<T>());
425 std::pop_heap (I (), I (), BinaryPredicate<T>());
426 std::make_heap (I (), I (), BinaryPredicate<T>());
427 std::sort_heap (I (), I (), BinaryPredicate<T>());
428}
429
430
432static int
433run_test (int, char**)
434{
435 typedef std::map<int, int> Map;
436 typedef std::multimap<int, int> MultiMap;
437
438 // verify that rel_ops operators can be instantiated
439 // on iterators of the containers below
440 TEST_OPERATORS (std::deque<int>);
441
442 TEST_INEQUALITY (std::list<int>);
443 TEST_INEQUALITY (Map);
444 TEST_INEQUALITY (MultiMap);
445 TEST_INEQUALITY (std::set<int>);
446 TEST_INEQUALITY (std::multiset<int>);
447
448#if !defined (_MSC_VER) || _MSC_VER > 1300
449 // prevent from testing with the braindead MSVC 6 and 7
450 // as a workaround for compiler bugs (PR #16828, 22268)
451
452 // prevent attempts to specialize rel_ops operators on
453 // native types (or pointers to such things)
454 test_iterator (std::basic_string<int>(),
455 std::basic_string<int>::iterator ());
456
457 test_iterator (std::vector<int>(),
458 std::vector<int>::iterator ());
459#endif // MSVC > 7.0
460
461 TEST_OPERATORS (std::vector<bool>);
462
463#define TEST_INPUT_ITERATORS(T) \
464 test_input_iterators (T (), std::deque<T>::iterator ()); \
465 test_input_iterators (T (), std::list<T>::iterator ()); \
466 test_input_iterators (std::map<T, T>::value_type (), \
467 std::map<T, T>::iterator ()); \
468 test_input_iterators (std::multimap<T, T>::value_type (), \
469 std::multimap<T, T>::iterator ()); \
470 test_input_iterators (T (), std::set<T>::iterator ()); \
471 test_input_iterators (T (), std::multiset<T, T>::iterator ()); \
472 test_input_iterators (T (), std::basic_string<T>::iterator ()); \
473 test_input_iterators (T (), std::vector<T>::iterator ())
474
475#define TEST_OUTPUT_ITERATORS(T) \
476 test_output_iterators (T (), std::deque<T>::iterator ()); \
477 test_output_iterators (T (), std::list<T>::iterator ()); \
478 test_output_iterators (T (), std::basic_string<T>::iterator ()); \
479 test_output_iterators (T (), std::vector<T>::iterator ())
480
481#define TEST_FORWARD_ITERATORS(T) \
482 test_forward_iterators (T (), std::deque<T>::iterator ()); \
483 test_forward_iterators (T (), std::list<T>::iterator ()); \
484 test_forward_iterators (T (), std::basic_string<T>::iterator ()); \
485 test_forward_iterators (T (), std::vector<T>::iterator ())
486
487#define TEST_BIDIRECTIONAL_ITERATORS(T) \
488 test_bidirectional_iterators (T (), std::deque<T>::iterator ()); \
489 test_bidirectional_iterators (T (), std::list<T>::iterator ()); \
490 test_bidirectional_iterators (T (), std::basic_string<T>::iterator ()); \
491 test_bidirectional_iterators (T (), std::vector<T>::iterator ())
492
493#define TEST_RANDOM_ACCESS_ITERATORS(T) \
494 test_random_access_iterators (T (), std::deque<T>::iterator ()); \
495 test_random_access_iterators (T (), std::basic_string<T>::iterator ()); \
496 test_random_access_iterators (T (), std::vector<T>::iterator ()); \
497
498 // verify that algorithms can be specialized on container
499 // iterators without causing ambiguities with rel_ops
505
506#if !defined (__HP_aCC) || _RWSTD_HP_aCC_MINOR > 3600
507
508 // working around an HP aCC bug (PR #28331)
514
515#endif // HP aCC > x.36
516
517 return 0;
518}
519
520
522int main (int argc, char *argv[])
523{
524 return rw_test (argc, argv, __FILE__,
525 "lib.operators",
526 "interactions with the rest of the implementation",
527 run_test,
528 0 /* no command line options */);
529}
void count(T, T, const U &,...)
#define T
Definition 17.names.cpp:163
#define t
Definition 17.names.cpp:136
#define Container
Definition 17.names.cpp:462
#define I
Definition 17.names.cpp:152
#define RandomAccessIterator
Definition 17.names.cpp:528
#define BidirectionalIterator
Definition 17.names.cpp:449
#define n
Definition 17.names.cpp:130
#define ForwardIterator
Definition 17.names.cpp:469
#define OutputIterator
Definition 17.names.cpp:515
void test_output_iterators(T, OutputIterator)
#define TEST_INEQUALITY(T)
#define TEST_OUTPUT_ITERATORS(T)
#define TEST_FORWARD_ITERATORS(T)
#define TEST_OPERATORS(T)
void test_input_iterators(T, InputIterator)
void test_bidirectional_iterators(T, BidirectionalIterator)
void test_forward_iterators(T, ForwardIterator)
#define TEST_BIDIRECTIONAL_ITERATORS(T)
#define TEST_INPUT_ITERATORS(T)
void test_random_access_iterators(T, RandomAccessIterator)
#define TEST_RANDOM_ACCESS_ITERATORS(T)
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
#define main
#define _USING(name)
Definition _defs.h:568
Definition json.h:5363
_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,...)
T operator()(const T &t, const T &) const
virtual conv_to_bool operator()(const UserClass &) const
Definition value.cpp:640