Now includes minmax_test in the test suite.

[SVN r23909]
This commit is contained in:
Hervé Brönnimann
2004-07-21 17:15:40 +00:00
parent 2b2086dd8f
commit 7a88ef8658
2 changed files with 43 additions and 36 deletions

View File

@ -1,78 +1,79 @@
#include <utility>
#include <functional>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <vector>
#include <list>
#include <set>
#include <iostream>
#include <boost/algorithm/minmax.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
class custom {
int _M_x;
friend bool operator<(custom const& x, custom const& y);
int m_x;
friend std::ostream& operator<<(std::ostream& str, custom const& x);
public:
explicit custom(int x = 0) : _M_x(x) {}
custom(custom const& y) : _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; }
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; }
};
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>
struct less_count : std::less<Value> {
typedef std::less<Value> Base;
less_count(less_count<Value> const& lc) : _M_counter(lc._M_counter) {}
less_count(int& counter) : _M_counter(counter) {}
less_count(less_count<Value> const& lc) : m_counter(lc.m_counter) {}
less_count(int& counter) : m_counter(counter) {}
bool operator()(Value const& a, Value const& b) const {
++_M_counter;
++m_counter;
return Base::operator()(a,b);
}
void reset() {
_M_counter = 0;
m_counter = 0;
}
private:
int& _M_counter;
int& m_counter;
};
template <class Value>
void test(int n, char* name)
void test(char*)
{
using namespace boost;
Value zero(0), one(1);
int counter = 0;
less_count<Value> lc(counter);
// Test functionality
tuple<Value const&, Value const&> result = minmax(0, 1);
BOOST_CHECK_EQUAL( result.get<0>(), 0 );
BOOST_CHECK_EQUAL( result.get<1>(), 1 );
tuple<Value const&, Value const&> result1 = minmax(zero, one);
BOOST_CHECK_EQUAL( result1.get<0>(), zero );
BOOST_CHECK_EQUAL( result1.get<1>(), one );
result = minmax(1, 0);
BOOST_CHECK_EQUAL( result.get<0>(), 0 );
BOOST_CHECK_EQUAL( result.get<1>(), 1 );
tuple<Value const&, Value const&> result2 = minmax(one, zero);
BOOST_CHECK_EQUAL( result2.get<0>(), zero );
BOOST_CHECK_EQUAL( result2.get<1>(), one );
// Test functionality and number of comparisons
int counter = 0;
BOOST_CHECK_EQUAL( result.get<0>(), 0 );
BOOST_CHECK_EQUAL( result.get<1>(), 1 );
lc.reset();
tuple<Value const&, Value const&> result3 = minmax(zero, one, lc );
BOOST_CHECK_EQUAL( result3.get<0>(), zero );
BOOST_CHECK_EQUAL( result3.get<1>(), one );
BOOST_CHECK_EQUAL( counter, 1 );
result = minmax(1, 0, less_count<Value>(counter) );
BOOST_CHECK_EQUAL( result.get<0>(), 0 );
BOOST_CHECK_EQUAL( result.get<1>(), 1 );
BOOST_CHECK_EQUAL( counter, 2);
lc.reset();
tuple<Value const&, Value const&> result4 = minmax(one, zero, lc );
BOOST_CHECK_EQUAL( result4.get<0>(), zero );
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<custom>(n, "custom ");
test<int>("builtin");
test<custom>("custom ");
return 0;
}