File: shared_ptr_test.cc

package info (click to toggle)
opentelemetry-cpp 1.23.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,368 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 38; python: 31
file content (199 lines) | stat: -rw-r--r-- 4,015 bytes parent folder | download
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <stddef.h>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>

#include "opentelemetry/nostd/shared_ptr.h"

using opentelemetry::nostd::shared_ptr;

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

  ~A() { destructed_ = true; }

  A(const A &)            = delete;
  A(A &&)                 = delete;
  A &operator=(const A &) = delete;
  A &operator=(A &&)      = delete;

private:
  bool &destructed_;
};

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

class C
{
public:
  virtual ~C() {}
  C() = default;

  C(const C &)            = delete;
  C(C &&)                 = delete;
  C &operator=(const C &) = delete;
  C &operator=(C &&)      = delete;
};

class D : public C
{};

TEST(SharedPtrTest, DefaultConstruction)
{
  shared_ptr<int> ptr1;
  EXPECT_EQ(ptr1.get(), nullptr);

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

TEST(SharedPtrTest, ExplicitConstruction)
{
  auto c = new C();
  shared_ptr<C> ptr1{c};
  EXPECT_EQ(ptr1.get(), c);

  auto d = new D();
  shared_ptr<C> ptr2{d};
  EXPECT_EQ(ptr2.get(), d);
}

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

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

TEST(SharedPtrTest, MoveConstructionFromStdSharedPtr)
{
  auto value = new int{123};
  std::shared_ptr<int> ptr1{value};
  shared_ptr<int> ptr2{std::move(ptr1)};
  EXPECT_EQ(ptr1.get(), nullptr);  // NOLINT
  EXPECT_EQ(ptr2.get(), value);
}

TEST(SharedPtrTest, Destruction)
{
  bool was_destructed;
  shared_ptr<A>{new A{was_destructed}};  // NOLINT
  EXPECT_TRUE(was_destructed);
}

TEST(SharedPtrTest, Assignment)
{
  auto value = new int{123};
  shared_ptr<int> ptr1;

  ptr1 = shared_ptr<int>(value);
  EXPECT_EQ(ptr1.get(), value);

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

  auto value2                = new int{234};
  const shared_ptr<int> ptr2 = shared_ptr<int>(value2);
  ptr1                       = ptr2;
  EXPECT_EQ(ptr1.get(), value2);

  auto value3 = new int{345};
  std::shared_ptr<int> ptr3(value3);
  ptr1 = ptr3;
  EXPECT_EQ(ptr1.get(), value3);
}

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

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

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

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

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

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

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

TEST(SharedPtrTest, Comparison)
{
  shared_ptr<int> ptr1{new int{123}};
  shared_ptr<int> ptr2{new int{456}};
  shared_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);
}

static void SharedPtrTest_Sort(size_t size = 10)
{
  std::vector<shared_ptr<const int>> nums;

  for (int i = static_cast<int>(size); i > 0; i--)
  {
    nums.push_back(shared_ptr<int>(new int(i)));
  }

  auto nums2 = nums;

  std::sort(nums.begin(), nums.end(),
            [](const shared_ptr<const int> &a, const shared_ptr<const int> &b) { return *a < *b; });

  EXPECT_NE(nums, nums2);

  std::reverse(nums2.begin(), nums2.end());

  EXPECT_EQ(nums, nums2);
}

TEST(SharedPtrTest, Sort)
{
  SharedPtrTest_Sort();
}