1/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "AData_test"
19
20#include <gtest/gtest.h>
21#include <utils/RefBase.h>
22//#include <utils/StrongPointer.h>
23
24#include <media/stagefright/foundation/AData.h>
25#include <media/stagefright/foundation/ABuffer.h>
26
27namespace android {
28
29class ADataTest : public ::testing::Test {
30};
31
32// ============ AUnion
33
34struct Events {
35    int dtor;
36    int ctor_empty;
37    int ctor_copy;
38};
39
40struct EventCounter : public RefBase {
41    EventCounter(int *counter, int magic=1234) : mCounter(counter), mMagic(magic) { }
42    virtual ~EventCounter() { ++*mCounter; mMagic = 0; }
43    int magic() const { return mMagic; }
44private:
45    int *mCounter;
46    int mMagic;
47};
48
49struct DerivedCounter : public EventCounter {
50    DerivedCounter(int *counter, int magic=1234) : EventCounter(counter, magic) { }
51};
52
53TEST_F(ADataTest, AUnion_Test) {
54    AUnion<int, const char *, char> u;
55    u.emplace<int>(4);
56    u.del<int>();
57    EXPECT_EQ(4, u.get<int>()); // verify that del<> is a no-op for trivial types, such as int.
58                                // specifically, verify that it does not clear the objet memory
59
60    u.emplace<const char *>("hello");
61    EXPECT_STREQ("hello", u.get<const char *>());
62    u.del<const char *>();
63
64    // u.del<char *>();
65    // u.emplace<const int>(4);
66    u.emplace<void>();
67    u.del<void>();
68
69    u.emplace<int>(~0);
70    u.del<int>();
71    EXPECT_EQ(~0, u.get<int>());
72    u.emplace<char>(0x15);
73    // verify that rest of memory after char is cleared upon construction
74    EXPECT_EQ(0, memcmp((char *)(&u) + sizeof(char), "\0\0\0", 3));
75    EXPECT_EQ(0x15, u.get<char>());
76    u.del<char>();
77
78    AUnion<EventCounter, EventCounter *> d;
79    int destructions = 0;
80
81    d.emplace<EventCounter>(&destructions);
82    d.del<EventCounter>();
83    EXPECT_EQ(1, destructions);
84
85    EventCounter *ctr = new EventCounter(&destructions);
86    d.emplace<EventCounter *>(ctr);
87    d.del<EventCounter *>();
88    EXPECT_EQ(1, destructions);
89
90    delete ctr;
91    EXPECT_EQ(2, destructions);
92
93    AUnion<std::shared_ptr<EventCounter>, std::unique_ptr<EventCounter>> md;
94    md.emplace<std::shared_ptr<EventCounter>>(new EventCounter(&destructions));
95    std::shared_ptr<EventCounter> copy(md.get<std::shared_ptr<EventCounter>>());
96    std::weak_ptr<EventCounter> weak(copy);
97    EXPECT_EQ(2, destructions);
98
99    copy.reset();
100    EXPECT_EQ(2, destructions);
101    md.del<std::shared_ptr<EventCounter>>();
102    EXPECT_EQ(3, destructions);
103    EXPECT_TRUE(weak.expired());
104
105    md.emplace<std::unique_ptr<EventCounter>>(new EventCounter(&destructions));
106    EXPECT_EQ(3, destructions);
107
108    std::unique_ptr<EventCounter> unique = std::move(md.get<std::unique_ptr<EventCounter>>());
109    EXPECT_EQ(3, destructions);
110    EXPECT_FALSE((bool)md.get<std::unique_ptr<EventCounter>>());
111
112    md.del<std::unique_ptr<EventCounter>>();
113    EXPECT_EQ(3, destructions);
114    md.emplace<std::unique_ptr<EventCounter>>(std::move(unique));
115    EXPECT_TRUE((bool)md.get<std::unique_ptr<EventCounter>>());
116    EXPECT_EQ(3, destructions);
117
118    md.del<std::unique_ptr<EventCounter>>();
119    EXPECT_EQ(4, destructions);
120}
121
122TEST_F(ADataTest, AData_StaticTest) {
123    using namespace std;
124
125    static_assert(is_copy_assignable<shared_ptr<EventCounter>>::value, "");
126    static_assert(is_copy_constructible<shared_ptr<EventCounter>>::value, "");
127    static_assert(is_default_constructible<shared_ptr<EventCounter>>::value, "");
128
129    static_assert(is_copy_assignable<weak_ptr<DerivedCounter>>::value, "");
130    static_assert(is_copy_constructible<weak_ptr<DerivedCounter>>::value, "");
131    static_assert(is_default_constructible<weak_ptr<DerivedCounter>>::value, "");
132
133    static_assert(!is_copy_assignable<unique_ptr<DerivedCounter>>::value, "");
134    static_assert(!is_copy_constructible<unique_ptr<DerivedCounter>>::value, "");
135    static_assert(is_default_constructible<unique_ptr<DerivedCounter>>::value, "");
136
137    static_assert(is_copy_assignable<sp<EventCounter>>::value, "");
138    static_assert(is_copy_constructible<sp<EventCounter>>::value, "");
139    static_assert(is_default_constructible<sp<EventCounter>>::value, "");
140
141    static_assert(is_copy_assignable<wp<EventCounter>>::value, "");
142    static_assert(is_copy_constructible<wp<EventCounter>>::value, "");
143    static_assert(is_default_constructible<wp<EventCounter>>::value, "");
144
145    static_assert(is_convertible<shared_ptr<DerivedCounter>, shared_ptr<EventCounter>>::value, "");
146    static_assert(!is_convertible<shared_ptr<EventCounter>, shared_ptr<DerivedCounter>>::value, "");
147
148    static_assert(is_convertible<unique_ptr<DerivedCounter>, unique_ptr<EventCounter>>::value, "");
149    static_assert(!is_convertible<unique_ptr<EventCounter>, unique_ptr<DerivedCounter>>::value, "");
150
151    static_assert(is_convertible<unique_ptr<DerivedCounter>, shared_ptr<EventCounter>>::value, "");
152    static_assert(!is_convertible<shared_ptr<DerivedCounter>, unique_ptr<EventCounter>>::value, "");
153
154    static_assert(is_convertible<weak_ptr<DerivedCounter>, weak_ptr<EventCounter>>::value, "");
155    static_assert(!is_convertible<weak_ptr<EventCounter>, weak_ptr<DerivedCounter>>::value, "");
156
157    static_assert(is_convertible<shared_ptr<DerivedCounter>, weak_ptr<EventCounter>>::value, "");
158    static_assert(!is_convertible<weak_ptr<DerivedCounter>, shared_ptr<EventCounter>>::value, "");
159
160    static_assert(is_convertible<sp<EventCounter>, sp<RefBase>>::value, "");
161    static_assert(is_convertible<sp<RefBase>, sp<EventCounter>>::value, "YES");
162
163    static_assert(is_convertible<wp<EventCounter>, wp<RefBase>>::value, "");
164    static_assert(is_convertible<wp<RefBase>, wp<EventCounter>>::value, "YES");
165
166    static_assert(is_convertible<sp<EventCounter>, wp<RefBase>>::value, "");
167    static_assert(!is_convertible<wp<EventCounter>, sp<RefBase>>::value, "");
168}
169
170TEST_F(ADataTest, AData_SampleTest) {
171    AData<int, float>::Basic data;
172    int i = 1;
173    float f = 7.0f;
174
175    data.set(5);
176    EXPECT_TRUE(data.find(&i));
177    EXPECT_FALSE(data.find(&f));
178    EXPECT_EQ(i, 5);
179
180    data.set(6.0f);
181    EXPECT_FALSE(data.find(&i));
182    EXPECT_TRUE(data.find(&f));
183    EXPECT_EQ(f, 6.0f);
184
185    AData<int, sp<RefBase>>::RelaxedBasic objdata; // relaxed type support
186    sp<ABuffer> buf = new ABuffer(16), buf2;
187    sp<RefBase> obj;
188
189    objdata.set(buf);
190    EXPECT_TRUE(objdata.find(&buf2));
191    EXPECT_EQ(buf, buf2);
192    EXPECT_FALSE(objdata.find(&i));
193    EXPECT_TRUE(objdata.find(&obj));
194    EXPECT_TRUE(obj == buf);
195
196    obj = buf;
197    objdata.set(obj); // storing as sp<RefBase>
198    EXPECT_FALSE(objdata.find(&buf2));  // not stored as ABuffer(!)
199    EXPECT_TRUE(objdata.find(&obj));
200}
201
202struct SampleTypeFlagger {
203    typedef unsigned type;
204    enum Flags : type {
205        kEmpty = 100,
206        kInt,
207        kConstCharPtr,
208        kEventCounter,
209        kEventCounterPointer,
210        kEventCounterSharedPointer,
211        kEventCounterUniquePointer,
212        kEventCounterWeakPointer,
213        kEventCounterSP,
214        kEventCounterWP,
215    };
216    constexpr static type mask = ~Flags(0);
217    constexpr static type flagFor(void*) { return kEmpty; }
218    constexpr static type flagFor(int*) { return kInt; }
219    constexpr static type flagFor(const char**) { return kConstCharPtr; }
220    constexpr static type flagFor(EventCounter*) { return kEventCounter; }
221    constexpr static type flagFor(EventCounter**) { return kEventCounterPointer; }
222    constexpr static
223    type flagFor(std::shared_ptr<EventCounter>*) { return kEventCounterSharedPointer; }
224    constexpr static
225    type flagFor(std::unique_ptr<EventCounter>*) { return kEventCounterUniquePointer; }
226    constexpr static type flagFor(std::weak_ptr<EventCounter>*) { return kEventCounterWeakPointer; }
227    constexpr static type flagFor(sp<EventCounter>*) { return kEventCounterSP; }
228    constexpr static type flagFor(wp<EventCounter>*) { return kEventCounterWP; }
229    constexpr static bool canDeleteAs(type object, type del) { return del == object; }
230    template <typename T> struct store { typedef T as_type; };
231};
232
233TEST_F(ADataTest, AData_SimpleTest) {
234    int _int = 0;
235    const char *_constCharPtr = NULL;
236    AData<int, const char *>::Custom<SampleTypeFlagger> u;
237    EXPECT_FALSE(u.used());
238    EXPECT_FALSE(u.find<int>(&_int));
239    EXPECT_FALSE(u.find<const char *>(&_constCharPtr));
240
241    EXPECT_TRUE(u.set<int>(4));
242    EXPECT_TRUE(u.used());
243    EXPECT_TRUE(u.find<int>(&_int));
244    EXPECT_EQ(4, _int);
245    EXPECT_FALSE(u.find<const char *>(&_constCharPtr));
246    EXPECT_EQ(NULL, _constCharPtr);
247
248    EXPECT_TRUE(u.clear());
249    EXPECT_FALSE(u.used());
250    EXPECT_FALSE(u.find<int>(&_int));
251    EXPECT_FALSE(u.find<const char *>(&_constCharPtr));
252
253    EXPECT_TRUE(u.set<int>(5));
254    EXPECT_TRUE(u.set<int>(6));
255    EXPECT_TRUE(u.find<int>(&_int));
256    EXPECT_EQ(6, _int);
257
258    EXPECT_TRUE(u.set<const char *>("hello"));
259    EXPECT_TRUE(u.used());
260    EXPECT_FALSE(u.find<int>(&_int));
261    EXPECT_TRUE(u.find<const char *>(&_constCharPtr));
262    EXPECT_STREQ("hello", _constCharPtr);
263
264    EXPECT_TRUE(u.clear());
265    EXPECT_FALSE(u.used());
266    EXPECT_FALSE(u.find<int>(&_int));
267    EXPECT_FALSE(u.find<const char *>(&_constCharPtr));
268
269    EXPECT_TRUE(u.set<const char *>("world"));
270    EXPECT_TRUE(u.set<const char *>("!!"));
271    EXPECT_TRUE(u.used());
272    EXPECT_FALSE(u.find<int>(&_int));
273    EXPECT_TRUE(u.find<const char *>(&_constCharPtr));
274    EXPECT_STREQ("!!", _constCharPtr);
275
276    EXPECT_FALSE(u.find(&_int));
277    EXPECT_TRUE(u.find(&_constCharPtr));
278}
279
280void set(std::unique_ptr<int> &dst, std::unique_ptr<int> &&src) {
281    dst = std::move(src);
282}
283
284void set(std::unique_ptr<int> &dst, std::unique_ptr<int> &src) {
285    dst = std::move(src);
286}
287
288TEST_F(ADataTest, AData_CopyMoveTest) {
289    int destructions = 0;
290    int _int = 0;
291    std::shared_ptr<EventCounter> _shared;
292    std::unique_ptr<EventCounter> _unique;
293    std::weak_ptr<EventCounter> _weak;
294    const std::shared_ptr<EventCounter> _constShared(new EventCounter(&destructions));
295    const std::unique_ptr<EventCounter> _constUnique = nullptr;
296
297    AData<int, std::weak_ptr<EventCounter>, std::shared_ptr<EventCounter>,
298            std::unique_ptr<EventCounter>>::Basic u;
299
300    // test that data is empty
301    EXPECT_FALSE(u.used());
302    EXPECT_FALSE(u.find(&_int));
303    EXPECT_FALSE(u.find(&_shared));
304    EXPECT_FALSE(u.remove(&_unique));
305    EXPECT_FALSE(u.find(&_weak));
306
307    // test that integer can be stored and read
308    EXPECT_TRUE(u.set<int>(1));
309    EXPECT_TRUE(u.used());
310    EXPECT_TRUE(u.find(&_int));
311    EXPECT_EQ(1, _int);
312    EXPECT_FALSE(u.find(&_shared));
313    EXPECT_FALSE(u.remove(&_unique));
314    EXPECT_FALSE(u.find(&_weak));
315
316    // test that movable type (unique_ptr) can be moved in and read out, and it moves
317    _unique = std::unique_ptr<EventCounter>(new EventCounter(&destructions, 123));
318    EXPECT_TRUE(u.set(std::move(_unique)));
319    EXPECT_FALSE((bool)_unique);
320    EXPECT_TRUE(u.used());
321    EXPECT_FALSE(u.find(&_int));
322    EXPECT_FALSE(u.find(&_shared));
323    EXPECT_FALSE(u.find(&_weak));
324    EXPECT_TRUE(u.remove(&_unique));
325    EXPECT_TRUE((bool)_unique);
326    if (_unique) {
327        EXPECT_EQ(123, _unique->magic());
328    }
329
330    // the unique value should have been removed but still accessible as nullptr
331    EXPECT_TRUE(u.remove(&_unique));
332    EXPECT_FALSE((bool)_unique);
333    EXPECT_EQ(1, destructions);
334
335    // test that movable-only type (unique_ptr) can be stored without moving (and is still
336    // moved)
337    _unique = std::unique_ptr<EventCounter>(new EventCounter(&destructions, 321));
338    EXPECT_TRUE(u.set(std::move(_unique)));
339    EXPECT_FALSE((bool)_unique);
340    EXPECT_TRUE(u.set(std::unique_ptr<EventCounter>(new EventCounter(&destructions, 1234))));
341    EXPECT_EQ(2, destructions);
342    EXPECT_TRUE(u.remove(&_unique));
343    EXPECT_TRUE((bool)_unique);
344    if (_unique) {
345        EXPECT_EQ(1234, _unique->magic());
346    }
347    EXPECT_TRUE(u.set(std::move(_unique)));
348    EXPECT_EQ(2, destructions);
349    EXPECT_TRUE(u.clear());
350    EXPECT_EQ(3, destructions);
351    EXPECT_FALSE(u.find(&_int));
352    EXPECT_FALSE(u.find(&_shared));
353    EXPECT_FALSE(u.remove(&_unique));
354    EXPECT_FALSE(u.find(&_weak));
355
356    // u.set(_constUnique);
357
358    // test that copiable & movable type (shared_ptr) is copied unless explicitly moved.
359    _shared = std::make_shared<EventCounter>(&destructions, 234);
360    EXPECT_EQ(1L, _shared.use_count());
361    EXPECT_TRUE(u.set(_shared));
362    EXPECT_TRUE((bool)_shared);
363    if (_shared) {
364        EXPECT_EQ(234, _shared->magic());
365    }
366
367    EXPECT_EQ(2L, _shared.use_count());
368    EXPECT_FALSE(u.find(&_int));
369    EXPECT_FALSE(u.remove(&_unique));
370    EXPECT_FALSE(u.find(&_weak));
371    EXPECT_TRUE(u.find(&_shared));
372    EXPECT_EQ(2L, _shared.use_count());
373    EXPECT_TRUE((bool)_shared);
374    if (_shared) {
375        EXPECT_EQ(234, _shared->magic());
376    }
377
378    // explicitly move in shared_ptr
379    EXPECT_TRUE(u.set(std::move(_shared)));
380    EXPECT_EQ(0, _shared.use_count()); // shared should be nullptr
381    EXPECT_FALSE((bool)_shared);
382    EXPECT_TRUE(u.find(&_shared));
383    EXPECT_EQ(2L, _shared.use_count()); // now both u and _shared contains the object
384    EXPECT_TRUE((bool)_shared);
385    if (_shared) {
386        EXPECT_EQ(234, _shared->magic());
387    }
388    EXPECT_FALSE(u.find(&_int));
389    EXPECT_FALSE(u.remove(&_unique));
390    EXPECT_FALSE(u.find(&_weak));
391    EXPECT_TRUE(u.find(&_shared));
392    EXPECT_EQ(2L, _shared.use_count()); // still both u and _shared contains the object
393
394    EXPECT_TRUE(u.clear());
395    EXPECT_TRUE(_shared.unique()); // now only _shared contains the object
396
397    EXPECT_TRUE(u.set(_constShared));
398    EXPECT_EQ(2L, _constShared.use_count()); // even though it is const, we can add a use count
399    EXPECT_TRUE(u.find(&_shared));
400    EXPECT_EQ(3L, _shared.use_count()); // now u, _shared and _constShared contains the const object
401    EXPECT_TRUE((bool)_shared);
402    if (_shared) {
403        EXPECT_EQ(1234, _shared->magic());
404    }
405
406    // test that weak pointer can be copied in (support for moving is from C++14 only)
407    _weak = _shared;
408    EXPECT_EQ(_weak.use_count(), _shared.use_count());
409    EXPECT_TRUE(u.set(_weak));
410
411    _weak.reset();
412    EXPECT_EQ(_weak.use_count(), 0);
413
414    EXPECT_FALSE(u.find(&_int));
415    EXPECT_FALSE(u.remove(&_unique));
416    EXPECT_FALSE(u.find(&_shared));
417    EXPECT_TRUE(u.find(&_weak));
418    EXPECT_EQ(_weak.use_count(), _shared.use_count());
419    EXPECT_EQ(_weak.lock(), _shared);
420
421    // we can remove a weak pointer multiple times
422    _weak.reset();
423    EXPECT_TRUE(u.find(&_weak));
424    EXPECT_EQ(_weak.use_count(), _shared.use_count());
425    EXPECT_EQ(_weak.lock(), _shared);
426    EXPECT_TRUE(u.clear());
427    EXPECT_FALSE(u.find(&_int));
428    EXPECT_FALSE(u.remove(&_unique));
429    EXPECT_FALSE(u.find(&_shared));
430    EXPECT_FALSE(u.find(&_weak));
431};
432
433TEST_F(ADataTest, AData_RelaxedCopyMoveTest) {
434    int destructions = 0;
435    int _int = 0;
436    std::shared_ptr<DerivedCounter> _shared;
437    std::unique_ptr<DerivedCounter> _unique, _unique2;
438    std::weak_ptr<DerivedCounter> _weak;
439    std::shared_ptr<EventCounter> _shared_base;
440    std::unique_ptr<EventCounter> _unique_base;
441    std::weak_ptr<EventCounter> _weak_base;
442    const std::shared_ptr<DerivedCounter> _constShared(new DerivedCounter(&destructions));
443    const std::unique_ptr<DerivedCounter> _constUnique = nullptr;
444
445    AData<int, std::unique_ptr<EventCounter>, std::shared_ptr<EventCounter>,
446            std::weak_ptr<EventCounter>>::RelaxedBasic u;
447
448    // test that data is empty
449    EXPECT_FALSE(u.used());
450    EXPECT_FALSE(u.find(&_int));
451    EXPECT_FALSE(u.find(&_shared));
452    EXPECT_FALSE(u.remove(&_unique));
453    EXPECT_FALSE(u.find(&_weak));
454    EXPECT_FALSE(u.find(&_shared_base));
455    EXPECT_FALSE(u.remove(&_unique_base));
456    EXPECT_FALSE(u.find(&_weak_base));
457
458    // test that integer can be stored and read
459    EXPECT_TRUE(u.set<int>(1));
460    EXPECT_TRUE(u.used());
461    EXPECT_TRUE(u.find(&_int));
462    EXPECT_EQ(1, _int);
463    EXPECT_FALSE(u.find(&_shared));
464    EXPECT_FALSE(u.remove(&_unique));
465    EXPECT_FALSE(u.find(&_weak));
466    EXPECT_FALSE(u.find(&_shared_base));
467    EXPECT_FALSE(u.remove(&_unique_base));
468    EXPECT_FALSE(u.find(&_weak_base));
469
470    // test that movable type (unique_ptr) can be moved in and read out, and it moves
471    _unique = std::unique_ptr<DerivedCounter>(new DerivedCounter(&destructions, 123));
472    EXPECT_TRUE(u.set(std::move(_unique)));
473    EXPECT_FALSE((bool)_unique);
474    EXPECT_TRUE(u.used());
475    EXPECT_FALSE(u.find(&_int));
476    EXPECT_FALSE(u.find(&_shared));
477    EXPECT_FALSE(u.find(&_weak));
478    EXPECT_TRUE(u.remove(&_unique));
479    EXPECT_TRUE((bool)_unique);
480    if (_unique) {
481        EXPECT_EQ(123, _unique->magic());
482    }
483
484    // the unique value should have been removed but still accessible as nullptr
485    EXPECT_TRUE(u.remove(&_unique));
486    EXPECT_FALSE((bool)_unique);
487    EXPECT_EQ(1, destructions);
488
489    EXPECT_FALSE(u.find(&_shared_base));
490    EXPECT_TRUE(u.remove(&_unique_base));
491    EXPECT_FALSE((bool)_unique_base);
492    EXPECT_FALSE(u.find(&_weak_base));
493
494    // test that movable-only type (unique_ptr) can be stored without moving (and is still
495    // moved)
496    _unique = std::unique_ptr<DerivedCounter>(new DerivedCounter(&destructions, 321));
497    EXPECT_TRUE(u.set(std::move(_unique)));
498    EXPECT_FALSE((bool)_unique);
499    EXPECT_TRUE(u.set(std::unique_ptr<DerivedCounter>(new DerivedCounter(&destructions, 1234))));
500    EXPECT_EQ(2, destructions);
501    EXPECT_TRUE(u.remove(&_unique));
502    EXPECT_TRUE((bool)_unique);
503    if (_unique) {
504        EXPECT_EQ(1234, _unique->magic());
505    }
506    EXPECT_TRUE(u.set(std::move(_unique)));
507    EXPECT_EQ(2, destructions);
508    EXPECT_TRUE(u.clear());
509    EXPECT_EQ(3, destructions);
510    EXPECT_FALSE(u.find(&_int));
511    EXPECT_FALSE(u.find(&_shared));
512    EXPECT_FALSE(u.remove(&_unique));
513    EXPECT_FALSE(u.find(&_weak));
514    EXPECT_FALSE(u.find(&_shared_base));
515    EXPECT_FALSE(u.remove(&_unique_base));
516    EXPECT_FALSE(u.find(&_weak_base));
517
518    // test that unique pointer can be set and removed as base type (but removed as derived only
519    // if it was set as derived type)
520    _unique = std::unique_ptr<DerivedCounter>(new DerivedCounter(&destructions, 321));
521    EXPECT_TRUE(u.set(std::move(_unique)));
522    EXPECT_FALSE((bool)_unique);
523    EXPECT_TRUE(u.remove(&_unique_base));
524    EXPECT_TRUE((bool)_unique_base);
525    if (_unique_base) {
526        EXPECT_EQ(321, _unique_base->magic());
527    }
528    EXPECT_TRUE(u.remove(&_unique));
529    EXPECT_FALSE((bool)_unique);
530
531    EXPECT_TRUE(u.set(std::move(_unique_base)));
532    EXPECT_FALSE((bool)_unique_base);
533    EXPECT_FALSE(u.remove(&_unique));
534    EXPECT_FALSE((bool)_unique);
535    EXPECT_TRUE(u.remove(&_unique_base));
536    EXPECT_TRUE((bool)_unique_base);
537    if (_unique_base) {
538        EXPECT_EQ(321, _unique_base->magic());
539    }
540
541    EXPECT_EQ(3, destructions);
542    EXPECT_TRUE(u.remove(&_unique_base));
543    EXPECT_EQ(4, destructions);
544    EXPECT_FALSE((bool)_unique_base);
545    EXPECT_FALSE(u.find(&_int));
546    EXPECT_FALSE(u.find(&_shared));
547    EXPECT_FALSE(u.find(&_shared_base));
548    EXPECT_FALSE(u.find(&_weak));
549    EXPECT_FALSE(u.find(&_weak_base));
550
551    // u.set(_constUnique);
552
553    // test that copiable & movable type (shared_ptr) is copied unless explicitly moved.
554    _shared = std::make_shared<DerivedCounter>(&destructions, 234);
555    EXPECT_EQ(1L, _shared.use_count());
556    EXPECT_TRUE(u.set(_shared));
557    EXPECT_TRUE((bool)_shared);
558    if (_shared) {
559        EXPECT_EQ(234, _shared->magic());
560    }
561
562    EXPECT_EQ(2L, _shared.use_count());
563    EXPECT_FALSE(u.find(&_int));
564    EXPECT_FALSE(u.remove(&_unique));
565    EXPECT_FALSE(u.find(&_weak));
566    EXPECT_TRUE(u.find(&_shared));
567    EXPECT_FALSE(u.remove(&_unique_base));
568    EXPECT_FALSE(u.find(&_weak_base));
569    EXPECT_EQ(2L, _shared.use_count());
570    EXPECT_TRUE((bool)_shared);
571    if (_shared) {
572        EXPECT_EQ(234, _shared->magic());
573    }
574
575    // explicitly move in shared_ptr
576    EXPECT_TRUE(u.set(std::move(_shared)));
577    EXPECT_EQ(0, _shared.use_count()); // shared should be nullptr
578    EXPECT_FALSE((bool)_shared);
579    EXPECT_TRUE(u.find(&_shared));
580    EXPECT_EQ(2L, _shared.use_count()); // now both u and _shared contains the object
581    EXPECT_TRUE((bool)_shared);
582    if (_shared) {
583        EXPECT_EQ(234, _shared->magic());
584    }
585    EXPECT_FALSE(u.find(&_int));
586    EXPECT_FALSE(u.remove(&_unique));
587    EXPECT_FALSE(u.find(&_weak));
588    EXPECT_FALSE(u.remove(&_unique_base));
589    EXPECT_FALSE(u.find(&_weak_base));
590    EXPECT_TRUE(u.find(&_shared));
591    EXPECT_EQ(2L, _shared.use_count()); // still both u and _shared contains the object
592
593    EXPECT_TRUE(u.clear());
594    EXPECT_TRUE(_shared.unique()); // now only _shared contains the object
595
596    EXPECT_TRUE(u.set(_constShared));
597    EXPECT_EQ(2L, _constShared.use_count()); // even though it is const, we can add a use count
598    EXPECT_TRUE(u.find(&_shared));
599    EXPECT_EQ(3L, _shared.use_count()); // now u, _shared and _constShared contains the const object
600    EXPECT_TRUE((bool)_shared);
601    if (_shared) {
602        EXPECT_EQ(1234, _shared->magic());
603    }
604
605    // test that shared pointer can be set and removed as base type (but removed as derived only
606    // if it was set as derived type)
607    EXPECT_TRUE(u.find(&_shared_base));
608    EXPECT_TRUE((bool)_shared_base);
609    if (_shared_base) {
610        EXPECT_EQ(1234, _shared_base->magic());
611    }
612    EXPECT_EQ(4L, _shared.use_count()); // now u, _shared, _constShared and _shared_base contains
613                                        // the const object
614    _shared.reset();
615    EXPECT_EQ(3L, _shared_base.use_count()); // now u, _constShared and _shared_base contains it
616    EXPECT_TRUE(u.clear());
617    EXPECT_EQ(2L, _shared_base.use_count()); // now _constShared and _shared_base contains it
618
619    EXPECT_TRUE(u.set(_shared_base));        // now u_ also contains it as base class
620    EXPECT_EQ(3L, _shared_base.use_count());
621    EXPECT_FALSE(u.find(&_shared)); // cannot get it as derived type
622    EXPECT_FALSE((bool)_shared);
623    _shared_base.reset();
624    EXPECT_TRUE(u.find(&_shared_base)); // can still get it as base type
625    EXPECT_TRUE((bool)_shared_base);
626    if (_shared_base) {
627        EXPECT_EQ(1234, _shared_base->magic());
628    }
629    _shared = std::static_pointer_cast<DerivedCounter>(_shared_base);
630    EXPECT_FALSE(u.find(&_int));
631    EXPECT_FALSE(u.remove(&_unique));
632    EXPECT_FALSE(u.remove(&_unique_base));
633    EXPECT_FALSE(u.find(&_weak));
634    EXPECT_FALSE(u.find(&_weak_base));
635
636    // test that weak pointer can be copied in (support for moving is from C++14 only)
637    _weak = _shared;
638    EXPECT_EQ(_weak.use_count(), _shared.use_count());
639    EXPECT_TRUE(u.set(_weak));
640
641    _weak.reset();
642    EXPECT_EQ(_weak.use_count(), 0);
643
644    EXPECT_FALSE(u.find(&_int));
645    EXPECT_FALSE(u.remove(&_unique));
646    EXPECT_FALSE(u.find(&_shared));
647    EXPECT_FALSE(u.remove(&_unique_base));
648    EXPECT_FALSE(u.find(&_shared_base));
649    EXPECT_TRUE(u.find(&_weak));
650    EXPECT_EQ(_weak.use_count(), _shared.use_count());
651    EXPECT_EQ(_weak.lock(), _shared);
652
653    // we can remove a weak pointer multiple times
654    _weak.reset();
655    EXPECT_TRUE(u.find(&_weak));
656    EXPECT_EQ(_weak.use_count(), _shared.use_count());
657    EXPECT_EQ(_weak.lock(), _shared);
658    EXPECT_TRUE(u.clear());
659    EXPECT_FALSE(u.find(&_int));
660    EXPECT_FALSE(u.remove(&_unique));
661    EXPECT_FALSE(u.find(&_shared));
662    EXPECT_FALSE(u.find(&_weak));
663    EXPECT_FALSE(u.remove(&_unique_base));
664    EXPECT_FALSE(u.find(&_shared_base));
665    EXPECT_FALSE(u.find(&_weak_base));
666
667    // test that weak pointer can be set and removed as base type (but removed as derived only
668    // if it was set as derived type)
669    _weak = _shared;
670    EXPECT_TRUE(u.set(_weak));
671    EXPECT_TRUE(u.find(&_weak_base));
672    EXPECT_FALSE(_weak_base.expired());
673    if (!_weak_base.expired()) {
674        EXPECT_EQ(1234, _weak_base.lock()->magic());
675    }
676    // now _shared, _constShared and _shared_base contains the const object
677    EXPECT_EQ(3L, _weak.use_count());
678    _weak.reset();
679    EXPECT_EQ(3L, _weak_base.use_count()); // _weak did not hold a reference
680    _shared.reset();
681    EXPECT_EQ(2L, _weak_base.use_count()); // now u, _constShared and _shared_base contains it
682    EXPECT_TRUE(u.clear());
683    EXPECT_FALSE(u.find(&_int));
684    EXPECT_FALSE(u.remove(&_unique));
685    EXPECT_FALSE(u.find(&_shared));
686    EXPECT_FALSE(u.find(&_weak));
687    EXPECT_FALSE(u.remove(&_unique_base));
688    EXPECT_FALSE(u.find(&_shared_base));
689    EXPECT_FALSE(u.find(&_weak_base));
690
691    EXPECT_TRUE(u.set(_weak_base)); // now u_ also contains it as base class
692    EXPECT_FALSE(u.find(&_weak));   // cannot get it as derived type
693    EXPECT_TRUE(_weak.expired());
694    _weak_base.reset();
695    EXPECT_TRUE(u.find(&_weak_base)); // can still get it as base type
696    EXPECT_FALSE(_weak_base.expired());
697    if (!_weak_base.expired()) {
698        EXPECT_EQ(1234, _weak_base.lock()->magic());
699    }
700};
701
702TEST_F(ADataTest, AData_AndroidSpTest) {
703    int destructions = 0;
704    int _int = 0;
705    sp<EventCounter> _shared;
706    wp<EventCounter> _weak;
707    const sp<EventCounter> _constShared(new EventCounter(&destructions));
708
709    AData<int, sp<EventCounter>, wp<EventCounter>>::Strict<uint8_t> u;
710
711    // test that data is empty
712    EXPECT_FALSE(u.used());
713    EXPECT_FALSE(u.find(&_int));
714    EXPECT_FALSE(u.find(&_shared));
715    EXPECT_FALSE(u.find(&_weak));
716
717    // test that integer can be stored and read
718    EXPECT_TRUE(u.set<int>(1));
719    EXPECT_TRUE(u.used());
720    EXPECT_TRUE(u.find(&_int));
721    EXPECT_EQ(1, _int);
722    EXPECT_FALSE(u.find(&_shared));
723    EXPECT_FALSE(u.find(&_weak));
724
725    // test that copiable & movable type (shared_ptr) is copied unless explicitly moved.
726    _shared = new EventCounter(&destructions, 234);
727    _weak = _shared; // used for tracking #234
728
729    EXPECT_TRUE(u.set(_shared));
730    EXPECT_TRUE((bool)_shared.get());
731    if (_shared.get()) {
732        EXPECT_EQ(234, _shared->magic());
733    }
734
735    _shared.clear();
736    EXPECT_EQ(NULL, _shared.get());
737    EXPECT_NE(nullptr, _weak.promote().get()); // u still holds object
738
739    EXPECT_FALSE(u.find(&_int));
740    EXPECT_FALSE(u.find(&_weak));
741    EXPECT_TRUE(u.find(&_shared)); // now u and _shared both hold object
742    EXPECT_TRUE((bool)_shared.get());
743    if (_shared.get()) {
744        EXPECT_EQ(234, _shared->magic());
745    }
746    // verify the find did not move out object
747    _shared.clear();
748    EXPECT_EQ(NULL, _shared.get());
749    EXPECT_NE(nullptr, _weak.promote().get()); // u still holds object
750    EXPECT_TRUE(u.find(&_shared)); // now u and _shared both hold object
751    if (_shared.get()) {
752        EXPECT_EQ(234, _shared->magic());
753    }
754
755    // verify that we can set object multiple times
756    EXPECT_TRUE(u.set(_shared));
757
758    // explicitly move in sp
759    EXPECT_TRUE(u.set(std::move(_shared)));
760    EXPECT_FALSE((bool)_shared.get()); // android also clears sp<> on move...
761    EXPECT_TRUE(u.find(&_shared)); // still can get it back
762    EXPECT_TRUE((bool)_shared.get());
763    if (_shared.get()) {
764        EXPECT_EQ(234, _shared->magic());
765    }
766    EXPECT_FALSE(u.find(&_int));
767    EXPECT_FALSE(u.find(&_weak));
768
769    EXPECT_TRUE(u.used());
770    EXPECT_TRUE(u.clear()); // now only _shared contains the object
771    EXPECT_FALSE(u.used());
772
773    // we still hold a copy
774    EXPECT_TRUE((bool)_shared.get());
775    EXPECT_FALSE(u.find(&_int));
776    EXPECT_FALSE(u.find(&_shared)); // _shared still contains the object
777
778    EXPECT_TRUE(u.set(_constShared));
779    EXPECT_TRUE(u.find(&_shared)); // now _shared contains _constShared
780    EXPECT_EQ(NULL, _weak.promote().get()); // original _shared is now lost
781
782    EXPECT_TRUE((bool)_shared.get());
783    if (_shared.get()) {
784        EXPECT_EQ(1234, _shared->magic());
785    }
786    EXPECT_TRUE(u.clear());
787
788    // test that wp can be copied in
789    _weak = _shared;
790    EXPECT_TRUE(u.set(_weak));
791
792    _weak.clear();
793
794    EXPECT_FALSE(u.find(&_int));
795    EXPECT_FALSE(u.find(&_shared));
796    EXPECT_TRUE(u.find(&_weak));
797    EXPECT_EQ(_weak.promote(), _shared);
798
799    // we can remove a weak pointer multiple times
800    _weak.clear();
801    EXPECT_TRUE(u.find(&_weak));
802    EXPECT_EQ(_weak.promote(), _shared);
803    EXPECT_TRUE(u.clear());
804    EXPECT_FALSE(u.find(&_int));
805    EXPECT_FALSE(u.find(&_shared));
806    EXPECT_FALSE(u.find(&_weak));
807};
808
809TEST_F(ADataTest, AData_RelaxedAndroidSpTest) {
810    int destructions = 0;
811    int _int = 0;
812    sp<EventCounter> _shared;
813    wp<EventCounter> _weak;
814    sp<RefBase> _shared_base;
815    wp<RefBase> _weak_base;
816    const sp<EventCounter> _constShared(new EventCounter(&destructions));
817
818    AData<int, sp<RefBase>, wp<RefBase>>::Relaxed<uint16_t> u;
819
820    // test that data is empty
821    EXPECT_FALSE(u.used());
822    EXPECT_FALSE(u.find(&_int));
823    EXPECT_FALSE(u.find(&_shared));
824    EXPECT_FALSE(u.find(&_weak));
825    EXPECT_FALSE(u.find(&_shared_base));
826    EXPECT_FALSE(u.find(&_weak_base));
827
828    // test that integer can be stored and read
829    EXPECT_TRUE(u.set<int>(1));
830    EXPECT_TRUE(u.used());
831    EXPECT_TRUE(u.find(&_int));
832    EXPECT_EQ(1, _int);
833    EXPECT_FALSE(u.find(&_shared));
834    EXPECT_FALSE(u.find(&_weak));
835    EXPECT_FALSE(u.find(&_shared_base));
836    EXPECT_FALSE(u.find(&_weak_base));
837
838    // test that copiable & movable type (shared_ptr) is copied unless explicitly moved.
839    _shared = new EventCounter(&destructions, 234);
840    _weak = _shared; // used for tracking #234
841
842    EXPECT_TRUE(u.set(_shared));
843    EXPECT_TRUE((bool)_shared.get());
844    if (_shared.get()) {
845        EXPECT_EQ(234, _shared->magic());
846    }
847
848    _shared.clear();
849    EXPECT_EQ(NULL, _shared.get());
850    EXPECT_NE(nullptr, _weak.promote().get()); // u still holds object
851
852    EXPECT_FALSE(u.find(&_int));
853    EXPECT_TRUE(u.find(&_shared)); // now u and _shared both hold object
854    EXPECT_TRUE((bool)_shared.get());
855    if (_shared.get()) {
856        EXPECT_EQ(234, _shared->magic());
857    }
858    // verify the find did not move out object
859    _shared.clear();
860    EXPECT_EQ(NULL, _shared.get());
861    EXPECT_NE(nullptr, _weak.promote().get()); // u still holds object
862    EXPECT_TRUE(u.find(&_shared)); // now u and _shared both hold object
863    if (_shared.get()) {
864        EXPECT_EQ(234, _shared->magic());
865    }
866
867    // verify that we can set object multiple times
868    EXPECT_TRUE(u.set(_shared));
869
870    // explicitly move in sp
871    EXPECT_TRUE(u.set(std::move(_shared)));
872    EXPECT_FALSE((bool)_shared.get()); // android also clears sp<> on move...
873    EXPECT_TRUE(u.find(&_shared)); // still can get it back
874    EXPECT_TRUE((bool)_shared.get());
875    if (_shared.get()) {
876        EXPECT_EQ(234, _shared->magic());
877    }
878    EXPECT_FALSE(u.find(&_int));
879    EXPECT_FALSE(u.find(&_weak));
880    EXPECT_FALSE(u.find(&_weak_base));
881
882    EXPECT_TRUE(u.used());
883    EXPECT_TRUE(u.clear()); // now only _shared contains the object
884    EXPECT_FALSE(u.used());
885
886    // we still hold a copy
887    EXPECT_TRUE((bool)_shared.get());
888    EXPECT_FALSE(u.find(&_int));
889    EXPECT_FALSE(u.find(&_shared)); // _shared still contains the object
890
891    EXPECT_TRUE(u.set(_constShared));
892    EXPECT_TRUE(u.find(&_shared)); // now _shared contains _constShared
893    EXPECT_EQ(NULL, _weak.promote().get()); // original _shared is now lost
894
895    EXPECT_TRUE((bool)_shared.get());
896    if (_shared.get()) {
897        EXPECT_EQ(1234, _shared->magic());
898    }
899    EXPECT_TRUE(u.clear());
900
901    // test that shared pointer can be set and removed as base type (but removed as derived only
902    // if it was set as derived type)
903    EXPECT_TRUE(u.set(_constShared));
904    EXPECT_TRUE(u.find(&_shared_base));
905    EXPECT_TRUE((bool)_shared_base.get());
906    if (_shared_base.get()) {
907        EXPECT_EQ(1234, static_cast<EventCounter*>(_shared_base.get())->magic());
908    }
909    _shared.clear();
910    EXPECT_TRUE(u.clear());
911    EXPECT_TRUE((bool)_shared_base.get());
912    if (_shared_base.get()) {
913        EXPECT_EQ(1234, static_cast<EventCounter*>(_shared_base.get())->magic());
914    }
915
916    EXPECT_TRUE(u.set(_shared_base)); // now u contains it as base class
917    EXPECT_TRUE((bool)_shared_base.get());
918    EXPECT_FALSE(u.find(&_shared)); // cannot get it as derived type
919    EXPECT_FALSE((bool)_shared.get());
920    _shared_base.clear();
921    EXPECT_TRUE(u.find(&_shared_base)); // can still get it as base type
922    EXPECT_TRUE((bool)_shared_base.get());
923    if (_shared_base.get()) {
924        EXPECT_EQ(1234, static_cast<EventCounter*>(_shared_base.get())->magic());
925    }
926    _shared = static_cast<DerivedCounter*>(_shared_base.get());
927    EXPECT_FALSE(u.find(&_int));
928    EXPECT_FALSE(u.find(&_weak));
929    EXPECT_FALSE(u.find(&_weak_base));
930
931    // test that wp can be copied in
932    _weak = _shared;
933    EXPECT_TRUE(u.set(_weak));
934
935    _weak.clear();
936
937    EXPECT_FALSE(u.find(&_int));
938    EXPECT_FALSE(u.find(&_shared));
939    EXPECT_FALSE(u.find(&_shared_base));
940    EXPECT_TRUE(u.find(&_weak));
941    EXPECT_EQ(_weak.promote(), _shared);
942
943    // we can remove a weak pointer multiple times
944    _weak.clear();
945    EXPECT_TRUE(u.find(&_weak));
946    EXPECT_EQ(_weak.promote(), _shared);
947    EXPECT_TRUE(u.clear());
948    EXPECT_FALSE(u.find(&_int));
949    EXPECT_FALSE(u.find(&_shared));
950    EXPECT_FALSE(u.find(&_weak));
951    EXPECT_FALSE(u.find(&_shared_base));
952    EXPECT_FALSE(u.find(&_weak_base));
953
954    // test that weak pointer can be set and removed as base type (but removed as derived only
955    // if it was set as derived type)
956    _weak = _shared;
957    EXPECT_TRUE(u.set(_weak));
958    EXPECT_TRUE(u.find(&_weak_base));
959    EXPECT_TRUE(_weak_base.promote().get() == _shared.get());
960
961    _weak.clear();
962    _shared.clear();
963    EXPECT_TRUE(u.clear());
964    EXPECT_FALSE(u.find(&_int));
965    EXPECT_FALSE(u.find(&_shared));
966    EXPECT_FALSE(u.find(&_weak));
967    EXPECT_FALSE(u.find(&_shared_base));
968    EXPECT_FALSE(u.find(&_weak_base));
969
970    EXPECT_TRUE(u.set(_weak_base)); // now u_ also contains it as base class
971    EXPECT_FALSE(u.find(&_weak));   // cannot get it as derived type
972    EXPECT_FALSE(_weak.promote().get());
973    _weak_base.clear();
974    EXPECT_TRUE(u.find(&_weak_base)); // can still get it as base type
975    EXPECT_TRUE(_weak_base.promote().get());
976    if (_weak_base.promote().get()) {
977        EXPECT_EQ(1234, static_cast<EventCounter*>(_weak_base.promote().get())->magic());
978    }
979};
980
981} // namespace android
982