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 // <memory> 11 12 // unique_ptr 13 14 // test reset 15 16 #include <memory> 17 #include <cassert> 18 19 #include "test_macros.h" 20 #include "unique_ptr_test_helper.h" 21 22 template <bool IsArray> test_reset_pointer()23void test_reset_pointer() { 24 typedef typename std::conditional<IsArray, A[], A>::type VT; 25 const int expect_alive = IsArray ? 3 : 1; 26 #if TEST_STD_VER >= 11 27 { 28 using U = std::unique_ptr<VT>; 29 U u; ((void)u); 30 ASSERT_NOEXCEPT(u.reset((A*)nullptr)); 31 } 32 #endif 33 { 34 std::unique_ptr<VT> p(newValue<VT>(expect_alive)); 35 assert(A::count == expect_alive); 36 A* i = p.get(); 37 assert(i != nullptr); 38 A* new_value = newValue<VT>(expect_alive); 39 assert(A::count == (expect_alive * 2)); 40 p.reset(new_value); 41 assert(A::count == expect_alive); 42 assert(p.get() == new_value); 43 } 44 assert(A::count == 0); 45 { 46 std::unique_ptr<const VT> p(newValue<const VT>(expect_alive)); 47 assert(A::count == expect_alive); 48 const A* i = p.get(); 49 assert(i != nullptr); 50 A* new_value = newValue<VT>(expect_alive); 51 assert(A::count == (expect_alive * 2)); 52 p.reset(new_value); 53 assert(A::count == expect_alive); 54 assert(p.get() == new_value); 55 } 56 assert(A::count == 0); 57 } 58 59 template <bool IsArray> test_reset_nullptr()60void test_reset_nullptr() { 61 typedef typename std::conditional<IsArray, A[], A>::type VT; 62 const int expect_alive = IsArray ? 3 : 1; 63 #if TEST_STD_VER >= 11 64 { 65 using U = std::unique_ptr<VT>; 66 U u; ((void)u); 67 ASSERT_NOEXCEPT(u.reset(nullptr)); 68 } 69 #endif 70 { 71 std::unique_ptr<VT> p(newValue<VT>(expect_alive)); 72 assert(A::count == expect_alive); 73 A* i = p.get(); 74 assert(i != nullptr); 75 p.reset(nullptr); 76 assert(A::count == 0); 77 assert(p.get() == nullptr); 78 } 79 assert(A::count == 0); 80 } 81 82 83 template <bool IsArray> test_reset_no_arg()84void test_reset_no_arg() { 85 typedef typename std::conditional<IsArray, A[], A>::type VT; 86 const int expect_alive = IsArray ? 3 : 1; 87 #if TEST_STD_VER >= 11 88 { 89 using U = std::unique_ptr<VT>; 90 U u; ((void)u); 91 ASSERT_NOEXCEPT(u.reset()); 92 } 93 #endif 94 { 95 std::unique_ptr<VT> p(newValue<VT>(expect_alive)); 96 assert(A::count == expect_alive); 97 A* i = p.get(); 98 assert(i != nullptr); 99 p.reset(); 100 assert(A::count == 0); 101 assert(p.get() == nullptr); 102 } 103 assert(A::count == 0); 104 } 105 main()106int main() { 107 { 108 test_reset_pointer</*IsArray*/ false>(); 109 test_reset_nullptr<false>(); 110 test_reset_no_arg<false>(); 111 } 112 { 113 test_reset_pointer</*IsArray*/true>(); 114 test_reset_nullptr<true>(); 115 test_reset_no_arg<true>(); 116 } 117 } 118