diff options
Diffstat (limited to 'libcxx/test/std/utilities/variant/variant.get')
5 files changed, 833 insertions, 0 deletions
diff --git a/libcxx/test/std/utilities/variant/variant.get/get_if_index.pass.cpp b/libcxx/test/std/utilities/variant/variant.get/get_if_index.pass.cpp new file mode 100644 index 00000000000..2ea88e346ae --- /dev/null +++ b/libcxx/test/std/utilities/variant/variant.get/get_if_index.pass.cpp @@ -0,0 +1,132 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// <variant> + +// template <size_t I, class... Types> +// constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>> +// get_if(variant<Types...>* v) noexcept; +// template <size_t I, class... Types> +// constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>> +// get_if(const variant<Types...>* v) noexcept; + +#include "test_macros.h" +#include "variant_test_helpers.hpp" +#include <cassert> +#include <memory> +#include <variant> + +void test_const_get_if() { + { + using V = std::variant<int>; + constexpr const V *v = nullptr; + static_assert(std::get_if<0>(v) == nullptr, ""); + } + { + using V = std::variant<int, long>; + constexpr V v(42); + ASSERT_NOEXCEPT(std::get_if<0>(&v)); + ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int const *); + static_assert(*std::get_if<0>(&v) == 42, ""); + static_assert(std::get_if<1>(&v) == nullptr, ""); + } + { + using V = std::variant<int, long>; + constexpr V v(42l); + ASSERT_SAME_TYPE(decltype(std::get_if<1>(&v)), long const *); + static_assert(*std::get_if<1>(&v) == 42, ""); + static_assert(std::get_if<0>(&v) == nullptr, ""); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + const V v(x); + ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int *); + assert(std::get_if<0>(&v) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int *); + assert(std::get_if<0>(&v) == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), const int *); + assert(std::get_if<0>(&v) == &x); + } +#endif +} + +void test_get_if() { + { + using V = std::variant<int>; + V *v = nullptr; + assert(std::get_if<0>(v) == nullptr); + } + { + using V = std::variant<int, long>; + V v(42); + ASSERT_NOEXCEPT(std::get_if<0>(&v)); + ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int *); + assert(*std::get_if<0>(&v) == 42); + assert(std::get_if<1>(&v) == nullptr); + } + { + using V = std::variant<int, long>; + V v(42l); + ASSERT_SAME_TYPE(decltype(std::get_if<1>(&v)), long *); + assert(*std::get_if<1>(&v) == 42); + assert(std::get_if<0>(&v) == nullptr); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int *); + assert(std::get_if<0>(&v) == &x); + } + { + using V = std::variant<const int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), const int *); + assert(std::get_if<0>(&v) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int *); + assert(std::get_if<0>(&v) == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), const int *); + assert(std::get_if<0>(&v) == &x); + } +#endif +} + +int main() { + test_const_get_if(); + test_get_if(); +} diff --git a/libcxx/test/std/utilities/variant/variant.get/get_if_type.pass.cpp b/libcxx/test/std/utilities/variant/variant.get/get_if_type.pass.cpp new file mode 100644 index 00000000000..59ddef3f9b3 --- /dev/null +++ b/libcxx/test/std/utilities/variant/variant.get/get_if_type.pass.cpp @@ -0,0 +1,130 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// <variant> + +// template <class T, class... Types> +// constexpr add_pointer_t<T> get_if(variant<Types...>* v) noexcept; +// template <class T, class... Types> +// constexpr add_pointer_t<const T> get_if(const variant<Types...>* v) +// noexcept; + +#include "test_macros.h" +#include "variant_test_helpers.hpp" +#include <cassert> +#include <variant> + +void test_const_get_if() { + { + using V = std::variant<int>; + constexpr const V *v = nullptr; + static_assert(std::get_if<int>(v) == nullptr, ""); + } + { + using V = std::variant<int, long>; + constexpr V v(42); + ASSERT_NOEXCEPT(std::get_if<int>(&v)); + ASSERT_SAME_TYPE(decltype(std::get_if<int>(&v)), int const *); + static_assert(*std::get_if<int>(&v) == 42, ""); + static_assert(std::get_if<long>(&v) == nullptr, ""); + } + { + using V = std::variant<int, long>; + constexpr V v(42l); + ASSERT_SAME_TYPE(decltype(std::get_if<long>(&v)), long const *); + static_assert(*std::get_if<long>(&v) == 42, ""); + static_assert(std::get_if<int>(&v) == nullptr, ""); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + const V v(x); + ASSERT_SAME_TYPE(decltype(std::get_if<int &>(&v)), int *); + assert(std::get_if<int &>(&v) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get_if<int &&>(&v)), int *); + assert(std::get_if<int &&>(&v) == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get_if<const int &&>(&v)), const int *); + assert(std::get_if<const int &&>(&v) == &x); + } +#endif +} + +void test_get_if() { + { + using V = std::variant<int>; + V *v = nullptr; + assert(std::get_if<int>(v) == nullptr); + } + { + using V = std::variant<int, long>; + V v(42); + ASSERT_NOEXCEPT(std::get_if<int>(&v)); + ASSERT_SAME_TYPE(decltype(std::get_if<int>(&v)), int *); + assert(*std::get_if<int>(&v) == 42); + assert(std::get_if<long>(&v) == nullptr); + } + { + using V = std::variant<int, long>; + V v(42l); + ASSERT_SAME_TYPE(decltype(std::get_if<long>(&v)), long *); + assert(*std::get_if<long>(&v) == 42); + assert(std::get_if<int>(&v) == nullptr); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get_if<int &>(&v)), int *); + assert(std::get_if<int &>(&v) == &x); + } + { + using V = std::variant<const int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get_if<const int &>(&v)), const int *); + assert(std::get_if<const int &>(&v) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get_if<int &&>(&v)), int *); + assert(std::get_if<int &&>(&v) == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get_if<const int &&>(&v)), const int *); + assert(std::get_if<const int &&>(&v) == &x); + } +#endif +} + +int main() { + test_const_get_if(); + test_get_if(); +} diff --git a/libcxx/test/std/utilities/variant/variant.get/get_index.pass.cpp b/libcxx/test/std/utilities/variant/variant.get/get_index.pass.cpp new file mode 100644 index 00000000000..657db9b0933 --- /dev/null +++ b/libcxx/test/std/utilities/variant/variant.get/get_index.pass.cpp @@ -0,0 +1,268 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// <variant> + +// template <size_t I, class... Types> +// constexpr variant_alternative_t<I, variant<Types...>>& +// get(variant<Types...>& v); +// template <size_t I, class... Types> +// constexpr variant_alternative_t<I, variant<Types...>>&& +// get(variant<Types...>&& v); +// template <size_t I, class... Types> +// constexpr variant_alternative_t<I, variant<Types...>> const& get(const +// variant<Types...>& v); +// template <size_t I, class... Types> +// constexpr variant_alternative_t<I, variant<Types...>> const&& get(const +// variant<Types...>&& v); + +#include "test_macros.h" +#include "variant_test_helpers.hpp" +#include <cassert> +#include <type_traits> +#include <utility> +#include <variant> + +void test_const_lvalue_get() { + { + using V = std::variant<int>; + constexpr V v(42); + // ASSERT_NOT_NOEXCEPT(std::get<0>(v)); + ASSERT_SAME_TYPE(decltype(std::get<0>(v)), int const &); + static_assert(std::get<0>(v) == 42, ""); + } + { + using V = std::variant<int, long>; + constexpr V v(42l); + ASSERT_SAME_TYPE(decltype(std::get<1>(v)), long const &); + static_assert(std::get<1>(v) == 42, ""); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + const V v(x); + ASSERT_SAME_TYPE(decltype(std::get<0>(v)), int &); + assert(&std::get<0>(v) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<0>(v)), int &); + assert(&std::get<0>(v) == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<0>(v)), const int &); + assert(&std::get<0>(v) == &x); + } +#endif +} + +void test_lvalue_get() { + { + using V = std::variant<int>; + V v(42); + ASSERT_NOT_NOEXCEPT(std::get<0>(v)); + ASSERT_SAME_TYPE(decltype(std::get<0>(v)), int &); + assert(std::get<0>(v) == 42); + } + { + using V = std::variant<int, long>; + V v(42l); + ASSERT_SAME_TYPE(decltype(std::get<1>(v)), long &); + assert(std::get<1>(v) == 42); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get<0>(v)), int &); + assert(&std::get<0>(v) == &x); + } + { + using V = std::variant<const int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get<0>(v)), const int &); + assert(&std::get<0>(v) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<0>(v)), int &); + assert(&std::get<0>(v) == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<0>(v)), const int &); + assert(&std::get<0>(v) == &x); + } +#endif +} + +void test_rvalue_get() { + { + using V = std::variant<int>; + V v(42); + ASSERT_NOT_NOEXCEPT(std::get<0>(std::move(v))); + ASSERT_SAME_TYPE(decltype(std::get<0>(std::move(v))), int &&); + assert(std::get<0>(std::move(v)) == 42); + } + { + using V = std::variant<int, long>; + V v(42l); + ASSERT_SAME_TYPE(decltype(std::get<1>(std::move(v))), long &&); + assert(std::get<1>(std::move(v)) == 42); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get<0>(std::move(v))), int &); + assert(&std::get<0>(std::move(v)) == &x); + } + { + using V = std::variant<const int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get<0>(std::move(v))), const int &); + assert(&std::get<0>(std::move(v)) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<0>(std::move(v))), int &&); + int &&xref = std::get<0>(std::move(v)); + assert(&xref == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<0>(std::move(v))), const int &&); + const int &&xref = std::get<0>(std::move(v)); + assert(&xref == &x); + } +#endif +} + +void test_const_rvalue_get() { + { + using V = std::variant<int>; + const V v(42); + ASSERT_NOT_NOEXCEPT(std::get<0>(std::move(v))); + ASSERT_SAME_TYPE(decltype(std::get<0>(std::move(v))), const int &&); + assert(std::get<0>(std::move(v)) == 42); + } + { + using V = std::variant<int, long>; + const V v(42l); + ASSERT_SAME_TYPE(decltype(std::get<1>(std::move(v))), const long &&); + assert(std::get<1>(std::move(v)) == 42); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + const V v(x); + ASSERT_SAME_TYPE(decltype(std::get<0>(std::move(v))), int &); + assert(&std::get<0>(std::move(v)) == &x); + } + { + using V = std::variant<const int &>; + int x = 42; + const V v(x); + ASSERT_SAME_TYPE(decltype(std::get<0>(std::move(v))), const int &); + assert(&std::get<0>(std::move(v)) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<0>(std::move(v))), int &&); + int &&xref = std::get<0>(std::move(v)); + assert(&xref == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<0>(std::move(v))), const int &&); + const int &&xref = std::get<0>(std::move(v)); + assert(&xref == &x); + } +#endif +} + +template <std::size_t I> using Idx = std::integral_constant<size_t, I>; + +void test_throws_for_all_value_categories() { +#ifndef TEST_HAS_NO_EXCEPTIONS + using V = std::variant<int, long>; + V v0(42); + const V &cv0 = v0; + assert(v0.index() == 0); + V v1(42l); + const V &cv1 = v1; + assert(v1.index() == 1); + std::integral_constant<size_t, 0> zero; + std::integral_constant<size_t, 1> one; + auto test = [](auto idx, auto &&v) { + using Idx = decltype(idx); + try { + std::get<Idx::value>(std::forward<decltype(v)>(v)); + } catch (std::bad_variant_access const &) { + return true; + } catch (...) { /* ... */ + } + return false; + }; + { // lvalue test cases + assert(test(one, v0)); + assert(test(zero, v1)); + } + { // const lvalue test cases + assert(test(one, cv0)); + assert(test(zero, cv1)); + } + { // rvalue test cases + assert(test(one, std::move(v0))); + assert(test(zero, std::move(v1))); + } + { // const rvalue test cases + assert(test(one, std::move(cv0))); + assert(test(zero, std::move(cv1))); + } +#endif +} + +int main() { + test_const_lvalue_get(); + test_lvalue_get(); + test_rvalue_get(); + test_const_rvalue_get(); + test_throws_for_all_value_categories(); +} diff --git a/libcxx/test/std/utilities/variant/variant.get/get_type.pass.cpp b/libcxx/test/std/utilities/variant/variant.get/get_type.pass.cpp new file mode 100644 index 00000000000..11991dca5b6 --- /dev/null +++ b/libcxx/test/std/utilities/variant/variant.get/get_type.pass.cpp @@ -0,0 +1,266 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// <variant> + +// template <class T, class... Types> constexpr T& get(variant<Types...>& v); +// template <class T, class... Types> constexpr T&& get(variant<Types...>&& v); +// template <class T, class... Types> constexpr const T& get(const +// variant<Types...>& v); +// template <class T, class... Types> constexpr const T&& get(const +// variant<Types...>&& v); + +#include "test_macros.h" +#include "variant_test_helpers.hpp" +#include <cassert> +#include <type_traits> +#include <utility> +#include <variant> + +void test_const_lvalue_get() { + { + using V = std::variant<int>; + constexpr V v(42); + // ASSERT_NOT_NOEXCEPT(std::get<int>(v)); + ASSERT_SAME_TYPE(decltype(std::get<0>(v)), int const &); + static_assert(std::get<int>(v) == 42, ""); + } + { + using V = std::variant<int, long>; + constexpr V v(42l); + ASSERT_SAME_TYPE(decltype(std::get<long>(v)), long const &); + static_assert(std::get<long>(v) == 42, ""); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + const V v(x); + ASSERT_SAME_TYPE(decltype(std::get<int &>(v)), int &); + assert(&std::get<int &>(v) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<int &&>(v)), int &); + assert(&std::get<int &&>(v) == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<const int &&>(v)), const int &); + assert(&std::get<const int &&>(v) == &x); + } +#endif +} + +void test_lvalue_get() { + { + using V = std::variant<int>; + V v(42); + ASSERT_NOT_NOEXCEPT(std::get<int>(v)); + ASSERT_SAME_TYPE(decltype(std::get<int>(v)), int &); + assert(std::get<int>(v) == 42); + } + { + using V = std::variant<int, long>; + V v(42l); + ASSERT_SAME_TYPE(decltype(std::get<long>(v)), long &); + assert(std::get<long>(v) == 42); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get<int &>(v)), int &); + assert(&std::get<int &>(v) == &x); + } + { + using V = std::variant<const int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get<const int &>(v)), const int &); + assert(&std::get<const int &>(v) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<int &&>(v)), int &); + assert(&std::get<int &&>(v) == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<const int &&>(v)), const int &); + assert(&std::get<const int &&>(v) == &x); + } +#endif +} + +void test_rvalue_get() { + { + using V = std::variant<int>; + V v(42); + ASSERT_NOT_NOEXCEPT(std::get<int>(std::move(v))); + ASSERT_SAME_TYPE(decltype(std::get<int>(std::move(v))), int &&); + assert(std::get<int>(std::move(v)) == 42); + } + { + using V = std::variant<int, long>; + V v(42l); + ASSERT_SAME_TYPE(decltype(std::get<long>(std::move(v))), long &&); + assert(std::get<long>(std::move(v)) == 42); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get<int &>(std::move(v))), int &); + assert(&std::get<int &>(std::move(v)) == &x); + } + { + using V = std::variant<const int &>; + int x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get<const int &>(std::move(v))), + const int &); + assert(&std::get<const int &>(std::move(v)) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<int &&>(std::move(v))), int &&); + int &&xref = std::get<int &&>(std::move(v)); + assert(&xref == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<const int &&>(std::move(v))), + const int &&); + const int &&xref = std::get<const int &&>(std::move(v)); + assert(&xref == &x); + } +#endif +} + +void test_const_rvalue_get() { + { + using V = std::variant<int>; + const V v(42); + ASSERT_NOT_NOEXCEPT(std::get<int>(std::move(v))); + ASSERT_SAME_TYPE(decltype(std::get<int>(std::move(v))), const int &&); + assert(std::get<int>(std::move(v)) == 42); + } + { + using V = std::variant<int, long>; + const V v(42l); + ASSERT_SAME_TYPE(decltype(std::get<long>(std::move(v))), const long &&); + assert(std::get<long>(std::move(v)) == 42); + } +// FIXME: Remove these once reference support is reinstated +#if !defined(TEST_VARIANT_HAS_NO_REFERENCES) + { + using V = std::variant<int &>; + int x = 42; + const V v(x); + ASSERT_SAME_TYPE(decltype(std::get<int &>(std::move(v))), int &); + assert(&std::get<int &>(std::move(v)) == &x); + } + { + using V = std::variant<const int &>; + int x = 42; + const V v(x); + ASSERT_SAME_TYPE(decltype(std::get<const int &>(std::move(v))), + const int &); + assert(&std::get<const int &>(std::move(v)) == &x); + } + { + using V = std::variant<int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<int &&>(std::move(v))), int &&); + int &&xref = std::get<int &&>(std::move(v)); + assert(&xref == &x); + } + { + using V = std::variant<const int &&>; + int x = 42; + const V v(std::move(x)); + ASSERT_SAME_TYPE(decltype(std::get<const int &&>(std::move(v))), + const int &&); + const int &&xref = std::get<const int &&>(std::move(v)); + assert(&xref == &x); + } +#endif +} + +template <class Tp> struct identity { using type = Tp; }; + +void test_throws_for_all_value_categories() { +#ifndef TEST_HAS_NO_EXCEPTIONS + using V = std::variant<int, long>; + V v0(42); + const V &cv0 = v0; + assert(v0.index() == 0); + V v1(42l); + const V &cv1 = v1; + assert(v1.index() == 1); + identity<int> zero; + identity<long> one; + auto test = [](auto idx, auto &&v) { + using Idx = decltype(idx); + try { + std::get<typename Idx::type>(std::forward<decltype(v)>(v)); + } catch (std::bad_variant_access const &) { + return true; + } catch (...) { /* ... */ + } + return false; + }; + { // lvalue test cases + assert(test(one, v0)); + assert(test(zero, v1)); + } + { // const lvalue test cases + assert(test(one, cv0)); + assert(test(zero, cv1)); + } + { // rvalue test cases + assert(test(one, std::move(v0))); + assert(test(zero, std::move(v1))); + } + { // const rvalue test cases + assert(test(one, std::move(cv0))); + assert(test(zero, std::move(cv1))); + } +#endif +} + +int main() { + test_const_lvalue_get(); + test_lvalue_get(); + test_rvalue_get(); + test_const_rvalue_get(); + test_throws_for_all_value_categories(); +} diff --git a/libcxx/test/std/utilities/variant/variant.get/holds_alternative.pass.cpp b/libcxx/test/std/utilities/variant/variant.get/holds_alternative.pass.cpp new file mode 100644 index 00000000000..34843869bb8 --- /dev/null +++ b/libcxx/test/std/utilities/variant/variant.get/holds_alternative.pass.cpp @@ -0,0 +1,37 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// <variant> + +// template <class T, class... Types> +// constexpr bool holds_alternative(const variant<Types...>& v) noexcept; + +#include <variant> + +int main() { + { + using V = std::variant<int>; + constexpr V v; + static_assert(std::holds_alternative<int>(v), ""); + } + { + using V = std::variant<int, long>; + constexpr V v; + static_assert(std::holds_alternative<int>(v), ""); + static_assert(!std::holds_alternative<long>(v), ""); + } + { // noexcept test + using V = std::variant<int>; + const V v; + static_assert(noexcept(std::holds_alternative<int>(v)), "must be noexcept"); + } +} |

