]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/experimental/any/any.class/any.assign/copy.pass.cpp
Vendor import of libc++ trunk r302418:
[FreeBSD/FreeBSD.git] / test / std / experimental / any / any.class / any.assign / copy.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 copy 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_copy_assign() {
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 }
61
62 template <class LHS>
63 void test_copy_assign_empty() {
64     assert(LHS::count == 0);
65     LHS::reset();
66     {
67         any lhs;
68         any const rhs(LHS(42));
69
70         assert(LHS::count == 1);
71         assert(LHS::copied == 0);
72
73         lhs = rhs;
74
75         assert(LHS::copied == 1);
76         assert(LHS::count == 2);
77
78         assertContains<LHS>(lhs, 42);
79         assertContains<LHS>(rhs, 42);
80     }
81     assert(LHS::count == 0);
82     LHS::reset();
83     {
84         any lhs(LHS(1));
85         any const rhs;
86
87         assert(LHS::count == 1);
88         assert(LHS::copied == 0);
89
90         lhs = rhs;
91
92         assert(LHS::copied == 0);
93         assert(LHS::count == 0);
94
95         assertEmpty<LHS>(lhs);
96         assertEmpty(rhs);
97     }
98     assert(LHS::count == 0);
99 }
100
101 void test_copy_assign_self() {
102     // empty
103     {
104         any a;
105         a = a;
106         assertEmpty(a);
107         assert(globalMemCounter.checkOutstandingNewEq(0));
108     }
109     assert(globalMemCounter.checkOutstandingNewEq(0));
110     // small
111     {
112         any a((small(1)));
113         assert(small::count == 1);
114
115         a = a;
116
117         assert(small::count == 1);
118         assertContains<small>(a, 1);
119         assert(globalMemCounter.checkOutstandingNewEq(0));
120     }
121     assert(small::count == 0);
122     assert(globalMemCounter.checkOutstandingNewEq(0));
123     // large
124     {
125         any a(large(1));
126         assert(large::count == 1);
127
128         a = a;
129
130         assert(large::count == 1);
131         assertContains<large>(a, 1);
132         assert(globalMemCounter.checkOutstandingNewEq(1));
133     }
134     assert(large::count == 0);
135     assert(globalMemCounter.checkOutstandingNewEq(0));
136 }
137
138 template <class Tp>
139 void test_copy_assign_throws()
140 {
141 #if !defined(TEST_HAS_NO_EXCEPTIONS)
142     auto try_throw =
143     [](any& lhs, any const& rhs) {
144         try {
145             lhs = rhs;
146             assert(false);
147         } catch (my_any_exception const &) {
148             // do nothing
149         } catch (...) {
150             assert(false);
151         }
152     };
153     // const lvalue to empty
154     {
155         any lhs;
156         any const rhs((Tp(1)));
157         assert(Tp::count == 1);
158
159         try_throw(lhs, rhs);
160
161         assert(Tp::count == 1);
162         assertEmpty<Tp>(lhs);
163         assertContains<Tp>(rhs);
164     }
165     {
166         any lhs((small(2)));
167         any const rhs((Tp(1)));
168         assert(small::count == 1);
169         assert(Tp::count == 1);
170
171         try_throw(lhs, rhs);
172
173         assert(small::count == 1);
174         assert(Tp::count == 1);
175         assertContains<small>(lhs, 2);
176         assertContains<Tp>(rhs);
177     }
178     {
179         any lhs((large(2)));
180         any const rhs((Tp(1)));
181         assert(large::count == 1);
182         assert(Tp::count == 1);
183
184         try_throw(lhs, rhs);
185
186         assert(large::count == 1);
187         assert(Tp::count == 1);
188         assertContains<large>(lhs, 2);
189         assertContains<Tp>(rhs);
190     }
191 #endif
192 }
193
194 int main() {
195     test_copy_assign<small1, small2>();
196     test_copy_assign<large1, large2>();
197     test_copy_assign<small, large>();
198     test_copy_assign<large, small>();
199     test_copy_assign_empty<small>();
200     test_copy_assign_empty<large>();
201     test_copy_assign_self();
202     test_copy_assign_throws<small_throws_on_copy>();
203     test_copy_assign_throws<large_throws_on_copy>();
204 }