1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker * Copyright (c) 2021, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker * All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker *
5*cfb92d14SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker *
16*cfb92d14SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker */
28*cfb92d14SAndroid Build Coastguard Worker
29*cfb92d14SAndroid Build Coastguard Worker #include "test_platform.h"
30*cfb92d14SAndroid Build Coastguard Worker
31*cfb92d14SAndroid Build Coastguard Worker #include <string.h>
32*cfb92d14SAndroid Build Coastguard Worker
33*cfb92d14SAndroid Build Coastguard Worker #include <openthread/config.h>
34*cfb92d14SAndroid Build Coastguard Worker
35*cfb92d14SAndroid Build Coastguard Worker #include "test_util.hpp"
36*cfb92d14SAndroid Build Coastguard Worker #include "common/code_utils.hpp"
37*cfb92d14SAndroid Build Coastguard Worker #include "common/owned_ptr.hpp"
38*cfb92d14SAndroid Build Coastguard Worker #include "common/retain_ptr.hpp"
39*cfb92d14SAndroid Build Coastguard Worker
40*cfb92d14SAndroid Build Coastguard Worker namespace ot {
41*cfb92d14SAndroid Build Coastguard Worker
42*cfb92d14SAndroid Build Coastguard Worker class TestObject : public RetainCountable
43*cfb92d14SAndroid Build Coastguard Worker {
44*cfb92d14SAndroid Build Coastguard Worker public:
TestObject(void)45*cfb92d14SAndroid Build Coastguard Worker TestObject(void)
46*cfb92d14SAndroid Build Coastguard Worker : mWasFreed(false)
47*cfb92d14SAndroid Build Coastguard Worker {
48*cfb92d14SAndroid Build Coastguard Worker }
49*cfb92d14SAndroid Build Coastguard Worker
Free(void)50*cfb92d14SAndroid Build Coastguard Worker void Free(void) { mWasFreed = true; }
ResetTestFlags(void)51*cfb92d14SAndroid Build Coastguard Worker void ResetTestFlags(void) { mWasFreed = false; }
GetRetainCount(void) const52*cfb92d14SAndroid Build Coastguard Worker uint16_t GetRetainCount(void) const { return RetainCountable::GetRetainCount(); }
WasFreed(void) const53*cfb92d14SAndroid Build Coastguard Worker bool WasFreed(void) const { return mWasFreed && (GetRetainCount() == 0); }
54*cfb92d14SAndroid Build Coastguard Worker
55*cfb92d14SAndroid Build Coastguard Worker private:
56*cfb92d14SAndroid Build Coastguard Worker bool mWasFreed;
57*cfb92d14SAndroid Build Coastguard Worker };
58*cfb92d14SAndroid Build Coastguard Worker
59*cfb92d14SAndroid Build Coastguard Worker static constexpr uint16_t kSkipRetainCountCheck = 0xffff;
60*cfb92d14SAndroid Build Coastguard Worker
61*cfb92d14SAndroid Build Coastguard Worker template <typename PointerType>
VerifyPointer(const PointerType & aPointer,const TestObject * aObject,uint16_t aRetainCount=kSkipRetainCountCheck)62*cfb92d14SAndroid Build Coastguard Worker void VerifyPointer(const PointerType &aPointer,
63*cfb92d14SAndroid Build Coastguard Worker const TestObject *aObject,
64*cfb92d14SAndroid Build Coastguard Worker uint16_t aRetainCount = kSkipRetainCountCheck)
65*cfb92d14SAndroid Build Coastguard Worker {
66*cfb92d14SAndroid Build Coastguard Worker if (aObject == nullptr)
67*cfb92d14SAndroid Build Coastguard Worker {
68*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aPointer.IsNull());
69*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aPointer.Get() == nullptr);
70*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aPointer == nullptr);
71*cfb92d14SAndroid Build Coastguard Worker }
72*cfb92d14SAndroid Build Coastguard Worker else
73*cfb92d14SAndroid Build Coastguard Worker {
74*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(!aPointer.IsNull());
75*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aPointer.Get() == aObject);
76*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aPointer == aObject);
77*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aPointer != nullptr);
78*cfb92d14SAndroid Build Coastguard Worker
79*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(!aPointer->WasFreed());
80*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(!(*aPointer).WasFreed());
81*cfb92d14SAndroid Build Coastguard Worker
82*cfb92d14SAndroid Build Coastguard Worker if (aRetainCount != kSkipRetainCountCheck)
83*cfb92d14SAndroid Build Coastguard Worker {
84*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aObject->GetRetainCount() == aRetainCount);
85*cfb92d14SAndroid Build Coastguard Worker }
86*cfb92d14SAndroid Build Coastguard Worker }
87*cfb92d14SAndroid Build Coastguard Worker
88*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aPointer == aPointer);
89*cfb92d14SAndroid Build Coastguard Worker }
90*cfb92d14SAndroid Build Coastguard Worker
TestOwnedPtr(void)91*cfb92d14SAndroid Build Coastguard Worker void TestOwnedPtr(void)
92*cfb92d14SAndroid Build Coastguard Worker {
93*cfb92d14SAndroid Build Coastguard Worker TestObject obj1;
94*cfb92d14SAndroid Build Coastguard Worker TestObject obj2;
95*cfb92d14SAndroid Build Coastguard Worker TestObject obj3;
96*cfb92d14SAndroid Build Coastguard Worker
97*cfb92d14SAndroid Build Coastguard Worker printf("\n====================================================================================\n");
98*cfb92d14SAndroid Build Coastguard Worker printf("Testing `OwnedPtr`\n");
99*cfb92d14SAndroid Build Coastguard Worker
100*cfb92d14SAndroid Build Coastguard Worker printf("\n - Default constructor (null pointer)");
101*cfb92d14SAndroid Build Coastguard Worker
102*cfb92d14SAndroid Build Coastguard Worker {
103*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr;
104*cfb92d14SAndroid Build Coastguard Worker
105*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, nullptr);
106*cfb92d14SAndroid Build Coastguard Worker }
107*cfb92d14SAndroid Build Coastguard Worker
108*cfb92d14SAndroid Build Coastguard Worker printf("\n - Constructor taking ownership of an object");
109*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
110*cfb92d14SAndroid Build Coastguard Worker
111*cfb92d14SAndroid Build Coastguard Worker {
112*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr(&obj1);
113*cfb92d14SAndroid Build Coastguard Worker
114*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, &obj1);
115*cfb92d14SAndroid Build Coastguard Worker }
116*cfb92d14SAndroid Build Coastguard Worker
117*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
118*cfb92d14SAndroid Build Coastguard Worker
119*cfb92d14SAndroid Build Coastguard Worker printf("\n - Move constructor taking over from another");
120*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard Worker {
123*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr1(&obj1);
124*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr2(ptr1.PassOwnership());
125*cfb92d14SAndroid Build Coastguard Worker
126*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, nullptr);
127*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, &obj1);
128*cfb92d14SAndroid Build Coastguard Worker }
129*cfb92d14SAndroid Build Coastguard Worker
130*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
131*cfb92d14SAndroid Build Coastguard Worker
132*cfb92d14SAndroid Build Coastguard Worker printf("\n - `Free()` method");
133*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
134*cfb92d14SAndroid Build Coastguard Worker
135*cfb92d14SAndroid Build Coastguard Worker {
136*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr(&obj1);
137*cfb92d14SAndroid Build Coastguard Worker
138*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, &obj1);
139*cfb92d14SAndroid Build Coastguard Worker
140*cfb92d14SAndroid Build Coastguard Worker ptr.Free();
141*cfb92d14SAndroid Build Coastguard Worker
142*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
143*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, nullptr);
144*cfb92d14SAndroid Build Coastguard Worker
145*cfb92d14SAndroid Build Coastguard Worker ptr.Free();
146*cfb92d14SAndroid Build Coastguard Worker
147*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
148*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, nullptr);
149*cfb92d14SAndroid Build Coastguard Worker }
150*cfb92d14SAndroid Build Coastguard Worker
151*cfb92d14SAndroid Build Coastguard Worker printf("\n - `Reset()` method");
152*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
153*cfb92d14SAndroid Build Coastguard Worker obj2.ResetTestFlags();
154*cfb92d14SAndroid Build Coastguard Worker obj3.ResetTestFlags();
155*cfb92d14SAndroid Build Coastguard Worker
156*cfb92d14SAndroid Build Coastguard Worker {
157*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr(&obj1);
158*cfb92d14SAndroid Build Coastguard Worker
159*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, &obj1);
160*cfb92d14SAndroid Build Coastguard Worker
161*cfb92d14SAndroid Build Coastguard Worker ptr.Reset(&obj2);
162*cfb92d14SAndroid Build Coastguard Worker
163*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
164*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(!obj2.WasFreed());
165*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, &obj2);
166*cfb92d14SAndroid Build Coastguard Worker
167*cfb92d14SAndroid Build Coastguard Worker ptr.Reset();
168*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj2.WasFreed());
169*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, nullptr);
170*cfb92d14SAndroid Build Coastguard Worker
171*cfb92d14SAndroid Build Coastguard Worker ptr.Reset(&obj3);
172*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, &obj3);
173*cfb92d14SAndroid Build Coastguard Worker }
174*cfb92d14SAndroid Build Coastguard Worker
175*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
176*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj2.WasFreed());
177*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj3.WasFreed());
178*cfb92d14SAndroid Build Coastguard Worker
179*cfb92d14SAndroid Build Coastguard Worker printf("\n - Self `Reset()`");
180*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
181*cfb92d14SAndroid Build Coastguard Worker
182*cfb92d14SAndroid Build Coastguard Worker {
183*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr1(&obj1);
184*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr2;
185*cfb92d14SAndroid Build Coastguard Worker
186*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1);
187*cfb92d14SAndroid Build Coastguard Worker
188*cfb92d14SAndroid Build Coastguard Worker ptr1.Reset(&obj1);
189*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1);
190*cfb92d14SAndroid Build Coastguard Worker
191*cfb92d14SAndroid Build Coastguard Worker ptr2.Reset(nullptr);
192*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, nullptr);
193*cfb92d14SAndroid Build Coastguard Worker }
194*cfb92d14SAndroid Build Coastguard Worker
195*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
196*cfb92d14SAndroid Build Coastguard Worker
197*cfb92d14SAndroid Build Coastguard Worker printf("\n - Move assignment (operator `=`)");
198*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
199*cfb92d14SAndroid Build Coastguard Worker obj2.ResetTestFlags();
200*cfb92d14SAndroid Build Coastguard Worker obj3.ResetTestFlags();
201*cfb92d14SAndroid Build Coastguard Worker
202*cfb92d14SAndroid Build Coastguard Worker {
203*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr1(&obj1);
204*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr2(&obj2);
205*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr3(&obj3);
206*cfb92d14SAndroid Build Coastguard Worker
207*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1);
208*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, &obj2);
209*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr3, &obj3);
210*cfb92d14SAndroid Build Coastguard Worker
211*cfb92d14SAndroid Build Coastguard Worker // Move from non-null (ptr1) to non-null (ptr2)
212*cfb92d14SAndroid Build Coastguard Worker ptr2 = ptr1.PassOwnership();
213*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, nullptr);
214*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, &obj1);
215*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(!obj1.WasFreed());
216*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj2.WasFreed());
217*cfb92d14SAndroid Build Coastguard Worker
218*cfb92d14SAndroid Build Coastguard Worker // Move from null (ptr1) to non-null (ptr3)
219*cfb92d14SAndroid Build Coastguard Worker ptr3 = ptr1.PassOwnership();
220*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, nullptr);
221*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr3, nullptr);
222*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj3.WasFreed());
223*cfb92d14SAndroid Build Coastguard Worker
224*cfb92d14SAndroid Build Coastguard Worker // Move from non-null (ptr2) to null (ptr1)
225*cfb92d14SAndroid Build Coastguard Worker ptr1 = ptr2.PassOwnership();
226*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1);
227*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, nullptr);
228*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(!obj1.WasFreed());
229*cfb92d14SAndroid Build Coastguard Worker
230*cfb92d14SAndroid Build Coastguard Worker // Move from null (ptr2) to null (ptr3)
231*cfb92d14SAndroid Build Coastguard Worker ptr3 = ptr2.PassOwnership();
232*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, nullptr);
233*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr3, nullptr);
234*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(!obj1.WasFreed());
235*cfb92d14SAndroid Build Coastguard Worker }
236*cfb92d14SAndroid Build Coastguard Worker
237*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
238*cfb92d14SAndroid Build Coastguard Worker
239*cfb92d14SAndroid Build Coastguard Worker printf("\n - Self move assignment (operator `=`)");
240*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
241*cfb92d14SAndroid Build Coastguard Worker
242*cfb92d14SAndroid Build Coastguard Worker {
243*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr1(&obj1);
244*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr2;
245*cfb92d14SAndroid Build Coastguard Worker
246*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1);
247*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, nullptr);
248*cfb92d14SAndroid Build Coastguard Worker
249*cfb92d14SAndroid Build Coastguard Worker // Move from non-null (ptr1) to itself
250*cfb92d14SAndroid Build Coastguard Worker ptr1 = ptr1.PassOwnership();
251*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1);
252*cfb92d14SAndroid Build Coastguard Worker
253*cfb92d14SAndroid Build Coastguard Worker // Move from null (ptr2) to itself
254*cfb92d14SAndroid Build Coastguard Worker ptr2 = ptr2.PassOwnership();
255*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, nullptr);
256*cfb92d14SAndroid Build Coastguard Worker }
257*cfb92d14SAndroid Build Coastguard Worker
258*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
259*cfb92d14SAndroid Build Coastguard Worker
260*cfb92d14SAndroid Build Coastguard Worker printf("\n - `Release()` method");
261*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
262*cfb92d14SAndroid Build Coastguard Worker
263*cfb92d14SAndroid Build Coastguard Worker {
264*cfb92d14SAndroid Build Coastguard Worker OwnedPtr<TestObject> ptr(&obj1);
265*cfb92d14SAndroid Build Coastguard Worker
266*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, &obj1);
267*cfb92d14SAndroid Build Coastguard Worker
268*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(ptr.Release() == &obj1);
269*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(!obj1.WasFreed());
270*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, nullptr);
271*cfb92d14SAndroid Build Coastguard Worker
272*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(ptr.Release() == nullptr);
273*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(!obj1.WasFreed());
274*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, nullptr);
275*cfb92d14SAndroid Build Coastguard Worker }
276*cfb92d14SAndroid Build Coastguard Worker
277*cfb92d14SAndroid Build Coastguard Worker printf("\n\n-- PASS\n");
278*cfb92d14SAndroid Build Coastguard Worker }
279*cfb92d14SAndroid Build Coastguard Worker
TestRetainPtr(void)280*cfb92d14SAndroid Build Coastguard Worker void TestRetainPtr(void)
281*cfb92d14SAndroid Build Coastguard Worker {
282*cfb92d14SAndroid Build Coastguard Worker TestObject obj1;
283*cfb92d14SAndroid Build Coastguard Worker TestObject obj2;
284*cfb92d14SAndroid Build Coastguard Worker TestObject obj3;
285*cfb92d14SAndroid Build Coastguard Worker
286*cfb92d14SAndroid Build Coastguard Worker printf("\n====================================================================================\n");
287*cfb92d14SAndroid Build Coastguard Worker printf("Testing `RetainPtr`\n");
288*cfb92d14SAndroid Build Coastguard Worker
289*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.GetRetainCount() == 0);
290*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj2.GetRetainCount() == 0);
291*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj3.GetRetainCount() == 0);
292*cfb92d14SAndroid Build Coastguard Worker
293*cfb92d14SAndroid Build Coastguard Worker printf("\n - Default constructor (null pointer)");
294*cfb92d14SAndroid Build Coastguard Worker
295*cfb92d14SAndroid Build Coastguard Worker {
296*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr;
297*cfb92d14SAndroid Build Coastguard Worker
298*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, nullptr);
299*cfb92d14SAndroid Build Coastguard Worker }
300*cfb92d14SAndroid Build Coastguard Worker
301*cfb92d14SAndroid Build Coastguard Worker printf("\n - Constructor taking over management of an object");
302*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
303*cfb92d14SAndroid Build Coastguard Worker
304*cfb92d14SAndroid Build Coastguard Worker {
305*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr(&obj1);
306*cfb92d14SAndroid Build Coastguard Worker
307*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, &obj1, 1);
308*cfb92d14SAndroid Build Coastguard Worker }
309*cfb92d14SAndroid Build Coastguard Worker
310*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
311*cfb92d14SAndroid Build Coastguard Worker
312*cfb92d14SAndroid Build Coastguard Worker printf("\n - Two constructed `RetainPtr`s of the same object");
313*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
314*cfb92d14SAndroid Build Coastguard Worker
315*cfb92d14SAndroid Build Coastguard Worker {
316*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr1(&obj1);
317*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr2(&obj1);
318*cfb92d14SAndroid Build Coastguard Worker
319*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1, 2);
320*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, &obj1, 2);
321*cfb92d14SAndroid Build Coastguard Worker }
322*cfb92d14SAndroid Build Coastguard Worker
323*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
324*cfb92d14SAndroid Build Coastguard Worker
325*cfb92d14SAndroid Build Coastguard Worker printf("\n - Copy constructor");
326*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
327*cfb92d14SAndroid Build Coastguard Worker
328*cfb92d14SAndroid Build Coastguard Worker {
329*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr1(&obj1);
330*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr2(ptr1);
331*cfb92d14SAndroid Build Coastguard Worker
332*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1, 2);
333*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, &obj1, 2);
334*cfb92d14SAndroid Build Coastguard Worker }
335*cfb92d14SAndroid Build Coastguard Worker
336*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
337*cfb92d14SAndroid Build Coastguard Worker
338*cfb92d14SAndroid Build Coastguard Worker printf("\n - `Reset()` method");
339*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
340*cfb92d14SAndroid Build Coastguard Worker obj2.ResetTestFlags();
341*cfb92d14SAndroid Build Coastguard Worker obj3.ResetTestFlags();
342*cfb92d14SAndroid Build Coastguard Worker
343*cfb92d14SAndroid Build Coastguard Worker {
344*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr(&obj1);
345*cfb92d14SAndroid Build Coastguard Worker
346*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, &obj1, 1);
347*cfb92d14SAndroid Build Coastguard Worker
348*cfb92d14SAndroid Build Coastguard Worker ptr.Reset(&obj2);
349*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
350*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(!obj2.WasFreed());
351*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, &obj2, 1);
352*cfb92d14SAndroid Build Coastguard Worker
353*cfb92d14SAndroid Build Coastguard Worker ptr.Reset();
354*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj2.WasFreed());
355*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, nullptr);
356*cfb92d14SAndroid Build Coastguard Worker
357*cfb92d14SAndroid Build Coastguard Worker ptr.Reset(&obj3);
358*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, &obj3, 1);
359*cfb92d14SAndroid Build Coastguard Worker }
360*cfb92d14SAndroid Build Coastguard Worker
361*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
362*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj2.WasFreed());
363*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj3.WasFreed());
364*cfb92d14SAndroid Build Coastguard Worker
365*cfb92d14SAndroid Build Coastguard Worker printf("\n - Self `Reset()`");
366*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
367*cfb92d14SAndroid Build Coastguard Worker
368*cfb92d14SAndroid Build Coastguard Worker {
369*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr1(&obj1);
370*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr2;
371*cfb92d14SAndroid Build Coastguard Worker
372*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1, 1);
373*cfb92d14SAndroid Build Coastguard Worker
374*cfb92d14SAndroid Build Coastguard Worker ptr1.Reset(&obj1);
375*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1, 1);
376*cfb92d14SAndroid Build Coastguard Worker
377*cfb92d14SAndroid Build Coastguard Worker ptr2.Reset(nullptr);
378*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, nullptr);
379*cfb92d14SAndroid Build Coastguard Worker }
380*cfb92d14SAndroid Build Coastguard Worker
381*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
382*cfb92d14SAndroid Build Coastguard Worker
383*cfb92d14SAndroid Build Coastguard Worker printf("\n - Assignment `=`");
384*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
385*cfb92d14SAndroid Build Coastguard Worker obj2.ResetTestFlags();
386*cfb92d14SAndroid Build Coastguard Worker
387*cfb92d14SAndroid Build Coastguard Worker {
388*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr1(&obj1);
389*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr2(&obj2);
390*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr3;
391*cfb92d14SAndroid Build Coastguard Worker
392*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1, 1);
393*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, &obj2, 1);
394*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr3, nullptr);
395*cfb92d14SAndroid Build Coastguard Worker
396*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(ptr1 != ptr2);
397*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(ptr1 != ptr3);
398*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(ptr2 != ptr3);
399*cfb92d14SAndroid Build Coastguard Worker
400*cfb92d14SAndroid Build Coastguard Worker // Set from non-null (ptr1) to non-null (ptr2)
401*cfb92d14SAndroid Build Coastguard Worker ptr2 = ptr1;
402*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1, 2);
403*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, &obj1, 2);
404*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj2.WasFreed());
405*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(ptr1 == ptr2);
406*cfb92d14SAndroid Build Coastguard Worker
407*cfb92d14SAndroid Build Coastguard Worker // Set from null (ptr3) to non-null (ptr1)
408*cfb92d14SAndroid Build Coastguard Worker ptr1 = ptr3;
409*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, nullptr);
410*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr3, nullptr);
411*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, &obj1, 1);
412*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(ptr1 == ptr3);
413*cfb92d14SAndroid Build Coastguard Worker
414*cfb92d14SAndroid Build Coastguard Worker // Move from null (ptr1) to null (ptr3)
415*cfb92d14SAndroid Build Coastguard Worker ptr3 = ptr1;
416*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, nullptr);
417*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr3, nullptr);
418*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(ptr1 == ptr3);
419*cfb92d14SAndroid Build Coastguard Worker
420*cfb92d14SAndroid Build Coastguard Worker // Move from non-null (ptr2) to null (ptr3)
421*cfb92d14SAndroid Build Coastguard Worker ptr3 = ptr2;
422*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, &obj1, 2);
423*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr3, &obj1, 2);
424*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(ptr2 == ptr3);
425*cfb92d14SAndroid Build Coastguard Worker }
426*cfb92d14SAndroid Build Coastguard Worker
427*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
428*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj2.WasFreed());
429*cfb92d14SAndroid Build Coastguard Worker
430*cfb92d14SAndroid Build Coastguard Worker printf("\n - Self assignment `=`");
431*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
432*cfb92d14SAndroid Build Coastguard Worker
433*cfb92d14SAndroid Build Coastguard Worker {
434*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr1(&obj1);
435*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr2;
436*cfb92d14SAndroid Build Coastguard Worker
437*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1, 1);
438*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, nullptr);
439*cfb92d14SAndroid Build Coastguard Worker
440*cfb92d14SAndroid Build Coastguard Worker // Set from non-null (ptr1) to itself. We use `*(&ptr1) to
441*cfb92d14SAndroid Build Coastguard Worker // silence `clang` error/warning not allowing explicit self
442*cfb92d14SAndroid Build Coastguard Worker // assignment to same variable.
443*cfb92d14SAndroid Build Coastguard Worker ptr1 = *(&ptr1);
444*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr1, &obj1, 1);
445*cfb92d14SAndroid Build Coastguard Worker
446*cfb92d14SAndroid Build Coastguard Worker // Set from null (ptr2) to itself
447*cfb92d14SAndroid Build Coastguard Worker ptr2 = *(&ptr2);
448*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr2, nullptr);
449*cfb92d14SAndroid Build Coastguard Worker }
450*cfb92d14SAndroid Build Coastguard Worker
451*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.WasFreed());
452*cfb92d14SAndroid Build Coastguard Worker
453*cfb92d14SAndroid Build Coastguard Worker printf("\n - `Release()` method");
454*cfb92d14SAndroid Build Coastguard Worker obj1.ResetTestFlags();
455*cfb92d14SAndroid Build Coastguard Worker
456*cfb92d14SAndroid Build Coastguard Worker {
457*cfb92d14SAndroid Build Coastguard Worker RetainPtr<TestObject> ptr(&obj1);
458*cfb92d14SAndroid Build Coastguard Worker
459*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, &obj1, 1);
460*cfb92d14SAndroid Build Coastguard Worker
461*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(ptr.Release() == &obj1);
462*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, nullptr);
463*cfb92d14SAndroid Build Coastguard Worker
464*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(ptr.Release() == nullptr);
465*cfb92d14SAndroid Build Coastguard Worker VerifyPointer(ptr, nullptr);
466*cfb92d14SAndroid Build Coastguard Worker }
467*cfb92d14SAndroid Build Coastguard Worker
468*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(!obj1.WasFreed());
469*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(obj1.GetRetainCount() == 1);
470*cfb92d14SAndroid Build Coastguard Worker
471*cfb92d14SAndroid Build Coastguard Worker printf("\n\n-- PASS\n");
472*cfb92d14SAndroid Build Coastguard Worker }
473*cfb92d14SAndroid Build Coastguard Worker
474*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
475*cfb92d14SAndroid Build Coastguard Worker
main(void)476*cfb92d14SAndroid Build Coastguard Worker int main(void)
477*cfb92d14SAndroid Build Coastguard Worker {
478*cfb92d14SAndroid Build Coastguard Worker ot::TestOwnedPtr();
479*cfb92d14SAndroid Build Coastguard Worker ot::TestRetainPtr();
480*cfb92d14SAndroid Build Coastguard Worker printf("\nAll tests passed.\n");
481*cfb92d14SAndroid Build Coastguard Worker return 0;
482*cfb92d14SAndroid Build Coastguard Worker }
483