1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++98, c++03, c++11, c++14
10 
11 // XFAIL: dylib-has-no-bad_any_cast && !no-exceptions
12 
13 // <any>
14 
15 // any(any &&) noexcept;
16 
17 #include <any>
18 #include <utility>
19 #include <type_traits>
20 #include <cassert>
21 
22 #include "any_helpers.h"
23 #include "count_new.h"
24 #include "test_macros.h"
25 
26 using std::any;
27 using std::any_cast;
28 
29 // Moves are always noexcept. The throws_on_move object
30 // must be stored dynamically so the pointer is moved and
31 // not the stored object.
32 void test_move_does_not_throw()
33 {
34 #if !defined(TEST_HAS_NO_EXCEPTIONS)
35     assert(throws_on_move::count == 0);
36     {
37         throws_on_move v(42);
38         any a(v);
39         assert(throws_on_move::count == 2);
40         // No allocations should be performed after this point.
41         DisableAllocationGuard g; ((void)g);
42         try {
43             any const a2(std::move(a));
44             assertEmpty(a);
45             assertContains<throws_on_move>(a2, 42);
46         } catch (...) {
47             assert(false);
48         }
49         assert(throws_on_move::count == 1);
50         assertEmpty(a);
51     }
52     assert(throws_on_move::count == 0);
53 #endif
54 }
55 
56 void test_move_empty() {
57     DisableAllocationGuard g; ((void)g); // no allocations should be performed.
58 
59     any a1;
60     any a2(std::move(a1));
61 
62     assertEmpty(a1);
63     assertEmpty(a2);
64 }
65 
66 template <class Type>
67 void test_move() {
68     assert(Type::count == 0);
69     Type::reset();
70     {
71         any a((Type(42)));
72         assert(Type::count == 1);
73         assert(Type::copied == 0);
74         assert(Type::moved == 1);
75 
76         // Moving should not perform allocations since it must be noexcept.
77         DisableAllocationGuard g; ((void)g);
78 
79         any a2(std::move(a));
80 
81         assert(Type::moved == 1 || Type::moved == 2); // zero or more move operations can be performed.
82         assert(Type::copied == 0); // no copies can be performed.
83         assert(Type::count == 1 + a.has_value());
84         assertContains<Type>(a2, 42);
85         LIBCPP_ASSERT(!a.has_value()); // Moves are always destructive.
86         if (a.has_value())
87             assertContains<Type>(a, 0);
88     }
89     assert(Type::count == 0);
90 }
91 
92 int main(int, char**)
93 {
94     // noexcept test
95     {
96         static_assert(
97             std::is_nothrow_move_constructible<any>::value
98           , "any must be nothrow move constructible"
99           );
100     }
101     test_move<small>();
102     test_move<large>();
103     test_move_empty();
104     test_move_does_not_throw();
105 
106   return 0;
107 }
108