]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/experimental/any/any.class/any.assign/value.pass.cpp
Vendor import of libc++ trunk r302418:
[FreeBSD/FreeBSD.git] / test / std / experimental / any / any.class / any.assign / value.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03, c++11
11
12 // XFAIL: with_system_cxx_lib=macosx10.12
13 // XFAIL: with_system_cxx_lib=macosx10.11
14 // XFAIL: with_system_cxx_lib=macosx10.10
15 // XFAIL: with_system_cxx_lib=macosx10.9
16 // XFAIL: with_system_cxx_lib=macosx10.7
17 // XFAIL: with_system_cxx_lib=macosx10.8
18
19 // <experimental/any>
20
21 // any& operator=(any const &);
22
23 // Test value copy and move assignment.
24
25 #include <experimental/any>
26 #include <cassert>
27
28 #include "experimental_any_helpers.h"
29 #include "count_new.hpp"
30 #include "test_macros.h"
31
32 using std::experimental::any;
33 using std::experimental::any_cast;
34
35 template <class LHS, class RHS>
36 void test_assign_value() {
37     assert(LHS::count == 0);
38     assert(RHS::count == 0);
39     LHS::reset();
40     RHS::reset();
41     {
42         any lhs(LHS(1));
43         any const rhs(RHS(2));
44
45         assert(LHS::count == 1);
46         assert(RHS::count == 1);
47         assert(RHS::copied == 0);
48
49         lhs = rhs;
50
51         assert(RHS::copied == 1);
52         assert(LHS::count == 0);
53         assert(RHS::count == 2);
54
55         assertContains<RHS>(lhs, 2);
56         assertContains<RHS>(rhs, 2);
57     }
58     assert(LHS::count == 0);
59     assert(RHS::count == 0);
60     LHS::reset();
61     RHS::reset();
62     {
63         any lhs(LHS(1));
64         any rhs(RHS(2));
65
66         assert(LHS::count == 1);
67         assert(RHS::count == 1);
68         assert(RHS::moved == 1);
69
70         lhs = std::move(rhs);
71
72         assert(RHS::moved >= 1);
73         assert(RHS::copied == 0);
74         assert(LHS::count == 0);
75         assert(RHS::count == 1);
76
77         assertContains<RHS>(lhs, 2);
78         assertEmpty<RHS>(rhs);
79     }
80     assert(LHS::count == 0);
81     assert(RHS::count == 0);
82 }
83
84 template <class RHS>
85 void test_assign_value_empty() {
86     assert(RHS::count == 0);
87     RHS::reset();
88     {
89         any lhs;
90         RHS rhs(42);
91         assert(RHS::count == 1);
92         assert(RHS::copied == 0);
93
94         lhs = rhs;
95
96         assert(RHS::count == 2);
97         assert(RHS::copied == 1);
98         assert(RHS::moved >= 0);
99         assertContains<RHS>(lhs, 42);
100     }
101     assert(RHS::count == 0);
102     RHS::reset();
103     {
104         any lhs;
105         RHS rhs(42);
106         assert(RHS::count == 1);
107         assert(RHS::moved == 0);
108
109         lhs = std::move(rhs);
110
111         assert(RHS::count == 2);
112         assert(RHS::copied == 0);
113         assert(RHS::moved >= 1);
114         assertContains<RHS>(lhs, 42);
115     }
116     assert(RHS::count == 0);
117     RHS::reset();
118 }
119
120
121 template <class Tp, bool Move = false>
122 void test_assign_throws() {
123 #if !defined(TEST_HAS_NO_EXCEPTIONS)
124     auto try_throw=
125     [](any& lhs, auto&& rhs) {
126         try {
127             Move ? lhs = std::move(rhs)
128                  : lhs = rhs;
129             assert(false);
130         } catch (my_any_exception const &) {
131             // do nothing
132         } catch (...) {
133             assert(false);
134         }
135     };
136     // const lvalue to empty
137     {
138         any lhs;
139         Tp rhs(1);
140         assert(Tp::count == 1);
141
142         try_throw(lhs, rhs);
143
144         assert(Tp::count == 1);
145         assertEmpty<Tp>(lhs);
146     }
147     {
148         any lhs((small(2)));
149         Tp  rhs(1);
150         assert(small::count == 1);
151         assert(Tp::count == 1);
152
153         try_throw(lhs, rhs);
154
155         assert(small::count == 1);
156         assert(Tp::count == 1);
157         assertContains<small>(lhs, 2);
158     }
159     {
160         any lhs((large(2)));
161         Tp rhs(1);
162         assert(large::count == 1);
163         assert(Tp::count == 1);
164
165         try_throw(lhs, rhs);
166
167         assert(large::count == 1);
168         assert(Tp::count == 1);
169         assertContains<large>(lhs, 2);
170     }
171 #endif
172 }
173
174 int main() {
175     test_assign_value<small1, small2>();
176     test_assign_value<large1, large2>();
177     test_assign_value<small, large>();
178     test_assign_value<large, small>();
179     test_assign_value_empty<small>();
180     test_assign_value_empty<large>();
181     test_assign_throws<small_throws_on_copy>();
182     test_assign_throws<large_throws_on_copy>();
183     test_assign_throws<throws_on_move, /* Move = */ true>();
184 }