diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2017-01-02 19:18:58 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2017-01-02 19:18:58 +0000 |
| commit | 53a420fba21cf1644972b34dcd811a43cdb8368d (patch) | |
| tree | 66a19f6f8b65215772549a51d688492ab8addc0d /test/libcxx/utilities/tuple/tuple.tuple | |
| parent | b50f1549701eb950921e5d6f2e55ba1a1dadbb43 (diff) | |
Notes
Diffstat (limited to 'test/libcxx/utilities/tuple/tuple.tuple')
4 files changed, 329 insertions, 0 deletions
diff --git a/test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.fail.cpp b/test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.fail.cpp new file mode 100644 index 000000000000..a35dfd696259 --- /dev/null +++ b/test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.fail.cpp @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// <tuple> + +// Test the diagnostics libc++ generates for invalid reference binding. +// Libc++ attempts to diagnose the following cases: +// * Constructing an lvalue reference from an rvalue. +// * Constructing an rvalue reference from an lvalue. + +#include <tuple> +#include <string> + +int main() { + std::allocator<void> alloc; + + // expected-error@tuple:* 4 {{static_assert failed "Attempted to construct a reference element in a tuple with an rvalue"}} + + // bind lvalue to rvalue + std::tuple<int const&> t(42); // expected-note {{requested here}} + std::tuple<int const&> t1(std::allocator_arg, alloc, 42); // expected-note {{requested here}} + // bind rvalue to constructed non-rvalue + std::tuple<std::string &&> t2("hello"); // expected-note {{requested here}} + std::tuple<std::string &&> t3(std::allocator_arg, alloc, "hello"); // expected-note {{requested here}} +} diff --git a/test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.pass.cpp b/test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.pass.cpp new file mode 100644 index 000000000000..a90a2912d3a8 --- /dev/null +++ b/test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.pass.cpp @@ -0,0 +1,71 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// <tuple> + +// Test the diagnostics libc++ generates for invalid reference binding. +// Libc++ attempts to diagnose the following cases: +// * Constructing an lvalue reference from an rvalue. +// * Constructing an rvalue reference from an lvalue. + +#include <tuple> +#include <string> +#include <functional> +#include <cassert> + +static_assert(std::is_constructible<int&, std::reference_wrapper<int>>::value, ""); +static_assert(std::is_constructible<int const&, std::reference_wrapper<int>>::value, ""); + + +int main() { + std::allocator<void> alloc; + int x = 42; + { + std::tuple<int&> t(std::ref(x)); + assert(&std::get<0>(t) == &x); + std::tuple<int&> t1(std::allocator_arg, alloc, std::ref(x)); + assert(&std::get<0>(t1) == &x); + } + { + auto r = std::ref(x); + auto const& cr = r; + std::tuple<int&> t(r); + assert(&std::get<0>(t) == &x); + std::tuple<int&> t1(cr); + assert(&std::get<0>(t1) == &x); + std::tuple<int&> t2(std::allocator_arg, alloc, r); + assert(&std::get<0>(t2) == &x); + std::tuple<int&> t3(std::allocator_arg, alloc, cr); + assert(&std::get<0>(t3) == &x); + } + { + std::tuple<int const&> t(std::ref(x)); + assert(&std::get<0>(t) == &x); + std::tuple<int const&> t2(std::cref(x)); + assert(&std::get<0>(t2) == &x); + std::tuple<int const&> t3(std::allocator_arg, alloc, std::ref(x)); + assert(&std::get<0>(t3) == &x); + std::tuple<int const&> t4(std::allocator_arg, alloc, std::cref(x)); + assert(&std::get<0>(t4) == &x); + } + { + auto r = std::ref(x); + auto cr = std::cref(x); + std::tuple<int const&> t(r); + assert(&std::get<0>(t) == &x); + std::tuple<int const&> t2(cr); + assert(&std::get<0>(t2) == &x); + std::tuple<int const&> t3(std::allocator_arg, alloc, r); + assert(&std::get<0>(t3) == &x); + std::tuple<int const&> t4(std::allocator_arg, alloc, cr); + assert(&std::get<0>(t4) == &x); + } +}
\ No newline at end of file diff --git a/test/libcxx/utilities/tuple/tuple.tuple/tuple.cnstr/disable_reduced_arity_initialization_extension.pass.cpp b/test/libcxx/utilities/tuple/tuple.tuple/tuple.cnstr/disable_reduced_arity_initialization_extension.pass.cpp new file mode 100644 index 000000000000..4808c51cd4d1 --- /dev/null +++ b/test/libcxx/utilities/tuple/tuple.tuple/tuple.cnstr/disable_reduced_arity_initialization_extension.pass.cpp @@ -0,0 +1,108 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// <tuple> + +// template <class... Types> class tuple; + +// template <class... UTypes> +// explicit tuple(UTypes&&... u); + +// UNSUPPORTED: c++98, c++03 + +#include <tuple> +#include <cassert> +#include <type_traits> +#include <string> +#include <system_error> + +#include "test_macros.h" +#include "test_convertible.hpp" +#include "MoveOnly.h" + +#if defined(_LIBCPP_ENABLE_TUPLE_IMPLICIT_REDUCED_ARITY_EXTENSION) +#error This macro should not be defined by default +#endif + +struct NoDefault { NoDefault() = delete; }; + + +// Make sure the _Up... constructor SFINAEs out when the types that +// are not explicitly initialized are not all default constructible. +// Otherwise, std::is_constructible would return true but instantiating +// the constructor would fail. +void test_default_constructible_extension_sfinae() +{ + typedef MoveOnly MO; + typedef NoDefault ND; + { + typedef std::tuple<MO, ND> Tuple; + static_assert(!std::is_constructible<Tuple, MO>::value, ""); + static_assert(std::is_constructible<Tuple, MO, ND>::value, ""); + static_assert(test_convertible<Tuple, MO, ND>(), ""); + } + { + typedef std::tuple<MO, MO, ND> Tuple; + static_assert(!std::is_constructible<Tuple, MO, MO>::value, ""); + static_assert(std::is_constructible<Tuple, MO, MO, ND>::value, ""); + static_assert(test_convertible<Tuple, MO, MO, ND>(), ""); + } + { + // Same idea as above but with a nested tuple type. + typedef std::tuple<MO, ND> Tuple; + typedef std::tuple<MO, Tuple, MO, MO> NestedTuple; + + static_assert(!std::is_constructible< + NestedTuple, MO, MO, MO, MO>::value, ""); + static_assert(std::is_constructible< + NestedTuple, MO, Tuple, MO, MO>::value, ""); + } +} + +using ExplicitTup = std::tuple<std::string, int, std::error_code>; +ExplicitTup doc_example() { + return ExplicitTup{"hello world", 42}; // explicit constructor called. OK. +} + +// Test that the example given in UsingLibcxx.rst actually works. +void test_example_from_docs() { + auto tup = doc_example(); + assert(std::get<0>(tup) == "hello world"); + assert(std::get<1>(tup) == 42); + assert(std::get<2>(tup) == std::error_code{}); +} + +int main() +{ + { + using E = MoveOnly; + using Tup = std::tuple<E, E, E>; + // Test that the reduced arity initialization extension is only + // allowed on the explicit constructor. + static_assert(test_convertible<Tup, E, E, E>(), ""); + + Tup t(E(0), E(1)); + static_assert(std::is_constructible<Tup, E, E>::value, ""); + static_assert(!test_convertible<Tup, E, E>(), ""); + assert(std::get<0>(t) == 0); + assert(std::get<1>(t) == 1); + assert(std::get<2>(t) == MoveOnly()); + + Tup t2(E(0)); + static_assert(std::is_constructible<Tup, E>::value, ""); + static_assert(!test_convertible<Tup, E>(), ""); + assert(std::get<0>(t) == 0); + assert(std::get<1>(t) == E()); + assert(std::get<2>(t) == E()); + } + // Check that SFINAE is properly applied with the default reduced arity + // constructor extensions. + test_default_constructible_extension_sfinae(); + test_example_from_docs(); +} diff --git a/test/libcxx/utilities/tuple/tuple.tuple/tuple.cnstr/enable_reduced_arity_initialization_extension.pass.cpp b/test/libcxx/utilities/tuple/tuple.tuple/tuple.cnstr/enable_reduced_arity_initialization_extension.pass.cpp new file mode 100644 index 000000000000..99b6eb78f268 --- /dev/null +++ b/test/libcxx/utilities/tuple/tuple.tuple/tuple.cnstr/enable_reduced_arity_initialization_extension.pass.cpp @@ -0,0 +1,117 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// <tuple> + +// template <class... Types> class tuple; + +// template <class... UTypes> +// explicit tuple(UTypes&&... u); + +// UNSUPPORTED: c++98, c++03 + +// MODULES_DEFINES: _LIBCPP_ENABLE_TUPLE_IMPLICIT_REDUCED_ARITY_EXTENSION +#define _LIBCPP_ENABLE_TUPLE_IMPLICIT_REDUCED_ARITY_EXTENSION +#include <tuple> +#include <cassert> +#include <type_traits> +#include <string> +#include <system_error> + +#include "test_macros.h" +#include "test_convertible.hpp" +#include "MoveOnly.h" + + +struct NoDefault { NoDefault() = delete; }; + + +// Make sure the _Up... constructor SFINAEs out when the types that +// are not explicitly initialized are not all default constructible. +// Otherwise, std::is_constructible would return true but instantiating +// the constructor would fail. +void test_default_constructible_extension_sfinae() +{ + typedef MoveOnly MO; + typedef NoDefault ND; + { + typedef std::tuple<MO, ND> Tuple; + static_assert(!std::is_constructible<Tuple, MO>::value, ""); + static_assert(std::is_constructible<Tuple, MO, ND>::value, ""); + static_assert(test_convertible<Tuple, MO, ND>(), ""); + } + { + typedef std::tuple<MO, MO, ND> Tuple; + static_assert(!std::is_constructible<Tuple, MO, MO>::value, ""); + static_assert(std::is_constructible<Tuple, MO, MO, ND>::value, ""); + static_assert(test_convertible<Tuple, MO, MO, ND>(), ""); + } + { + // Same idea as above but with a nested tuple type. + typedef std::tuple<MO, ND> Tuple; + typedef std::tuple<MO, Tuple, MO, MO> NestedTuple; + + static_assert(!std::is_constructible< + NestedTuple, MO, MO, MO, MO>::value, ""); + static_assert(std::is_constructible< + NestedTuple, MO, Tuple, MO, MO>::value, ""); + } + { + typedef std::tuple<MO, int> Tuple; + typedef std::tuple<MO, Tuple, MO, MO> NestedTuple; + + static_assert(std::is_constructible< + NestedTuple, MO, MO, MO, MO>::value, ""); + static_assert(test_convertible< + NestedTuple, MO, MO, MO, MO>(), ""); + + static_assert(std::is_constructible< + NestedTuple, MO, Tuple, MO, MO>::value, ""); + static_assert(test_convertible< + NestedTuple, MO, Tuple, MO, MO>(), ""); + } +} + +std::tuple<std::string, int, std::error_code> doc_example() { + return {"hello world", 42}; +} + +// Test that the example given in UsingLibcxx.rst actually works. +void test_example_from_docs() { + auto tup = doc_example(); + assert(std::get<0>(tup) == "hello world"); + assert(std::get<1>(tup) == 42); + assert(std::get<2>(tup) == std::error_code{}); +} + +int main() +{ + + { + using E = MoveOnly; + using Tup = std::tuple<E, E, E>; + static_assert(test_convertible<Tup, E, E, E>(), ""); + + Tup t = {E(0), E(1)}; + static_assert(test_convertible<Tup, E, E>(), ""); + assert(std::get<0>(t) == 0); + assert(std::get<1>(t) == 1); + assert(std::get<2>(t) == MoveOnly()); + + Tup t2 = {E(0)}; + static_assert(test_convertible<Tup, E>(), ""); + assert(std::get<0>(t) == 0); + assert(std::get<1>(t) == E()); + assert(std::get<2>(t) == E()); + } + // Check that SFINAE is properly applied with the default reduced arity + // constructor extensions. + test_default_constructible_extension_sfinae(); + test_example_from_docs(); +} |
