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, c++14
11
12 // XFAIL: availability=macosx10.13
13 // XFAIL: availability=macosx10.12
14 // XFAIL: availability=macosx10.11
15 // XFAIL: availability=macosx10.10
16 // XFAIL: availability=macosx10.9
17 // XFAIL: availability=macosx10.8
18 // XFAIL: availability=macosx10.7
19
20 // <any>
21
22 // template <class ValueType>
23 // ValueType const* any_cast(any const *) noexcept;
24 //
25 // template <class ValueType>
26 // ValueType * any_cast(any *) noexcept;
27
28 #include <any>
29 #include <type_traits>
30 #include <cassert>
31
32 #include "any_helpers.h"
33
34 using std::any;
35 using std::any_cast;
36
37 // Test that the operators are properly noexcept.
test_cast_is_noexcept()38 void test_cast_is_noexcept() {
39 any a;
40 static_assert(noexcept(any_cast<int>(&a)), "");
41
42 any const& ca = a;
43 static_assert(noexcept(any_cast<int>(&ca)), "");
44 }
45
46 // Test that the return type of any_cast is correct.
test_cast_return_type()47 void test_cast_return_type() {
48 any a;
49 static_assert(std::is_same<decltype(any_cast<int>(&a)), int*>::value, "");
50 static_assert(std::is_same<decltype(any_cast<int const>(&a)), int const*>::value, "");
51
52 any const& ca = a;
53 static_assert(std::is_same<decltype(any_cast<int>(&ca)), int const*>::value, "");
54 static_assert(std::is_same<decltype(any_cast<int const>(&ca)), int const*>::value, "");
55 }
56
57 // Test that any_cast handles null pointers.
test_cast_nullptr()58 void test_cast_nullptr() {
59 any* a = nullptr;
60 assert(nullptr == any_cast<int>(a));
61 assert(nullptr == any_cast<int const>(a));
62
63 any const* ca = nullptr;
64 assert(nullptr == any_cast<int>(ca));
65 assert(nullptr == any_cast<int const>(ca));
66 }
67
68 // Test casting an empty object.
test_cast_empty()69 void test_cast_empty() {
70 {
71 any a;
72 assert(nullptr == any_cast<int>(&a));
73 assert(nullptr == any_cast<int const>(&a));
74
75 any const& ca = a;
76 assert(nullptr == any_cast<int>(&ca));
77 assert(nullptr == any_cast<int const>(&ca));
78 }
79 // Create as non-empty, then make empty and run test.
80 {
81 any a(42);
82 a.reset();
83 assert(nullptr == any_cast<int>(&a));
84 assert(nullptr == any_cast<int const>(&a));
85
86 any const& ca = a;
87 assert(nullptr == any_cast<int>(&ca));
88 assert(nullptr == any_cast<int const>(&ca));
89 }
90 }
91
92 template <class Type>
test_cast()93 void test_cast() {
94 assert(Type::count == 0);
95 Type::reset();
96 {
97 any a((Type(42)));
98 any const& ca = a;
99 assert(Type::count == 1);
100 assert(Type::copied == 0);
101 assert(Type::moved == 1);
102
103 // Try a cast to a bad type.
104 // NOTE: Type cannot be an int.
105 assert(any_cast<int>(&a) == nullptr);
106 assert(any_cast<int const>(&a) == nullptr);
107 assert(any_cast<int const volatile>(&a) == nullptr);
108
109 // Try a cast to the right type, but as a pointer.
110 assert(any_cast<Type*>(&a) == nullptr);
111 assert(any_cast<Type const*>(&a) == nullptr);
112
113 // Check getting a unqualified type from a non-const any.
114 Type* v = any_cast<Type>(&a);
115 assert(v != nullptr);
116 assert(v->value == 42);
117
118 // change the stored value and later check for the new value.
119 v->value = 999;
120
121 // Check getting a const qualified type from a non-const any.
122 Type const* cv = any_cast<Type const>(&a);
123 assert(cv != nullptr);
124 assert(cv == v);
125 assert(cv->value == 999);
126
127 // Check getting a unqualified type from a const any.
128 cv = any_cast<Type>(&ca);
129 assert(cv != nullptr);
130 assert(cv == v);
131 assert(cv->value == 999);
132
133 // Check getting a const-qualified type from a const any.
134 cv = any_cast<Type const>(&ca);
135 assert(cv != nullptr);
136 assert(cv == v);
137 assert(cv->value == 999);
138
139 // Check that no more objects were created, copied or moved.
140 assert(Type::count == 1);
141 assert(Type::copied == 0);
142 assert(Type::moved == 1);
143 }
144 assert(Type::count == 0);
145 }
146
test_cast_non_copyable_type()147 void test_cast_non_copyable_type()
148 {
149 // Even though 'any' never stores non-copyable types
150 // we still need to support any_cast<NoCopy>(ptr)
151 struct NoCopy { NoCopy(NoCopy const&) = delete; };
152 std::any a(42);
153 std::any const& ca = a;
154 assert(std::any_cast<NoCopy>(&a) == nullptr);
155 assert(std::any_cast<NoCopy>(&ca) == nullptr);
156 }
157
test_fn()158 void test_fn() {}
159
test_cast_function_pointer()160 void test_cast_function_pointer() {
161 using T = void(*)();
162 std::any a(test_fn);
163 // An any can never store a function type, but we should at least be able
164 // to ask.
165 assert(std::any_cast<void()>(&a) == nullptr);
166 T fn_ptr = std::any_cast<T>(a);
167 assert(fn_ptr == test_fn);
168 }
169
main()170 int main() {
171 test_cast_is_noexcept();
172 test_cast_return_type();
173 test_cast_nullptr();
174 test_cast_empty();
175 test_cast<small>();
176 test_cast<large>();
177 test_cast_non_copyable_type();
178 test_cast_function_pointer();
179 }
180