summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/unique_ptr/modifiers/constexpr.cc
blob: 81908fdc0812cf403c8e6b7dd103a7d1f37f3079 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// { dg-options "-std=gnu++23" }
// { dg-do compile { target c++23 } }

#include <memory>
#include <testsuite_hooks.h>

constexpr bool
test_release()
{
  std::unique_ptr<int> p1;
  int* r = p1.release();
  VERIFY( !r );
  VERIFY( !p1 );

  std::unique_ptr<int> p2(new int(2));
  r = p2.release();
  VERIFY( r );
  VERIFY( !p2 );
  delete r;

  std::unique_ptr<int[]> a1;
  r = a1.release();
  VERIFY( !r );
  VERIFY( !a1 );

  std::unique_ptr<int[]> a2(new int[2]{});
  r = a2.release();
  VERIFY( r );
  VERIFY( !a2 );
  delete[] r;

  return true;
}
static_assert( test_release() );

constexpr bool
test_reset()
{
  std::unique_ptr<int> p1;
  p1.reset();
  VERIFY( !p1 );
  p1.reset(nullptr);
  VERIFY( !p1 );
  p1.reset(new int(2));
  VERIFY( *p1 == 2 );
  p1.reset(new int(3));
  VERIFY( *p1 == 3 );
  p1.reset(nullptr);
  VERIFY( !p1 );

  std::unique_ptr<int[]> a1;
  a1.reset();
  VERIFY( !a1 );
  a1.reset(nullptr);
  VERIFY( !a1 );
  a1.reset(new int[]{2,3});
  VERIFY( a1[0] == 2 );
  a1.reset(new int[]{4,5,6});
  VERIFY( a1[1] == 5 );
  a1.reset(nullptr);
  VERIFY( !a1 );

  std::unique_ptr<const int[]> a2;
  a2.reset(new int[2]{});

  return true;
}
static_assert( test_reset() );