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// pointer unique_ptr<T>::get() const noexcept;
14// pointer unique_ptr<T[]>::get() const noexcept;
15
16#include <memory>
17#include <cassert>
18#include <cstddef>
19
20#include "test_macros.h"
21
22template <class T>
23TEST_CONSTEXPR_CXX23 void test_basic() {
24 // non-const element type
25 {
26 // non-const access
27 {
28 T* x = new T;
29 std::unique_ptr<T> ptr(x);
30 ASSERT_SAME_TYPE(decltype(ptr.get()), T*);
31 ASSERT_NOEXCEPT(ptr.get());
32 assert(ptr.get() == x);
33 }
34
35 // const access
36 {
37 T* x = new T;
38 std::unique_ptr<T> const ptr(x);
39 ASSERT_SAME_TYPE(decltype(ptr.get()), T*);
40 ASSERT_NOEXCEPT(ptr.get());
41 assert(ptr.get() == x);
42 }
43 }
44
45 // const element type
46 {
47 // non-const access
48 {
49 T* x = new T;
50 std::unique_ptr<T const> ptr(x);
51 ASSERT_SAME_TYPE(decltype(ptr.get()), T const*);
52 assert(ptr.get() == x);
53 }
54
55 // const access
56 {
57 T* x = new T;
58 std::unique_ptr<T const> const ptr(x);
59 ASSERT_SAME_TYPE(decltype(ptr.get()), T const*);
60 assert(ptr.get() == x);
61 }
62 }
63
64 // Same thing but for unique_ptr<T[]>
65 // non-const element type
66 {
67 // non-const access
68 {
69 T* x = new T[3];
70 std::unique_ptr<T[]> ptr(x);
71 ASSERT_SAME_TYPE(decltype(ptr.get()), T*);
72 ASSERT_NOEXCEPT(ptr.get());
73 assert(ptr.get() == x);
74 }
75
76 // const access
77 {
78 T* x = new T[3];
79 std::unique_ptr<T[]> const ptr(x);
80 ASSERT_SAME_TYPE(decltype(ptr.get()), T*);
81 ASSERT_NOEXCEPT(ptr.get());
82 assert(ptr.get() == x);
83 }
84 }
85
86 // const element type
87 {
88 // non-const access
89 {
90 T* x = new T[3];
91 std::unique_ptr<T const[]> ptr(x);
92 ASSERT_SAME_TYPE(decltype(ptr.get()), T const*);
93 assert(ptr.get() == x);
94 }
95
96 // const access
97 {
98 T* x = new T[3];
99 std::unique_ptr<T const[]> const ptr(x);
100 ASSERT_SAME_TYPE(decltype(ptr.get()), T const*);
101 assert(ptr.get() == x);
102 }
103 }
104}
105
106template <std::size_t Size>
107struct WithSize {
108 char padding[Size];
109};
110
111TEST_CONSTEXPR_CXX23 bool test() {
112 test_basic<char>();
113 test_basic<int>();
114 test_basic<WithSize<1> >();
115 test_basic<WithSize<2> >();
116 test_basic<WithSize<3> >();
117 test_basic<WithSize<4> >();
118 test_basic<WithSize<8> >();
119 test_basic<WithSize<16> >();
120 test_basic<WithSize<256> >();
121
122 return true;
123}
124
125int main(int, char**) {
126 test();
127#if TEST_STD_VER >= 23
128 static_assert(test());
129#endif
130
131 return 0;
132}
133

source code of libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.observers/get.pass.cpp