| 
									
										
										
										
											2004-07-29 20:27:38 +00:00
										 |  |  | //  (C) Copyright Herve Bronnimann 2004.
 | 
					
						
							|  |  |  | //  Use, modification and distribution are subject to the
 | 
					
						
							|  |  |  | //  Boost Software License, Version 1.0. (See accompanying file
 | 
					
						
							|  |  |  | //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | #include <utility>
 | 
					
						
							|  |  |  | #include <functional>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-23 17:17:37 +00:00
										 |  |  | #include <boost/config.hpp>
 | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | #include <boost/algorithm/minmax.hpp>
 | 
					
						
							| 
									
										
										
										
											2013-02-08 17:21:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define BOOST_TEST_MAIN
 | 
					
						
							|  |  |  | #include <boost/test/unit_test.hpp>
 | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class custom { | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  |   int m_x; | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  |   friend std::ostream& operator<<(std::ostream& str, custom const& x); | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  |   explicit custom(int x = 0) : m_x(x) {} | 
					
						
							|  |  |  |   custom(custom const& y) : m_x(y.m_x) {} | 
					
						
							|  |  |  |   bool operator==(custom const& y) const { return m_x == y.m_x; } | 
					
						
							|  |  |  |   bool operator<(custom const& y) const { return m_x < y.m_x; } | 
					
						
							|  |  |  |   custom operator+(custom const& y) const { return custom(m_x+y.m_x); } | 
					
						
							|  |  |  |   custom& operator+=(custom const& y) { m_x += y.m_x; return *this; } | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  | std::ostream& | 
					
						
							|  |  |  | operator<<(std::ostream& str, custom const& x) | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  |   return  str << x.m_x; | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <class Value> | 
					
						
							|  |  |  | struct less_count : std::less<Value> { | 
					
						
							|  |  |  |   typedef std::less<Value> Base; | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  |   less_count(less_count<Value> const& lc) : m_counter(lc.m_counter) {} | 
					
						
							|  |  |  |   less_count(int& counter) : m_counter(counter) {} | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  |   bool operator()(Value const& a, Value const& b) const { | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  |     ++m_counter; | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  |     return Base::operator()(a,b); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   void reset() { | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  |     m_counter = 0; | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  |   int& m_counter; | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-14 07:15:51 +00:00
										 |  |  | using namespace boost; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | template <class Value> | 
					
						
							| 
									
										
										
										
											2004-07-23 17:17:37 +00:00
										 |  |  | void test(BOOST_EXPLICIT_TEMPLATE_TYPE(Value)) | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  |   Value zero(0), one(1); | 
					
						
							|  |  |  |   int counter = 0; | 
					
						
							|  |  |  |   less_count<Value> lc(counter); | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Test functionality
 | 
					
						
							| 
									
										
										
										
											2009-05-17 00:39:22 +00:00
										 |  |  |   tuple<Value const&, Value const&> result1 = boost::minmax(zero, one); | 
					
						
							| 
									
										
										
										
											2004-07-22 13:43:01 +00:00
										 |  |  |   BOOST_CHECK_EQUAL( get<0>(result1), zero ); | 
					
						
							|  |  |  |   BOOST_CHECK_EQUAL( get<1>(result1), one ); | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-17 00:39:22 +00:00
										 |  |  |   tuple<Value const&, Value const&> result2 = boost::minmax(one, zero); | 
					
						
							| 
									
										
										
										
											2004-07-22 13:43:01 +00:00
										 |  |  |   BOOST_CHECK_EQUAL( get<0>(result2), zero ); | 
					
						
							|  |  |  |   BOOST_CHECK_EQUAL( get<1>(result2), one ); | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   // Test functionality and number of comparisons
 | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  |   lc.reset(); | 
					
						
							| 
									
										
										
										
											2009-05-17 00:39:22 +00:00
										 |  |  |   tuple<Value const&, Value const&> result3 = boost::minmax(zero, one, lc ); | 
					
						
							| 
									
										
										
										
											2004-07-22 13:43:01 +00:00
										 |  |  |   BOOST_CHECK_EQUAL( get<0>(result3), zero ); | 
					
						
							|  |  |  |   BOOST_CHECK_EQUAL( get<1>(result3), one ); | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  |   BOOST_CHECK_EQUAL( counter, 1 ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  |   lc.reset(); | 
					
						
							| 
									
										
										
										
											2009-05-17 00:39:22 +00:00
										 |  |  |   tuple<Value const&, Value const&> result4 = boost::minmax(one, zero, lc ); | 
					
						
							| 
									
										
										
										
											2004-07-22 13:43:01 +00:00
										 |  |  |   BOOST_CHECK_EQUAL( get<0>(result4), zero ); | 
					
						
							|  |  |  |   BOOST_CHECK_EQUAL( get<1>(result4), one ); | 
					
						
							| 
									
										
										
										
											2004-07-21 17:15:40 +00:00
										 |  |  |   BOOST_CHECK_EQUAL( counter, 1); | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-08 17:21:43 +00:00
										 |  |  | BOOST_AUTO_TEST_CASE( test_main ) | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-07-23 16:13:43 +00:00
										 |  |  |   test<int>(); // ("builtin");
 | 
					
						
							|  |  |  |   test<custom>(); // ("custom ");
 | 
					
						
							| 
									
										
										
										
											2004-07-01 20:15:51 +00:00
										 |  |  | } |