From e6918187568dbd01842d8d1d2c808ce16a894239 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 13:54:28 +0200 Subject: Adding upstream version 18.2.2. Signed-off-by: Daniel Baumann --- src/boost/libs/variant2/test/CMakeLists.txt | 11 + src/boost/libs/variant2/test/Jamfile | 128 ++++++++ .../test/cmake_install_test/CMakeLists.txt | 17 + .../variant2/test/cmake_subdir_test/CMakeLists.txt | 21 ++ .../libs/variant2/test/is_output_streamable.cpp | 41 +++ src/boost/libs/variant2/test/quick.cpp | 13 + src/boost/libs/variant2/test/throw_exception.cpp | 20 ++ .../libs/variant2/test/variant_alternative.cpp | 154 +++++++++ .../variant2/test/variant_convert_construct.cpp | 175 ++++++++++ .../test/variant_convert_construct_throw.cpp | 95 ++++++ .../libs/variant2/test/variant_copy_assign.cpp | 194 +++++++++++ .../libs/variant2/test/variant_copy_assign_cx.cpp | 116 +++++++ .../variant2/test/variant_copy_assign_throw.cpp | 55 ++++ .../libs/variant2/test/variant_copy_construct.cpp | 162 ++++++++++ .../variant2/test/variant_copy_construct_cx.cpp | 112 +++++++ .../variant2/test/variant_copy_construct_throw.cpp | 69 ++++ .../variant2/test/variant_default_construct.cpp | 103 ++++++ .../variant2/test/variant_default_construct_cx.cpp | 51 +++ .../variant2/test/variant_derived_construct.cpp | 33 ++ src/boost/libs/variant2/test/variant_destroy.cpp | 205 ++++++++++++ .../libs/variant2/test/variant_emplace_index.cpp | 292 +++++++++++++++++ .../variant2/test/variant_emplace_index_cx.cpp | 136 ++++++++ .../libs/variant2/test/variant_emplace_type.cpp | 194 +++++++++++ .../libs/variant2/test/variant_emplace_type_cx.cpp | 91 ++++++ src/boost/libs/variant2/test/variant_eq_ne.cpp | 99 ++++++ src/boost/libs/variant2/test/variant_eq_ne_cx.cpp | 105 ++++++ .../libs/variant2/test/variant_get_by_index.cpp | 355 +++++++++++++++++++++ .../libs/variant2/test/variant_get_by_index_cx.cpp | 96 ++++++ .../libs/variant2/test/variant_get_by_type.cpp | 313 ++++++++++++++++++ .../libs/variant2/test/variant_get_by_type_cx.cpp | 90 ++++++ src/boost/libs/variant2/test/variant_hash.cpp | 86 +++++ .../variant2/test/variant_holds_alternative.cpp | 78 +++++ .../variant2/test/variant_holds_alternative_cx.cpp | 43 +++ .../test/variant_in_place_index_construct.cpp | 138 ++++++++ .../test/variant_in_place_index_construct_cx.cpp | 121 +++++++ .../test/variant_in_place_type_construct.cpp | 134 ++++++++ .../test/variant_in_place_type_construct_cx.cpp | 120 +++++++ src/boost/libs/variant2/test/variant_lt_gt.cpp | 94 ++++++ src/boost/libs/variant2/test/variant_lt_gt_cx.cpp | 100 ++++++ .../libs/variant2/test/variant_many_types.cpp | 109 +++++++ .../libs/variant2/test/variant_move_assign.cpp | 194 +++++++++++ .../libs/variant2/test/variant_move_assign_cx.cpp | 107 +++++++ .../variant2/test/variant_move_assign_throw.cpp | 55 ++++ .../libs/variant2/test/variant_move_construct.cpp | 146 +++++++++ .../variant2/test/variant_move_construct_cx.cpp | 104 ++++++ .../variant2/test/variant_move_construct_throw.cpp | 69 ++++ .../libs/variant2/test/variant_ostream_insert.cpp | 43 +++ src/boost/libs/variant2/test/variant_size.cpp | 104 ++++++ src/boost/libs/variant2/test/variant_special.cpp | 103 ++++++ src/boost/libs/variant2/test/variant_subset.cpp | 159 +++++++++ src/boost/libs/variant2/test/variant_swap.cpp | 259 +++++++++++++++ src/boost/libs/variant2/test/variant_trivial.cpp | 95 ++++++ .../libs/variant2/test/variant_value_assign.cpp | 206 ++++++++++++ .../libs/variant2/test/variant_value_assign_cx.cpp | 106 ++++++ .../libs/variant2/test/variant_value_construct.cpp | 124 +++++++ .../variant2/test/variant_value_construct_cx.cpp | 109 +++++++ src/boost/libs/variant2/test/variant_valueless.cpp | 260 +++++++++++++++ src/boost/libs/variant2/test/variant_visit.cpp | 130 ++++++++ .../libs/variant2/test/variant_visit_by_index.cpp | 126 ++++++++ .../libs/variant2/test/variant_visit_derived.cpp | 56 ++++ src/boost/libs/variant2/test/variant_visit_r.cpp | 98 ++++++ 61 files changed, 7222 insertions(+) create mode 100644 src/boost/libs/variant2/test/CMakeLists.txt create mode 100644 src/boost/libs/variant2/test/Jamfile create mode 100644 src/boost/libs/variant2/test/cmake_install_test/CMakeLists.txt create mode 100644 src/boost/libs/variant2/test/cmake_subdir_test/CMakeLists.txt create mode 100644 src/boost/libs/variant2/test/is_output_streamable.cpp create mode 100644 src/boost/libs/variant2/test/quick.cpp create mode 100644 src/boost/libs/variant2/test/throw_exception.cpp create mode 100644 src/boost/libs/variant2/test/variant_alternative.cpp create mode 100644 src/boost/libs/variant2/test/variant_convert_construct.cpp create mode 100644 src/boost/libs/variant2/test/variant_convert_construct_throw.cpp create mode 100644 src/boost/libs/variant2/test/variant_copy_assign.cpp create mode 100644 src/boost/libs/variant2/test/variant_copy_assign_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_copy_assign_throw.cpp create mode 100644 src/boost/libs/variant2/test/variant_copy_construct.cpp create mode 100644 src/boost/libs/variant2/test/variant_copy_construct_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_copy_construct_throw.cpp create mode 100644 src/boost/libs/variant2/test/variant_default_construct.cpp create mode 100644 src/boost/libs/variant2/test/variant_default_construct_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_derived_construct.cpp create mode 100644 src/boost/libs/variant2/test/variant_destroy.cpp create mode 100644 src/boost/libs/variant2/test/variant_emplace_index.cpp create mode 100644 src/boost/libs/variant2/test/variant_emplace_index_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_emplace_type.cpp create mode 100644 src/boost/libs/variant2/test/variant_emplace_type_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_eq_ne.cpp create mode 100644 src/boost/libs/variant2/test/variant_eq_ne_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_get_by_index.cpp create mode 100644 src/boost/libs/variant2/test/variant_get_by_index_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_get_by_type.cpp create mode 100644 src/boost/libs/variant2/test/variant_get_by_type_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_hash.cpp create mode 100644 src/boost/libs/variant2/test/variant_holds_alternative.cpp create mode 100644 src/boost/libs/variant2/test/variant_holds_alternative_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_in_place_index_construct.cpp create mode 100644 src/boost/libs/variant2/test/variant_in_place_index_construct_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_in_place_type_construct.cpp create mode 100644 src/boost/libs/variant2/test/variant_in_place_type_construct_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_lt_gt.cpp create mode 100644 src/boost/libs/variant2/test/variant_lt_gt_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_many_types.cpp create mode 100644 src/boost/libs/variant2/test/variant_move_assign.cpp create mode 100644 src/boost/libs/variant2/test/variant_move_assign_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_move_assign_throw.cpp create mode 100644 src/boost/libs/variant2/test/variant_move_construct.cpp create mode 100644 src/boost/libs/variant2/test/variant_move_construct_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_move_construct_throw.cpp create mode 100644 src/boost/libs/variant2/test/variant_ostream_insert.cpp create mode 100644 src/boost/libs/variant2/test/variant_size.cpp create mode 100644 src/boost/libs/variant2/test/variant_special.cpp create mode 100644 src/boost/libs/variant2/test/variant_subset.cpp create mode 100644 src/boost/libs/variant2/test/variant_swap.cpp create mode 100644 src/boost/libs/variant2/test/variant_trivial.cpp create mode 100644 src/boost/libs/variant2/test/variant_value_assign.cpp create mode 100644 src/boost/libs/variant2/test/variant_value_assign_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_value_construct.cpp create mode 100644 src/boost/libs/variant2/test/variant_value_construct_cx.cpp create mode 100644 src/boost/libs/variant2/test/variant_valueless.cpp create mode 100644 src/boost/libs/variant2/test/variant_visit.cpp create mode 100644 src/boost/libs/variant2/test/variant_visit_by_index.cpp create mode 100644 src/boost/libs/variant2/test/variant_visit_derived.cpp create mode 100644 src/boost/libs/variant2/test/variant_visit_r.cpp (limited to 'src/boost/libs/variant2/test') diff --git a/src/boost/libs/variant2/test/CMakeLists.txt b/src/boost/libs/variant2/test/CMakeLists.txt new file mode 100644 index 000000000..0c317a246 --- /dev/null +++ b/src/boost/libs/variant2/test/CMakeLists.txt @@ -0,0 +1,11 @@ +# Copyright 2018, 2019 Peter Dimov +# 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(BoostTestJamfile OPTIONAL RESULT_VARIABLE HAVE_BOOST_TEST) + +if(HAVE_BOOST_TEST) + +boost_test_jamfile(FILE Jamfile LINK_LIBRARIES Boost::variant2 Boost::core Boost::container_hash) + +endif() diff --git a/src/boost/libs/variant2/test/Jamfile b/src/boost/libs/variant2/test/Jamfile new file mode 100644 index 000000000..2fbb975f1 --- /dev/null +++ b/src/boost/libs/variant2/test/Jamfile @@ -0,0 +1,128 @@ +# Boost.Variant2 Library Test Jamfile +# +# Copyright 2015-2019 Peter Dimov +# +# 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 + +import testing ; +import ../../config/checks/config : requires ; + +project + : default-build + + extra + + : requirements + + [ requires cxx11_variadic_templates cxx11_template_aliases cxx11_decltype cxx11_constexpr ] + + msvc:on + gcc:on + clang:on + ; + +run quick.cpp ; + +run variant_size.cpp ; +run variant_alternative.cpp ; + +run variant_holds_alternative.cpp ; +compile variant_holds_alternative_cx.cpp : msvc-14.0:no ; + +run variant_get_by_index.cpp ; +compile variant_get_by_index_cx.cpp : msvc-14.0:no ; + +run variant_get_by_type.cpp ; +compile variant_get_by_type_cx.cpp : msvc-14.0:no ; + +run variant_default_construct.cpp ; +compile variant_default_construct_cx.cpp : msvc-14.0:no ; + +run variant_copy_construct.cpp ; +compile variant_copy_construct_cx.cpp : msvc-14.0:no ; + +run variant_move_construct.cpp ; +compile variant_move_construct_cx.cpp : [ requires cxx14_constexpr ] ; + +run variant_value_construct.cpp ; +compile variant_value_construct_cx.cpp : msvc-14.0:no ; + +run variant_in_place_index_construct.cpp ; +compile variant_in_place_index_construct_cx.cpp : msvc-14.0:no ; + +run variant_in_place_type_construct.cpp ; +compile variant_in_place_type_construct_cx.cpp : msvc-14.0:no ; + +run variant_copy_assign.cpp ; +compile variant_copy_assign_cx.cpp : [ requires cxx14_constexpr ] ; + +run variant_move_assign.cpp ; +compile variant_move_assign_cx.cpp : [ requires cxx14_constexpr ] ; + +run variant_value_assign.cpp ; +compile variant_value_assign_cx.cpp : [ requires cxx14_constexpr ] ; + +run variant_emplace_index.cpp ; +compile variant_emplace_index_cx.cpp : [ requires cxx14_constexpr ] ; + +run variant_emplace_type.cpp ; +compile variant_emplace_type_cx.cpp : [ requires cxx14_constexpr ] ; + +run variant_swap.cpp ; + +run variant_eq_ne.cpp ; +compile variant_eq_ne_cx.cpp : [ requires cxx14_constexpr ] ; + +run variant_lt_gt.cpp ; +compile variant_lt_gt_cx.cpp : [ requires cxx14_constexpr ] ; + +run variant_destroy.cpp ; +run variant_visit.cpp ; + +run variant_convert_construct.cpp ; +run variant_subset.cpp ; +run variant_valueless.cpp ; + +run variant_copy_construct_throw.cpp ; +run variant_move_construct_throw.cpp ; +run variant_convert_construct_throw.cpp ; + +run variant_copy_assign_throw.cpp ; +run variant_move_assign_throw.cpp ; + +local NX = + off + msvc:/wd4530 + msvc:/wd4577 + ; + +run variant_get_by_index.cpp throw_exception.cpp : : : $(NX) : variant_get_by_index_nx ; +compile variant_get_by_index_cx.cpp : $(NX) msvc-14.0:no : variant_get_by_index_cx_nx ; + +run variant_get_by_type.cpp throw_exception.cpp : : : $(NX) : variant_get_by_type_nx ; +compile variant_get_by_type_cx.cpp : $(NX) msvc-14.0:no : variant_get_by_type_cx_nx ; + +run variant_subset.cpp throw_exception.cpp : : : $(NX) : variant_subset_nx ; + +run variant_hash.cpp ; + +run variant_trivial.cpp ; +run variant_special.cpp ; + +run variant_visit_derived.cpp ; + +run variant_many_types.cpp ; + +run variant_visit_r.cpp : : : + gcc,windows:release + gcc,cygwin:release + ; + +compile variant_derived_construct.cpp ; + +run variant_visit_by_index.cpp ; + +run variant_ostream_insert.cpp ; +run is_output_streamable.cpp ; diff --git a/src/boost/libs/variant2/test/cmake_install_test/CMakeLists.txt b/src/boost/libs/variant2/test/cmake_install_test/CMakeLists.txt new file mode 100644 index 000000000..62a1176e8 --- /dev/null +++ b/src/boost/libs/variant2/test/cmake_install_test/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright 2018, 2019 Peter Dimov +# 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 + +cmake_minimum_required(VERSION 3.5...3.16) + +project(cmake_install_test LANGUAGES CXX) + +find_package(boost_variant2 REQUIRED) + +add_executable(quick ../quick.cpp) +target_link_libraries(quick Boost::variant2) + +enable_testing() +add_test(quick quick) + +add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C $) diff --git a/src/boost/libs/variant2/test/cmake_subdir_test/CMakeLists.txt b/src/boost/libs/variant2/test/cmake_subdir_test/CMakeLists.txt new file mode 100644 index 000000000..036030b83 --- /dev/null +++ b/src/boost/libs/variant2/test/cmake_subdir_test/CMakeLists.txt @@ -0,0 +1,21 @@ +# Copyright 2018, 2019 Peter Dimov +# 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 + +cmake_minimum_required(VERSION 3.5...3.16) + +project(cmake_subdir_test LANGUAGES CXX) + +add_subdirectory(../.. boostorg/variant2) + +add_subdirectory(../../../assert boostorg/assert) +add_subdirectory(../../../config boostorg/config) +add_subdirectory(../../../mp11 boostorg/mp11) + +add_executable(quick ../quick.cpp) +target_link_libraries(quick Boost::variant2) + +enable_testing() +add_test(quick quick) + +add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C $) diff --git a/src/boost/libs/variant2/test/is_output_streamable.cpp b/src/boost/libs/variant2/test/is_output_streamable.cpp new file mode 100644 index 000000000..1b0f2fe3e --- /dev/null +++ b/src/boost/libs/variant2/test/is_output_streamable.cpp @@ -0,0 +1,41 @@ +// Copyright 2021 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include + +struct X +{ +}; + +struct Y +{ +}; + +std::ostream& operator<<( std::ostream& os, Y const& /*y*/ ) +{ + os << "Y()"; + return os; +} + +int main() +{ + BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable)); + BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable)); + BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable)); + BOOST_TEST_TRAIT_FALSE((boost::variant2::detail::is_output_streamable)); + BOOST_TEST_TRAIT_FALSE((boost::variant2::detail::is_output_streamable)); + BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable)); + + BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable)); + + BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable>)); + BOOST_TEST_TRAIT_FALSE((boost::variant2::detail::is_output_streamable>)); + BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable>)); + BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable>)); + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/quick.cpp b/src/boost/libs/variant2/test/quick.cpp new file mode 100644 index 000000000..2ef2ca74d --- /dev/null +++ b/src/boost/libs/variant2/test/quick.cpp @@ -0,0 +1,13 @@ +// Copyright 2019 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include + +using namespace boost::variant2; + +int main() +{ + variant v( 2 ); + return get<1>( v ) == 2? 0: 1; +} diff --git a/src/boost/libs/variant2/test/throw_exception.cpp b/src/boost/libs/variant2/test/throw_exception.cpp new file mode 100644 index 000000000..9eaccc119 --- /dev/null +++ b/src/boost/libs/variant2/test/throw_exception.cpp @@ -0,0 +1,20 @@ + +// Copyright 2019 Peter Dimov. +// +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +namespace boost +{ + +void throw_exception( std::exception const & e ) +{ + std::fprintf( stderr, "Exception: %s\n", e.what() ); + std::terminate(); +} + +} // namespace boost diff --git a/src/boost/libs/variant2/test/variant_alternative.cpp b/src/boost/libs/variant2/test/variant_alternative.cpp new file mode 100644 index 000000000..600c26c99 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_alternative.cpp @@ -0,0 +1,154 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; +using namespace boost::mp11; + +template using var_alt_t = variant_alternative_t; + +int main() +{ + BOOST_TEST_TRAIT_TRUE((std::is_same>, void>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const>, void const>)); + BOOST_TEST_TRAIT_TRUE((std::is_same volatile>, void volatile>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const volatile>, void const volatile>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same&>, char&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&>, char const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same&&>, char&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&&>, char const&&>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same>, void>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const>, void const>)); + BOOST_TEST_TRAIT_TRUE((std::is_same volatile>, void volatile>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const volatile>, void const volatile>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same&>, char&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&>, char const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same&&>, char&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&&>, char const&&>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same>, int>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const>, int const>)); + BOOST_TEST_TRAIT_TRUE((std::is_same volatile>, int volatile>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const volatile>, int const volatile>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same&>, int&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&>, int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same&&>, int&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&&>, int const&&>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same>, void>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const>, void const>)); + BOOST_TEST_TRAIT_TRUE((std::is_same volatile>, void volatile>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const volatile>, void const volatile>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same&>, char&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&>, char const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same&&>, char&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&&>, char const&&>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same>, int>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const>, int const>)); + BOOST_TEST_TRAIT_TRUE((std::is_same volatile>, int volatile>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const volatile>, int const volatile>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same&>, int&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&>, int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same&&>, int&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&&>, int const&&>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same>, float>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const>, float const>)); + BOOST_TEST_TRAIT_TRUE((std::is_same volatile>, float volatile>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const volatile>, float const volatile>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same&>, float&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&>, float const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same&&>, float&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same const&&>, float const&&>)); + + variant_alternative<0, void>(); + variant_alternative<0, void const>(); + variant_alternative<0, void volatile>(); + variant_alternative<0, void const volatile>(); + + variant_alternative<0, int&>(); + variant_alternative<0, int const&>(); + variant_alternative<0, int&&>(); + variant_alternative<0, int const&&>(); + + variant_alternative<0, variant<>>(); + variant_alternative<0, variant<> const>(); + variant_alternative<0, variant<> volatile>(); + variant_alternative<0, variant<> const volatile>(); + + variant_alternative<0, variant<>&>(); + variant_alternative<0, variant<> const&>(); + variant_alternative<0, variant<>&&>(); + variant_alternative<0, variant<> const&&>(); + + variant_alternative<1, variant>(); + variant_alternative<1, variant const>(); + variant_alternative<1, variant volatile>(); + variant_alternative<1, variant const volatile>(); + + variant_alternative<1, variant&>(); + variant_alternative<1, variant const&>(); + variant_alternative<1, variant&&>(); + variant_alternative<1, variant const&&>(); + + BOOST_TEST_TRAIT_FALSE((mp_valid, void>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, void const>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, void volatile>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, void const volatile>)); + + BOOST_TEST_TRAIT_FALSE((mp_valid, int&>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, int const&>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, int&&>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, int const&&>)); + + BOOST_TEST_TRAIT_FALSE((mp_valid, variant<>>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant<> const>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant<> volatile>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant<> const volatile>)); + + BOOST_TEST_TRAIT_FALSE((mp_valid, variant<>&>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant<> const&>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant<>&&>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant<> const&&>)); + + BOOST_TEST_TRAIT_TRUE((mp_valid, variant>)); + BOOST_TEST_TRAIT_TRUE((mp_valid, variant const>)); + BOOST_TEST_TRAIT_TRUE((mp_valid, variant volatile>)); + BOOST_TEST_TRAIT_TRUE((mp_valid, variant const volatile>)); + + BOOST_TEST_TRAIT_TRUE((mp_valid, variant&>)); + BOOST_TEST_TRAIT_TRUE((mp_valid, variant const&>)); + BOOST_TEST_TRAIT_TRUE((mp_valid, variant&&>)); + BOOST_TEST_TRAIT_TRUE((mp_valid, variant const&&>)); + + BOOST_TEST_TRAIT_FALSE((mp_valid, variant>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant const>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant volatile>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant const volatile>)); + + BOOST_TEST_TRAIT_FALSE((mp_valid, variant&>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant const&>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant&&>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, variant const&&>)); + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_convert_construct.cpp b/src/boost/libs/variant2/test/variant_convert_construct.cpp new file mode 100644 index 000000000..dbf61d907 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_convert_construct.cpp @@ -0,0 +1,175 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ + int v; + + X1(): v(0) {} + explicit X1(int v): v(v) {} + X1(X1 const& r): v(r.v) {} + X1(X1&& r): v(r.v) {} + X1& operator=( X1 const& r ) { v = r.v; return *this; } + X1& operator=( X1&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct X2 +{ + int v; + + X2(): v(0) {} + explicit X2(int v): v(v) {} + X2(X2 const& r): v(r.v) {} + X2(X2&& r): v(r.v) {} + X2& operator=( X2 const& r ) { v = r.v; return *this; } + X2& operator=( X2&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +int main() +{ + { + variant v( 1 ); + + variant v2( v ); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v ), get( v2 ) ); + + variant v3( std::move(v) ); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant const v( 1 ); + + variant v2( v ); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v ), get( v2 ) ); + + variant v3( std::move(v) ); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant v( 1 ); + + variant v2( v ); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v ), get( v2 ) ); + + variant v3( std::move(v) ); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant const v( 1 ); + + variant v2( v ); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v ), get( v2 ) ); + + variant v3( std::move(v) ); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant v( 3.14f ); + + variant v2( v ); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v ), get( v2 ) ); + + variant v3( std::move(v) ); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant v( 3.15f ); + + variant v2( v ); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v ), get( v2 ) ); + + variant v3( std::move(v) ); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant v( "s1" ); + + variant v2( v ); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v ), get( v2 ) ); + + variant v3( std::move(v) ); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant v{ X1{1} }; + + variant v2( v ); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v ).v, get( v2 ).v ); + + variant v3( std::move(v) ); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ).v, get( v3 ).v ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_convert_construct_throw.cpp b/src/boost/libs/variant2/test/variant_convert_construct_throw.cpp new file mode 100644 index 000000000..bb53d7ce5 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_convert_construct_throw.cpp @@ -0,0 +1,95 @@ + +// Copyright 2019 Peter Dimov +// +// 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 + +#if defined(_MSC_VER) +# pragma warning( disable: 4702 ) // unreachable code +#endif + +#include +#include +#include + +using namespace boost::variant2; + +struct X +{ + static int instances; + + X() + { + ++instances; + } + + X( X const& ) + { + throw std::runtime_error( "X(X const&)" ); + } + + ~X() + { + --instances; + } +}; + +int X::instances = 0; + +void test_copy() +{ + X::instances = 0; + + { + variant v1; + + BOOST_TEST_EQ( X::instances, 1 ); + + try + { + variant v2( v1 ); + BOOST_TEST_EQ( X::instances, 2 ); + } + catch( std::exception const& ) + { + } + + BOOST_TEST_EQ( X::instances, 1 ); + } + + BOOST_TEST_EQ( X::instances, 0 ); +} + +void test_move() +{ + X::instances = 0; + + { + variant v1; + + BOOST_TEST_EQ( X::instances, 1 ); + + try + { + variant v2( std::move( v1 ) ); + BOOST_TEST_EQ( X::instances, 2 ); + } + catch( std::exception const& ) + { + } + + BOOST_TEST_EQ( X::instances, 1 ); + } + + BOOST_TEST_EQ( X::instances, 0 ); +} + +int main() +{ + test_copy(); + test_move(); + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_copy_assign.cpp b/src/boost/libs/variant2/test/variant_copy_assign.cpp new file mode 100644 index 000000000..c8ed44f75 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_copy_assign.cpp @@ -0,0 +1,194 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ + int v; + + X1(): v(0) {} + explicit X1(int v): v(v) {} + X1(X1 const& r): v(r.v) {} + X1(X1&& r): v(r.v) {} + X1& operator=( X1 const& r ) { v = r.v; return *this; } + X1& operator=( X1&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct X2 +{ + int v; + + X2(): v(0) {} + explicit X2(int v): v(v) {} + X2(X2 const& r): v(r.v) {} + X2(X2&& r): v(r.v) {} + X2& operator=( X2 const& r ) { v = r.v; return *this; } + X2& operator=( X2&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct Y +{ + Y& operator=( Y const& ) = delete; +}; + +int main() +{ + { + variant v; + BOOST_TEST_EQ( get<0>(v), 0 ); + + variant v2( 1 ); + + v = v2; + BOOST_TEST_EQ( get<0>(v), 1 ); + + variant const v3( 2 ); + + v = v3; + BOOST_TEST_EQ( get<0>(v), 2 ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + variant v2( 1 ); + + v = v2; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + + variant v3( 3.14f ); + + v = v3; + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.14f ); + + variant const v4( 3.15f ); + + v = v4; + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.15f ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + variant v2( in_place_index_t<1>{}, 1 ); + + v = v2; + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 1 ); + + variant v3( 3.14f ); + + v = v3; + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.14f ); + + variant const v4( 3.15f ); + + v = v4; + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.15f ); + + variant v5( "s1" ); + + v = v5; + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s1") ); + + variant const v6( "s2" ); + + v = v6; + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s2") ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 0 ); + + variant v2( X1{1} ); + + v = v2; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + + variant v3( in_place_index_t<1>{}, 2 ); + + v = v3; + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 2 ); + + variant const v4( in_place_index_t<1>{}, 3 ); + + v = v4; + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 3 ); + + variant const v5( in_place_index_t<0>{}, 4 ); + + v = v5; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 4 ); + } + + { + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_assignable>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_assignable>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_assignable>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_assignable>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable>)); + + BOOST_TEST_TRAIT_TRUE((std::is_copy_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_copy_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_copy_assignable>)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_copy_assign_cx.cpp b/src/boost/libs/variant2/test/variant_copy_assign_cx.cpp new file mode 100644 index 000000000..8be25794a --- /dev/null +++ b/src/boost/libs/variant2/test/variant_copy_assign_cx.cpp @@ -0,0 +1,116 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +enum E +{ + v +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr T test( A const& a ) +{ + V v; + + v = a; + + return get(v); +} + +int main() +{ + { + constexpr variant v( 1 ); + constexpr auto w = test, int>( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( 1 ); + constexpr auto w = test, X>( v ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr variant v( 1 ); + constexpr auto w = test, Y>( v ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr variant v( 1 ); + constexpr auto w = test, int>( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( 3.0f ); + constexpr auto w = test, float>( v ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr variant v( 3.0f ); + constexpr auto w = test, float>( v ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr variant v( 1 ); + constexpr auto w = test, X>( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( X(1) ); + constexpr auto w = test, X>( v ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr variant v( 1 ); + constexpr auto w = test, Y>( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( Y(1) ); + constexpr auto w = test, Y>( v ); + STATIC_ASSERT( w == 1 ); + } + +#endif +} diff --git a/src/boost/libs/variant2/test/variant_copy_assign_throw.cpp b/src/boost/libs/variant2/test/variant_copy_assign_throw.cpp new file mode 100644 index 000000000..a13dccbec --- /dev/null +++ b/src/boost/libs/variant2/test/variant_copy_assign_throw.cpp @@ -0,0 +1,55 @@ + +// Copyright 2019 Peter Dimov +// +// 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 + +#if defined(_MSC_VER) +# pragma warning( disable: 4702 ) // unreachable code +#endif + +#include +#include +#include + +using namespace boost::variant2; + +struct Y1 +{ + Y1() noexcept {} // =default fails on msvc-14.0 + + Y1(Y1 const&) + { + throw std::runtime_error( "Y1(Y1 const&)" ); + } + + Y1& operator=(Y1 const&) = default; +}; + +struct Y2 +{ + Y2() noexcept {} + + Y2(Y2 const&) + { + throw std::runtime_error( "Y2(Y2 const&)" ); + } + + Y2& operator=(Y2 const&) = default; +}; + +void test() +{ + variant v1( in_place_type_t{} ); + variant v2( in_place_type_t{} ); + + BOOST_TEST_THROWS( v1 = v2, std::runtime_error ) +} + +int main() +{ + test(); + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_copy_construct.cpp b/src/boost/libs/variant2/test/variant_copy_construct.cpp new file mode 100644 index 000000000..169a2faf0 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_copy_construct.cpp @@ -0,0 +1,162 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include +#include +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ + X1() {} + X1(X1 const&) {} + X1(X1&&) {} +}; + +inline bool operator==( X1, X1 ) { return true; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); + +struct X2 +{ + X2() {} + X2(X2 const&) {} + X2(X2&&) {} +}; + +inline bool operator==( X2, X2 ) { return true; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); + +struct Y +{ + Y( Y const& ) = delete; +}; + +struct D +{ + ~D() {} +}; + +inline bool operator==( D, D ) { return true; } + +template static void test( V const & v ) +{ + V v2( v ); + + BOOST_TEST_EQ( v.index(), v2.index() ); + BOOST_TEST( v == v2 ); + + BOOST_TEST_TRAIT_TRUE((std::is_copy_constructible)); +} + +int main() +{ + test( variant() ); + test( variant(1) ); + + test( variant() ); + test( variant(1) ); + + test( variant() ); + test( variant(1) ); + test( variant(3.14f) ); + + test( variant() ); + test( variant(1) ); + test( variant(3.14f) ); + + test( variant() ); + test( variant("test") ); + + test( variant() ); + test( variant("test") ); + + test( variant() ); + test( variant(1) ); + test( variant(3.14f) ); + test( variant("test") ); + + test( variant() ); + + test( variant() ); + test( variant(3.14f) ); + + test( variant() ); + + test( variant("test") ); + + test( variant() ); + + test( variant() ); + + test( variant() ); + test( variant() ); + test( variant() ); + test( variant() ); + + { + variant v; + v.emplace(); + + test( v ); + } + + { + variant v; + v.emplace(); + + test( v ); + } + +#if !BOOST_WORKAROUND( __GNUC__, < 5 ) + + test( variant() ); + +#endif + + { + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible>)); + + BOOST_TEST_TRAIT_TRUE((std::is_copy_constructible>)); + +#if !BOOST_WORKAROUND( BOOST_MSVC, <= 1910 ) + + BOOST_TEST_TRAIT_FALSE((std::is_copy_constructible>)); + +#endif + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_copy_construct_cx.cpp b/src/boost/libs/variant2/test/variant_copy_construct_cx.cpp new file mode 100644 index 000000000..cd3a7c2b0 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_copy_construct_cx.cpp @@ -0,0 +1,112 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +enum E +{ + v +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr T test( V const v ) +{ + return get( v ); +} + +int main() +{ + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( 3.0f ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr variant v( 3.0f ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( X(1) ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( Y(1) ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + +#endif +} diff --git a/src/boost/libs/variant2/test/variant_copy_construct_throw.cpp b/src/boost/libs/variant2/test/variant_copy_construct_throw.cpp new file mode 100644 index 000000000..81b44d9f4 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_copy_construct_throw.cpp @@ -0,0 +1,69 @@ + +// Copyright 2019 Peter Dimov +// +// 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 + +#if defined(_MSC_VER) +# pragma warning( disable: 4702 ) // unreachable code +#endif + +#include +#include +#include + +using namespace boost::variant2; + +struct X +{ + static int instances; + + X() + { + ++instances; + } + + X( X const& ) + { + throw std::runtime_error( "X(X const&)" ); + } + + ~X() + { + --instances; + } +}; + +int X::instances = 0; + +void test() +{ + X::instances = 0; + + { + variant v1; + + BOOST_TEST_EQ( X::instances, 1 ); + + try + { + variant v2( v1 ); + BOOST_TEST_EQ( X::instances, 2 ); + } + catch( std::exception const& ) + { + } + + BOOST_TEST_EQ( X::instances, 1 ); + } + + BOOST_TEST_EQ( X::instances, 0 ); +} + +int main() +{ + test(); + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_default_construct.cpp b/src/boost/libs/variant2/test/variant_default_construct.cpp new file mode 100644 index 000000000..894934cb9 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_default_construct.cpp @@ -0,0 +1,103 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +struct X +{ + X(); +}; + +struct Y +{ + Y() = delete; +}; + +int main() +{ + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), std::string() ); + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), std::string() ); + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), std::string() ); + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), std::string() ); + } + + { + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_default_constructible>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_default_constructible>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_default_constructible>)); + + BOOST_TEST_TRAIT_TRUE((std::is_default_constructible>)); + BOOST_TEST_TRAIT_FALSE((std::is_default_constructible>)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_default_construct_cx.cpp b/src/boost/libs/variant2/test/variant_default_construct_cx.cpp new file mode 100644 index 000000000..d3325c6b1 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_default_construct_cx.cpp @@ -0,0 +1,51 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +int main() +{ + { + constexpr variant v; + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 0 ); + } + + { + constexpr variant v; + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 0 ); + } + + { + constexpr variant v; + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 0 ); + } + + { + constexpr variant v; + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 0 ); + } + + { + constexpr variant v; + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 0 ); + } +} diff --git a/src/boost/libs/variant2/test/variant_derived_construct.cpp b/src/boost/libs/variant2/test/variant_derived_construct.cpp new file mode 100644 index 000000000..6844336e6 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_derived_construct.cpp @@ -0,0 +1,33 @@ +// Copyright 2021 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include + +using namespace boost::variant2; + +template class X: variant +{ + using base = variant; + using base::base; +}; + +struct Y +{ + Y( Y const& rhs ) = default; + + template Y( T const& t ) + { + t.bar(); + } +}; + +int main() +{ + using W = X; + + W a( 1 ); + W b( a ); + + (void)b; +} diff --git a/src/boost/libs/variant2/test/variant_destroy.cpp b/src/boost/libs/variant2/test/variant_destroy.cpp new file mode 100644 index 000000000..392a2d7dc --- /dev/null +++ b/src/boost/libs/variant2/test/variant_destroy.cpp @@ -0,0 +1,205 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +struct X1 +{ + static int instances; + + int v; + + X1(): v(0) { ++instances; } + explicit X1(int v): v(v) { ++instances; } + X1(X1 const& r): v(r.v) { ++instances; } + X1(X1&& r): v(r.v) { ++instances; } + + ~X1() noexcept { --instances; } + + X1& operator=( X1 const& r ) { v = r.v; return *this; } + X1& operator=( X1&& r ) { v = r.v; return *this; } +}; + +int X1::instances = 0; + +struct X2 +{ + static int instances; + + int v; + + X2(): v(0) { ++instances; } + explicit X2(int v): v(v) { ++instances; } + X2(X2 const& r): v(r.v) { ++instances; } + X2(X2&& r): v(r.v) { ++instances; } + + ~X2() noexcept { --instances; } + + X2& operator=( X2 const& r ) { v = r.v; return *this; } + X2& operator=( X2&& r ) { v = r.v; return *this; } +}; + +int X2::instances = 0; + +struct Y1 +{ + static int instances; + + int v; + + Y1() noexcept: v(0) { ++instances; } + explicit Y1(int v) noexcept: v(v) { ++instances; } + Y1(Y1 const& r) noexcept: v(r.v) { ++instances; } + Y1(Y1&& r) noexcept: v(r.v) { ++instances; } + + ~Y1() noexcept { --instances; } + + Y1& operator=( Y1 const& r ) noexcept { v = r.v; return *this; } + Y1& operator=( Y1&& r ) noexcept { v = r.v; return *this; } +}; + +int Y1::instances = 0; + +struct Y2 +{ + static int instances; + + int v; + + Y2() noexcept: v(0) { ++instances; } + explicit Y2(int v) noexcept: v(v) { ++instances; } + Y2(Y2 const& r) noexcept: v(r.v) { ++instances; } + Y2(Y2&& r) noexcept: v(r.v) { ++instances; } + + ~Y2() noexcept { --instances; } + + Y2& operator=( Y2 const& r ) noexcept { v = r.v; return *this; } + Y2& operator=( Y2&& r ) noexcept { v = r.v; return *this; } +}; + +int Y2::instances = 0; + +int main() +{ + BOOST_TEST_EQ( Y1::instances, 0 ); + + { + variant v; + BOOST_TEST_EQ( Y1::instances, 1 ); + + { + variant v2; + BOOST_TEST_EQ( Y1::instances, 2 ); + + v = v2; + BOOST_TEST_EQ( Y1::instances, 2 ); + } + + BOOST_TEST_EQ( Y1::instances, 1 ); + + v = Y1{1}; + BOOST_TEST_EQ( Y1::instances, 1 ); + } + + BOOST_TEST_EQ( Y1::instances, 0 ); + BOOST_TEST_EQ( Y2::instances, 0 ); + + { + variant v; + + BOOST_TEST_EQ( Y1::instances, 1 ); + BOOST_TEST_EQ( Y2::instances, 0 ); + + { + variant v2; + BOOST_TEST_EQ( Y1::instances, 2 ); + BOOST_TEST_EQ( Y2::instances, 0 ); + + v = v2; + BOOST_TEST_EQ( Y1::instances, 2 ); + BOOST_TEST_EQ( Y2::instances, 0 ); + + v2 = Y2{1}; + BOOST_TEST_EQ( Y1::instances, 1 ); + BOOST_TEST_EQ( Y2::instances, 1 ); + + v = v2; + BOOST_TEST_EQ( Y1::instances, 0 ); + BOOST_TEST_EQ( Y2::instances, 2 ); + } + + BOOST_TEST_EQ( Y1::instances, 0 ); + BOOST_TEST_EQ( Y2::instances, 1 ); + + v.emplace<0>(); + + BOOST_TEST_EQ( Y1::instances, 1 ); + BOOST_TEST_EQ( Y2::instances, 0 ); + + v.emplace(); + + BOOST_TEST_EQ( Y1::instances, 0 ); + BOOST_TEST_EQ( Y2::instances, 1 ); + } + + BOOST_TEST_EQ( Y1::instances, 0 ); + BOOST_TEST_EQ( Y2::instances, 0 ); + + BOOST_TEST_EQ( X1::instances, 0 ); + BOOST_TEST_EQ( X2::instances, 0 ); + + { + variant v; + + BOOST_TEST_EQ( X1::instances, 1 ); + BOOST_TEST_EQ( X2::instances, 0 ); + + { + variant v2; + BOOST_TEST_EQ( X1::instances, 2 ); + BOOST_TEST_EQ( X2::instances, 0 ); + + v = v2; + BOOST_TEST_EQ( X1::instances, 2 ); + BOOST_TEST_EQ( X2::instances, 0 ); + + v2 = X2{1}; + BOOST_TEST_EQ( X1::instances, 1 ); + BOOST_TEST_EQ( X2::instances, 1 ); + + v = v2; + BOOST_TEST_EQ( X1::instances, 0 ); + BOOST_TEST_EQ( X2::instances, 2 ); + } + + BOOST_TEST_EQ( X1::instances, 0 ); + BOOST_TEST_EQ( X2::instances, 1 ); + + v.emplace<0>(); + + BOOST_TEST_EQ( X1::instances, 1 ); + BOOST_TEST_EQ( X2::instances, 0 ); + + v.emplace(); + + BOOST_TEST_EQ( X1::instances, 0 ); + BOOST_TEST_EQ( X2::instances, 1 ); + } + + BOOST_TEST_EQ( X1::instances, 0 ); + BOOST_TEST_EQ( X2::instances, 0 ); + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_emplace_index.cpp b/src/boost/libs/variant2/test/variant_emplace_index.cpp new file mode 100644 index 000000000..340cbcb82 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_emplace_index.cpp @@ -0,0 +1,292 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ + int v; + + X1(): v(0) {} + explicit X1(int v): v(v) {} + X1(X1 const& r): v(r.v) {} + X1(X1&& r): v(r.v) {} + X1& operator=( X1 const& r ) { v = r.v; return *this; } + X1& operator=( X1&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct X2 +{ + int v; + + X2(): v(0) {} + explicit X2(int v): v(v) {} + X2(X2 const& r): v(r.v) {} + X2(X2&& r): v(r.v) {} + X2& operator=( X2 const& r ) { v = r.v; return *this; } + X2& operator=( X2&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct Y1 +{ + int v = 1; + + Y1() = default; + Y1(Y1 const&) = delete; + Y1(Y1&&) = delete; +}; + +STATIC_ASSERT( !std::is_copy_constructible::value ); +STATIC_ASSERT( !std::is_move_constructible::value ); + +struct Y2 +{ + int v = 2; + + Y2() = default; + Y2(Y2 const&) = delete; + Y2(Y2&&) = delete; +}; + +STATIC_ASSERT( !std::is_copy_constructible::value ); +STATIC_ASSERT( !std::is_move_constructible::value ); + +struct Z1 +{ + static int instances; + + int v = 1; + + Z1() { ++instances; } + ~Z1() { --instances; } + + Z1(Z1 const&) = delete; + Z1(Z1&&) = delete; +}; + +int Z1::instances = 0; + +struct Z2 +{ + static int instances; + + int v = 2; + + Z2() { ++instances; } + ~Z2() { --instances; } + + Z2(Z2 const&) = delete; + Z2(Z2&&) = delete; +}; + +int Z2::instances = 0; + +int main() +{ + { + variant v; + BOOST_TEST_EQ( get<0>(v), 0 ); + + v.emplace<0>( 1 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + + v.emplace<0>(); + BOOST_TEST_EQ( get<0>(v), 0 ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + v.emplace<0>( 1 ); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + + v.emplace<1>( 3.14f ); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.14f ); + + v.emplace<1>(); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 0 ); + + v.emplace<0>(); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + v.emplace<0>( 1 ); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + + v.emplace<1>(); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 0 ); + + v.emplace<1>( 1 ); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 1 ); + + v.emplace<2>( 3.14f ); + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.14f ); + + v.emplace<2>(); + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 0 ); + + v.emplace<3>( "s1" ); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s1") ); + + v.emplace<3>( "s2" ); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s2") ); + + v.emplace<3>(); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string() ); + + v.emplace<3>( { 'a', 'b' } ); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), (std::string{ 'a', 'b'}) ); + + v.emplace<3>( { 'c', 'd' }, std::allocator() ); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), (std::string{ 'c', 'd'}) ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 0 ); + + v.emplace<0>( 1 ); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + + v.emplace<1>( 2 ); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 2 ); + + v.emplace<0>(); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 0 ); + + v.emplace<0>( 4 ); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 4 ); + + v.emplace<1>(); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 0 ); + + v.emplace<1>( 6 ); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 6 ); + + v.emplace<0>( 3 ); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 3 ); + + v.emplace<0>( 4 ); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 4 ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + + v.emplace<0>(); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + + v.emplace<1>(); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 2 ); + + v.emplace<1>(); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 2 ); + + v.emplace<0>(); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + BOOST_TEST_EQ( Z1::instances, 1 ); + BOOST_TEST_EQ( Z2::instances, 0 ); + + v.emplace<0>(); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + BOOST_TEST_EQ( Z1::instances, 1 ); + BOOST_TEST_EQ( Z2::instances, 0 ); + + v.emplace<1>(); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 2 ); + BOOST_TEST_EQ( Z1::instances, 0 ); + BOOST_TEST_EQ( Z2::instances, 1 ); + + v.emplace<1>(); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 2 ); + BOOST_TEST_EQ( Z1::instances, 0 ); + BOOST_TEST_EQ( Z2::instances, 1 ); + + v.emplace<0>(); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + BOOST_TEST_EQ( Z1::instances, 1 ); + BOOST_TEST_EQ( Z2::instances, 0 ); + } + + BOOST_TEST_EQ( Z1::instances, 0 ); + BOOST_TEST_EQ( Z2::instances, 0 ); + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_emplace_index_cx.cpp b/src/boost/libs/variant2/test/variant_emplace_index_cx.cpp new file mode 100644 index 000000000..f66f99fc1 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_emplace_index_cx.cpp @@ -0,0 +1,136 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr explicit X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr explicit Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr A test( A const& a ) +{ + V v; + + v.template emplace( a ); + + return get(v); +} + +int main() +{ + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 1>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 1>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 2>( 2.0f ); + STATIC_ASSERT( w == 2.0f ); + } + + { + constexpr auto w = test, 3>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, 4>( 4 ); + STATIC_ASSERT( w == 4 ); + } + + { + constexpr auto w = test, 5>( 5 ); + STATIC_ASSERT( w == 5 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 1>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 2>( 2.0f ); + STATIC_ASSERT( w == 2.0f ); + } + + { + constexpr auto w = test, 3>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, 4>( 4 ); + STATIC_ASSERT( w == 4 ); + } + + { + constexpr auto w = test, 5>( 5 ); + STATIC_ASSERT( w == 5 ); + } + +#endif +} diff --git a/src/boost/libs/variant2/test/variant_emplace_type.cpp b/src/boost/libs/variant2/test/variant_emplace_type.cpp new file mode 100644 index 000000000..df8b364a1 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_emplace_type.cpp @@ -0,0 +1,194 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ + int v; + + X1(): v(0) {} + explicit X1(int v): v(v) {} + X1(X1 const& r): v(r.v) {} + X1(X1&& r): v(r.v) {} + X1& operator=( X1 const& r ) { v = r.v; return *this; } + X1& operator=( X1&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct X2 +{ + int v; + + X2(): v(0) {} + explicit X2(int v): v(v) {} + X2(X2 const& r): v(r.v) {} + X2(X2&& r): v(r.v) {} + X2& operator=( X2 const& r ) { v = r.v; return *this; } + X2& operator=( X2&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct Y1 +{ +}; + +struct Y2 +{ + ~Y2() {} +}; + +struct Guard +{ + explicit Guard(int) {} + Guard(Guard&&) = delete; +}; + +int main() +{ + { + variant v; + BOOST_TEST_EQ( get<0>(v), 0 ); + + v.emplace( 1 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + + v.emplace(); + BOOST_TEST_EQ( get<0>(v), 0 ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + v.emplace( 1 ); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + + v.emplace( 3.14f ); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.14f ); + + v.emplace(); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 0 ); + + v.emplace(); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + v.emplace( 3.14f ); + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.14f ); + + v.emplace(); + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 0 ); + + v.emplace( "s1" ); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s1") ); + + v.emplace( "s2" ); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s2") ); + + v.emplace(); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string() ); + + v.emplace( { 'a', 'b' } ); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), (std::string{ 'a', 'b' }) ); + + v.emplace( { 'c', 'd' }, std::allocator() ); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), (std::string{ 'c', 'd' }) ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 0 ); + + v.emplace( 1 ); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + + v.emplace( 2 ); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 2 ); + + v.emplace(); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 0 ); + + v.emplace( 4 ); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 4 ); + + v.emplace(); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 0 ); + + v.emplace( 6 ); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 6 ); + + v.emplace( 3 ); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 3 ); + + v.emplace( 4 ); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 4 ); + } + + { + variant v; + v.emplace( 1 ); + } + + { + variant v; + v.emplace( 1 ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_emplace_type_cx.cpp b/src/boost/libs/variant2/test/variant_emplace_type_cx.cpp new file mode 100644 index 000000000..2449b12ba --- /dev/null +++ b/src/boost/libs/variant2/test/variant_emplace_type_cx.cpp @@ -0,0 +1,91 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr explicit X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr explicit Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr A test( A const& a ) +{ + V v; + + v.template emplace( a ); + + return get(v); +} + +int main() +{ + { + constexpr auto w = test, int>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, X>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr auto w = test, int>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, float>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, float>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, X>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#endif +} diff --git a/src/boost/libs/variant2/test/variant_eq_ne.cpp b/src/boost/libs/variant2/test/variant_eq_ne.cpp new file mode 100644 index 000000000..87e2ecba5 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_eq_ne.cpp @@ -0,0 +1,99 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +struct X +{ +}; + +inline bool operator==( X const&, X const& ) { return false; } +inline bool operator!=( X const&, X const& ) { return false; } + +int main() +{ + { + variant v1, v2, v3( 1 ), v4( 1 ); + + BOOST_TEST( v1 == v2 ); + BOOST_TEST_NOT( v1 != v2 ); + + BOOST_TEST( v1 != v3 ); + BOOST_TEST_NOT( v1 == v3 ); + + BOOST_TEST( v3 == v4 ); + BOOST_TEST_NOT( v3 != v4 ); + } + + { + variant v1, v2, v3( 1 ), v4( 1 ), v5( 3.14f ), v6( 3.14f ); + + BOOST_TEST( v1 == v2 ); + BOOST_TEST_NOT( v1 != v2 ); + + BOOST_TEST( v1 != v3 ); + BOOST_TEST_NOT( v1 == v3 ); + + BOOST_TEST( v3 == v4 ); + BOOST_TEST_NOT( v3 != v4 ); + + BOOST_TEST( v1 != v5 ); + BOOST_TEST_NOT( v1 == v5 ); + + BOOST_TEST( v3 != v5 ); + BOOST_TEST_NOT( v3 == v5 ); + + BOOST_TEST( v5 == v6 ); + BOOST_TEST_NOT( v5 != v6 ); + } + + { + variant v1, v2, v3( in_place_index_t<1>{} ), v4( in_place_index_t<1>{} ), v5( 3.14f ), v6( 3.14f ); + + BOOST_TEST( v1 == v2 ); + BOOST_TEST_NOT( v1 != v2 ); + + BOOST_TEST( v1 != v3 ); + BOOST_TEST_NOT( v1 == v3 ); + + BOOST_TEST( v3 == v4 ); + BOOST_TEST_NOT( v3 != v4 ); + + BOOST_TEST( v1 != v5 ); + BOOST_TEST_NOT( v1 == v5 ); + + BOOST_TEST( v3 != v5 ); + BOOST_TEST_NOT( v3 == v5 ); + + BOOST_TEST( v5 == v6 ); + BOOST_TEST_NOT( v5 != v6 ); + } + + { + variant v1, v2; + + BOOST_TEST_NOT( v1 == v2 ); + BOOST_TEST_NOT( v1 != v2 ); + } + + { + variant v1, v2; + + BOOST_TEST( v1 == v2 ); + BOOST_TEST_NOT( v1 != v2 ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_eq_ne_cx.cpp b/src/boost/libs/variant2/test/variant_eq_ne_cx.cpp new file mode 100644 index 000000000..bfd90c201 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_eq_ne_cx.cpp @@ -0,0 +1,105 @@ + +// Copyright 2017, 2019 Peter Dimov +// +// 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 +using namespace boost::variant2; + +#if !defined(BOOST_MP11_HAS_CXX14_CONSTEXPR) + +#include + +BOOST_PRAGMA_MESSAGE("Skipping constexpr op==, op!= test because BOOST_MP11_HAS_CXX14_CONSTEXPR is not defined") + +int main() {} + +#else + +struct X +{ +}; + +inline constexpr bool operator==( X const&, X const& ) { return false; } +inline constexpr bool operator!=( X const&, X const& ) { return false; } + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +int main() +{ + { + constexpr variant v1, v2, v3( 1 ), v4( 1 ); + + STATIC_ASSERT( v1 == v2 ); + STATIC_ASSERT( !(v1 != v2) ); + + STATIC_ASSERT( v1 != v3 ); + STATIC_ASSERT( !(v1 == v3) ); + + STATIC_ASSERT( v3 == v4 ); + STATIC_ASSERT( !(v3 != v4) ); + } + + { + constexpr variant v1, v2, v3( 1 ), v4( 1 ), v5( 3.14f ), v6( 3.14f ); + + STATIC_ASSERT( v1 == v2 ); + STATIC_ASSERT( !(v1 != v2) ); + + STATIC_ASSERT( v1 != v3 ); + STATIC_ASSERT( !(v1 == v3) ); + + STATIC_ASSERT( v3 == v4 ); + STATIC_ASSERT( !(v3 != v4) ); + + STATIC_ASSERT( v1 != v5 ); + STATIC_ASSERT( !(v1 == v5) ); + + STATIC_ASSERT( v3 != v5 ); + STATIC_ASSERT( !(v3 == v5) ); + + STATIC_ASSERT( v5 == v6 ); + STATIC_ASSERT( !(v5 != v6) ); + } + + { + constexpr variant v1, v2, v3( in_place_index_t<1>{} ), v4( in_place_index_t<1>{} ), v5( 3.14f ), v6( 3.14f ); + + STATIC_ASSERT( v1 == v2 ); + STATIC_ASSERT( !(v1 != v2) ); + + STATIC_ASSERT( v1 != v3 ); + STATIC_ASSERT( !(v1 == v3) ); + + STATIC_ASSERT( v3 == v4 ); + STATIC_ASSERT( !(v3 != v4) ); + + STATIC_ASSERT( v1 != v5 ); + STATIC_ASSERT( !(v1 == v5) ); + + STATIC_ASSERT( v3 != v5 ); + STATIC_ASSERT( !(v3 == v5) ); + + STATIC_ASSERT( v5 == v6 ); + STATIC_ASSERT( !(v5 != v6) ); + } + + { + constexpr variant v1, v2; + + STATIC_ASSERT( !(v1 == v2) ); + STATIC_ASSERT( !(v1 != v2) ); + } + + { + constexpr variant v1, v2; + + STATIC_ASSERT( v1 == v2 ); + STATIC_ASSERT( !(v1 != v2) ); + } +} + +#endif diff --git a/src/boost/libs/variant2/test/variant_get_by_index.cpp b/src/boost/libs/variant2/test/variant_get_by_index.cpp new file mode 100644 index 000000000..846d0bab0 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_get_by_index.cpp @@ -0,0 +1,355 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +#if defined( __clang__ ) && defined( __has_warning ) +# if __has_warning( "-Wdeprecated-volatile" ) +# pragma clang diagnostic ignored "-Wdeprecated-volatile" +# endif +#endif + +#include +#include +#include +#include +#include +#include + +using namespace boost::variant2; + +int main() +{ + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + } + + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const volatile&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const volatile&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const volatile*>)); + } + + { + variant v; + + BOOST_TEST_EQ( get<0>(v), 0 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 0 ); + } + + { + variant const v; + + BOOST_TEST_EQ( get<0>(v), 0 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 0 ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( get<0>(v), 1 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 1 ); + } + + { + variant const v( 1 ); + + BOOST_TEST_EQ( get<0>(v), 1 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 1 ); + } + + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float const*>)); + } + + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float volatile&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float volatile&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float volatile*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float const volatile&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float const volatile&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float const volatile*>)); + } + + { + variant v; + + BOOST_TEST_EQ( get<0>(v), 0 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_THROWS( get<1>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<1>(&v), nullptr ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 0 ); + } + + { + variant const v; + + BOOST_TEST_EQ( get<0>(v), 0 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_THROWS( get<1>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<1>(&v), nullptr ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 0 ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( get<0>(v), 1 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_THROWS( get<1>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<1>(&v), nullptr ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 1 ); + } + + { + variant const v( 1 ); + + BOOST_TEST_EQ( get<0>(v), 1 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_THROWS( get<1>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<1>(&v), nullptr ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 1 ); + } + + { + variant v( 3.14f ); + + BOOST_TEST_THROWS( get<0>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<0>(&v), nullptr ); + + BOOST_TEST_EQ( get<1>(v), 3.14f ); + BOOST_TEST_EQ( get_if<1>(&v), &get<1>(v) ); + + BOOST_TEST_EQ( get<1>(std::move(v)), 3.14f ); + } + + { + variant const v( 3.14f ); + + BOOST_TEST_THROWS( get<0>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<0>(&v), nullptr ); + + BOOST_TEST_EQ( get<1>(v), 3.14f ); + BOOST_TEST_EQ( get_if<1>(&v), &get<1>(v) ); + + BOOST_TEST_EQ( get<1>(std::move(v)), 3.14f ); + } + + { + variant v; + + BOOST_TEST_EQ( get<0>(v), 0 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_THROWS( get<1>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<1>(&v), nullptr ); + + BOOST_TEST_THROWS( get<2>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<2>(&v), nullptr ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 0 ); + } + + { + variant const v; + + BOOST_TEST_EQ( get<0>(v), 0 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_THROWS( get<1>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<1>(&v), nullptr ); + + BOOST_TEST_THROWS( get<2>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<2>(&v), nullptr ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 0 ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( get<0>(v), 1 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_THROWS( get<1>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<1>(&v), nullptr ); + + BOOST_TEST_THROWS( get<2>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<2>(&v), nullptr ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 1 ); + } + + { + variant const v( 1 ); + + BOOST_TEST_EQ( get<0>(v), 1 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + + BOOST_TEST_THROWS( get<1>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<1>(&v), nullptr ); + + BOOST_TEST_THROWS( get<2>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<2>(&v), nullptr ); + + BOOST_TEST_EQ( get<0>(std::move(v)), 1 ); + } + + { + variant v( 3.14f ); + + BOOST_TEST_THROWS( get<0>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<0>(&v), nullptr ); + + BOOST_TEST_THROWS( get<1>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<1>(&v), nullptr ); + + BOOST_TEST_EQ( get<2>(v), 3.14f ); + BOOST_TEST_EQ( get_if<2>(&v), &get<2>(v) ); + + BOOST_TEST_EQ( get<2>(std::move(v)), 3.14f ); + } + + { + variant const v( 3.14f ); + + BOOST_TEST_THROWS( get<0>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<0>(&v), nullptr ); + + BOOST_TEST_THROWS( get<1>(v), bad_variant_access ); + BOOST_TEST_EQ( get_if<1>(&v), nullptr ); + + BOOST_TEST_EQ( get<2>(v), 3.14f ); + BOOST_TEST_EQ( get_if<2>(&v), &get<2>(v) ); + + BOOST_TEST_EQ( get<2>(std::move(v)), 3.14f ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_get_by_index_cx.cpp b/src/boost/libs/variant2/test/variant_get_by_index_cx.cpp new file mode 100644 index 000000000..026b9d3c8 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_get_by_index_cx.cpp @@ -0,0 +1,96 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +#if BOOST_WORKAROUND(BOOST_GCC, < 50000) +# define STATIC_ASSERT_IF(...) +#else +# define STATIC_ASSERT_IF(...) static_assert(__VA_ARGS__, #__VA_ARGS__) +#endif + +int main() +{ + { + constexpr variant v; + + STATIC_ASSERT( get<0>(v) == 0 ); + + STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( get<0>(v) == 1 ); + + STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) ); + } + + { + constexpr variant v; + + STATIC_ASSERT( get<0>(v) == 0 ); + + STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) ); + STATIC_ASSERT_IF( get_if<1>(&v) == nullptr ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( get<0>(v) == 1 ); + + STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) ); + STATIC_ASSERT_IF( get_if<1>(&v) == nullptr ); + } + + { + constexpr variant v( 3.14f ); + + STATIC_ASSERT( get<1>(v) == 3.14f ); + + STATIC_ASSERT_IF( get_if<0>(&v) == nullptr ); + STATIC_ASSERT_IF( get_if<1>(&v) == &get<1>(v) ); + } + + { + constexpr variant v; + + STATIC_ASSERT( get<0>(v) == 0 ); + + STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) ); + STATIC_ASSERT_IF( get_if<1>(&v) == nullptr ); + STATIC_ASSERT_IF( get_if<2>(&v) == nullptr ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( get<0>(v) == 1 ); + + STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) ); + STATIC_ASSERT_IF( get_if<1>(&v) == nullptr ); + STATIC_ASSERT_IF( get_if<2>(&v) == nullptr ); + } + + { + constexpr variant v( 3.14f ); + + STATIC_ASSERT( get<2>(v) == 3.14f ); + + STATIC_ASSERT_IF( get_if<0>(&v) == nullptr ); + STATIC_ASSERT_IF( get_if<1>(&v) == nullptr ); + STATIC_ASSERT_IF( get_if<2>(&v) == &get<2>(v) ); + } +} diff --git a/src/boost/libs/variant2/test/variant_get_by_type.cpp b/src/boost/libs/variant2/test/variant_get_by_type.cpp new file mode 100644 index 000000000..8f44a7e52 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_get_by_type.cpp @@ -0,0 +1,313 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +#if defined( __clang__ ) && defined( __has_warning ) +# if __has_warning( "-Wdeprecated-volatile" ) +# pragma clang diagnostic ignored "-Wdeprecated-volatile" +# endif +#endif + +#include +#include +#include +#include +#include +#include + +using namespace boost::variant2; + +int main() +{ + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + } + + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const volatile&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const volatile&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const volatile*>)); + } + + { + variant v; + + BOOST_TEST_EQ( get(v), 0 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 0 ); + } + + { + variant const v; + + BOOST_TEST_EQ( get(v), 0 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 0 ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( get(v), 1 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 1 ); + } + + { + variant const v( 1 ); + + BOOST_TEST_EQ( get(v), 1 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 1 ); + } + + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float const*>)); + } + + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float volatile&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float volatile&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float volatile*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float const volatile&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float const volatile&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float const volatile*>)); + } + + { + variant v; + + BOOST_TEST_EQ( get(v), 0 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_THROWS( get(v), bad_variant_access ); + BOOST_TEST_EQ( get_if(&v), nullptr ); + + BOOST_TEST_EQ( get(std::move(v)), 0 ); + } + + { + variant const v; + + BOOST_TEST_EQ( get(v), 0 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_THROWS( get(v), bad_variant_access ); + BOOST_TEST_EQ( get_if(&v), nullptr ); + + BOOST_TEST_EQ( get(std::move(v)), 0 ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( get(v), 1 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_THROWS( get(v), bad_variant_access ); + BOOST_TEST_EQ( get_if(&v), nullptr ); + + BOOST_TEST_EQ( get(std::move(v)), 1 ); + } + + { + variant const v( 1 ); + + BOOST_TEST_EQ( get(v), 1 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_THROWS( get(v), bad_variant_access ); + BOOST_TEST_EQ( get_if(&v), nullptr ); + + BOOST_TEST_EQ( get(std::move(v)), 1 ); + } + + { + variant v( 3.14f ); + + BOOST_TEST_THROWS( get(v), bad_variant_access ); + BOOST_TEST_EQ( get_if(&v), nullptr ); + + BOOST_TEST_EQ( get(v), 3.14f ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 3.14f ); + } + + { + variant const v( 3.14f ); + + BOOST_TEST_THROWS( get(v), bad_variant_access ); + BOOST_TEST_EQ( get_if(&v), nullptr ); + + BOOST_TEST_EQ( get(v), 3.14f ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 3.14f ); + } + + { + variant v; + + BOOST_TEST_EQ( get(v), 0 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 0 ); + } + + { + variant const v; + + BOOST_TEST_EQ( get(v), 0 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 0 ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( get(v), 1 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 1 ); + } + + { + variant const v( 1 ); + + BOOST_TEST_EQ( get(v), 1 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 1 ); + } + + { + variant v( 3.14f ); + + BOOST_TEST_EQ( get(v), 3.14f ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 3.14f ); + } + + { + variant const v( 3.14f ); + + BOOST_TEST_EQ( get(v), 3.14f ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_EQ( get(std::move(v)), 3.14f ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant * p = 0; + + BOOST_TEST_EQ( get_if(p), nullptr ); + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant const * p = 0; + + BOOST_TEST_EQ( get_if(p), nullptr ); + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_get_by_type_cx.cpp b/src/boost/libs/variant2/test/variant_get_by_type_cx.cpp new file mode 100644 index 000000000..0f9a40c51 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_get_by_type_cx.cpp @@ -0,0 +1,90 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +#if BOOST_WORKAROUND(BOOST_GCC, < 50000) +# define STATIC_ASSERT_IF(...) +#else +# define STATIC_ASSERT_IF(...) static_assert(__VA_ARGS__, #__VA_ARGS__) +#endif + +int main() +{ + { + constexpr variant v; + + STATIC_ASSERT( get(v) == 0 ); + + STATIC_ASSERT_IF( get_if(&v) == &get(v) ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( get(v) == 1 ); + + STATIC_ASSERT_IF( get_if(&v) == &get(v) ); + } + + { + constexpr variant v; + + STATIC_ASSERT( get(v) == 0 ); + + STATIC_ASSERT_IF( get_if(&v) == &get(v) ); + STATIC_ASSERT_IF( get_if(&v) == nullptr ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( get(v) == 1 ); + + STATIC_ASSERT_IF( get_if(&v) == &get(v) ); + STATIC_ASSERT_IF( get_if(&v) == nullptr ); + } + + { + constexpr variant v( 3.14f ); + + STATIC_ASSERT( get(v) == 3.14f ); + + STATIC_ASSERT_IF( get_if(&v) == nullptr ); + STATIC_ASSERT_IF( get_if(&v) == &get(v) ); + } + + { + constexpr variant v; + + STATIC_ASSERT( get(v) == 0 ); + + STATIC_ASSERT_IF( get_if(&v) == &get(v) ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( get(v) == 1 ); + + STATIC_ASSERT_IF( get_if(&v) == &get(v) ); + } + + { + constexpr variant v( 3.14f ); + + STATIC_ASSERT( get(v) == 3.14f ); + + STATIC_ASSERT_IF( get_if(&v) == &get(v) ); + } +} diff --git a/src/boost/libs/variant2/test/variant_hash.cpp b/src/boost/libs/variant2/test/variant_hash.cpp new file mode 100644 index 000000000..5233132db --- /dev/null +++ b/src/boost/libs/variant2/test/variant_hash.cpp @@ -0,0 +1,86 @@ + +// Copyright 2020 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#if defined(_MSC_VER) +# pragma warning( disable: 4244 ) // conversion from int to float, possible loss of data +#endif + +#include +#include +#include +#include +#include +#include + +using namespace boost::variant2; + +template class Hash, class T1, class T2, class T3> void test() +{ + variant v1( in_place_index_t<0>{} ); + std::size_t h1 = Hash()( v1 ); + + variant v2( in_place_index_t<1>{} ); + std::size_t h2 = Hash()( v2 ); + + variant v3( in_place_index_t<2>{} ); + std::size_t h3 = Hash()( v3 ); + + BOOST_TEST_NE( h1, h2 ); + BOOST_TEST_NE( h1, h3 ); + BOOST_TEST_NE( h2, h3 ); +} + +template class Hash, class T> void test2() +{ + variant v1( 0 ); + std::size_t h1 = Hash()( v1 ); + + variant v2( 1 ); + std::size_t h2 = Hash()( v2 ); + + variant v3( 2 ); + std::size_t h3 = Hash()( v3 ); + + BOOST_TEST_NE( h1, h2 ); + BOOST_TEST_NE( h1, h3 ); + BOOST_TEST_NE( h2, h3 ); +} + +struct X +{ + int m = 0; +}; + +std::size_t hash_value( X const& x ) +{ + return boost::hash()( x.m ); +} + +struct Y {}; // no hash support + +int main() +{ + test(); + test(); + + test(); + test(); + + test>(); + + test2(); + test2(); + + test2(); + test2(); + +#if !BOOST_WORKAROUND(BOOST_MSVC, < 1910) && ( !defined(_LIBCPP_STD_VER) || _LIBCPP_STD_VER > 11 ) + + BOOST_TEST_TRAIT_FALSE(( detail::is_hash_enabled )); + +#endif + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_holds_alternative.cpp b/src/boost/libs/variant2/test/variant_holds_alternative.cpp new file mode 100644 index 000000000..4931f40fe --- /dev/null +++ b/src/boost/libs/variant2/test/variant_holds_alternative.cpp @@ -0,0 +1,78 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +int main() +{ + { + variant v; + BOOST_TEST( holds_alternative( v ) ); + } + + { + variant v; + BOOST_TEST( holds_alternative( v ) ); + BOOST_TEST( !holds_alternative( v ) ); + } + + { + variant v( 3.14f ); + BOOST_TEST( !holds_alternative( v ) ); + BOOST_TEST( holds_alternative( v ) ); + } + + { + variant v; + BOOST_TEST( holds_alternative( v ) ); + } + + { + variant v( 3.14f ); + BOOST_TEST( holds_alternative( v ) ); + } + + { + variant v; + BOOST_TEST( holds_alternative( v ) ); + BOOST_TEST( !holds_alternative( v ) ); + BOOST_TEST( !holds_alternative( v ) ); + } + + { + variant v( 3.14f ); + BOOST_TEST( !holds_alternative( v ) ); + BOOST_TEST( holds_alternative( v ) ); + BOOST_TEST( !holds_alternative( v ) ); + } + + { + variant v( "text" ); + BOOST_TEST( !holds_alternative( v ) ); + BOOST_TEST( !holds_alternative( v ) ); + BOOST_TEST( holds_alternative( v ) ); + } + + { + variant v( 3.14f ); + BOOST_TEST( holds_alternative( v ) ); + BOOST_TEST( !holds_alternative( v ) ); + } + + { + variant v( "text" ); + BOOST_TEST( !holds_alternative( v ) ); + BOOST_TEST( holds_alternative( v ) ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_holds_alternative_cx.cpp b/src/boost/libs/variant2/test/variant_holds_alternative_cx.cpp new file mode 100644 index 000000000..c2bdc0a61 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_holds_alternative_cx.cpp @@ -0,0 +1,43 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +int main() +{ + { + constexpr variant v; + STATIC_ASSERT( holds_alternative( v ) ); + } + + { + constexpr variant v; + STATIC_ASSERT( holds_alternative( v ) ); + STATIC_ASSERT( !holds_alternative( v ) ); + } + + { + constexpr variant v( 3.14f ); + STATIC_ASSERT( !holds_alternative( v ) ); + STATIC_ASSERT( holds_alternative( v ) ); + } + + { + constexpr variant v; + STATIC_ASSERT( holds_alternative( v ) ); + } + + { + constexpr variant v( 3.14f ); + STATIC_ASSERT( holds_alternative( v ) ); + } +} diff --git a/src/boost/libs/variant2/test/variant_in_place_index_construct.cpp b/src/boost/libs/variant2/test/variant_in_place_index_construct.cpp new file mode 100644 index 000000000..75821d328 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_in_place_index_construct.cpp @@ -0,0 +1,138 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +struct X +{ + X() = default; + X( in_place_index_t<0> ) = delete; +}; + +int main() +{ + { + variant v( in_place_index_t<0>{} ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + } + + { + variant v( in_place_index_t<0>{} ); + + BOOST_TEST_EQ( v.index(), 0 ); + } + + { + variant v( in_place_index_t<0>{}, 1 ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + } + + { + variant v( in_place_index_t<0>{} ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + } + + { + variant v( in_place_index_t<0>{}, 1 ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + } + + { + variant v( in_place_index_t<1>{} ); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 0 ); + } + + { + variant v( in_place_index_t<1>{}, 3.14f ); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.14f ); + } + + { + variant v( in_place_index_t<0>{}, 1 ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + } + + { + variant v( in_place_index_t<1>{}, 1 ); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 1 ); + } + + { + variant v( in_place_index_t<2>{}, 3.14f ); + + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.14f ); + } + + { + variant v( in_place_index_t<3>{}, 3.14f ); + + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), 3.14f ); + } + + { + variant v( in_place_index_t<4>{}, "text" ); + + BOOST_TEST_EQ( v.index(), 4 ); + BOOST_TEST_EQ( get<4>(v), std::string("text") ); + } + + { + variant v( in_place_index_t<5>{}, "text" ); + + BOOST_TEST_EQ( v.index(), 5 ); + BOOST_TEST_EQ( get<5>(v), std::string("text") ); + } + + { + variant v( in_place_index_t<5>{}, 4, 'a' ); + + BOOST_TEST_EQ( v.index(), 5 ); + BOOST_TEST_EQ( get<5>(v), std::string( 4, 'a' ) ); + } + + { + variant v( in_place_index_t<4>{}, { 'a', 'b', 'c' } ); + + BOOST_TEST_EQ( v.index(), 4 ); + BOOST_TEST_EQ( get<4>(v), (std::string{ 'a', 'b', 'c' }) ); + } + + { + variant v( in_place_index_t<5>{}, { 'a', 'b', 'c' }, std::allocator() ); + + BOOST_TEST_EQ( v.index(), 5 ); + BOOST_TEST_EQ( get<5>(v), (std::string{ 'a', 'b', 'c' }) ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_in_place_index_construct_cx.cpp b/src/boost/libs/variant2/test/variant_in_place_index_construct_cx.cpp new file mode 100644 index 000000000..ea4b3ab2f --- /dev/null +++ b/src/boost/libs/variant2/test/variant_in_place_index_construct_cx.cpp @@ -0,0 +1,121 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +struct X +{ + constexpr X() = default; + constexpr explicit X(int, int) {} + X( in_place_index_t<0> ) = delete; +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +int main() +{ + { + constexpr variant v( in_place_index_t<0>{} ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 0 ); + } + + { + constexpr variant v( in_place_index_t<0>{} ); + + STATIC_ASSERT( v.index() == 0 ); + } + + { + constexpr variant v( in_place_index_t<0>{}, 1 ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 1 ); + } + + { + constexpr variant v( in_place_index_t<0>{} ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 0 ); + } + + { + constexpr variant v( in_place_index_t<0>{}, 1 ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 1 ); + } + + { + constexpr variant v( in_place_index_t<1>{} ); + + STATIC_ASSERT( v.index() == 1 ); + STATIC_ASSERT( get<1>(v) == 0 ); + } + + { + constexpr variant v( in_place_index_t<1>{}, 3.14f ); + + STATIC_ASSERT( v.index() == 1 ); + STATIC_ASSERT( get<1>(v) == 3.14f ); + } + + { + constexpr variant v( in_place_index_t<0>{}, 1 ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 1 ); + } + + { + constexpr variant v( in_place_index_t<1>{}, 1 ); + + STATIC_ASSERT( v.index() == 1 ); + STATIC_ASSERT( get<1>(v) == 1 ); + } + + { + constexpr variant v( in_place_index_t<2>{}, 3.14f ); + + STATIC_ASSERT( v.index() == 2 ); + STATIC_ASSERT( get<2>(v) == 3.14f ); + } + + { + constexpr variant v( in_place_index_t<3>{}, 3.14f ); + + STATIC_ASSERT( v.index() == 3 ); + STATIC_ASSERT( get<3>(v) == 3.14f ); + } + + { + constexpr variant v( in_place_index_t<4>{} ); + + STATIC_ASSERT( v.index() == 4 ); + } + +#if BOOST_WORKAROUND(BOOST_GCC, >= 100000 && BOOST_GCC < 120000) + + // no idea why this fails on g++ 10/11 + +#else + + { + constexpr variant v( in_place_index_t<5>{}, 0, 0 ); + + STATIC_ASSERT( v.index() == 5 ); + } + +#endif +} diff --git a/src/boost/libs/variant2/test/variant_in_place_type_construct.cpp b/src/boost/libs/variant2/test/variant_in_place_type_construct.cpp new file mode 100644 index 000000000..3c8265acc --- /dev/null +++ b/src/boost/libs/variant2/test/variant_in_place_type_construct.cpp @@ -0,0 +1,134 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +struct X +{ + X() = default; + template X( in_place_type_t ) = delete; +}; + +int main() +{ + { + variant v( in_place_type_t{} ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + BOOST_TEST( holds_alternative(v) ); + } + + { + variant v( in_place_type_t{} ); + + BOOST_TEST_EQ( v.index(), 0 ); + + BOOST_TEST( holds_alternative(v) ); + } + + { + variant v( in_place_type_t{}, 1 ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + + BOOST_TEST( holds_alternative(v) ); + } + + { + variant v( in_place_type_t{} ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + BOOST_TEST( holds_alternative(v) ); + } + + { + variant v( in_place_type_t{}, 1 ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + + BOOST_TEST( holds_alternative(v) ); + } + + { + variant v( in_place_type_t{} ); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 0 ); + + BOOST_TEST( holds_alternative(v) ); + } + + { + variant v( in_place_type_t{}, 3.14f ); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.14f ); + + BOOST_TEST( holds_alternative(v) ); + } + + { + variant v( in_place_type_t{}, 3.14f ); + + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.14f ); + + BOOST_TEST( holds_alternative(v) ); + } + + { + variant v( in_place_type_t{}, "text" ); + + BOOST_TEST_EQ( v.index(), 4 ); + BOOST_TEST_EQ( get<4>(v), std::string("text") ); + + BOOST_TEST( holds_alternative(v) ); + } + + { + variant v( in_place_type_t{}, 4, 'a' ); + + BOOST_TEST_EQ( v.index(), 4 ); + BOOST_TEST_EQ( get<4>(v), std::string( 4, 'a' ) ); + + BOOST_TEST( holds_alternative(v) ); + } + + { + variant v( in_place_type_t{}, { 'a', 'b', 'c' } ); + + BOOST_TEST_EQ( v.index(), 4 ); + BOOST_TEST_EQ( get<4>(v), (std::string{ 'a', 'b', 'c' }) ); + + BOOST_TEST( holds_alternative(v) ); + } + + { + variant v( in_place_type_t{}, { 'a', 'b', 'c' }, std::allocator() ); + + BOOST_TEST_EQ( v.index(), 4 ); + BOOST_TEST_EQ( get<4>(v), (std::string{ 'a', 'b', 'c' }) ); + + BOOST_TEST( holds_alternative(v) ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_in_place_type_construct_cx.cpp b/src/boost/libs/variant2/test/variant_in_place_type_construct_cx.cpp new file mode 100644 index 000000000..832de3d26 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_in_place_type_construct_cx.cpp @@ -0,0 +1,120 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +struct X +{ + constexpr X() = default; + constexpr explicit X(int, int) {} + template X( in_place_type_t ) = delete; +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +int main() +{ + { + constexpr variant v( in_place_type_t{} ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 0 ); + + STATIC_ASSERT( holds_alternative(v) ); + } + + { + constexpr variant v( in_place_type_t{} ); + + STATIC_ASSERT( v.index() == 0 ); + + STATIC_ASSERT( holds_alternative(v) ); + } + + { + constexpr variant v( in_place_type_t{}, 1 ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 1 ); + + STATIC_ASSERT( holds_alternative(v) ); + } + + { + constexpr variant v( in_place_type_t{} ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 0 ); + + STATIC_ASSERT( holds_alternative(v) ); + } + + { + constexpr variant v( in_place_type_t{}, 1 ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 1 ); + + STATIC_ASSERT( holds_alternative(v) ); + } + + { + constexpr variant v( in_place_type_t{} ); + + STATIC_ASSERT( v.index() == 1 ); + STATIC_ASSERT( get<1>(v) == 0 ); + + STATIC_ASSERT( holds_alternative(v) ); + } + + { + constexpr variant v( in_place_type_t{}, 3.14f ); + + STATIC_ASSERT( v.index() == 1 ); + STATIC_ASSERT( get<1>(v) == 3.14f ); + + STATIC_ASSERT( holds_alternative(v) ); + } + + { + constexpr variant v( in_place_type_t{}, 3.14f ); + + STATIC_ASSERT( v.index() == 2 ); + STATIC_ASSERT( get<2>(v) == 3.14f ); + + STATIC_ASSERT( holds_alternative(v) ); + } + + { + constexpr variant v( in_place_type_t{} ); + + STATIC_ASSERT( v.index() == 4 ); + + STATIC_ASSERT( holds_alternative(v) ); + } + +#if BOOST_WORKAROUND(BOOST_GCC, >= 100000 && BOOST_GCC < 120000) + + // no idea why this fails on g++ 10/11 + +#else + + { + constexpr variant v( in_place_type_t{}, 0, 0 ); + + STATIC_ASSERT( v.index() == 4 ); + + STATIC_ASSERT( holds_alternative(v) ); + } + +#endif +} diff --git a/src/boost/libs/variant2/test/variant_lt_gt.cpp b/src/boost/libs/variant2/test/variant_lt_gt.cpp new file mode 100644 index 000000000..f2a931fb0 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_lt_gt.cpp @@ -0,0 +1,94 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +struct X +{ +}; + +inline bool operator<( X const&, X const& ) { return false; } +inline bool operator>( X const&, X const& ) { return false; } +inline bool operator<=( X const&, X const& ) { return false; } +inline bool operator>=( X const&, X const& ) { return false; } + +#define TEST_EQ( v1, v2 ) \ + BOOST_TEST_NOT( v1 < v2 ); \ + BOOST_TEST_NOT( v1 > v2 ); \ + BOOST_TEST( v1 <= v2 ); \ + BOOST_TEST( v1 >= v2 ); + +#define TEST_LE( v1, v3 ) \ + BOOST_TEST( v1 < v3 ); \ + BOOST_TEST( v3 > v1 ); \ + BOOST_TEST_NOT( v1 > v3 ); \ + BOOST_TEST_NOT( v3 < v1 ); \ + BOOST_TEST( v1 <= v3 ); \ + BOOST_TEST( v3 >= v1 ); \ + BOOST_TEST_NOT( v1 >= v3 ); \ + BOOST_TEST_NOT( v3 <= v1 ); + +int main() +{ + { + variant v1, v2, v3( 1 ), v4( 1 ); + + TEST_EQ( v1, v2 ) + TEST_LE( v1, v3 ) + TEST_EQ( v3, v4 ) + } + + { + variant v1, v2, v3( 1 ), v4( 1 ), v5( 3.14f ), v6( 3.14f ); + + TEST_EQ( v1, v2 ) + TEST_LE( v1, v3 ) + TEST_EQ( v3, v4 ) + TEST_LE( v1, v5 ) + TEST_LE( v3, v5 ) + TEST_EQ( v5, v6 ) + } + + { + variant v1, v2, v3( in_place_index_t<1>{} ), v4( in_place_index_t<1>{} ), v5( 3.14f ), v6( 3.14f ); + + TEST_EQ( v1, v2 ) + TEST_LE( v1, v3 ) + TEST_EQ( v3, v4 ) + TEST_LE( v1, v5 ) + TEST_LE( v3, v5 ) + TEST_EQ( v5, v6 ) + } + + { + variant v1, v2; + + BOOST_TEST_NOT( v1 < v2 ); + BOOST_TEST_NOT( v1 > v2 ); + BOOST_TEST_NOT( v1 <= v2 ); + BOOST_TEST_NOT( v1 >= v2 ); + } + + { + variant v1, v2; + + BOOST_TEST_NOT( v1 < v2 ); + BOOST_TEST_NOT( v1 > v2 ); + BOOST_TEST( v1 <= v2 ); + BOOST_TEST( v1 >= v2 ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_lt_gt_cx.cpp b/src/boost/libs/variant2/test/variant_lt_gt_cx.cpp new file mode 100644 index 000000000..6d1dfc9cb --- /dev/null +++ b/src/boost/libs/variant2/test/variant_lt_gt_cx.cpp @@ -0,0 +1,100 @@ + +// Copyright 2017, 2019 Peter Dimov +// +// 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 +using namespace boost::variant2; + +#if !defined(BOOST_MP11_HAS_CXX14_CONSTEXPR) + +#include + +BOOST_PRAGMA_MESSAGE("Skipping constexpr op<, op<= test because BOOST_MP11_HAS_CXX14_CONSTEXPR is not defined") + +int main() {} + +#else + +struct X +{ +}; + +inline constexpr bool operator<( X const&, X const& ) { return false; } +inline constexpr bool operator>( X const&, X const& ) { return false; } +inline constexpr bool operator<=( X const&, X const& ) { return false; } +inline constexpr bool operator>=( X const&, X const& ) { return false; } + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +#define TEST_EQ( v1, v2 ) \ + STATIC_ASSERT( !(v1 < v2) ); \ + STATIC_ASSERT( !(v1 > v2) ); \ + STATIC_ASSERT( v1 <= v2 ); \ + STATIC_ASSERT( v1 >= v2 ); + +#define TEST_LE( v1, v3 ) \ + STATIC_ASSERT( v1 < v3 ); \ + STATIC_ASSERT( v3 > v1 ); \ + STATIC_ASSERT( !(v1 > v3) ); \ + STATIC_ASSERT( !(v3 < v1) ); \ + STATIC_ASSERT( v1 <= v3 ); \ + STATIC_ASSERT( v3 >= v1 ); \ + STATIC_ASSERT( !(v1 >= v3) ); \ + STATIC_ASSERT( !(v3 <= v1) ); + +int main() +{ + { + constexpr variant v1, v2, v3( 1 ), v4( 1 ); + + TEST_EQ( v1, v2 ) + TEST_LE( v1, v3 ) + TEST_EQ( v3, v4 ) + } + + { + constexpr variant v1, v2, v3( 1 ), v4( 1 ), v5( 3.14f ), v6( 3.14f ); + + TEST_EQ( v1, v2 ) + TEST_LE( v1, v3 ) + TEST_EQ( v3, v4 ) + TEST_LE( v1, v5 ) + TEST_LE( v3, v5 ) + TEST_EQ( v5, v6 ) + } + + { + constexpr variant v1, v2, v3( in_place_index_t<1>{} ), v4( in_place_index_t<1>{} ), v5( 3.14f ), v6( 3.14f ); + + TEST_EQ( v1, v2 ) + TEST_LE( v1, v3 ) + TEST_EQ( v3, v4 ) + TEST_LE( v1, v5 ) + TEST_LE( v3, v5 ) + TEST_EQ( v5, v6 ) + } + + { + constexpr variant v1, v2; + + STATIC_ASSERT( !(v1 < v2) ); + STATIC_ASSERT( !(v1 > v2) ); + STATIC_ASSERT( !(v1 <= v2) ); + STATIC_ASSERT( !(v1 >= v2) ); + } + + { + constexpr variant v1, v2; + + STATIC_ASSERT( !(v1 < v2) ); + STATIC_ASSERT( !(v1 > v2) ); + STATIC_ASSERT( v1 <= v2 ); + STATIC_ASSERT( v1 >= v2 ); + } +} + +#endif diff --git a/src/boost/libs/variant2/test/variant_many_types.cpp b/src/boost/libs/variant2/test/variant_many_types.cpp new file mode 100644 index 000000000..70c1645c4 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_many_types.cpp @@ -0,0 +1,109 @@ + +// Copyright 2020 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#if defined(_MSC_VER) +# pragma warning( disable: 4503 ) // decorated name length exceeded +#endif + +#include +#include +#include + +using namespace boost::mp11; + +template struct X +{ + static int const value = I::value; + int v_; +}; + +template int const X::value; + +template struct Y +{ + static int const value = I::value; + int v_; + + Y() = default; + Y( Y const& ) = default; + + explicit Y( int v ): v_( v ) {} + + Y& operator=( Y const& ) noexcept = default; + + Y& operator=( Y&& r ) noexcept + { + v_ = r.v_; + return *this; + } +}; + +template int const Y::value; + +template struct Z +{ + static int const value = I::value; + int v_; + + ~Z() {} +}; + +template int const Z::value; + +template struct F1 +{ + template void operator()( T ) const + { + int const i = T::value; + + T t{ i * 2 }; + + using boost::variant2::get; + + { + V v( t ); + + BOOST_TEST_EQ( v.index(), i ); + BOOST_TEST_EQ( get( v ).v_, t.v_ ); + BOOST_TEST_EQ( get( v ).v_, t.v_ ); + } + + { + V const v( t ); + + BOOST_TEST_EQ( v.index(), i ); + BOOST_TEST_EQ( get( v ).v_, t.v_ ); + BOOST_TEST_EQ( get( v ).v_, t.v_ ); + } + + { + V v; + + v = t; + + BOOST_TEST_EQ( v.index(), i ); + BOOST_TEST_EQ( get( v ).v_, t.v_ ); + BOOST_TEST_EQ( get( v ).v_, t.v_ ); + } + } +}; + +template void test() +{ + mp_for_each( F1() ); +} + +int main() +{ + int const N = 32; + + using V = mp_rename, boost::variant2::variant>; + + test< mp_transform >(); + test< mp_transform >(); + test< mp_transform >(); + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_move_assign.cpp b/src/boost/libs/variant2/test/variant_move_assign.cpp new file mode 100644 index 000000000..632f06812 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_move_assign.cpp @@ -0,0 +1,194 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ + int v; + + X1(): v(0) {} + explicit X1(int v): v(v) {} + X1(X1 const& r): v(r.v) {} + X1(X1&& r): v(r.v) {} + X1& operator=( X1 const& r ) { v = r.v; return *this; } + X1& operator=( X1&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct X2 +{ + int v; + + X2(): v(0) {} + explicit X2(int v): v(v) {} + X2(X2 const& r): v(r.v) {} + X2(X2&& r): v(r.v) {} + X2& operator=( X2 const& r ) { v = r.v; return *this; } + X2& operator=( X2&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct Y +{ + Y& operator=( Y&& ) = delete; +}; + +int main() +{ + { + variant v; + BOOST_TEST_EQ( get<0>(v), 0 ); + + variant v2( 1 ); + + v = std::move(v2); + BOOST_TEST_EQ( get<0>(v), 1 ); + + variant v3( 2 ); + + v = std::move(v3); + BOOST_TEST_EQ( get<0>(v), 2 ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + variant v2( 1 ); + + v = std::move(v2); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + + variant v3( 3.14f ); + + v = std::move(v3); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.14f ); + + variant v4( 3.15f ); + + v = std::move(v4); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.15f ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + variant v2( in_place_index_t<1>{}, 1 ); + + v = std::move(v2); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 1 ); + + variant v3( 3.14f ); + + v = std::move(v3); + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.14f ); + + variant v4( 3.15f ); + + v = std::move(v4); + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.15f ); + + variant v5( "s1" ); + + v = std::move(v5); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s1") ); + + variant v6( "s2" ); + + v = std::move(v6); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s2") ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 0 ); + + variant v2( X1{1} ); + + v = std::move(v2); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + + variant v3( in_place_index_t<1>{}, 2 ); + + v = std::move(v3); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 2 ); + + variant v4( in_place_index_t<1>{}, 3 ); + + v = std::move(v4); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 3 ); + + variant v5( in_place_index_t<0>{}, 4 ); + + v = std::move(v5); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 4 ); + } + + { + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable>)); + + BOOST_TEST_TRAIT_TRUE((std::is_move_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_move_assignable>)); + BOOST_TEST_TRAIT_FALSE((std::is_move_assignable>)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_move_assign_cx.cpp b/src/boost/libs/variant2/test/variant_move_assign_cx.cpp new file mode 100644 index 000000000..d7ab65fa7 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_move_assign_cx.cpp @@ -0,0 +1,107 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +enum E +{ + v +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr T test( A&& a ) +{ + V v; + + v = std::forward(a); + + return get(v); +} + +int main() +{ + { + constexpr auto w = test, int>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, X>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr auto w = test, int>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, float>( variant( 3.0f ) ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, float>( variant( 3.0f ) ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, X>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, X>( variant( X(1) ) ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, Y>( variant( Y(1) ) ); + STATIC_ASSERT( w == 1 ); + } + +#endif +} diff --git a/src/boost/libs/variant2/test/variant_move_assign_throw.cpp b/src/boost/libs/variant2/test/variant_move_assign_throw.cpp new file mode 100644 index 000000000..c1df59442 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_move_assign_throw.cpp @@ -0,0 +1,55 @@ + +// Copyright 2019 Peter Dimov +// +// 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 + +#if defined(_MSC_VER) +# pragma warning( disable: 4702 ) // unreachable code +#endif + +#include +#include +#include + +using namespace boost::variant2; + +struct Y1 +{ + Y1() noexcept {} // =default fails on msvc-14.0 + + Y1(Y1&&) + { + throw std::runtime_error( "Y1(Y1&&)" ); + } + + Y1& operator=(Y1&&) = default; +}; + +struct Y2 +{ + Y2() noexcept {} + + Y2(Y2&&) + { + throw std::runtime_error( "Y2(Y2&&)" ); + } + + Y2& operator=(Y2&&) = default; +}; + +void test() +{ + variant v1( in_place_type_t{} ); + variant v2( in_place_type_t{} ); + + BOOST_TEST_THROWS( v1 = std::move( v2 ), std::runtime_error ) +} + +int main() +{ + test(); + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_move_construct.cpp b/src/boost/libs/variant2/test/variant_move_construct.cpp new file mode 100644 index 000000000..dd83c8c18 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_move_construct.cpp @@ -0,0 +1,146 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ + X1() {} + X1(X1 const&) {} + X1(X1&&) {} +}; + +inline bool operator==( X1, X1 ) { return true; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); + +struct X2 +{ + X2() {} + X2(X2 const&) {} + X2(X2&&) {} +}; + +inline bool operator==( X2, X2 ) { return true; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); + +struct Y +{ + Y( Y&& ) = delete; +}; + +template static void test( V&& v ) +{ + V v2( v ); + V v3( std::move(v) ); + + BOOST_TEST_EQ( v2.index(), v3.index() ); + BOOST_TEST( v2 == v3 ); +} + +int main() +{ + test( variant() ); + test( variant(1) ); + + test( variant() ); + test( variant(1) ); + + test( variant() ); + test( variant(1) ); + test( variant(3.14f) ); + + test( variant() ); + test( variant(1) ); + test( variant(3.14f) ); + + test( variant() ); + test( variant("test") ); + + test( variant() ); + test( variant("test") ); + + test( variant() ); + test( variant(1) ); + test( variant(3.14f) ); + test( variant("test") ); + + test( variant() ); + + test( variant() ); + test( variant(3.14f) ); + + test( variant() ); + + test( variant("test") ); + + test( variant() ); + + test( variant() ); + + test( variant() ); + test( variant() ); + test( variant() ); + test( variant() ); + + { + variant v; + v.emplace(); + + test( std::move(v) ); + } + + { + variant v; + v.emplace(); + + test( std::move(v) ); + } + + { + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible>)); + BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible>)); + + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible>)); + BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible>)); + + BOOST_TEST_TRAIT_TRUE((std::is_move_constructible>)); + +#if !BOOST_WORKAROUND( BOOST_MSVC, <= 1910 ) + + BOOST_TEST_TRAIT_FALSE((std::is_move_constructible>)); + +#endif + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_move_construct_cx.cpp b/src/boost/libs/variant2/test/variant_move_construct_cx.cpp new file mode 100644 index 000000000..1e49b7f88 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_move_construct_cx.cpp @@ -0,0 +1,104 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +enum E +{ + v +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr T test( V&& v ) +{ + V v2( std::forward(v) ); + return get( v2 ); +} + +int main() +{ + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test( variant( 3.0f ) ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test( variant( 3.0f ) ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test( variant( X(1) ) ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test( variant( Y(1) ) ); + STATIC_ASSERT( w == 1 ); + } + +#endif +} diff --git a/src/boost/libs/variant2/test/variant_move_construct_throw.cpp b/src/boost/libs/variant2/test/variant_move_construct_throw.cpp new file mode 100644 index 000000000..aabf7d268 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_move_construct_throw.cpp @@ -0,0 +1,69 @@ + +// Copyright 2019 Peter Dimov +// +// 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 + +#if defined(_MSC_VER) +# pragma warning( disable: 4702 ) // unreachable code +#endif + +#include +#include +#include + +using namespace boost::variant2; + +struct X +{ + static int instances; + + X() + { + ++instances; + } + + X( X const& ) + { + throw std::runtime_error( "X(X const&)" ); + } + + ~X() + { + --instances; + } +}; + +int X::instances = 0; + +void test() +{ + X::instances = 0; + + { + variant v1; + + BOOST_TEST_EQ( X::instances, 1 ); + + try + { + variant v2( std::move( v1 ) ); + BOOST_TEST_EQ( X::instances, 2 ); + } + catch( std::exception const& ) + { + } + + BOOST_TEST_EQ( X::instances, 1 ); + } + + BOOST_TEST_EQ( X::instances, 0 ); +} + +int main() +{ + test(); + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_ostream_insert.cpp b/src/boost/libs/variant2/test/variant_ostream_insert.cpp new file mode 100644 index 000000000..fca7fe611 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_ostream_insert.cpp @@ -0,0 +1,43 @@ +// Copyright 2021 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include + +using namespace boost::variant2; + +template std::string to_string( T const& t ) +{ + std::ostringstream os; + + os << t; + + return os.str(); +} + +int main() +{ + { + BOOST_TEST_EQ( to_string( monostate() ), "monostate" ); + } + + { + variant v; + + BOOST_TEST_EQ( to_string( v ), to_string( monostate() ) ); + + v = 1; + BOOST_TEST_EQ( to_string( v ), to_string( 1 ) ); + + v = 3.14f; + BOOST_TEST_EQ( to_string( v ), to_string( 3.14f ) ); + + v = "test"; + BOOST_TEST_EQ( to_string( v ), to_string( "test" ) ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_size.cpp b/src/boost/libs/variant2/test/variant_size.cpp new file mode 100644 index 000000000..81e63ac18 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_size.cpp @@ -0,0 +1,104 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; +using namespace boost::mp11; + +template using var_size_t = mp_size_t::value>; + +int main() +{ + BOOST_TEST_EQ( (variant_size>::value), 0 ); + BOOST_TEST_EQ( (variant_size const>::value), 0 ); + BOOST_TEST_EQ( (variant_size volatile>::value), 0 ); + BOOST_TEST_EQ( (variant_size const volatile>::value), 0 ); + + BOOST_TEST_EQ( (variant_size&>::value), 0 ); + BOOST_TEST_EQ( (variant_size const&>::value), 0 ); + BOOST_TEST_EQ( (variant_size&&>::value), 0 ); + BOOST_TEST_EQ( (variant_size const&&>::value), 0 ); + + BOOST_TEST_EQ( (variant_size>::value), 1 ); + BOOST_TEST_EQ( (variant_size const>::value), 1 ); + BOOST_TEST_EQ( (variant_size volatile>::value), 1 ); + BOOST_TEST_EQ( (variant_size const volatile>::value), 1 ); + + BOOST_TEST_EQ( (variant_size&>::value), 1 ); + BOOST_TEST_EQ( (variant_size const&>::value), 1 ); + BOOST_TEST_EQ( (variant_size&&>::value), 1 ); + BOOST_TEST_EQ( (variant_size const&&>::value), 1 ); + + BOOST_TEST_EQ( (variant_size>::value), 2 ); + BOOST_TEST_EQ( (variant_size const>::value), 2 ); + BOOST_TEST_EQ( (variant_size volatile>::value), 2 ); + BOOST_TEST_EQ( (variant_size const volatile>::value), 2 ); + + BOOST_TEST_EQ( (variant_size&>::value), 2 ); + BOOST_TEST_EQ( (variant_size const&>::value), 2 ); + BOOST_TEST_EQ( (variant_size&&>::value), 2 ); + BOOST_TEST_EQ( (variant_size const&&>::value), 2 ); + + BOOST_TEST_EQ( (variant_size>::value), 3 ); + BOOST_TEST_EQ( (variant_size const>::value), 3 ); + BOOST_TEST_EQ( (variant_size volatile>::value), 3 ); + BOOST_TEST_EQ( (variant_size const volatile>::value), 3 ); + + BOOST_TEST_EQ( (variant_size&>::value), 3 ); + BOOST_TEST_EQ( (variant_size const&>::value), 3 ); + BOOST_TEST_EQ( (variant_size&&>::value), 3 ); + BOOST_TEST_EQ( (variant_size const&&>::value), 3 ); + + BOOST_TEST_EQ( (variant_size>::value), 4 ); + BOOST_TEST_EQ( (variant_size const>::value), 4 ); + BOOST_TEST_EQ( (variant_size volatile>::value), 4 ); + BOOST_TEST_EQ( (variant_size const volatile>::value), 4 ); + + BOOST_TEST_EQ( (variant_size&>::value), 4 ); + BOOST_TEST_EQ( (variant_size const&>::value), 4 ); + BOOST_TEST_EQ( (variant_size&&>::value), 4 ); + BOOST_TEST_EQ( (variant_size const&&>::value), 4 ); + + variant_size(); + variant_size(); + variant_size(); + variant_size(); + + variant_size(); + variant_size(); + variant_size(); + variant_size(); + + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + + BOOST_TEST_TRAIT_TRUE((mp_valid>)); + BOOST_TEST_TRAIT_TRUE((mp_valid const>)); + BOOST_TEST_TRAIT_TRUE((mp_valid volatile>)); + BOOST_TEST_TRAIT_TRUE((mp_valid const volatile>)); + + BOOST_TEST_TRAIT_TRUE((mp_valid&>)); + BOOST_TEST_TRAIT_TRUE((mp_valid const&>)); + BOOST_TEST_TRAIT_TRUE((mp_valid&&>)); + BOOST_TEST_TRAIT_TRUE((mp_valid const&&>)); + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_special.cpp b/src/boost/libs/variant2/test/variant_special.cpp new file mode 100644 index 000000000..956d927b3 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_special.cpp @@ -0,0 +1,103 @@ +// Copyright 2020 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#if defined(_MSC_VER) && _MSC_VER < 1910 +# pragma warning(disable: 4503) // decorated name length exceeded +#endif + +#include +#include +#include +#include + +#include +using namespace boost::mp11; + +// + +using namespace boost::variant2; + +struct D +{ + ~D() noexcept {} +}; + +struct CC1 +{ + CC1( CC1 const& ) {} +}; + +struct CC2 +{ + CC2( CC2 const& ) = delete; +}; + +struct MC1 +{ + MC1( MC1 && ) {} +}; + +struct MC2 +{ + MC2( MC2 && ) = delete; +}; + +struct CA1 +{ + CA1& operator=( CA1 const& ) { return *this; } +}; + +struct CA2 +{ + CA2& operator=( CA2 const& ) = delete; +}; + +struct MA1 +{ + MA1& operator=( MA1 && ) { return *this; } +}; + +struct MA2 +{ + MA2& operator=( MA2 && ) = delete; +}; + +struct test +{ + template void operator()( mp_list ) const noexcept + { + using U = mp_inherit; + +#if !BOOST_WORKAROUND( __GNUC__, < 5 ) + + BOOST_TEST_EQ( std::is_copy_constructible>::value, std::is_copy_constructible::value ); + BOOST_TEST_EQ( std::is_nothrow_copy_constructible>::value, std::is_nothrow_copy_constructible::value ); + +#endif + +#if !BOOST_WORKAROUND(BOOST_MSVC, < 1910) + + BOOST_TEST_EQ( std::is_move_constructible>::value, std::is_move_constructible::value ); + +#else + + BOOST_TEST_GE( std::is_move_constructible>::value, std::is_move_constructible::value ); + +#endif + + BOOST_TEST_EQ( std::is_nothrow_move_constructible>::value, std::is_nothrow_move_constructible::value ); + + BOOST_TEST_EQ( std::is_copy_assignable>::value, std::is_copy_constructible::value && std::is_copy_assignable::value ); + BOOST_TEST_EQ( std::is_nothrow_copy_assignable>::value, std::is_nothrow_copy_constructible::value && std::is_copy_assignable::value ); + + BOOST_TEST_EQ( std::is_move_assignable>::value, std::is_move_constructible::value && std::is_move_assignable::value ); + BOOST_TEST_EQ( std::is_nothrow_move_assignable>::value, std::is_nothrow_move_constructible::value && std::is_move_assignable::value ); + } +}; + +int main() +{ + mp_for_each< mp_power_set< mp_list > >( test() ); + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_subset.cpp b/src/boost/libs/variant2/test/variant_subset.cpp new file mode 100644 index 000000000..90c9bc211 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_subset.cpp @@ -0,0 +1,159 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +#if defined(_MSC_VER) +# pragma warning( disable: 4702 ) // unreachable code +#endif + +#include +#include +#include +#include +#include +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ + int v; + + X1(): v(0) {} + explicit X1(int v): v(v) {} + X1(X1 const& r): v(r.v) {} + X1(X1&& r): v(r.v) {} + X1& operator=( X1 const& r ) { v = r.v; return *this; } + X1& operator=( X1&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct X2 +{ + int v; + + X2(): v(0) {} + explicit X2(int v): v(v) {} + X2(X2 const& r): v(r.v) {} + X2(X2&& r): v(r.v) {} + X2& operator=( X2 const& r ) { v = r.v; return *this; } + X2& operator=( X2&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +int main() +{ + { + variant v1( 1 ); + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ), get( v2 ) ); + + BOOST_TEST_THROWS( v1.subset(), bad_variant_access ); + + variant v3 = std::move(v1).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + + BOOST_TEST_THROWS( std::move(v1).subset(), bad_variant_access ); + } + + { + variant const v1( 1 ); + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ), get( v2 ) ); + + BOOST_TEST_THROWS( v1.subset(), bad_variant_access ); + + variant v3 = std::move(v1).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + + BOOST_TEST_THROWS( std::move(v1).subset(), bad_variant_access ); + } + + { + variant v1( 1 ); + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ), get( v2 ) ); + + variant v3 = std::move(v1).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant v1( 1 ); + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ), get( v2 ) ); + + variant v3 = std::move(v1).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant v1( "s1" ); + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ), get( v2 ) ); + + variant v3 = std::move(v1).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant v1{ X1{1} }; + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ).v, get( v2 ).v ); + + variant v3 = std::move( v1 ).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ).v, get( v3 ).v ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_swap.cpp b/src/boost/libs/variant2/test/variant_swap.cpp new file mode 100644 index 000000000..3e80f0f2a --- /dev/null +++ b/src/boost/libs/variant2/test/variant_swap.cpp @@ -0,0 +1,259 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ + int v; + + X1(): v(0) {} + explicit X1(int v): v(v) {} + X1(X1 const& r): v(r.v) {} + X1(X1&& r): v(r.v) {} + X1& operator=( X1 const& r ) { v = r.v; return *this; } + X1& operator=( X1&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct X2 +{ + int v; + + X2(): v(0) {} + explicit X2(int v): v(v) {} + X2(X2 const& r): v(r.v) {} + X2(X2&& r): v(r.v) {} + X2& operator=( X2 const& r ) { v = r.v; return *this; } + X2& operator=( X2&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +int main() +{ + { + variant v; + BOOST_TEST_EQ( get<0>(v), 0 ); + + variant v2( 1 ); + BOOST_TEST_EQ( get<0>(v2), 1 ); + + swap( v, v2 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + BOOST_TEST_EQ( get<0>(v2), 0 ); + + variant v3( 2 ); + BOOST_TEST_EQ( get<0>(v3), 2 ); + + swap( v, v3 ); + BOOST_TEST_EQ( get<0>(v), 2 ); + BOOST_TEST_EQ( get<0>(v3), 1 ); + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + variant v2( 1 ); + + BOOST_TEST_EQ( v2.index(), 0 ); + BOOST_TEST_EQ( get<0>(v2), 1 ); + + swap( v, v2 ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + + BOOST_TEST_EQ( v2.index(), 0 ); + BOOST_TEST_EQ( get<0>(v2), 0 ); + + variant v3( 3.14f ); + + BOOST_TEST_EQ( v3.index(), 1 ); + BOOST_TEST_EQ( get<1>(v3), 3.14f ); + + swap( v, v3 ); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.14f ); + + BOOST_TEST_EQ( v3.index(), 0 ); + BOOST_TEST_EQ( get<0>(v3), 1 ); + + variant v4( 3.15f ); + + BOOST_TEST_EQ( v4.index(), 1 ); + BOOST_TEST_EQ( get<1>(v4), 3.15f ); + + swap( v, v4 ); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.15f ); + + BOOST_TEST_EQ( v4.index(), 1 ); + BOOST_TEST_EQ( get<1>(v4), 3.14f ); + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + variant v2( in_place_index_t<1>{}, 1 ); + + BOOST_TEST_EQ( v2.index(), 1 ); + BOOST_TEST_EQ( get<1>(v2), 1 ); + + swap( v, v2 ); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 1 ); + + BOOST_TEST_EQ( v2.index(), 0 ); + BOOST_TEST_EQ( get<0>(v2), 0 ); + + variant v3( 3.14f ); + + BOOST_TEST_EQ( v3.index(), 2 ); + BOOST_TEST_EQ( get<2>(v3), 3.14f ); + + swap( v, v3 ); + + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.14f ); + + BOOST_TEST_EQ( v3.index(), 1 ); + BOOST_TEST_EQ( get<1>(v3), 1 ); + + variant v4( 3.15f ); + + BOOST_TEST_EQ( v4.index(), 2 ); + BOOST_TEST_EQ( get<2>(v4), 3.15f ); + + swap( v, v4 ); + + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.15f ); + + BOOST_TEST_EQ( v4.index(), 2 ); + BOOST_TEST_EQ( get<2>(v4), 3.14f ); + + variant v5( "s1" ); + + BOOST_TEST_EQ( v5.index(), 3 ); + BOOST_TEST_EQ( get<3>(v5), std::string("s1") ); + + swap( v, v5 ); + + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s1") ); + + BOOST_TEST_EQ( v5.index(), 2 ); + BOOST_TEST_EQ( get<2>(v5), 3.15f ); + + variant v6( "s2" ); + + BOOST_TEST_EQ( v6.index(), 3 ); + BOOST_TEST_EQ( get<3>(v6), std::string("s2") ); + + swap( v, v6 ); + + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s2") ); + + BOOST_TEST_EQ( v6.index(), 3 ); + BOOST_TEST_EQ( get<3>(v6), std::string("s1") ); + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 0 ); + + variant v2( X1{1} ); + + BOOST_TEST_EQ( v2.index(), 0 ); + BOOST_TEST_EQ( get<0>(v2).v, 1 ); + + swap( v, v2 ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + + BOOST_TEST_EQ( v2.index(), 0 ); + BOOST_TEST_EQ( get<0>(v2).v, 0 ); + + variant v3( in_place_index_t<1>{}, 2 ); + + BOOST_TEST_EQ( v3.index(), 1 ); + BOOST_TEST_EQ( get<1>(v3).v, 2 ); + + swap( v, v3 ); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 2 ); + + BOOST_TEST_EQ( v3.index(), 0 ); + BOOST_TEST_EQ( get<0>(v3).v, 1 ); + + variant v4( in_place_index_t<1>{}, 3 ); + + BOOST_TEST_EQ( v4.index(), 1 ); + BOOST_TEST_EQ( get<1>(v4).v, 3 ); + + swap( v, v4 ); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 3 ); + + BOOST_TEST_EQ( v4.index(), 1 ); + BOOST_TEST_EQ( get<1>(v4).v, 2 ); + + variant v5( in_place_index_t<0>{}, 4 ); + + BOOST_TEST_EQ( v5.index(), 0 ); + BOOST_TEST_EQ( get<0>(v5).v, 4 ); + + swap( v, v5 ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 4 ); + + BOOST_TEST_EQ( v5.index(), 1 ); + BOOST_TEST_EQ( get<1>(v5).v, 3 ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_trivial.cpp b/src/boost/libs/variant2/test/variant_trivial.cpp new file mode 100644 index 000000000..c8137842a --- /dev/null +++ b/src/boost/libs/variant2/test/variant_trivial.cpp @@ -0,0 +1,95 @@ +// Copyright 2020 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#if defined(_MSC_VER) && _MSC_VER < 1910 +# pragma warning(disable: 4503) // decorated name length exceeded +#endif + +#include +#include +#include +#include + +#include +using namespace boost::mp11; + +// + +struct D +{ + ~D() noexcept {} +}; + +struct CC1 +{ + CC1( CC1 const& ) noexcept {} +}; + +struct CC2 +{ + CC2( CC2 const& ) = delete; +}; + +struct MC1 +{ + MC1( MC1 && ) noexcept {} +}; + +struct MC2 +{ + MC2( MC2 && ) = delete; +}; + +struct CA1 +{ + CA1& operator=( CA1 const& ) noexcept { return *this; } +}; + +struct CA2 +{ + CA2& operator=( CA2 const& ) = delete; +}; + +struct MA1 +{ + MA1& operator=( MA1 && ) noexcept { return *this; } +}; + +struct MA2 +{ + MA2& operator=( MA2 && ) = delete; +}; + +using namespace boost::variant2; +namespace v2d = boost::variant2::detail; + +struct test +{ + template void operator()( mp_list ) const noexcept + { + using U = mp_inherit; + +#if !BOOST_WORKAROUND( __GNUC__, < 5 ) + + BOOST_TEST_EQ( v2d::is_trivially_copy_constructible>::value, v2d::is_trivially_copy_constructible::value ); + BOOST_TEST_EQ( v2d::is_trivially_copy_assignable>::value, std::is_trivially_destructible::value && v2d::is_trivially_copy_constructible::value && v2d::is_trivially_copy_assignable::value ); + +#endif + + BOOST_TEST_EQ( std::is_trivially_destructible>::value, std::is_trivially_destructible::value ); + +#if !BOOST_WORKAROUND(BOOST_LIBSTDCXX_VERSION, < 50000) + + BOOST_TEST_EQ( v2d::is_trivially_move_constructible>::value, v2d::is_trivially_move_constructible::value ); + BOOST_TEST_EQ( v2d::is_trivially_move_assignable>::value, std::is_trivially_destructible::value && v2d::is_trivially_move_constructible::value && v2d::is_trivially_move_assignable::value ); + +#endif + } +}; + +int main() +{ + mp_for_each< mp_power_set< mp_list > >( test() ); + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_value_assign.cpp b/src/boost/libs/variant2/test/variant_value_assign.cpp new file mode 100644 index 000000000..2519adc08 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_value_assign.cpp @@ -0,0 +1,206 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ + int v; + + X1(): v(0) {} + explicit X1(int v): v(v) {} + X1(X1 const& r): v(r.v) {} + X1(X1&& r): v(r.v) {} + X1& operator=( X1 const& r ) { v = r.v; return *this; } + X1& operator=( X1&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct X2 +{ + int v; + + X2(): v(0) {} + explicit X2(int v): v(v) {} + X2(X2 const& r): v(r.v) {} + X2(X2&& r): v(r.v) {} + X2& operator=( X2 const& r ) { v = r.v; return *this; } + X2& operator=( X2&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +int main() +{ + { + variant v; + BOOST_TEST_EQ( get<0>(v), 0 ); + + v = 1; + BOOST_TEST_EQ( get<0>(v), 1 ); + + v = 2; + BOOST_TEST_EQ( get<0>(v), 2 ); + + int w1 = 3; + + v = w1; + BOOST_TEST_EQ( get<0>(v), 3 ); + + int const w2 = 4; + + v = w2; + BOOST_TEST_EQ( get<0>(v), 4 ); + + v = std::move( w1 ); + BOOST_TEST_EQ( get<0>(v), 3 ); + + v = std::move( w2 ); + BOOST_TEST_EQ( get<0>(v), 4 ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + v = 1; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + + v = 3.14f; + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.14f ); + + float w1 = 3.15f; + + v = w1; + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.15f ); + + int const w2 = 2; + + v = w2; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 2 ); + + v = std::move(w1); + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v), 3.15f ); + + v = std::move(w2); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 2 ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 0 ); + + v = 3.14f; + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.14f ); + + float const w1 = 3.15f; + + v = w1; + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.15f ); + + variant v5( "s1" ); + + v = "s1"; + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s1") ); + + std::string w2( "s2" ); + + v = w2; + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s2") ); + + v = std::move(w1); + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST_EQ( get<2>(v), 3.15f ); + + v = std::move(w2); + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST_EQ( get<3>(v), std::string("s2") ); + } + + { + variant v; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 0 ); + + v = X1{1}; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 1 ); + + v = X2{2}; + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 2 ); + + X1 w1{3}; + + v = w1; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 3 ); + + X1 const w2{4}; + + v = w2; + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 4 ); + + X2 w3{5}; + + v = w3; + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 5 ); + + X2 const w4{6}; + + v = w4; + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST_EQ( get<1>(v).v, 6 ); + + v = std::move(w1); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 3 ); + + v = std::move(w2); + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v).v, 4 ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_value_assign_cx.cpp b/src/boost/libs/variant2/test/variant_value_assign_cx.cpp new file mode 100644 index 000000000..0a29a0afd --- /dev/null +++ b/src/boost/libs/variant2/test/variant_value_assign_cx.cpp @@ -0,0 +1,106 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +enum E +{ + v +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr A test( A const& a ) +{ + V v; + + v = a; + + return get(v); +} + +int main() +{ + { + constexpr auto w = test, int>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, X>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr auto w = test, int>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, float>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, float>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, X>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, X>( X(1) ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, Y>( Y(1) ); + STATIC_ASSERT( w == 1 ); + } + +#endif +} diff --git a/src/boost/libs/variant2/test/variant_value_construct.cpp b/src/boost/libs/variant2/test/variant_value_construct.cpp new file mode 100644 index 000000000..56df89679 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_value_construct.cpp @@ -0,0 +1,124 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 +#include +#include + +using namespace boost::variant2; + +struct X +{ + operator int() const { return 2; } +}; + +int main() +{ + { + variant v( 1 ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + } + + { + variant v( 'a' ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 'a' ); + } + + { + variant v( X{} ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 2 ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 1 ); + } + + { + variant v( 'a' ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 'a' ); + } + + { + variant v( X{} ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST_EQ( get<0>(v), 2 ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST( holds_alternative(v) ); + BOOST_TEST_EQ( get<0>(v), 1 ); + } + + { + variant v( 'a' ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST( holds_alternative(v) ); + BOOST_TEST_EQ( get<0>(v), 'a' ); + } + + { + variant v( X{} ); + + BOOST_TEST_EQ( v.index(), 0 ); + BOOST_TEST( holds_alternative(v) ); + BOOST_TEST_EQ( get<0>(v), 2 ); + } + + { + variant v( 3.14f ); + + BOOST_TEST_EQ( v.index(), 1 ); + BOOST_TEST( holds_alternative(v) ); + BOOST_TEST_EQ( get<1>(v), 3.14f ); + } + + { + variant v( "text" ); + + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST( holds_alternative(v) ); + BOOST_TEST_EQ( get<2>(v), std::string("text") ); + } + + { + variant v( 3.14f ); + + BOOST_TEST_EQ( v.index(), 2 ); + BOOST_TEST( holds_alternative(v) ); + BOOST_TEST_EQ( get<2>(v), 3.14f ); + } + + { + variant v( "text" ); + + BOOST_TEST_EQ( v.index(), 3 ); + BOOST_TEST( holds_alternative(v) ); + BOOST_TEST_EQ( get<3>(v), std::string("text") ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_value_construct_cx.cpp b/src/boost/libs/variant2/test/variant_value_construct_cx.cpp new file mode 100644 index 000000000..5ffad252a --- /dev/null +++ b/src/boost/libs/variant2/test/variant_value_construct_cx.cpp @@ -0,0 +1,109 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +using namespace boost::variant2; + +struct X +{ + constexpr operator int() const { return 2; } +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +int main() +{ + { + constexpr variant v( 1 ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 1 ); + } + + { + constexpr variant v( 'a' ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 'a' ); + } + + { + constexpr variant v( X{} ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 2 ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 1 ); + } + + { + constexpr variant v( 'a' ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 'a' ); + } + + { + constexpr variant v( X{} ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( get<0>(v) == 2 ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( holds_alternative(v) ); + STATIC_ASSERT( get<0>(v) == 1 ); + } + + { + constexpr variant v( 'a' ); + + STATIC_ASSERT( v.index() == 0 ); + STATIC_ASSERT( holds_alternative(v) ); + STATIC_ASSERT( get<0>(v) == 'a' ); + } + + { + constexpr variant v( 3.14f ); + + STATIC_ASSERT( v.index() == 1 ); + STATIC_ASSERT( holds_alternative(v) ); + STATIC_ASSERT( get<1>(v) == 3.14f ); + } + + { + constexpr variant v( X{} ); + + STATIC_ASSERT( v.index() == 2 ); + STATIC_ASSERT( holds_alternative(v) ); + } + + { + constexpr variant v( 3.14f ); + + STATIC_ASSERT( v.index() == 2 ); + STATIC_ASSERT( holds_alternative(v) ); + STATIC_ASSERT( get<2>(v) == 3.14f ); + } + + { + constexpr variant v( X{} ); + + STATIC_ASSERT( v.index() == 3 ); + STATIC_ASSERT( holds_alternative(v) ); + } +} diff --git a/src/boost/libs/variant2/test/variant_valueless.cpp b/src/boost/libs/variant2/test/variant_valueless.cpp new file mode 100644 index 000000000..d5d18d776 --- /dev/null +++ b/src/boost/libs/variant2/test/variant_valueless.cpp @@ -0,0 +1,260 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +#if defined(_MSC_VER) +# pragma warning( disable: 4702 ) // unreachable code +#endif + +#include +#include +#include +#include +#include +#include + +using namespace boost::variant2; +namespace v2d = boost::variant2::detail; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +// + +enum E1 { e1 }; +enum E1x { e1x }; + +struct X1 +{ + X1() = default; + + X1( E1 ) noexcept {} + X1( E1x ) { throw std::runtime_error( "X1(E1x)" ); } +}; + +STATIC_ASSERT( std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( std::is_trivially_destructible::value ); +STATIC_ASSERT( v2d::is_trivially_move_assignable::value ); +STATIC_ASSERT( std::is_nothrow_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_constructible::value ); + +enum E2 { e2 }; +enum E2x { e2x }; + +struct X2 +{ + X2(); + ~X2(); + + X2( E2 ) noexcept {} + X2( E2x ) { throw std::runtime_error( "X2(E2x)" ); } +}; + +X2::X2() {} +X2::~X2() {} + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_trivially_destructible::value ); +STATIC_ASSERT( std::is_nothrow_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_constructible::value ); + +enum E3 { e3 }; +enum E3x { e3x }; + +struct X3 +{ + X3(); + + X3( X3 const& ) {} + X3( X3&& ) {} + + X3( E3 ) noexcept {} + X3( E3x ) { throw std::runtime_error( "X3(E3x)" ); } + + X3& operator=( X3 const& ) = default; + X3& operator=( X3&& ) = default; +}; + +X3::X3() {} + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( std::is_trivially_destructible::value ); +//STATIC_ASSERT( v2d::is_trivially_move_assignable::value ); +STATIC_ASSERT( std::is_nothrow_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_constructible::value ); + +// + +STATIC_ASSERT( std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( std::is_trivially_destructible::value ); + +// + +int main() +{ + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + + try + { + v = e1x; + BOOST_ERROR( "`v = e1x;` failed to throw" ); + } + catch( std::exception const& ) + { + // strong guarantee + BOOST_TEST_EQ( v.index(), 0 ); + } + } + + { + variant v( e2 ); + + BOOST_TEST_EQ( v.index(), 1 ); + + try + { + v = e1x; + BOOST_ERROR( "`v = e1x;` failed to throw" ); + } + catch( std::exception const& ) + { + // strong guarantee + BOOST_TEST_EQ( v.index(), 1 ); + } + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + + try + { + v = e1x; + BOOST_ERROR( "`v = e1x;` failed to throw" ); + } + catch( std::exception const& ) + { + // strong guarantee + BOOST_TEST_EQ( v.index(), 0 ); + } + } + + { + variant v( e2 ); + + BOOST_TEST_EQ( v.index(), 1 ); + + try + { + v = e1x; + BOOST_ERROR( "`v = e1x;` failed to throw" ); + } + catch( std::exception const& ) + { + // strong guarantee + BOOST_TEST_EQ( v.index(), 1 ); + } + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + + try + { + v = e3x; + BOOST_ERROR( "`v = e3x;` failed to throw" ); + } + catch( std::exception const& ) + { + // strong guarantee + BOOST_TEST_EQ( v.index(), 0 ); + } + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + + try + { + v = e3x; + BOOST_ERROR( "`v = e3x;` failed to throw" ); + } + catch( std::exception const& ) + { + // strong guarantee + BOOST_TEST_EQ( v.index(), 0 ); + } + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + + try + { + v = e3x; + BOOST_ERROR( "`v = e3x;` failed to throw" ); + } + catch( std::exception const& ) + { + // double buffered, no change + BOOST_TEST_EQ( v.index(), 0 ); + } + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + + try + { + v = e1x; + BOOST_ERROR( "`v = e1x;` failed to throw" ); + } + catch( std::exception const& ) + { + // strong guarantee + BOOST_TEST_EQ( v.index(), 0 ); + } + } + + { + variant v; + + BOOST_TEST_EQ( v.index(), 0 ); + + try + { + v = e1x; + BOOST_ERROR( "`v = e1x;` failed to throw" ); + } + catch( std::exception const& ) + { + // strong guarantee + BOOST_TEST_EQ( v.index(), 0 ); + } + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_visit.cpp b/src/boost/libs/variant2/test/variant_visit.cpp new file mode 100644 index 000000000..4f9de242f --- /dev/null +++ b/src/boost/libs/variant2/test/variant_visit.cpp @@ -0,0 +1,130 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +#if defined(_MSC_VER) +# pragma warning( disable: 4244 ) // conversion from float to int, possible loss of data +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace boost::variant2; +using boost::mp11::mp_size_t; + +struct X +{ +}; + +struct F +{ + mp_size_t<1> operator()( X& ) const; + mp_size_t<2> operator()( X const& ) const; + mp_size_t<3> operator()( X&& ) const; + mp_size_t<4> operator()( X const&& ) const; +}; + +int main() +{ + { + BOOST_TEST_EQ( (visit( []{ return 5; } )), 5 ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( (visit( []( int x ){ return x; }, v )), 1 ); + + visit( []( int x ){ BOOST_TEST_EQ( x, 1 ); }, v ); + visit( []( int x ){ BOOST_TEST_EQ( x, 1 ); }, std::move(v) ); + } + + { + variant const v( 2 ); + + BOOST_TEST_EQ( (visit( []( int x ){ return x; }, v )), 2 ); + + visit( []( int x ){ BOOST_TEST_EQ( x, 2 ); }, v ); + visit( []( int x ){ BOOST_TEST_EQ( x, 2 ); }, std::move(v) ); + } + + { + variant v( 3 ); + + BOOST_TEST_EQ( (visit( []( int x ){ return x; }, v )), 3 ); + + visit( []( int x ){ BOOST_TEST_EQ( x, 3 ); }, v ); + visit( []( int x ){ BOOST_TEST_EQ( x, 3 ); }, std::move(v) ); + } + + { + variant const v( 4 ); + + BOOST_TEST_EQ( (visit( []( int x ){ return x; }, v )), 4 ); + + visit( []( int x ){ BOOST_TEST_EQ( x, 4 ); }, v ); + visit( []( int x ){ BOOST_TEST_EQ( x, 4 ); }, std::move(v) ); + } + + { + variant v1( 1 ); + variant const v2( 3.14f ); + + BOOST_TEST_EQ( (visit( []( int x1, float x2 ){ return (int)(x1 * 1000) + (int)(x2 * 100); }, v1, v2 )), 1314 ); + + visit( []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, v1, v2 ); + visit( []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, std::move(v1), std::move(v2) ); + } + + { + variant v1( 1 ); + variant const v2( 3.14f ); + variant v3( 6.28 ); + + BOOST_TEST_EQ( (visit( []( int x1, float x2, double x3 ){ return (int)(x1 * 100) * 1000000 + (int)(x2 * 100) * 1000 + (int)(x3 * 100); }, v1, v2, v3 )), 100314628 ); + + visit( []( int x1, float x2, double x3 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); BOOST_TEST_EQ( x3, 6.28 ); }, v1, v2, v3 ); + visit( []( int x1, float x2, double x3 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); BOOST_TEST_EQ( x3, 6.28 ); }, std::move(v1), std::move(v2), std::move(v3) ); + } + + { + variant v1( 1 ); + variant const v2( 3.14f ); + variant v3( 6.28 ); + variant const v4( 'A' ); + + BOOST_TEST_EQ( (visit( []( int x1, float x2, double x3, char x4 ){ return (long long)(x1 * 100) * 100000000 + (long long)(x2 * 100) * 100000 + (long long)(x3 * 10000) + (int)x4; }, v1, v2, v3, v4 )), 10031462800 + 'A' ); + + visit( []( int x1, float x2, double x3, char x4 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); BOOST_TEST_EQ( x3, 6.28 ); BOOST_TEST_EQ( x4, 'A' ); }, v1, v2, v3, v4 ); + visit( []( int x1, float x2, double x3, char x4 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); BOOST_TEST_EQ( x3, 6.28 ); BOOST_TEST_EQ( x4, 'A' ); }, std::move(v1), std::move(v2), std::move(v3), std::move(v4) ); + } + + { + variant v; + variant const cv; + + BOOST_TEST_EQ( decltype(visit(F{}, v))::value, 1 ); + BOOST_TEST_EQ( decltype(visit(F{}, cv))::value, 2 ); + BOOST_TEST_EQ( decltype(visit(F{}, std::move(v)))::value, 3 ); + +#if !BOOST_WORKAROUND(BOOST_GCC, < 40900) + + // g++ 4.8 doesn't handle const&& particularly well + BOOST_TEST_EQ( decltype(visit(F{}, std::move(cv)))::value, 4 ); + +#endif + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_visit_by_index.cpp b/src/boost/libs/variant2/test/variant_visit_by_index.cpp new file mode 100644 index 000000000..865e5329a --- /dev/null +++ b/src/boost/libs/variant2/test/variant_visit_by_index.cpp @@ -0,0 +1,126 @@ +// Copyright 2017, 2021 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +using namespace boost::variant2; +using boost::mp11::mp_int; + +struct X +{ +}; + +struct F1 +{ + int operator()( X& ) const { return 1; } + int operator()( X const& ) const { return 2; } + int operator()( X&& ) const { return 3; } + int operator()( X const&& ) const { return 4; } +}; + +struct F2 +{ + mp_int<1> operator()( X& ) const { return {}; } + mp_int<2> operator()( X const& ) const { return {}; } + mp_int<3> operator()( X&& ) const { return {}; } + mp_int<4> operator()( X const&& ) const { return {}; } +}; + +int main() +{ + { + variant v; + + visit_by_index( v, + []( int& x ){ BOOST_TEST_EQ( x, 0 ); }, + []( int& ){ BOOST_ERROR( "incorrect alternative" ); }, + []( float& ){ BOOST_ERROR( "incorrect alternative" ); } ); + } + + { + variant v( in_place_index_t<0>(), 1 ); + + visit_by_index( v, + []( int const& x ){ BOOST_TEST_EQ( x, 1 ); }, + []( int& ){ BOOST_ERROR( "incorrect alternative" ); }, + []( float const& ){ BOOST_ERROR( "incorrect alternative" ); } ); + } + + { + variant const v( in_place_index_t<1>(), 2 ); + + visit_by_index( v, + []( int const& ){ BOOST_ERROR( "incorrect alternative" ); }, + []( int const& x ){ BOOST_TEST_EQ( x, 2 ); }, + []( float const& ){ BOOST_ERROR( "incorrect alternative" ); } ); + } + + { + variant const v( 3.14f ); + + visit_by_index( v, + []( int const& ){ BOOST_ERROR( "incorrect alternative" ); }, + []( int const& ){ BOOST_ERROR( "incorrect alternative" ); }, + []( float const& x ){ BOOST_TEST_EQ( x, 3.14f ); } ); + } + + { + variant const v( 7 ); + + auto r = visit_by_index( v, + []( int const& x ) -> double { return x; }, + []( float const& x ) -> double { return x; } ); + + BOOST_TEST_TRAIT_SAME( decltype(r), double ); + BOOST_TEST_EQ( r, 7.0 ); + } + + { + variant const v( 2.0f ); + + auto r = visit_by_index( v, + []( int const& x ) { return x + 0.0; }, + []( float const& x ) { return x + 0.0; } ); + + BOOST_TEST_TRAIT_SAME( decltype(r), double ); + BOOST_TEST_EQ( r, 2.0 ); + } + + { + variant const v( 3.0 ); + + auto r = visit_by_index( v, + []( int const& x ) { return x; }, + []( float const& x ) { return x; }, + []( double const& x ) { return x; } ); + + BOOST_TEST_TRAIT_SAME( decltype(r), double ); + BOOST_TEST_EQ( r, 3.0 ); + } + + { + variant v; + variant const cv; + + F1 f1; + + BOOST_TEST_EQ( visit_by_index( v, f1 ), 1 ); + BOOST_TEST_EQ( visit_by_index( cv, f1 ), 2 ); + BOOST_TEST_EQ( visit_by_index( std::move( v ), f1 ), 3 ); + BOOST_TEST_EQ( visit_by_index( std::move( cv ), f1 ), 4 ); + + F2 f2; + + BOOST_TEST_EQ( visit_by_index( v, f2 ), 1 ); + BOOST_TEST_EQ( visit_by_index( cv, f2 ), 2 ); + BOOST_TEST_EQ( visit_by_index( std::move( v ), f2 ), 3 ); + BOOST_TEST_EQ( visit_by_index( std::move( cv ), f2 ), 4 ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_visit_derived.cpp b/src/boost/libs/variant2/test/variant_visit_derived.cpp new file mode 100644 index 000000000..8a162b5da --- /dev/null +++ b/src/boost/libs/variant2/test/variant_visit_derived.cpp @@ -0,0 +1,56 @@ +// Copyright 2017, 2020 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#if defined(_MSC_VER) +# pragma warning( disable: 4244 ) // conversion from float to int, possible loss of data +#endif + +#include +#include +#include +#include +#include + +struct X: boost::variant2::variant +{ +#if BOOST_WORKAROUND( BOOST_MSVC, < 1940 ) + + template explicit X( T&& t ): variant( std::forward( t ) ) {}; + +#else + + using variant::variant; + +#endif +}; + +template struct Y: boost::variant2::variant +{ + using boost::variant2::variant::variant; +}; + +int main() +{ + { + X v1( 1 ); + X const v2( 3.14f ); + + BOOST_TEST_EQ( (visit( []( int x1, float x2 ){ return (int)(x1 * 1000) + (int)(x2 * 100); }, v1, v2 )), 1314 ); + + visit( []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, v1, v2 ); + visit( []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, std::move(v1), std::move(v2) ); + } + + { + Y v1( 1 ); + Y const v2( 3.14f ); + + BOOST_TEST_EQ( (visit( []( int x1, float x2 ){ return (int)(x1 * 1000) + (int)(x2 * 100); }, v1, v2 )), 1314 ); + + visit( []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, v1, v2 ); + visit( []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, std::move(v1), std::move(v2) ); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/variant2/test/variant_visit_r.cpp b/src/boost/libs/variant2/test/variant_visit_r.cpp new file mode 100644 index 000000000..15ab60a3b --- /dev/null +++ b/src/boost/libs/variant2/test/variant_visit_r.cpp @@ -0,0 +1,98 @@ + +// Copyright 2017 Peter Dimov. +// +// 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 + +#if defined(_MSC_VER) +# pragma warning( disable: 4244 ) // conversion from float to int, possible loss of data +#endif + +#include +#include + +using namespace boost::variant2; + +struct F1 +{ + template T1 operator()( T1 t1 ) const + { + return t1; + } +}; + +struct F2 +{ + template auto operator()( T1 t1, T2 t2 ) const -> decltype( t1 + t2 ) + { + return t1 + t2; + } +}; + +struct F3 +{ + template auto operator()( T1 t1, T2 t2, T3 t3 ) const -> decltype( t1 + t2 + t3 ) + { + return t1 + t2 + t3; + } +}; + +struct F4 +{ + template auto operator()( T1 t1, T2 t2, T3 t3, T4 t4 ) const -> decltype( t1 + t2 + t3 + t4 ) + { + return t1 + t2 + t3 + t4; + } +}; + +int main() +{ + { + BOOST_TEST_EQ( (visit( []{ return 3.14f; } )), 3 ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( visit( F1(), v ), 1 ); + BOOST_TEST_EQ( visit( F1(), v ), 1.0f ); + } + + { + variant const v( 3.14f ); + + BOOST_TEST_EQ( visit( F1(), v ), 3 ); + BOOST_TEST_EQ( visit( F1(), v ), 3.14f ); + } + + { + variant v1( 1 ); + variant const v2( 3.14f ); + + BOOST_TEST_EQ( visit( F2(), v1, v2 ), 4 ); + BOOST_TEST_EQ( visit( F2(), v1, v2 ), 1 + 3.14f ); + } + + { + variant v1( 1 ); + variant const v2( 3.14f ); + variant v3( 6.28 ); + + BOOST_TEST_EQ( visit( F3(), v1, v2, v3 ), 10 ); + BOOST_TEST_EQ( visit( F3(), v1, v2, v3 ), static_cast( 1 + 3.14f + 6.28 ) ); + } + + { + variant v1( 1 ); + variant const v2( 3.14f ); + variant v3( 6.28 ); + variant const v4( 'A' ); + + BOOST_TEST_EQ( visit( F4(), v1, v2, v3, v4 ), 10 + 'A' ); + BOOST_TEST_EQ( visit( F4(), v1, v2, v3, v4 ), static_cast( 1 + 3.14f + 6.28 + 'A' ) ); + } + + return boost::report_errors(); +} -- cgit v1.2.3