]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/experimental/any/any.class/any.cons/copy.pass.cpp
Vendor import of libc++ trunk r302418:
[FreeBSD/FreeBSD.git] / test / std / experimental / any / any.class / any.cons / 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(any const &);
22
23 #include <experimental/any>
24 #include <cassert>
25
26 #include "experimental_any_helpers.h"
27 #include "count_new.hpp"
28 #include "test_macros.h"
29
30 using std::experimental::any;
31 using std::experimental::any_cast;
32
33 template <class Type>
34 void test_copy_throws() {
35 #if !defined(TEST_HAS_NO_EXCEPTIONS)
36     assert(Type::count == 0);
37     {
38         any const a((Type(42)));
39         assert(Type::count == 1);
40         try {
41             any const a2(a);
42             assert(false);
43         } catch (my_any_exception const &) {
44             // do nothing
45         } catch (...) {
46             assert(false);
47         }
48         assert(Type::count == 1);
49         assertContains<Type>(a, 42);
50     }
51     assert(Type::count == 0);
52 #endif
53 }
54
55 void test_copy_empty() {
56     DisableAllocationGuard g; ((void)g); // No allocations should occur.
57     any a1;
58     any a2(a1);
59
60     assertEmpty(a1);
61     assertEmpty(a2);
62 }
63
64 template <class Type>
65 void test_copy()
66 {
67     // Copying small types should not perform any allocations.
68     DisableAllocationGuard g(isSmallType<Type>()); ((void)g);
69     assert(Type::count == 0);
70     Type::reset();
71     {
72         any a((Type(42)));
73         assert(Type::count == 1);
74         assert(Type::copied == 0);
75
76         any a2(a);
77
78         assert(Type::copied == 1);
79         assert(Type::count == 2);
80         assertContains<Type>(a, 42);
81         assertContains<Type>(a, 42);
82
83         // Modify a and check that a2 is unchanged
84         modifyValue<Type>(a, -1);
85         assertContains<Type>(a, -1);
86         assertContains<Type>(a2, 42);
87
88         // modify a2 and check that a is unchanged
89         modifyValue<Type>(a2, 999);
90         assertContains<Type>(a, -1);
91         assertContains<Type>(a2, 999);
92
93         // clear a and check that a2 is unchanged
94         a.clear();
95         assertEmpty(a);
96         assertContains<Type>(a2, 999);
97     }
98     assert(Type::count == 0);
99 }
100
101 int main() {
102     test_copy<small>();
103     test_copy<large>();
104     test_copy_empty();
105     test_copy_throws<small_throws_on_copy>();
106     test_copy_throws<large_throws_on_copy>();
107 }