forked from boostorg/tuple
Make boost::tuple tuple-like to support structured bindings
This commit is contained in:
@@ -5,4 +5,7 @@ test-suite tuple :
|
||||
[ run tuple_test_bench.cpp ]
|
||||
[ run io_test.cpp ]
|
||||
[ run another_tuple_test_bench.cpp ]
|
||||
[ run std_tuple_size.cpp ]
|
||||
[ run std_tuple_element.cpp ]
|
||||
[ run structured_bindings.cpp ]
|
||||
;
|
||||
|
56
test/std_tuple_element.cpp
Normal file
56
test/std_tuple_element.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
// Copyright 2017 Peter Dimov.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/core/lightweight_test_trait.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/config/pragma_message.hpp>
|
||||
|
||||
#if defined(BOOST_NO_CXX11_HDR_TUPLE)
|
||||
|
||||
BOOST_PRAGMA_MESSAGE("Skipping std::tuple_element tests for lack of <tuple>")
|
||||
int main() {}
|
||||
|
||||
#else
|
||||
|
||||
#include <tuple>
|
||||
|
||||
template<class Tp, std::size_t I, class E> void test()
|
||||
{
|
||||
BOOST_TEST_TRAIT_TRUE((boost::is_same<typename std::tuple_element<I, Tp>::type, E>));
|
||||
|
||||
typedef typename Tp::inherited Tp2;
|
||||
BOOST_TEST_TRAIT_TRUE((boost::is_same<typename std::tuple_element<I, Tp2>::type, E>));
|
||||
}
|
||||
|
||||
template<int> struct X
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
test<boost::tuple<X<0> const>, 0, X<0> const>();
|
||||
|
||||
test<boost::tuple<X<0> const, X<1> const>, 0, X<0> const>();
|
||||
test<boost::tuple<X<0> const, X<1> const>, 1, X<1> const>();
|
||||
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const>, 0, X<0> const>();
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const>, 1, X<1> const>();
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const>, 2, X<2> const>();
|
||||
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const, X<3> const>, 0, X<0> const>();
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const, X<3> const>, 1, X<1> const>();
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const, X<3> const>, 2, X<2> const>();
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const, X<3> const>, 3, X<3> const>();
|
||||
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const, X<3> const, X<4> const>, 0, X<0> const>();
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const, X<3> const, X<4> const>, 1, X<1> const>();
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const, X<3> const, X<4> const>, 2, X<2> const>();
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const, X<3> const, X<4> const>, 3, X<3> const>();
|
||||
test<boost::tuple<X<0> const, X<1> const, X<2> const, X<3> const, X<4> const>, 4, X<4> const>();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#endif
|
61
test/std_tuple_size.cpp
Normal file
61
test/std_tuple_size.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2017 Peter Dimov.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/config/pragma_message.hpp>
|
||||
|
||||
#if defined(BOOST_NO_CXX11_HDR_TUPLE)
|
||||
|
||||
BOOST_PRAGMA_MESSAGE("Skipping std::tuple_size tests for lack of <tuple>")
|
||||
int main() {}
|
||||
|
||||
#else
|
||||
|
||||
#include <tuple>
|
||||
|
||||
template<class Tp> void test( std::size_t x )
|
||||
{
|
||||
BOOST_TEST_EQ( std::tuple_size< Tp >::value, x );
|
||||
BOOST_TEST_EQ( std::tuple_size< typename Tp::inherited >::value, x );
|
||||
}
|
||||
|
||||
struct V
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
test< boost::tuple<> >( 0 );
|
||||
test< boost::tuple<V> >( 1 );
|
||||
test< boost::tuple<V, V> >( 2 );
|
||||
test< boost::tuple<V, V, V> >( 3 );
|
||||
test< boost::tuple<V, V, V, V> >( 4 );
|
||||
test< boost::tuple<V, V, V, V, V> >( 5 );
|
||||
test< boost::tuple<V, V, V, V, V, V> >( 6 );
|
||||
test< boost::tuple<V, V, V, V, V, V, V> >( 7 );
|
||||
test< boost::tuple<V, V, V, V, V, V, V, V> >( 8 );
|
||||
test< boost::tuple<V, V, V, V, V, V, V, V, V> >( 9 );
|
||||
test< boost::tuple<V, V, V, V, V, V, V, V, V, V> >( 10 );
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_DECLTYPE)
|
||||
|
||||
BOOST_TEST_EQ( std::tuple_size<decltype(boost::make_tuple())>::value, 0 );
|
||||
BOOST_TEST_EQ( std::tuple_size<decltype(boost::make_tuple(1))>::value, 1 );
|
||||
BOOST_TEST_EQ( std::tuple_size<decltype(boost::make_tuple(1, 2))>::value, 2 );
|
||||
BOOST_TEST_EQ( std::tuple_size<decltype(boost::make_tuple(1, 2, 3))>::value, 3 );
|
||||
BOOST_TEST_EQ( std::tuple_size<decltype(boost::make_tuple(1, 2, 3, 4))>::value, 4 );
|
||||
BOOST_TEST_EQ( std::tuple_size<decltype(boost::make_tuple(1, 2, 3, 4, 5))>::value, 5 );
|
||||
BOOST_TEST_EQ( std::tuple_size<decltype(boost::make_tuple(1, 2, 3, 4, 5, 6))>::value, 6 );
|
||||
BOOST_TEST_EQ( std::tuple_size<decltype(boost::make_tuple(1, 2, 3, 4, 5, 6, 7))>::value, 7 );
|
||||
BOOST_TEST_EQ( std::tuple_size<decltype(boost::make_tuple(1, 2, 3, 4, 5, 6, 7, 8))>::value, 8 );
|
||||
BOOST_TEST_EQ( std::tuple_size<decltype(boost::make_tuple(1, 2, 3, 4, 5, 6, 7, 8, 9))>::value, 9 );
|
||||
BOOST_TEST_EQ( std::tuple_size<decltype(boost::make_tuple(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))>::value, 10 );
|
||||
|
||||
#endif
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#endif
|
77
test/structured_bindings.cpp
Normal file
77
test/structured_bindings.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
// Copyright 2017 Peter Dimov.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/config/pragma_message.hpp>
|
||||
|
||||
#if defined(BOOST_NO_CXX17_STRUCTURED_BINDINGS)
|
||||
|
||||
BOOST_PRAGMA_MESSAGE("Skipping structured bindings test, not supported")
|
||||
int main() {}
|
||||
|
||||
#else
|
||||
|
||||
int main()
|
||||
{
|
||||
// make_tuple
|
||||
|
||||
{
|
||||
auto [x1] = boost::make_tuple( 1 );
|
||||
BOOST_TEST_EQ( x1, 1 );
|
||||
}
|
||||
|
||||
{
|
||||
auto [x1, x2] = boost::make_tuple( 1, 2 );
|
||||
BOOST_TEST_EQ( x1, 1 );
|
||||
BOOST_TEST_EQ( x2, 2 );
|
||||
}
|
||||
|
||||
{
|
||||
auto [x1, x2, x3] = boost::make_tuple( 1, 2, 3 );
|
||||
BOOST_TEST_EQ( x1, 1 );
|
||||
BOOST_TEST_EQ( x2, 2 );
|
||||
BOOST_TEST_EQ( x3, 3 );
|
||||
}
|
||||
|
||||
{
|
||||
auto [x1, x2, x3, x4] = boost::make_tuple( 1, 2, 3, 4 );
|
||||
BOOST_TEST_EQ( x1, 1 );
|
||||
BOOST_TEST_EQ( x2, 2 );
|
||||
BOOST_TEST_EQ( x3, 3 );
|
||||
BOOST_TEST_EQ( x4, 4 );
|
||||
}
|
||||
|
||||
// tuple
|
||||
|
||||
{
|
||||
auto [x1] = boost::tuple<int>( 1 );
|
||||
BOOST_TEST_EQ( x1, 1 );
|
||||
}
|
||||
|
||||
{
|
||||
auto [x1, x2] = boost::tuple<int, int>( 1, 2 );
|
||||
BOOST_TEST_EQ( x1, 1 );
|
||||
BOOST_TEST_EQ( x2, 2 );
|
||||
}
|
||||
|
||||
{
|
||||
auto [x1, x2, x3] = boost::tuple<int, int, int>( 1, 2, 3 );
|
||||
BOOST_TEST_EQ( x1, 1 );
|
||||
BOOST_TEST_EQ( x2, 2 );
|
||||
BOOST_TEST_EQ( x3, 3 );
|
||||
}
|
||||
|
||||
{
|
||||
auto [x1, x2, x3, x4] = boost::tuple<int, int, int, int>( 1, 2, 3, 4 );
|
||||
BOOST_TEST_EQ( x1, 1 );
|
||||
BOOST_TEST_EQ( x2, 2 );
|
||||
BOOST_TEST_EQ( x3, 3 );
|
||||
BOOST_TEST_EQ( x4, 4 );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user