From 43a83f500eb53b2f29b6807252db13b12abfd50e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20J=C3=B8rgen=20Ottosen?= Date: Mon, 5 Sep 2005 19:44:09 +0000 Subject: [PATCH] added test for extension mechanism [SVN r30815] --- test/Jamfile | 1 + test/extension_mechanism.cpp | 110 +++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100755 test/extension_mechanism.cpp diff --git a/test/Jamfile b/test/Jamfile index c80a535..4a8ef6d 100755 --- a/test/Jamfile +++ b/test/Jamfile @@ -35,6 +35,7 @@ test-suite range : [ range-test algorithm_example ] [ range-test reversible_range ] [ range-test const_ranges ] + [ range-test extension_mechanism ] # [ range-test mfc : $(VC71_ROOT)/atlmfc/include ] ; diff --git a/test/extension_mechanism.cpp b/test/extension_mechanism.cpp new file mode 100755 index 0000000..273abb2 --- /dev/null +++ b/test/extension_mechanism.cpp @@ -0,0 +1,110 @@ +// Boost.Range library +// +// Copyright Thorsten Ottosen 2003-2004. Use, modification and +// distribution is 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) +// +// For more information, see http://www.boost.org/libs/range/ +// + + +#include + +#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) +# pragma warn -8091 // supress warning in Boost.Test +# pragma warn -8057 // unused argument argc/argv in Boost.Test +#endif + +#include +#include +#include +#include + +// +// Generic range algorithm +// +template< class Rng > +typename boost::range_result_iterator::type foo_algo( Rng& r ) +{ + // + // This will only compile for Rng = UDT if the qualified calls + // find boost_range_XXX via ADL. + // + return boost::size(r) == 0u ? boost::begin(r) : boost::end(r); +} + +namespace Foo +{ + // + // Our sample UDT + // + struct X + { + typedef std::vector data_t; + typedef data_t::iterator iterator; + typedef data_t::const_iterator const_iterator; + typedef data_t::size_type size_type; + + data_t vec; + + void push_back( int i ) + { vec.push_back(i); } + }; + + // + // The required functions. No type-traits need + // to be defined because X defines the proper set of + // nested types. + // + inline X::iterator boost_range_begin( X& x ) + { + return x.vec.begin(); + } + + inline X::const_iterator boost_range_begin( const X& x ) + { + return x.vec.begin(); + } + + inline X::iterator boost_range_end( X& x ) + { + return x.vec.end(); + } + + inline X::const_iterator boost_range_end( const X& x ) + { + return x.vec.end(); + } + + inline X::size_type boost_range_size( const X& x ) + { + return x.vec.size(); + } +} + +void check_extension() +{ + Foo::X x; + x.push_back(3); + const Foo::X x2; + + foo_algo( x ); + foo_algo( x2 ); +} + +using boost::unit_test::test_suite; + +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" ); + + test->add( BOOST_TEST_CASE( &check_extension ) ); + + return test; +} + + + + +