1
0
forked from boostorg/mp11
Files
boost_mp11/test/tuple_for_each.cpp

176 lines
3.5 KiB
C++
Raw Normal View History

2015-06-24 14:57:32 +03:00
2017-12-12 00:34:17 +02:00
// Copyright 2015 Peter Dimov.
2015-06-24 14:57:32 +03:00
//
// 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
2017-12-12 00:34:17 +02:00
#if defined(_MSC_VER)
#pragma warning( disable: 4244 ) // 'initializing': conversion from 'int' to 'char', possible loss of data
#endif
2017-06-08 16:35:55 +03:00
#include <boost/mp11/tuple.hpp>
2015-06-24 14:57:32 +03:00
#include <boost/core/lightweight_test.hpp>
#include <tuple>
#include <memory>
#include <utility>
#include <array>
int main()
{
using boost::mp11::tuple_for_each;
2015-06-24 14:57:32 +03:00
{
std::tuple<int, short, char> tp{ 1, 2, 3 };
{
int s = 0;
tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
2015-06-24 14:57:32 +03:00
BOOST_TEST_EQ( s, 123 );
}
{
int s = 0;
tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
2015-06-24 14:57:32 +03:00
BOOST_TEST_EQ( s, 123 );
}
}
2017-06-08 17:19:28 +03:00
{
std::tuple<int, short, char> const tp{ 1, 2, 3 };
{
int s = 0;
tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
BOOST_TEST_EQ( s, 123 );
}
{
int s = 0;
tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
BOOST_TEST_EQ( s, 123 );
}
}
#if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 8
#else
2015-06-24 14:57:32 +03:00
{
std::tuple<std::unique_ptr<int>, std::unique_ptr<int>, std::unique_ptr<int>> tp{ std::unique_ptr<int>(new int(1)), std::unique_ptr<int>(new int(2)), std::unique_ptr<int>(new int(3)) };
int s = 0;
tuple_for_each( std::move(tp), [&]( std::unique_ptr<int> p ){ s = s * 10 + *p; } );
2015-06-24 14:57:32 +03:00
BOOST_TEST_EQ( s, 123 );
}
#endif
2015-06-24 14:57:32 +03:00
{
std::pair<int, short> tp{ 1, 2 };
{
int s = 0;
tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
2015-06-24 14:57:32 +03:00
BOOST_TEST_EQ( s, 12 );
}
{
int s = 0;
tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
2015-06-24 14:57:32 +03:00
BOOST_TEST_EQ( s, 12 );
}
}
2017-06-08 17:19:28 +03:00
{
std::pair<int, short> const tp{ 1, 2 };
{
int s = 0;
tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
BOOST_TEST_EQ( s, 12 );
}
{
int s = 0;
tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
BOOST_TEST_EQ( s, 12 );
}
}
2015-06-24 14:57:32 +03:00
{
std::array<int, 3> tp{{ 1, 2, 3 }};
{
int s = 0;
tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
2015-06-24 14:57:32 +03:00
BOOST_TEST_EQ( s, 123 );
}
{
int s = 0;
tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
2015-06-24 14:57:32 +03:00
BOOST_TEST_EQ( s, 123 );
}
}
2017-06-08 17:19:28 +03:00
{
std::array<int, 3> const tp{{ 1, 2, 3 }};
{
int s = 0;
tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
BOOST_TEST_EQ( s, 123 );
}
{
int s = 0;
tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
BOOST_TEST_EQ( s, 123 );
}
}
{
std::tuple<> tp;
BOOST_TEST_EQ( tuple_for_each( tp, 11 ), 11 );
BOOST_TEST_EQ( tuple_for_each( std::move( tp ), 12 ), 12 );
}
{
std::array<int, 0> tp;
BOOST_TEST_EQ( tuple_for_each( tp, 11 ), 11 );
BOOST_TEST_EQ( tuple_for_each( std::move( tp ), 12 ), 12 );
}
2015-06-24 14:57:32 +03:00
return boost::report_errors();
}