diff options
author | Eric Fiselier <eric@efcs.ca> | 2016-08-11 03:13:11 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2016-08-11 03:13:11 +0000 |
commit | e739d54f86e3a7f0b051f7190ffd8d40ed05ca44 (patch) | |
tree | 727c7dc5e78e62ba5c27625e6f7d1de952c8c0c4 /test/std/utilities/any/any.nonmembers/make_any.pass.cpp | |
parent | 2d08bc9a9ff2429bec70e0c9ad81b63f846cab03 (diff) |
[libcxx] Add std::any
Summary:
This patch adds std::any by moving/adapting <experimental/any>.
This patch also implements the std::any parts of p0032r3 (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0032r3.pdf)
and LWG 2509 (http://cplusplus.github.io/LWG/lwg-defects.html#2509).
I plan to push it in a day or two if there are no comments.
Reviewers: mclow.lists, EricWF
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D22733
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@278310 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/std/utilities/any/any.nonmembers/make_any.pass.cpp')
-rw-r--r-- | test/std/utilities/any/any.nonmembers/make_any.pass.cpp | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/test/std/utilities/any/any.nonmembers/make_any.pass.cpp b/test/std/utilities/any/any.nonmembers/make_any.pass.cpp new file mode 100644 index 000000000..143b60dd8 --- /dev/null +++ b/test/std/utilities/any/any.nonmembers/make_any.pass.cpp @@ -0,0 +1,140 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// <any> + +// template <class T, class ...Args> any make_any(Args&&...); +// template <class T, class U, class ...Args> +// any make_any(initializer_list<U>, Args&&...); + +#include <any> +#include <cassert> + +#include "any_helpers.h" +#include "count_new.hpp" +#include "test_macros.h" + +using std::any; +using std::any_cast; + + +template <class Type> +void test_make_any_type() { + // constructing from a small type should perform no allocations. + DisableAllocationGuard g(isSmallType<Type>()); ((void)g); + assert(Type::count == 0); + Type::reset(); + { + any a = std::make_any<Type>(); + + assert(Type::count == 1); + assert(Type::copied == 0); + assert(Type::moved == 0); + assertContains<Type>(a, 0); + } + assert(Type::count == 0); + Type::reset(); + { + any a = std::make_any<Type>(101); + + assert(Type::count == 1); + assert(Type::copied == 0); + assert(Type::moved == 0); + assertContains<Type>(a, 101); + } + assert(Type::count == 0); + Type::reset(); + { + any a = std::make_any<Type>(-1, 42, -1); + + assert(Type::count == 1); + assert(Type::copied == 0); + assert(Type::moved == 0); + assertContains<Type>(a, 42); + } + assert(Type::count == 0); + Type::reset(); +} + +template <class Type> +void test_make_any_type_tracked() { + // constructing from a small type should perform no allocations. + DisableAllocationGuard g(isSmallType<Type>()); ((void)g); + { + any a = std::make_any<Type>(); + assertArgsMatch<Type>(a); + } + { + any a = std::make_any<Type>(-1, 42, -1); + assertArgsMatch<Type, int, int, int>(a); + } + // initializer_list constructor tests + { + any a = std::make_any<Type>({-1, 42, -1}); + assertArgsMatch<Type, std::initializer_list<int>>(a); + } + { + int x = 42; + any a = std::make_any<Type>({-1, 42, -1}, x); + assertArgsMatch<Type, std::initializer_list<int>, int&>(a); + } +} + +#ifndef TEST_HAS_NO_EXCEPTIONS + +struct SmallThrows { + SmallThrows(int) { throw 42; } + SmallThrows(std::initializer_list<int>, int) { throw 42; } +}; +static_assert(IsSmallObject<SmallThrows>::value, ""); + +struct LargeThrows { + LargeThrows(int) { throw 42; } + LargeThrows(std::initializer_list<int>, int) { throw 42; } + int data[10]; +}; +static_assert(!IsSmallObject<LargeThrows>::value, ""); + +template <class Type> +void test_make_any_throws() +{ + { + try { + std::make_any<Type>(101); + assert(false); + } catch (int const&) { + } + } + { + try { + std::make_any<Type>({1, 2, 3}, 101); + assert(false); + } catch (int const&) { + } + } +} + +#endif + +int main() { + test_make_any_type<small>(); + test_make_any_type<large>(); + test_make_any_type<small_throws_on_copy>(); + test_make_any_type<large_throws_on_copy>(); + test_make_any_type<throws_on_move>(); + test_make_any_type_tracked<small_tracked_t>(); + test_make_any_type_tracked<large_tracked_t>(); +#ifndef TEST_HAS_NO_EXCEPTIONS + test_make_any_throws<SmallThrows>(); + test_make_any_throws<LargeThrows>(); + +#endif +}
\ No newline at end of file |