APM:Libraries
test_own_ptr.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2016 Intel Corporation. All rights reserved.
3  *
4  * This file is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This file is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include <AP_gtest.h>
18 
19 #include <utility>
20 #include <AP_HAL/utility/OwnPtr.h>
21 
22 TEST(OwnPtrTest, SamePointer)
23 {
24  int *a = new int{42};
25  AP_HAL::OwnPtr<int> own(a);
26 
27  EXPECT_TRUE(own == a);
28  EXPECT_TRUE(a == own);
29 
30  EXPECT_FALSE(own != a);
31  EXPECT_FALSE(a != own);
32 
33  EXPECT_EQ(a, own.get());
34 }
35 
36 TEST(OwnPtrTest, MoveOwnership)
37 {
38  int *a = new int{42};
39  AP_HAL::OwnPtr<int> own(a);
40  AP_HAL::OwnPtr<int> own2 = std::move(own);
41 
42  EXPECT_EQ(own2.get(), a);
43  EXPECT_EQ(own.get(), nullptr);
44 }
45 
46 class TestDeleted {
47 public:
48  TestDeleted(unsigned int &d) : deleted(d) { }
50 
51  unsigned int &deleted;
52 };
53 
54 TEST(OwnPtrTest, DeleteOutOfScope)
55 {
56  unsigned int deleted = 0;
57 
58  {
59  AP_HAL::OwnPtr<TestDeleted> own(new TestDeleted{deleted});
60  }
61 
62  EXPECT_EQ(deleted, 1U);
63 }
64 
65 TEST(OwnPtrTest, DeleteOutOfScopeAfterMove)
66 {
67  unsigned int deleted = 0;
69 
70  {
71  AP_HAL::OwnPtr<TestDeleted> own2(new TestDeleted{deleted});
72  own = std::move(own2);
73  }
74 
75  // own2 is now out of scope, but it has been moved already
76  EXPECT_EQ(deleted, 0U);
77 
78  // now remove also 'own'
79  own.clear();
80 
81  EXPECT_EQ(deleted, 1U);
82 }
83 
84 class TestCall {
85 public:
86  int foo() { return 42; }
87 };
88 
89 TEST(OwnPtrTest, CallMethod)
90 {
92  EXPECT_EQ(own->foo(), 42);
93  EXPECT_EQ((*own).foo(), 42);
94 }
95 
97 public:
99 
101 };
102 
103 TEST(OwnPtrTest, MoveToConstructor)
104 {
105  unsigned int deleted = 0;
106 
107  AP_HAL::OwnPtr<TestDeleted> own(new TestDeleted{deleted});
108  {
109  EXPECT_EQ(0U, deleted);
110  TestDestructor destructor{std::move(own)};
111  EXPECT_EQ(0U, deleted);
112  }
113  EXPECT_EQ(1U, deleted);
114 }
115 
117 {
118  return AP_HAL::OwnPtr<TestDeleted>(new TestDeleted(deleted));
119 }
120 
121 TEST(OwnPtrTest, ReturnType)
122 {
123  unsigned int deleted = 0;
124 
125  auto own = create_test_deleted(deleted);
126  EXPECT_EQ(0U, deleted);
127  {
128  auto own2 = create_test_deleted(deleted);
129  EXPECT_EQ(0U, deleted);
130  }
131  EXPECT_EQ(1U, deleted);
132  own.clear();
133  EXPECT_EQ(2U, deleted);
134 }
135 
136 TEST(OwnPtrTest, ReplacePointer)
137 {
138  unsigned int deleted1 = 0;
139  unsigned int deleted2 = 0;
140 
141  auto own = create_test_deleted(deleted1);
142  EXPECT_EQ(0U, deleted1);
143  {
144  own = create_test_deleted(deleted2);
145  EXPECT_EQ(1U, deleted1);
146  }
147  EXPECT_EQ(0U, deleted2);
148  own = nullptr;
149  EXPECT_EQ(1U, deleted2);
150 }
151 
152 TEST(OwnPtrTest, ReplaceWithRawPointer)
153 {
154  unsigned int deleted1 = 0;
155 
156  auto own = create_test_deleted(deleted1);
157  EXPECT_EQ(0U, deleted1);
158  {
159  own = new TestDeleted{deleted1};
160  EXPECT_EQ(1U, deleted1);
161  }
162 }
163 
164 TEST(OwnPtrTest, Empty)
165 {
166  int *a = new int{42};
167 
168  AP_HAL::OwnPtr<int> own1;
169  EXPECT_FALSE(own1);
170 
171  own1 = a;
172  EXPECT_TRUE((bool) own1);
173 }
174 
175 class A {
176 public:
177  A(int a) : _a(a) { }
178  int _a;
179 };
180 
181 class B : public A {
182 public:
183  B() : A(42) { }
184 };
185 
186 TEST(OwnPtrTest, Inheritance)
187 {
188  A *a = new A(21);
189  B *b = new B();
190  AP_HAL::OwnPtr<A> own_a(a);
191  AP_HAL::OwnPtr<B> own_b(b);
192 
193  own_a = std::move(own_b);
194  EXPECT_EQ(b, own_a.get());
195  EXPECT_EQ(42, own_a->_a);
196 }
197 
198 AP_GTEST_MAIN()
static AP_HAL::OwnPtr< TestDeleted > create_test_deleted(unsigned int &deleted)
AP_HAL::OwnPtr< TestDeleted > _v
void clear()
Definition: OwnPtr.h:76
unsigned int & deleted
TestDeleted(unsigned int &d)
float v
Definition: Printf.cpp:15
A(int a)
TEST(OwnPtrTest, SamePointer)
TestDestructor(AP_HAL::OwnPtr< TestDeleted > v)
int _a
T * get() const
Definition: OwnPtr.h:88