133 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <memory>10 11// unique_ptr12 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 type25 {26 // non-const access27 {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 access36 {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 type46 {47 // non-const access48 {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 access56 {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 type66 {67 // non-const access68 {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 access77 {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 type87 {88 // non-const access89 {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 access97 {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 >= 23128 static_assert(test());129#endif130 131 return 0;132}133