From 80e66ac1d3cdb67a35ca0c57f8e4a00cacd0f019 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Wed, 23 Nov 2016 01:02:51 +0000 Subject: Add tests but disable them for libc++ llvm-svn: 287728 --- .../variant/variant.get/get_index.pass.cpp | 268 +++++++++++++++++++++ 1 file changed, 268 insertions(+) create mode 100644 libcxx/test/std/utilities/variant/variant.get/get_index.pass.cpp (limited to 'libcxx/test/std/utilities/variant/variant.get/get_index.pass.cpp') 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 + +// + +// template +// constexpr variant_alternative_t>& +// get(variant& v); +// template +// constexpr variant_alternative_t>&& +// get(variant&& v); +// template +// constexpr variant_alternative_t> const& get(const +// variant& v); +// template +// constexpr variant_alternative_t> const&& get(const +// variant&& v); + +#include "test_macros.h" +#include "variant_test_helpers.hpp" +#include +#include +#include +#include + +void test_const_lvalue_get() { + { + using V = std::variant; + 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; + 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 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 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; + 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; + 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; + 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 x = 42; + V v(x); + ASSERT_SAME_TYPE(decltype(std::get<0>(v)), int &); + assert(&std::get<0>(v) == &x); + } + { + using V = std::variant; + 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 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; + 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; + 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; + 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 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; + 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 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; + 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; + 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; + 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 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; + 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 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; + 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 using Idx = std::integral_constant; + +void test_throws_for_all_value_categories() { +#ifndef TEST_HAS_NO_EXCEPTIONS + using V = std::variant; + 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 zero; + std::integral_constant one; + auto test = [](auto idx, auto &&v) { + using Idx = decltype(idx); + try { + std::get(std::forward(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(); +} -- cgit v1.2.3