• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  (C) Copyright Herve Bronnimann 2004.
2 //  Use, modification and distribution are subject to the
3 //  Boost Software License, Version 1.0. (See accompanying file
4 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include <utility>
7 #include <functional>
8 
9 #include <boost/config.hpp>
10 #include <boost/algorithm/minmax.hpp>
11 
12 #define BOOST_TEST_MAIN
13 #include <boost/test/unit_test.hpp>
14 
15 class custom {
16   int m_x;
17   friend std::ostream& operator<<(std::ostream& str, custom const& x);
18 public:
custom(int x=0)19   explicit custom(int x = 0) : m_x(x) {}
custom(custom const & y)20   custom(custom const& y) : m_x(y.m_x) {}
operator ==(custom const & y) const21   bool operator==(custom const& y) const { return m_x == y.m_x; }
operator <(custom const & y) const22   bool operator<(custom const& y) const { return m_x < y.m_x; }
operator +(custom const & y) const23   custom operator+(custom const& y) const { return custom(m_x+y.m_x); }
operator +=(custom const & y)24   custom& operator+=(custom const& y) { m_x += y.m_x; return *this; }
25 };
26 
27 std::ostream&
operator <<(std::ostream & str,custom const & x)28 operator<<(std::ostream& str, custom const& x)
29 {
30   return  str << x.m_x;
31 }
32 
33 template <class Value>
34 struct less_count : std::less<Value> {
35   typedef std::less<Value> Base;
less_countless_count36   less_count(less_count<Value> const& lc) : m_counter(lc.m_counter) {}
less_countless_count37   less_count(int& counter) : m_counter(counter) {}
operator ()less_count38   bool operator()(Value const& a, Value const& b) const {
39     ++m_counter;
40     return Base::operator()(a,b);
41   }
resetless_count42   void reset() {
43     m_counter = 0;
44   }
45 private:
46   int& m_counter;
47 };
48 
49 using namespace boost;
50 
51 template <class Value>
test(BOOST_EXPLICIT_TEMPLATE_TYPE (Value))52 void test(BOOST_EXPLICIT_TEMPLATE_TYPE(Value))
53 {
54   Value zero(0), one(1);
55   int counter = 0;
56   less_count<Value> lc(counter);
57 
58   // Test functionality
59   tuple<Value const&, Value const&> result1 = boost::minmax(zero, one);
60   BOOST_CHECK_EQUAL( get<0>(result1), zero );
61   BOOST_CHECK_EQUAL( get<1>(result1), one );
62 
63   tuple<Value const&, Value const&> result2 = boost::minmax(one, zero);
64   BOOST_CHECK_EQUAL( get<0>(result2), zero );
65   BOOST_CHECK_EQUAL( get<1>(result2), one );
66 
67   // Test functionality and number of comparisons
68   lc.reset();
69   tuple<Value const&, Value const&> result3 = boost::minmax(zero, one, lc );
70   BOOST_CHECK_EQUAL( get<0>(result3), zero );
71   BOOST_CHECK_EQUAL( get<1>(result3), one );
72   BOOST_CHECK_EQUAL( counter, 1 );
73 
74   lc.reset();
75   tuple<Value const&, Value const&> result4 = boost::minmax(one, zero, lc );
76   BOOST_CHECK_EQUAL( get<0>(result4), zero );
77   BOOST_CHECK_EQUAL( get<1>(result4), one );
78   BOOST_CHECK_EQUAL( counter, 1);
79 }
80 
BOOST_AUTO_TEST_CASE(test_main)81 BOOST_AUTO_TEST_CASE( test_main )
82 {
83   test<int>(); // ("builtin");
84   test<custom>(); // ("custom ");
85 }
86