John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <utils/StrongPointer.h> |
| 20 | #include <utils/RefBase.h> |
| 21 | |
| 22 | using namespace android; |
| 23 | |
Steven Moreland | da75cef | 2021-03-31 16:05:04 +0000 | [diff] [blame] | 24 | class SPFoo : virtual public RefBase { |
| 25 | public: |
Colin Cross | fe06c63 | 2017-02-23 17:48:08 -0800 | [diff] [blame] | 26 | explicit SPFoo(bool* deleted_check) : mDeleted(deleted_check) { |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 27 | *mDeleted = false; |
| 28 | } |
| 29 | |
Colin Cross | fe06c63 | 2017-02-23 17:48:08 -0800 | [diff] [blame] | 30 | ~SPFoo() { |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 31 | *mDeleted = true; |
| 32 | } |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 33 | |
| 34 | private: |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 35 | bool* mDeleted; |
| 36 | }; |
| 37 | |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 38 | class SPLightFoo : virtual public VirtualLightRefBase { |
| 39 | public: |
| 40 | explicit SPLightFoo(bool* deleted_check) : mDeleted(deleted_check) { *mDeleted = false; } |
| 41 | |
| 42 | ~SPLightFoo() { *mDeleted = true; } |
| 43 | |
| 44 | private: |
| 45 | bool* mDeleted; |
| 46 | }; |
| 47 | |
| 48 | template <typename T> |
| 49 | class StrongPointer : public ::testing::Test {}; |
| 50 | |
| 51 | using RefBaseTypes = ::testing::Types<SPFoo, SPLightFoo>; |
| 52 | TYPED_TEST_CASE(StrongPointer, RefBaseTypes); |
| 53 | |
| 54 | TYPED_TEST(StrongPointer, move) { |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 55 | bool isDeleted; |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 56 | sp<TypeParam> sp1 = sp<TypeParam>::make(&isDeleted); |
| 57 | TypeParam* foo = sp1.get(); |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 58 | ASSERT_EQ(1, foo->getStrongCount()); |
| 59 | { |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 60 | sp<TypeParam> sp2 = std::move(sp1); |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 61 | ASSERT_EQ(1, foo->getStrongCount()) << "std::move failed, incremented refcnt"; |
| 62 | ASSERT_EQ(nullptr, sp1.get()) << "std::move failed, sp1 is still valid"; |
| 63 | // The strong count isn't increasing, let's double check the old object |
| 64 | // is properly reset and doesn't early delete |
| 65 | sp1 = std::move(sp2); |
| 66 | } |
| 67 | ASSERT_FALSE(isDeleted) << "deleted too early! still has a reference!"; |
| 68 | { |
| 69 | // Now let's double check it deletes on time |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 70 | sp<TypeParam> sp2 = std::move(sp1); |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 71 | } |
| 72 | ASSERT_TRUE(isDeleted) << "foo was leaked!"; |
| 73 | } |
Steven Moreland | 306f8b5 | 2019-12-19 16:32:00 -0800 | [diff] [blame] | 74 | |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 75 | TYPED_TEST(StrongPointer, NullptrComparison) { |
| 76 | sp<TypeParam> foo; |
Steven Moreland | 306f8b5 | 2019-12-19 16:32:00 -0800 | [diff] [blame] | 77 | ASSERT_EQ(foo, nullptr); |
| 78 | ASSERT_EQ(nullptr, foo); |
| 79 | } |
| 80 | |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 81 | TYPED_TEST(StrongPointer, PointerComparison) { |
Steven Moreland | 306f8b5 | 2019-12-19 16:32:00 -0800 | [diff] [blame] | 82 | bool isDeleted; |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 83 | sp<TypeParam> foo = sp<TypeParam>::make(&isDeleted); |
Steven Moreland | 306f8b5 | 2019-12-19 16:32:00 -0800 | [diff] [blame] | 84 | ASSERT_EQ(foo.get(), foo); |
| 85 | ASSERT_EQ(foo, foo.get()); |
| 86 | ASSERT_NE(nullptr, foo); |
| 87 | ASSERT_NE(foo, nullptr); |
| 88 | } |
Steven Moreland | da75cef | 2021-03-31 16:05:04 +0000 | [diff] [blame] | 89 | |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 90 | TYPED_TEST(StrongPointer, Deleted) { |
Steven Moreland | da75cef | 2021-03-31 16:05:04 +0000 | [diff] [blame] | 91 | bool isDeleted; |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 92 | sp<TypeParam> foo = sp<TypeParam>::make(&isDeleted); |
Steven Moreland | da75cef | 2021-03-31 16:05:04 +0000 | [diff] [blame] | 93 | |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 94 | auto foo2 = sp<TypeParam>::fromExisting(foo.get()); |
Steven Moreland | da75cef | 2021-03-31 16:05:04 +0000 | [diff] [blame] | 95 | |
Steven Moreland | c2dc7cd | 2021-05-04 21:27:56 +0000 | [diff] [blame^] | 96 | EXPECT_FALSE(isDeleted); |
| 97 | foo = nullptr; |
| 98 | EXPECT_FALSE(isDeleted); |
| 99 | foo2 = nullptr; |
| 100 | EXPECT_TRUE(isDeleted); |
| 101 | } |
| 102 | |
| 103 | TYPED_TEST(StrongPointer, AssertStrongRefExists) { |
| 104 | bool isDeleted; |
| 105 | TypeParam* foo = new TypeParam(&isDeleted); |
| 106 | EXPECT_DEATH(sp<TypeParam>::fromExisting(foo), ""); |
Steven Moreland | da75cef | 2021-03-31 16:05:04 +0000 | [diff] [blame] | 107 | delete foo; |
| 108 | } |