minmax_macro v0.1

[SVN r3105]
This commit is contained in:
Eric Niebler
2006-07-24 20:31:01 +00:00
parent b683dba9be
commit e82315742a
9 changed files with 801 additions and 0 deletions

21
minmax_macro/test/Jamfile.v2 Executable file
View File

@ -0,0 +1,21 @@
# (C) Copyright 2004: Eric Niebler
# Distributed under 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)
# bring in rules for testing
import testing ;
project
: requirements
<library>/boost/test//boost_unit_test_framework
<link>static
<include>../../../..
;
test-suite "minmax_macro"
: [ run basic.cpp ]
[ run promotions.cpp ]
[ run rvalue_lvalue.cpp ]
[ run tricky.cpp ]
[ run eval_once.cpp ]
;

44
minmax_macro/test/basic.cpp Executable file
View File

@ -0,0 +1,44 @@
///////////////////////////////////////////////////////////////////////////////
// basic.hpp test file
//
// Copyright 2006 Eric Niebler.
// Distributed under 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)
//
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/minmax_macro.hpp>
void basic_min_test()
{
BOOST_CHECK_EQUAL(1, BOOST_MIN(1,2));
BOOST_CHECK_EQUAL(1, BOOST_MIN(2,1));
int one=1, two=2;
BOOST_CHECK_EQUAL(one, BOOST_MIN(one,two));
BOOST_CHECK_EQUAL(one, BOOST_MIN(two,one));
}
void basic_max_test()
{
BOOST_CHECK_EQUAL(2, BOOST_MAX(1,2));
BOOST_CHECK_EQUAL(2, BOOST_MAX(2,1));
int one=1, two=2;
BOOST_CHECK_EQUAL(two, BOOST_MAX(one,two));
BOOST_CHECK_EQUAL(two, BOOST_MAX(two,one));
}
///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
boost::unit_test::test_suite* init_unit_test_suite( int argc, char* argv[] )
{
boost::unit_test::test_suite *test = BOOST_TEST_SUITE("basic min/max test");
test->add(BOOST_TEST_CASE(&basic_min_test));
test->add(BOOST_TEST_CASE(&basic_max_test));
return test;
}

37
minmax_macro/test/eval_once.cpp Executable file
View File

@ -0,0 +1,37 @@
///////////////////////////////////////////////////////////////////////////////
// eval_once.hpp test file
//
// Copyright 2006 Eric Niebler.
// Distributed under 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)
//
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/minmax_macro.hpp>
void test_eval_once()
{
int i = 0;
int j = 1;
int k = BOOST_MIN(i++,j);
BOOST_CHECK_EQUAL(1, i);
BOOST_CHECK_EQUAL(0, k);
k = BOOST_MAX(i,++j);
BOOST_CHECK_EQUAL(2, j);
BOOST_CHECK_EQUAL(2, k);
}
///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
boost::unit_test::test_suite* init_unit_test_suite( int argc, char* argv[] )
{
boost::unit_test::test_suite *test = BOOST_TEST_SUITE("eval_once min/max test");
test->add(BOOST_TEST_CASE(&test_eval_once));
return test;
}

View File

@ -0,0 +1,54 @@
///////////////////////////////////////////////////////////////////////////////
// promotions.hpp test file
//
// Copyright 2006 Eric Niebler.
// Distributed under 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)
//
#include <boost/test/unit_test.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/algorithm/minmax_macro.hpp>
template<typename Result, typename T> void check_is_same(T)
{
BOOST_MPL_ASSERT((boost::is_same<T,Result>));
}
void test_promotions()
{
char ch = 0;
short sh = 0;
int in = 0;
unsigned un = 0;
float fl = 0;
double db = 0;
check_is_same<int>(BOOST_MIN(ch,sh));
check_is_same<int>(BOOST_MAX(ch,sh));
check_is_same<int>(BOOST_MIN(ch,in));
check_is_same<int>(BOOST_MAX(ch,in));
check_is_same<unsigned>(BOOST_MIN(un,in));
check_is_same<unsigned>(BOOST_MAX(un,in));
check_is_same<float>(BOOST_MIN(in,fl));
check_is_same<float>(BOOST_MAX(in,fl));
check_is_same<double>(BOOST_MIN(db,fl));
check_is_same<double>(BOOST_MAX(db,fl));
}
///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
boost::unit_test::test_suite* init_unit_test_suite( int argc, char* argv[] )
{
boost::unit_test::test_suite *test = BOOST_TEST_SUITE("promotions min/max test");
test->add(BOOST_TEST_CASE(&test_promotions));
return test;
}

View File

@ -0,0 +1,94 @@
///////////////////////////////////////////////////////////////////////////////
// rvalue_lvalue.hpp test file
//
// Copyright 2006 Eric Niebler.
// Distributed under 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)
//
// Test code from Howard Hinnant (TODO: get permission!)
#include <boost/test/included/unit_test.hpp>
#include <boost/algorithm/minmax_macro.hpp>
class A
{
private:
int i_;
public:
A(int i) : i_(i) {}
~A() {i_ = 0;}
A(const A& a) : i_(a.i_) {}
operator int() const {return i_;}
friend bool operator<(const A& x, const A& y)
{return x.i_ < y.i_;}
friend bool operator==(const A& x, const A& y)
{return x.i_ == y.i_;}
};
void test_rvalue_lvalue()
{
// lvalues of the same type:
{
A a3(3);
A a2(2);
BOOST_MIN(a2, a3) = A(1); // a2 == 1, no copy, no move
BOOST_CHECK_EQUAL(a2, A(1));
BOOST_CHECK_EQUAL(a3, A(3));
}
//{
// // lvalues of the same type with different cv
// const A a3(3);
// A a2(2);
// BOOST_MIN(a2, a3) = A(1); // illegal operands 'const A' = 'A'
//}
{
const A a3(3);
A a2(2);
const A& a_ref = BOOST_MIN(a2, a3);
a2 = A(1); // a_ref == 1, a_ref refers to a2, no copy, no move
BOOST_CHECK_EQUAL(a_ref, A(1));
}
{
// lvalue / rvalue mix of the same type
A a3(3);
const A& a_ref = BOOST_MIN(A(2), a3);
BOOST_CHECK_EQUAL(a_ref, A(2));
// a_ref refers to temporary returned (by value) from BOOST_MIN, a_ref == 2, A(A&&) executed to move from A(2)
// A(const A&) - or some copy constructor - required to be accessible
}
{
A a1(1);
const A& a_ref = BOOST_MIN(A(2), a1);
BOOST_CHECK_EQUAL(a_ref, A(1));
// a_ref refers to temporary returned (by value) from BOOST_MIN, a_ref == 1, A(const A&) executed to copy from a1
}
{
// rvalue / rvalue
const A& a_ref = BOOST_MIN(A(2), A(3));
BOOST_CHECK_EQUAL(a_ref, A(2));
// a_ref refers to temporary returned (by value) from BOOST_MIN, a_ref == 2, A(A&&) executed to move from A(2)
// accessible copy constructor not required
}
}
///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
boost::unit_test::test_suite* init_unit_test_suite( int argc, char* argv[] )
{
boost::unit_test::test_suite *test = BOOST_TEST_SUITE("rvalue/lvalue min/max test");
test->add(BOOST_TEST_CASE(&test_rvalue_lvalue));
return test;
}

44
minmax_macro/test/tricky.cpp Executable file
View File

@ -0,0 +1,44 @@
///////////////////////////////////////////////////////////////////////////////
// tricky.hpp test file
//
// Copyright 2006 Eric Niebler.
// Distributed under 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)
//
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/minmax_macro.hpp>
struct Base
{
virtual bool operator <(Base const &) const = 0;
};
struct Derived : Base
{
virtual bool operator <(Base const &that) const
{
return dynamic_cast<void const*>(this) < dynamic_cast<void const*>(&that);
}
};
void test_tricky()
{
Derived d;
Base &b = d;
Base &lvalue = BOOST_MIN(d,b);
}
///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
boost::unit_test::test_suite* init_unit_test_suite( int argc, char* argv[] )
{
boost::unit_test::test_suite *test = BOOST_TEST_SUITE("tricky min/max test");
test->add(BOOST_TEST_CASE(&test_tricky));
return test;
}