Eric Fiselier | 38236b5 | 2016-01-19 21:52:04 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 9 | #ifndef ANY_HELPERS_H |
| 10 | #define ANY_HELPERS_H |
| 11 | |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 12 | #include <typeinfo> |
| 13 | #include <type_traits> |
| 14 | #include <cassert> |
| 15 | |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 16 | namespace std { namespace experimental {} } |
| 17 | |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 18 | #include "test_macros.h" |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 19 | #include "type_id.h" |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 20 | |
| 21 | #if !defined(TEST_HAS_NO_RTTI) |
| 22 | #define RTTI_ASSERT(X) assert(X) |
| 23 | #else |
| 24 | #define RTTI_ASSERT(X) |
| 25 | #endif |
| 26 | |
| 27 | template <class _Tp> |
| 28 | struct IsSmallObject |
| 29 | : public std::integral_constant<bool |
| 30 | , sizeof(_Tp) <= (sizeof(void*)*3) |
| 31 | && std::alignment_of<void*>::value |
| 32 | % std::alignment_of<_Tp>::value == 0 |
| 33 | && std::is_nothrow_move_constructible<_Tp>::value |
| 34 | > |
| 35 | {}; |
| 36 | |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 37 | template <class T> |
| 38 | bool containsType(std::any const& a) { |
| 39 | #if !defined(TEST_HAS_NO_RTTI) |
| 40 | return a.type() == typeid(T); |
| 41 | #else |
| 42 | return a.has_value() && std::any_cast<T>(&a) != nullptr; |
| 43 | #endif |
| 44 | } |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 45 | |
| 46 | // Return 'true' if 'Type' will be considered a small type by 'any' |
| 47 | template <class Type> |
| 48 | bool isSmallType() { |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 49 | return IsSmallObject<Type>::value; |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Assert that an object is empty. If the object used to contain an object |
| 53 | // of type 'LastType' check that it can no longer be accessed. |
| 54 | template <class LastType = int> |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 55 | void assertEmpty(std::any const& a) { |
| 56 | using namespace std; |
| 57 | assert(!a.has_value()); |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 58 | RTTI_ASSERT(a.type() == typeid(void)); |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 59 | assert(any_cast<LastType const>(&a) == nullptr); |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Assert that an 'any' object stores the specified 'Type' and 'value'. |
| 63 | template <class Type> |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 64 | void assertContains(std::any const& a, int value = 1) { |
| 65 | assert(a.has_value()); |
| 66 | assert(containsType<Type>(a)); |
| 67 | assert(std::any_cast<Type const &>(a).value == value); |
| 68 | } |
| 69 | |
| 70 | template <> |
| 71 | void assertContains<int>(std::any const& a, int value) { |
| 72 | assert(a.has_value()); |
| 73 | assert(containsType<int>(a)); |
| 74 | assert(std::any_cast<int const &>(a) == value); |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Modify the value of a "test type" stored within an any to the specified |
| 78 | // 'value'. |
| 79 | template <class Type> |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 80 | void modifyValue(std::any& a, int value) { |
| 81 | using namespace std; |
| 82 | using namespace std::experimental; |
| 83 | assert(a.has_value()); |
| 84 | assert(containsType<Type>(a)); |
| 85 | any_cast<Type&>(a).value = value; |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // A test type that will trigger the small object optimization within 'any'. |
| 89 | template <int Dummy = 0> |
| 90 | struct small_type |
| 91 | { |
| 92 | static int count; |
| 93 | static int copied; |
| 94 | static int moved; |
| 95 | static int const_copied; |
| 96 | static int non_const_copied; |
| 97 | |
| 98 | static void reset() { |
| 99 | small_type::copied = 0; |
| 100 | small_type::moved = 0; |
| 101 | small_type::const_copied = 0; |
| 102 | small_type::non_const_copied = 0; |
| 103 | } |
| 104 | |
| 105 | int value; |
| 106 | |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 107 | explicit small_type(int val = 0) : value(val) { |
| 108 | ++count; |
| 109 | } |
| 110 | explicit small_type(int, int val, int) : value(val) { |
| 111 | ++count; |
| 112 | } |
| 113 | small_type(std::initializer_list<int> il) : value(*il.begin()) { |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 114 | ++count; |
| 115 | } |
| 116 | |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 117 | small_type(small_type const & other) noexcept { |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 118 | value = other.value; |
| 119 | ++count; |
| 120 | ++copied; |
| 121 | ++const_copied; |
| 122 | } |
| 123 | |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 124 | small_type(small_type& other) noexcept { |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 125 | value = other.value; |
| 126 | ++count; |
| 127 | ++copied; |
| 128 | ++non_const_copied; |
| 129 | } |
| 130 | |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 131 | small_type(small_type && other) noexcept { |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 132 | value = other.value; |
| 133 | other.value = 0; |
| 134 | ++count; |
| 135 | ++moved; |
| 136 | } |
| 137 | |
| 138 | ~small_type() { |
| 139 | value = -1; |
| 140 | --count; |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | small_type& operator=(small_type const&) = delete; |
| 145 | small_type& operator=(small_type&&) = delete; |
| 146 | }; |
| 147 | |
| 148 | template <int Dummy> |
| 149 | int small_type<Dummy>::count = 0; |
| 150 | |
| 151 | template <int Dummy> |
| 152 | int small_type<Dummy>::copied = 0; |
| 153 | |
| 154 | template <int Dummy> |
| 155 | int small_type<Dummy>::moved = 0; |
| 156 | |
| 157 | template <int Dummy> |
| 158 | int small_type<Dummy>::const_copied = 0; |
| 159 | |
| 160 | template <int Dummy> |
| 161 | int small_type<Dummy>::non_const_copied = 0; |
| 162 | |
| 163 | typedef small_type<> small; |
| 164 | typedef small_type<1> small1; |
| 165 | typedef small_type<2> small2; |
| 166 | |
| 167 | |
| 168 | // A test type that will NOT trigger the small object optimization in any. |
| 169 | template <int Dummy = 0> |
| 170 | struct large_type |
| 171 | { |
| 172 | static int count; |
| 173 | static int copied; |
| 174 | static int moved; |
| 175 | static int const_copied; |
| 176 | static int non_const_copied; |
| 177 | |
| 178 | static void reset() { |
| 179 | large_type::copied = 0; |
| 180 | large_type::moved = 0; |
| 181 | large_type::const_copied = 0; |
| 182 | large_type::non_const_copied = 0; |
| 183 | } |
| 184 | |
| 185 | int value; |
| 186 | |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 187 | large_type(int val = 0) : value(val) { |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 188 | ++count; |
| 189 | data[0] = 0; |
| 190 | } |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 191 | large_type(int, int val, int) : value(val) { |
| 192 | ++count; |
| 193 | data[0] = 0; |
| 194 | } |
| 195 | large_type(std::initializer_list<int> il) : value(*il.begin()) { |
| 196 | ++count; |
| 197 | } |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 198 | large_type(large_type const & other) { |
| 199 | value = other.value; |
| 200 | ++count; |
| 201 | ++copied; |
| 202 | ++const_copied; |
| 203 | } |
| 204 | |
| 205 | large_type(large_type & other) { |
| 206 | value = other.value; |
| 207 | ++count; |
| 208 | ++copied; |
| 209 | ++non_const_copied; |
| 210 | } |
| 211 | |
| 212 | large_type(large_type && other) { |
| 213 | value = other.value; |
| 214 | other.value = 0; |
| 215 | ++count; |
| 216 | ++moved; |
| 217 | } |
| 218 | |
| 219 | ~large_type() { |
| 220 | value = 0; |
| 221 | --count; |
| 222 | } |
| 223 | |
| 224 | private: |
| 225 | large_type& operator=(large_type const&) = delete; |
| 226 | large_type& operator=(large_type &&) = delete; |
| 227 | int data[10]; |
| 228 | }; |
| 229 | |
| 230 | template <int Dummy> |
| 231 | int large_type<Dummy>::count = 0; |
| 232 | |
| 233 | template <int Dummy> |
| 234 | int large_type<Dummy>::copied = 0; |
| 235 | |
| 236 | template <int Dummy> |
| 237 | int large_type<Dummy>::moved = 0; |
| 238 | |
| 239 | template <int Dummy> |
| 240 | int large_type<Dummy>::const_copied = 0; |
| 241 | |
| 242 | template <int Dummy> |
| 243 | int large_type<Dummy>::non_const_copied = 0; |
| 244 | |
| 245 | typedef large_type<> large; |
| 246 | typedef large_type<1> large1; |
| 247 | typedef large_type<2> large2; |
| 248 | |
| 249 | // The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy' |
| 250 | // and 'throws_on_move'. |
| 251 | struct my_any_exception {}; |
| 252 | |
| 253 | void throwMyAnyExpression() { |
| 254 | #if !defined(TEST_HAS_NO_EXCEPTIONS) |
| 255 | throw my_any_exception(); |
| 256 | #else |
| 257 | assert(false && "Exceptions are disabled"); |
| 258 | #endif |
| 259 | } |
| 260 | |
| 261 | // A test type that will trigger the small object optimization within 'any'. |
| 262 | // this type throws if it is copied. |
| 263 | struct small_throws_on_copy |
| 264 | { |
| 265 | static int count; |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 266 | static int copied; |
| 267 | static int moved; |
| 268 | static void reset() { count = copied = moved = 0; } |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 269 | int value; |
| 270 | |
| 271 | explicit small_throws_on_copy(int val = 0) : value(val) { |
| 272 | ++count; |
| 273 | } |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 274 | explicit small_throws_on_copy(int, int val, int) : value(val) { |
| 275 | ++count; |
| 276 | } |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 277 | small_throws_on_copy(small_throws_on_copy const &) { |
| 278 | throwMyAnyExpression(); |
| 279 | } |
| 280 | |
| 281 | small_throws_on_copy(small_throws_on_copy && other) throw() { |
| 282 | value = other.value; |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 283 | ++count; ++moved; |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | ~small_throws_on_copy() { |
| 287 | --count; |
| 288 | } |
| 289 | private: |
| 290 | small_throws_on_copy& operator=(small_throws_on_copy const&) = delete; |
| 291 | small_throws_on_copy& operator=(small_throws_on_copy &&) = delete; |
| 292 | }; |
| 293 | |
| 294 | int small_throws_on_copy::count = 0; |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 295 | int small_throws_on_copy::copied = 0; |
| 296 | int small_throws_on_copy::moved = 0; |
| 297 | |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 298 | |
| 299 | // A test type that will NOT trigger the small object optimization within 'any'. |
| 300 | // this type throws if it is copied. |
| 301 | struct large_throws_on_copy |
| 302 | { |
| 303 | static int count; |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 304 | static int copied; |
| 305 | static int moved; |
| 306 | static void reset() { count = copied = moved = 0; } |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 307 | int value = 0; |
| 308 | |
| 309 | explicit large_throws_on_copy(int val = 0) : value(val) { |
| 310 | data[0] = 0; |
| 311 | ++count; |
| 312 | } |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 313 | explicit large_throws_on_copy(int, int val, int) : value(val) { |
| 314 | data[0] = 0; |
| 315 | ++count; |
| 316 | } |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 317 | large_throws_on_copy(large_throws_on_copy const &) { |
| 318 | throwMyAnyExpression(); |
| 319 | } |
| 320 | |
| 321 | large_throws_on_copy(large_throws_on_copy && other) throw() { |
| 322 | value = other.value; |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 323 | ++count; ++moved; |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | ~large_throws_on_copy() { |
| 327 | --count; |
| 328 | } |
| 329 | |
| 330 | private: |
| 331 | large_throws_on_copy& operator=(large_throws_on_copy const&) = delete; |
| 332 | large_throws_on_copy& operator=(large_throws_on_copy &&) = delete; |
| 333 | int data[10]; |
| 334 | }; |
| 335 | |
| 336 | int large_throws_on_copy::count = 0; |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 337 | int large_throws_on_copy::copied = 0; |
| 338 | int large_throws_on_copy::moved = 0; |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 339 | |
| 340 | // A test type that throws when it is moved. This object will NOT trigger |
| 341 | // the small object optimization in 'any'. |
| 342 | struct throws_on_move |
| 343 | { |
| 344 | static int count; |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 345 | static int copied; |
| 346 | static int moved; |
| 347 | static void reset() { count = copied = moved = 0; } |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 348 | int value; |
| 349 | |
| 350 | explicit throws_on_move(int val = 0) : value(val) { ++count; } |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 351 | explicit throws_on_move(int, int val, int) : value(val) { ++count; } |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 352 | throws_on_move(throws_on_move const & other) { |
| 353 | value = other.value; |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 354 | ++count; ++copied; |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | throws_on_move(throws_on_move &&) { |
| 358 | throwMyAnyExpression(); |
| 359 | } |
| 360 | |
| 361 | ~throws_on_move() { |
| 362 | --count; |
| 363 | } |
| 364 | private: |
| 365 | throws_on_move& operator=(throws_on_move const&) = delete; |
| 366 | throws_on_move& operator=(throws_on_move &&) = delete; |
| 367 | }; |
| 368 | |
| 369 | int throws_on_move::count = 0; |
Eric Fiselier | 324506b | 2016-08-11 03:13:11 +0000 | [diff] [blame] | 370 | int throws_on_move::copied = 0; |
| 371 | int throws_on_move::moved = 0; |
| 372 | |
| 373 | struct small_tracked_t { |
| 374 | small_tracked_t() |
| 375 | : arg_types(&makeArgumentID<>()) {} |
| 376 | small_tracked_t(small_tracked_t const&) noexcept |
| 377 | : arg_types(&makeArgumentID<small_tracked_t const&>()) {} |
| 378 | small_tracked_t(small_tracked_t &&) noexcept |
| 379 | : arg_types(&makeArgumentID<small_tracked_t &&>()) {} |
| 380 | template <class ...Args> |
| 381 | explicit small_tracked_t(Args&&...) |
| 382 | : arg_types(&makeArgumentID<Args...>()) {} |
| 383 | template <class ...Args> |
| 384 | explicit small_tracked_t(std::initializer_list<int>, Args&&...) |
| 385 | : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {} |
| 386 | |
| 387 | TypeID const* arg_types; |
| 388 | }; |
| 389 | static_assert(IsSmallObject<small_tracked_t>::value, "must be small"); |
| 390 | |
| 391 | struct large_tracked_t { |
| 392 | large_tracked_t() |
| 393 | : arg_types(&makeArgumentID<>()) { dummy[0] = 42; } |
| 394 | large_tracked_t(large_tracked_t const&) noexcept |
| 395 | : arg_types(&makeArgumentID<large_tracked_t const&>()) {} |
| 396 | large_tracked_t(large_tracked_t &&) noexcept |
| 397 | : arg_types(&makeArgumentID<large_tracked_t &&>()) {} |
| 398 | template <class ...Args> |
| 399 | explicit large_tracked_t(Args&&...) |
| 400 | : arg_types(&makeArgumentID<Args...>()) {} |
| 401 | template <class ...Args> |
| 402 | explicit large_tracked_t(std::initializer_list<int>, Args&&...) |
| 403 | : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {} |
| 404 | |
| 405 | TypeID const* arg_types; |
| 406 | int dummy[10]; |
| 407 | }; |
| 408 | |
| 409 | static_assert(!IsSmallObject<large_tracked_t>::value, "must be small"); |
| 410 | |
| 411 | |
| 412 | template <class Type, class ...Args> |
| 413 | void assertArgsMatch(std::any const& a) { |
| 414 | using namespace std; |
| 415 | using namespace std::experimental; |
| 416 | assert(a.has_value()); |
| 417 | assert(containsType<Type>(a)); |
| 418 | assert(any_cast<Type const &>(a).arg_types == &makeArgumentID<Args...>()); |
| 419 | }; |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 420 | |
| 421 | |
| 422 | #endif |