|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r82089 - in trunk: boost libs/array/test
From: marshall_at_[hidden]
Date: 2012-12-18 19:53:32
Author: marshall
Date: 2012-12-18 19:53:31 EST (Tue, 18 Dec 2012)
New Revision: 82089
URL: http://svn.boost.org/trac/boost/changeset/82089
Log:
Add boost::get<> support to Boost.Array
Added:
trunk/libs/array/test/array_getfail1.cpp (contents, props changed)
trunk/libs/array/test/array_getfail2.cpp (contents, props changed)
Text files modified:
trunk/boost/array.hpp | 14 +++++++
trunk/libs/array/test/Jamfile.v2 | 2 +
trunk/libs/array/test/array6.cpp | 39 +++++++++------------
trunk/libs/array/test/array7.cpp | 71 +++++++++++++++++++++++----------------
trunk/libs/array/test/array_hash.cpp | 39 +++++++++------------
5 files changed, 90 insertions(+), 75 deletions(-)
Modified: trunk/boost/array.hpp
==============================================================================
--- trunk/boost/array.hpp (original)
+++ trunk/boost/array.hpp 2012-12-18 19:53:31 EST (Tue, 18 Dec 2012)
@@ -437,10 +437,22 @@
return boost::hash_range(arr.begin(), arr.end());
}
+ template <size_t Idx, typename T, size_t N>
+ T &get(boost::array<T,N> &arr) BOOST_NOEXCEPT {
+ BOOST_STATIC_ASSERT_MSG ( Idx < N, "boost::get<>(boost::array &) index out of range" );
+ return arr[Idx];
+ }
+
+ template <size_t Idx, typename T, size_t N>
+ const T &get(const boost::array<T,N> &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 <size_t Idx, typename T, size_t N>
T &get(boost::array<T,N> &arr) BOOST_NOEXCEPT {
Modified: trunk/libs/array/test/Jamfile.v2
==============================================================================
--- trunk/libs/array/test/Jamfile.v2 (original)
+++ trunk/libs/array/test/Jamfile.v2 2012-12-18 19:53:31 EST (Tue, 18 Dec 2012)
@@ -13,5 +13,7 @@
[ run array5.cpp ]
[ run array6.cpp ]
[ run array7.cpp ]
+ [ compile-fail array_getfail1.cpp ]
+ [ compile-fail array_getfail2.cpp ]
[ run array_hash.cpp ]
;
Modified: trunk/libs/array/test/array6.cpp
==============================================================================
--- trunk/libs/array/test/array6.cpp (original)
+++ trunk/libs/array/test/array6.cpp 2012-12-18 19:53:31 EST (Tue, 18 Dec 2012)
@@ -10,39 +10,32 @@
#include <boost/array.hpp>
#include <algorithm>
-namespace {
-unsigned int failed_tests = 0;
+#include <boost/test/included/test_exec_monitor.hpp>
-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 };
+namespace {
+ 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;
}
Modified: trunk/libs/array/test/array7.cpp
==============================================================================
--- trunk/libs/array/test/array7.cpp (original)
+++ trunk/libs/array/test/array7.cpp 2012-12-18 19:53:31 EST (Tue, 18 Dec 2012)
@@ -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 <array>
#endif
-namespace {
-unsigned int failed_tests = 0;
+#include <boost/test/included/test_exec_monitor.hpp>
-#ifndef BOOST_NO_CXX11_HDR_ARRAY
-void fail_test( const char * reason ) {
- ++failed_tests;
- std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl;
-}
+namespace {
-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;
}
Added: trunk/libs/array/test/array_getfail1.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/array/test/array_getfail1.cpp 2012-12-18 19:53:31 EST (Tue, 18 Dec 2012)
@@ -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 <string>
+#include <iostream>
+#include <algorithm>
+#ifndef BOOST_NO_CXX11_HDR_ARRAY
+#include <array>
+#endif
+
+#include <boost/array.hpp>
+#include <boost/static_assert.hpp>
+
+
+#include <boost/test/included/test_exec_monitor.hpp>
+
+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;
+}
+
Added: trunk/libs/array/test/array_getfail2.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/array/test/array_getfail2.cpp 2012-12-18 19:53:31 EST (Tue, 18 Dec 2012)
@@ -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 <string>
+#include <iostream>
+#include <boost/array.hpp>
+#include <algorithm>
+#ifndef BOOST_NO_CXX11_HDR_ARRAY
+#include <array>
+#endif
+
+#include <boost/test/included/test_exec_monitor.hpp>
+
+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;
+}
+
Modified: trunk/libs/array/test/array_hash.cpp
==============================================================================
--- trunk/libs/array/test/array_hash.cpp (original)
+++ trunk/libs/array/test/array_hash.cpp 2012-12-18 19:53:31 EST (Tue, 18 Dec 2012)
@@ -11,39 +11,34 @@
#include <algorithm>
#include <boost/functional/hash.hpp>
-namespace {
-unsigned int failed_tests = 0;
+#include <boost/test/included/test_exec_monitor.hpp>
-void fail_test( const char * reason ) {
- ++failed_tests;
- std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl;
-}
+namespace {
-template< class T >
-void RunTests()
-{
-// std::size_t hash0 = boost::hash<boost::array<T,0> > () ( boost::array<T, 0> ());
-// std::size_t hash1 = boost::hash<boost::array<T,1> > () ( boost::array<T, 1> ());
+ template< class T >
+ void RunTests()
+ {
+ // std::size_t hash0 = boost::hash<boost::array<T,0> > () ( boost::array<T, 0> ());
+ // std::size_t hash1 = boost::hash<boost::array<T,1> > () ( boost::array<T, 1> ());
- 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<barr> () ( test_barr );
- std::size_t ahash = boost::hash<arr> () ( test_arr );
- if ( ahash != bhash )
- fail_test ( "Array_hash: Hash-mismatch on " );
-}
+ std::size_t bhash = boost::hash<barr> () ( test_barr );
+ std::size_t ahash = boost::hash<arr> () ( 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;
}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk