forked from boostorg/algorithm
Now includes minmax_test in the test suite.
[SVN r23909]
This commit is contained in:
@ -23,5 +23,11 @@ DEPENDS all : test ;
|
|||||||
:
|
:
|
||||||
: minmax_element
|
: minmax_element
|
||||||
]
|
]
|
||||||
|
[ run
|
||||||
|
minmax_test.cpp
|
||||||
|
: :
|
||||||
|
:
|
||||||
|
: minmax
|
||||||
|
]
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
@ -1,78 +1,79 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <algorithm>
|
|
||||||
#include <numeric>
|
|
||||||
#include <iterator>
|
|
||||||
#include <vector>
|
|
||||||
#include <list>
|
|
||||||
#include <set>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <boost/algorithm/minmax.hpp>
|
#include <boost/algorithm/minmax.hpp>
|
||||||
#include <boost/test/included/test_exec_monitor.hpp>
|
#include <boost/test/included/test_exec_monitor.hpp>
|
||||||
|
|
||||||
class custom {
|
class custom {
|
||||||
int _M_x;
|
int m_x;
|
||||||
friend bool operator<(custom const& x, custom const& y);
|
|
||||||
friend std::ostream& operator<<(std::ostream& str, custom const& x);
|
friend std::ostream& operator<<(std::ostream& str, custom const& x);
|
||||||
public:
|
public:
|
||||||
explicit custom(int x = 0) : _M_x(x) {}
|
explicit custom(int x = 0) : m_x(x) {}
|
||||||
custom(custom const& y) : _M_x(y._M_x) {}
|
custom(custom const& y) : m_x(y.m_x) {}
|
||||||
custom operator+(custom const& y) const { return custom(_M_x+y._M_x); }
|
bool operator==(custom const& y) const { return m_x == y.m_x; }
|
||||||
custom& operator+=(custom const& y) { _M_x += y._M_x; return *this; }
|
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; }
|
||||||
};
|
};
|
||||||
|
|
||||||
bool operator< (custom const& x, custom const& y)
|
std::ostream&
|
||||||
|
operator<<(std::ostream& str, custom const& x)
|
||||||
{
|
{
|
||||||
return x._M_x < y._M_x;
|
return str << x.m_x;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Value>
|
template <class Value>
|
||||||
struct less_count : std::less<Value> {
|
struct less_count : std::less<Value> {
|
||||||
typedef std::less<Value> Base;
|
typedef std::less<Value> Base;
|
||||||
less_count(less_count<Value> const& lc) : _M_counter(lc._M_counter) {}
|
less_count(less_count<Value> const& lc) : m_counter(lc.m_counter) {}
|
||||||
less_count(int& counter) : _M_counter(counter) {}
|
less_count(int& counter) : m_counter(counter) {}
|
||||||
bool operator()(Value const& a, Value const& b) const {
|
bool operator()(Value const& a, Value const& b) const {
|
||||||
++_M_counter;
|
++m_counter;
|
||||||
return Base::operator()(a,b);
|
return Base::operator()(a,b);
|
||||||
}
|
}
|
||||||
void reset() {
|
void reset() {
|
||||||
_M_counter = 0;
|
m_counter = 0;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
int& _M_counter;
|
int& m_counter;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class Value>
|
template <class Value>
|
||||||
void test(int n, char* name)
|
void test(char*)
|
||||||
{
|
{
|
||||||
using namespace boost;
|
using namespace boost;
|
||||||
|
Value zero(0), one(1);
|
||||||
|
int counter = 0;
|
||||||
|
less_count<Value> lc(counter);
|
||||||
|
|
||||||
// Test functionality
|
// Test functionality
|
||||||
tuple<Value const&, Value const&> result = minmax(0, 1);
|
tuple<Value const&, Value const&> result1 = minmax(zero, one);
|
||||||
BOOST_CHECK_EQUAL( result.get<0>(), 0 );
|
BOOST_CHECK_EQUAL( result1.get<0>(), zero );
|
||||||
BOOST_CHECK_EQUAL( result.get<1>(), 1 );
|
BOOST_CHECK_EQUAL( result1.get<1>(), one );
|
||||||
|
|
||||||
result = minmax(1, 0);
|
tuple<Value const&, Value const&> result2 = minmax(one, zero);
|
||||||
BOOST_CHECK_EQUAL( result.get<0>(), 0 );
|
BOOST_CHECK_EQUAL( result2.get<0>(), zero );
|
||||||
BOOST_CHECK_EQUAL( result.get<1>(), 1 );
|
BOOST_CHECK_EQUAL( result2.get<1>(), one );
|
||||||
|
|
||||||
// Test functionality and number of comparisons
|
// Test functionality and number of comparisons
|
||||||
int counter = 0;
|
lc.reset();
|
||||||
BOOST_CHECK_EQUAL( result.get<0>(), 0 );
|
tuple<Value const&, Value const&> result3 = minmax(zero, one, lc );
|
||||||
BOOST_CHECK_EQUAL( result.get<1>(), 1 );
|
BOOST_CHECK_EQUAL( result3.get<0>(), zero );
|
||||||
|
BOOST_CHECK_EQUAL( result3.get<1>(), one );
|
||||||
BOOST_CHECK_EQUAL( counter, 1 );
|
BOOST_CHECK_EQUAL( counter, 1 );
|
||||||
|
|
||||||
result = minmax(1, 0, less_count<Value>(counter) );
|
lc.reset();
|
||||||
BOOST_CHECK_EQUAL( result.get<0>(), 0 );
|
tuple<Value const&, Value const&> result4 = minmax(one, zero, lc );
|
||||||
BOOST_CHECK_EQUAL( result.get<1>(), 1 );
|
BOOST_CHECK_EQUAL( result4.get<0>(), zero );
|
||||||
BOOST_CHECK_EQUAL( counter, 2);
|
BOOST_CHECK_EQUAL( result4.get<1>(), one );
|
||||||
|
BOOST_CHECK_EQUAL( counter, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_main( int argc, char* argv[] )
|
int test_main( int , char* [] )
|
||||||
{
|
{
|
||||||
test<int>(n, "builtin");
|
test<int>("builtin");
|
||||||
test<custom>(n, "custom ");
|
test<custom>("custom ");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user