brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.5 KiB · 275ab3d Raw
144 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// test reset14 15#include <memory>16#include <cassert>17 18#include "test_macros.h"19#include "unique_ptr_test_helper.h"20 21template <bool IsArray>22TEST_CONSTEXPR_CXX23 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 >= 1126  {27    using U = std::unique_ptr<VT>;28    U u;29    ((void)u);30    ASSERT_NOEXCEPT(u.reset((A*)nullptr));31  }32#endif33  {34    std::unique_ptr<VT> p(newValue<VT>(expect_alive));35    if (!TEST_IS_CONSTANT_EVALUATED)36      assert(A::count == expect_alive);37    A* i = p.get();38    assert(i != nullptr);39    A* new_value = newValue<VT>(expect_alive);40    if (!TEST_IS_CONSTANT_EVALUATED)41      assert(A::count == (expect_alive * 2));42    p.reset(new_value);43    if (!TEST_IS_CONSTANT_EVALUATED)44      assert(A::count == expect_alive);45    assert(p.get() == new_value);46  }47  if (!TEST_IS_CONSTANT_EVALUATED)48    assert(A::count == 0);49  {50    std::unique_ptr<const VT> p(newValue<const VT>(expect_alive));51    if (!TEST_IS_CONSTANT_EVALUATED)52      assert(A::count == expect_alive);53    const A* i = p.get();54    assert(i != nullptr);55    A* new_value = newValue<VT>(expect_alive);56    if (!TEST_IS_CONSTANT_EVALUATED)57      assert(A::count == (expect_alive * 2));58    p.reset(new_value);59    if (!TEST_IS_CONSTANT_EVALUATED)60      assert(A::count == expect_alive);61    assert(p.get() == new_value);62  }63  if (!TEST_IS_CONSTANT_EVALUATED)64    assert(A::count == 0);65}66 67template <bool IsArray>68TEST_CONSTEXPR_CXX23 void test_reset_nullptr() {69  typedef typename std::conditional<IsArray, A[], A>::type VT;70  const int expect_alive = IsArray ? 3 : 1;71#if TEST_STD_VER >= 1172  {73    using U = std::unique_ptr<VT>;74    U u;75    ((void)u);76    ASSERT_NOEXCEPT(u.reset(nullptr));77  }78#endif79  {80    std::unique_ptr<VT> p(newValue<VT>(expect_alive));81    if (!TEST_IS_CONSTANT_EVALUATED)82      assert(A::count == expect_alive);83    A* i = p.get();84    assert(i != nullptr);85    p.reset(nullptr);86    if (!TEST_IS_CONSTANT_EVALUATED)87      assert(A::count == 0);88    assert(p.get() == nullptr);89  }90  if (!TEST_IS_CONSTANT_EVALUATED)91    assert(A::count == 0);92}93 94template <bool IsArray>95TEST_CONSTEXPR_CXX23 void test_reset_no_arg() {96  typedef typename std::conditional<IsArray, A[], A>::type VT;97  const int expect_alive = IsArray ? 3 : 1;98#if TEST_STD_VER >= 1199  {100    using U = std::unique_ptr<VT>;101    U u;102    ((void)u);103    ASSERT_NOEXCEPT(u.reset());104  }105#endif106  {107    std::unique_ptr<VT> p(newValue<VT>(expect_alive));108    if (!TEST_IS_CONSTANT_EVALUATED)109      assert(A::count == expect_alive);110    A* i = p.get();111    assert(i != nullptr);112    p.reset();113    if (!TEST_IS_CONSTANT_EVALUATED)114      assert(A::count == 0);115    assert(p.get() == nullptr);116  }117  if (!TEST_IS_CONSTANT_EVALUATED)118    assert(A::count == 0);119}120 121TEST_CONSTEXPR_CXX23 bool test() {122  {123    test_reset_pointer</*IsArray*/ false>();124    test_reset_nullptr<false>();125    test_reset_no_arg<false>();126  }127  {128    test_reset_pointer</*IsArray*/true>();129    test_reset_nullptr<true>();130    test_reset_no_arg<true>();131  }132 133  return true;134}135 136int main(int, char**) {137  test();138#if TEST_STD_VER >= 23139  static_assert(test());140#endif141 142  return 0;143}144