• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <memory>
10 
11 // unique_ptr
12 
13 // test reset
14 
15 #include <memory>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 #include "unique_ptr_test_helper.h"
20 
21 template <bool IsArray>
test_reset_pointer()22 void test_reset_pointer() {
23   typedef typename std::conditional<IsArray, A[], A>::type VT;
24   const int expect_alive = IsArray ? 3 : 1;
25 #if TEST_STD_VER >= 11
26   {
27     using U = std::unique_ptr<VT>;
28     U u; ((void)u);
29     ASSERT_NOEXCEPT(u.reset((A*)nullptr));
30   }
31 #endif
32   {
33     std::unique_ptr<VT> p(newValue<VT>(expect_alive));
34     assert(A::count == expect_alive);
35     A* i = p.get();
36     assert(i != nullptr);
37     A* new_value = newValue<VT>(expect_alive);
38     assert(A::count == (expect_alive * 2));
39     p.reset(new_value);
40     assert(A::count == expect_alive);
41     assert(p.get() == new_value);
42   }
43   assert(A::count == 0);
44   {
45     std::unique_ptr<const VT> p(newValue<const VT>(expect_alive));
46     assert(A::count == expect_alive);
47     const A* i = p.get();
48     assert(i != nullptr);
49     A* new_value = newValue<VT>(expect_alive);
50     assert(A::count == (expect_alive * 2));
51     p.reset(new_value);
52     assert(A::count == expect_alive);
53     assert(p.get() == new_value);
54   }
55   assert(A::count == 0);
56 }
57 
58 template <bool IsArray>
test_reset_nullptr()59 void test_reset_nullptr() {
60   typedef typename std::conditional<IsArray, A[], A>::type VT;
61   const int expect_alive = IsArray ? 3 : 1;
62 #if TEST_STD_VER >= 11
63   {
64     using U = std::unique_ptr<VT>;
65     U u; ((void)u);
66     ASSERT_NOEXCEPT(u.reset(nullptr));
67   }
68 #endif
69   {
70     std::unique_ptr<VT> p(newValue<VT>(expect_alive));
71     assert(A::count == expect_alive);
72     A* i = p.get();
73     assert(i != nullptr);
74     p.reset(nullptr);
75     assert(A::count == 0);
76     assert(p.get() == nullptr);
77   }
78   assert(A::count == 0);
79 }
80 
81 
82 template <bool IsArray>
test_reset_no_arg()83 void test_reset_no_arg() {
84   typedef typename std::conditional<IsArray, A[], A>::type VT;
85   const int expect_alive = IsArray ? 3 : 1;
86 #if TEST_STD_VER >= 11
87   {
88     using U = std::unique_ptr<VT>;
89     U u; ((void)u);
90     ASSERT_NOEXCEPT(u.reset());
91   }
92 #endif
93   {
94     std::unique_ptr<VT> p(newValue<VT>(expect_alive));
95     assert(A::count == expect_alive);
96     A* i = p.get();
97     assert(i != nullptr);
98     p.reset();
99     assert(A::count == 0);
100     assert(p.get() == nullptr);
101   }
102   assert(A::count == 0);
103 }
104 
main(int,char **)105 int main(int, char**) {
106   {
107     test_reset_pointer</*IsArray*/ false>();
108     test_reset_nullptr<false>();
109     test_reset_no_arg<false>();
110   }
111   {
112     test_reset_pointer</*IsArray*/true>();
113     test_reset_nullptr<true>();
114     test_reset_no_arg<true>();
115   }
116 
117   return 0;
118 }
119