diff --git a/include/boost/array.hpp b/include/boost/array.hpp index b290c4b..4b35bf4 100644 --- a/include/boost/array.hpp +++ b/include/boost/array.hpp @@ -437,10 +437,22 @@ namespace boost { return boost::hash_range(arr.begin(), arr.end()); } + template + T &get(boost::array &arr) BOOST_NOEXCEPT { + BOOST_STATIC_ASSERT_MSG ( Idx < N, "boost::get<>(boost::array &) index out of range" ); + return arr[Idx]; + } + + template + const T &get(const boost::array &arr) BOOST_NOEXCEPT { + BOOST_STATIC_ASSERT_MSG ( Idx < N, "boost::get<>(const boost::array &) index out of range" ); + return arr[Idx]; + } + } /* namespace boost */ #ifndef BOOST_NO_CXX11_HDR_ARRAY -// If we don't have std::array, I'm assuming that we don't have std::get +// If we don't have std::array, I'm assuming that we don't have std::get namespace std { template T &get(boost::array &arr) BOOST_NOEXCEPT { diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index f6db6a1..fab405e 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -13,5 +13,7 @@ test-suite array : [ run array5.cpp ] [ run array6.cpp ] [ run array7.cpp ] + [ compile-fail array_getfail1.cpp ] + [ compile-fail array_getfail2.cpp ] [ run array_hash.cpp ] ; diff --git a/test/array6.cpp b/test/array6.cpp index 658cec6..57eb51e 100644 --- a/test/array6.cpp +++ b/test/array6.cpp @@ -10,39 +10,32 @@ #include #include +#include + + namespace { -unsigned int failed_tests = 0; - -void fail_test( const char * reason ) { - ++failed_tests; - std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl; -} - -template< class T > -void RunTests() -{ - typedef boost::array< T, 5 > test_type; - typedef T arr[5]; - test_type test_case; // = { 1, 1, 2, 3, 5 }; + template< class T > + void RunTests() + { + typedef boost::array< T, 5 > test_type; + typedef T arr[5]; + test_type test_case; // = { 1, 1, 2, 3, 5 }; - arr &aRef = get_c_array ( test_case ); - if ( &*test_case.begin () != &aRef[0] ) - fail_test ( "Array6: Same thing not equal?(1)" ); + arr &aRef = get_c_array ( test_case ); + BOOST_CHECK ( &*test_case.begin () == &aRef[0] ); - const arr &caRef = get_c_array ( test_case ); - typename test_type::const_iterator iter = test_case.begin (); - if ( &*iter != &caRef[0] ) - fail_test ( "Array6: Same thing not equal?(2)" ); + const arr &caRef = get_c_array ( test_case ); + typename test_type::const_iterator iter = test_case.begin (); + BOOST_CHECK ( &*iter == &caRef[0] ); + } } -} - -int main() +int test_main( int , char* [] ) { RunTests< bool >(); RunTests< void * >(); RunTests< long double >(); RunTests< std::string >(); - return failed_tests; + return 0; } diff --git a/test/array7.cpp b/test/array7.cpp index 844b09c..b3d99aa 100644 --- a/test/array7.cpp +++ b/test/array7.cpp @@ -1,5 +1,5 @@ -/* tests for using class array<> specialization for size 0 - * (C) Copyright Alisdair Meredith 2006. +/* tests using std::get on boost:array + * (C) Copyright Marshall Clow 2012 * 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) @@ -13,42 +13,55 @@ #include #endif +#include + namespace { -unsigned int failed_tests = 0; -#ifndef BOOST_NO_CXX11_HDR_ARRAY -void fail_test( const char * reason ) { - ++failed_tests; - std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl; -} - -template< class T > -void RunTests() -{ - typedef boost::array< T, 5 > test_type; - typedef T arr[5]; - test_type test_case; // = { 1, 1, 2, 3, 5 }; + #ifndef BOOST_NO_CXX11_HDR_ARRAY + template< class T > + void RunStdTests() + { + typedef boost::array< T, 5 > test_type; + typedef T arr[5]; + test_type test_case; // = { 1, 1, 2, 3, 5 }; - T &aRef = std::get<0> ( test_case ); - if ( &*test_case.begin () != &aRef ) - fail_test ( "Array7: Same thing not equal?(1)" ); + T &aRef = std::get<0> ( test_case ); + BOOST_CHECK ( &*test_case.begin () == &aRef ); - const T &caRef = std::get<0> ( test_case ); - if ( &*test_case.cbegin () != &caRef ) - fail_test ( "Array7: Same thing not equal?(2)" ); -} -#endif + const T &caRef = std::get<0> ( test_case ); + BOOST_CHECK ( &*test_case.cbegin () == &caRef ); + } + #endif + + template< class T > + void RunBoostTests() + { + typedef boost::array< T, 5 > test_type; + typedef T arr[5]; + test_type test_case; // = { 1, 1, 2, 3, 5 }; + + T &aRef = boost::get<0> ( test_case ); + BOOST_CHECK ( &*test_case.begin () == &aRef ); + + const T &caRef = boost::get<0> ( test_case ); + BOOST_CHECK ( &*test_case.cbegin () == &caRef ); + } } -int main() +int test_main( int , char* [] ) { + RunBoostTests< bool >(); + RunBoostTests< void * >(); + RunBoostTests< long double >(); + RunBoostTests< std::string >(); + #ifndef BOOST_NO_CXX11_HDR_ARRAY - RunTests< bool >(); - RunTests< void * >(); - RunTests< long double >(); - RunTests< std::string >(); + RunStdTests< bool >(); + RunStdTests< void * >(); + RunStdTests< long double >(); + RunStdTests< std::string >(); #endif - return failed_tests; + return 0; } diff --git a/test/array_getfail1.cpp b/test/array_getfail1.cpp new file mode 100644 index 0000000..cdae8ed --- /dev/null +++ b/test/array_getfail1.cpp @@ -0,0 +1,51 @@ +/* tests using std::get on boost:array + * (C) Copyright Marshall Clow 2012 + * 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 +#include +#include +#ifndef BOOST_NO_CXX11_HDR_ARRAY +#include +#endif + +#include +#include + + +#include + +namespace { + + #ifndef BOOST_NO_CXX11_HDR_ARRAY + template< class T > + void RunStdTests() + { + typedef boost::array< T, 5 > test_type; + typedef T arr[5]; + test_type test_case; // = { 1, 1, 2, 3, 5 }; + + T &aRef = std::get<5> ( test_case ); // should fail to compile + BOOST_CHECK ( &*test_case.begin () == &aRef ); + } + #endif + +} + +int test_main( int , char* [] ) +{ + +#ifndef BOOST_NO_CXX11_HDR_ARRAY + RunStdTests< bool >(); + RunStdTests< void * >(); + RunStdTests< long double >(); + RunStdTests< std::string >(); +#else + BOOST_STATIC_ASSERT ( false ); // fail on C++03 systems. +#endif + return 0; +} + diff --git a/test/array_getfail2.cpp b/test/array_getfail2.cpp new file mode 100644 index 0000000..0b49510 --- /dev/null +++ b/test/array_getfail2.cpp @@ -0,0 +1,64 @@ +/* tests using std::get on boost:array + * (C) Copyright Marshall Clow 2012 + * 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 +#include +#include +#include +#ifndef BOOST_NO_CXX11_HDR_ARRAY +#include +#endif + +#include + +namespace { + + #ifndef BOOST_NO_CXX11_HDR_ARRAY + template< class T > + void RunStdTests() + { + typedef boost::array< T, 5 > test_type; + typedef T arr[5]; + test_type test_case; // = { 1, 1, 2, 3, 5 }; + + T &aRef = std::get<0> ( test_case ); + BOOST_CHECK ( &*test_case.begin () == &aRef ); + + const T &caRef = std::get<0> ( test_case ); + BOOST_CHECK ( &*test_case.cbegin () == &caRef ); + } + #endif + + template< class T > + void RunBoostTests() + { + typedef boost::array< T, 5 > test_type; + typedef T arr[5]; + test_type test_case; // = { 1, 1, 2, 3, 5 }; + + T &aRef = boost::get<5> ( test_case ); + BOOST_CHECK ( &*test_case.begin () == &aRef ); + } + +} + +int test_main( int , char* [] ) +{ + RunBoostTests< bool >(); + RunBoostTests< void * >(); + RunBoostTests< long double >(); + RunBoostTests< std::string >(); + +#ifndef BOOST_NO_CXX11_HDR_ARRAY + RunStdTests< bool >(); + RunStdTests< void * >(); + RunStdTests< long double >(); + RunStdTests< std::string >(); +#endif + return 0; +} + diff --git a/test/array_hash.cpp b/test/array_hash.cpp index 474e29c..0859296 100644 --- a/test/array_hash.cpp +++ b/test/array_hash.cpp @@ -11,39 +11,34 @@ #include #include +#include + namespace { -unsigned int failed_tests = 0; -void fail_test( const char * reason ) { - ++failed_tests; - std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl; -} - -template< class T > -void RunTests() -{ -// std::size_t hash0 = boost::hash > () ( boost::array ()); -// std::size_t hash1 = boost::hash > () ( boost::array ()); + template< class T > + void RunTests() + { + // std::size_t hash0 = boost::hash > () ( boost::array ()); + // std::size_t hash1 = boost::hash > () ( boost::array ()); - typedef boost::array< T, 5 > barr; - typedef T arr[5]; - barr test_barr = {{ 1, 1, 2, 3, 5 }}; - arr test_arr = { 1, 1, 2, 3, 5 }; + typedef boost::array< T, 5 > barr; + typedef T arr[5]; + barr test_barr = {{ 1, 1, 2, 3, 5 }}; + arr test_arr = { 1, 1, 2, 3, 5 }; - std::size_t bhash = boost::hash () ( test_barr ); - std::size_t ahash = boost::hash () ( test_arr ); - if ( ahash != bhash ) - fail_test ( "Array_hash: Hash-mismatch on " ); -} + std::size_t bhash = boost::hash () ( test_barr ); + std::size_t ahash = boost::hash () ( test_arr ); + BOOST_CHECK ( ahash == bhash ); + } } -int main() +int test_main( int , char* [] ) { RunTests< int >(); RunTests< long >(); RunTests< long double >(); - return failed_tests; + return 0; }