summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/opentelemetry-cpp/api/test/nostd/unique_ptr_test.cc
blob: 1d2e6c062f8afe9ce2fcc69963a093ee471d6a83 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/nostd/unique_ptr.h"

#include <gtest/gtest.h>

using opentelemetry::nostd::unique_ptr;

class A
{
public:
  explicit A(bool &destructed) noexcept : destructed_{destructed} { destructed_ = false; }

  ~A() { destructed_ = true; }

private:
  bool &destructed_;
};

class B
{
public:
  int f() const { return 123; }
};

TEST(UniquePtrTest, DefaultConstruction)
{
  unique_ptr<int> ptr1;
  EXPECT_EQ(ptr1.get(), nullptr);

  unique_ptr<int> ptr2{nullptr};
  EXPECT_EQ(ptr2.get(), nullptr);
}

TEST(UniquePtrTest, ExplicitConstruction)
{
  auto value = new int{123};
  unique_ptr<int> ptr1{value};
  EXPECT_EQ(ptr1.get(), value);

  auto array = new int[5];
  unique_ptr<int[]> ptr2{array};
  EXPECT_EQ(ptr2.get(), array);
}

TEST(UniquePtrTest, MoveConstruction)
{
  auto value = new int{123};
  unique_ptr<int> ptr1{value};
  unique_ptr<int> ptr2{std::move(ptr1)};
  EXPECT_EQ(ptr1.get(), nullptr);
  EXPECT_EQ(ptr2.get(), value);
}

TEST(UniquePtrTest, MoveConstructionFromDifferentType)
{
  auto value = new int{123};
  unique_ptr<int> ptr1{value};
  unique_ptr<const int> ptr2{std::move(ptr1)};
  EXPECT_EQ(ptr1.get(), nullptr);
  EXPECT_EQ(ptr2.get(), value);
}

TEST(UniquePtrTest, MoveConstructionFromStdUniquePtr)
{
  auto value = new int{123};
  std::unique_ptr<int> ptr1{value};
  unique_ptr<int> ptr2{std::move(ptr1)};
  EXPECT_EQ(ptr1.get(), nullptr);
  EXPECT_EQ(ptr2.get(), value);
}

TEST(UniquePtrTest, Destruction)
{
  bool was_destructed;
  unique_ptr<A>{new A{was_destructed}};
  EXPECT_TRUE(was_destructed);
}

TEST(UniquePtrTest, StdUniquePtrConversionOperator)
{
  auto value = new int{123};
  unique_ptr<int> ptr1{value};
  std::unique_ptr<int> ptr2{std::move(ptr1)};
  EXPECT_EQ(ptr1.get(), nullptr);
  EXPECT_EQ(ptr2.get(), value);

  value = new int{456};
  ptr1  = unique_ptr<int>{value};
  ptr2  = std::move(ptr1);
  EXPECT_EQ(ptr1.get(), nullptr);
  EXPECT_EQ(ptr2.get(), value);

  ptr2 = nullptr;
  EXPECT_EQ(ptr2.get(), nullptr);

  EXPECT_TRUE((std::is_assignable<std::unique_ptr<int>, unique_ptr<int> &&>::value));
  EXPECT_FALSE((std::is_assignable<std::unique_ptr<int>, unique_ptr<int> &>::value));
}

TEST(UniquePtrTest, BoolConversionOpertor)
{
  auto value = new int{123};
  unique_ptr<int> ptr1{value};

  EXPECT_TRUE(ptr1);
  EXPECT_FALSE(unique_ptr<int>{});
}

TEST(UniquePtrTest, PointerOperators)
{
  auto value = new int{123};
  unique_ptr<int> ptr1{value};

  EXPECT_EQ(&*ptr1, value);
  EXPECT_EQ(
      unique_ptr<B> { new B }->f(), 123);
}

TEST(UniquePtrTest, Reset)
{
  bool was_destructed1;
  unique_ptr<A> ptr{new A{was_destructed1}};
  bool was_destructed2;
  ptr.reset(new A{was_destructed2});
  EXPECT_TRUE(was_destructed1);
  EXPECT_FALSE(was_destructed2);
  ptr.reset();
  EXPECT_TRUE(was_destructed2);
}

TEST(UniquePtrTest, Release)
{
  auto value = new int{123};
  unique_ptr<int> ptr{value};
  EXPECT_EQ(ptr.release(), value);
  EXPECT_EQ(ptr.get(), nullptr);
  delete value;
}

TEST(UniquePtrTest, Swap)
{
  auto value1 = new int{123};
  unique_ptr<int> ptr1{value1};

  auto value2 = new int{456};
  unique_ptr<int> ptr2{value2};
  ptr1.swap(ptr2);

  EXPECT_EQ(ptr1.get(), value2);
  EXPECT_EQ(ptr2.get(), value1);
}

TEST(UniquePtrTest, Comparison)
{
  unique_ptr<int> ptr1{new int{123}};
  unique_ptr<int> ptr2{new int{456}};
  unique_ptr<int> ptr3{};

  EXPECT_EQ(ptr1, ptr1);
  EXPECT_NE(ptr1, ptr2);

  EXPECT_NE(ptr1, nullptr);
  EXPECT_NE(nullptr, ptr1);

  EXPECT_EQ(ptr3, nullptr);
  EXPECT_EQ(nullptr, ptr3);
}