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