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