| 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 |  | 
| Colin Cross | fe06c63 | 2017-02-23 17:48:08 -0800 | [diff] [blame] | 24 | class SPFoo : public LightRefBase<SPFoo> { | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 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 |     } | 
 | 33 | private: | 
 | 34 |     bool* mDeleted; | 
 | 35 | }; | 
 | 36 |  | 
 | 37 | TEST(StrongPointer, move) { | 
 | 38 |     bool isDeleted; | 
| Colin Cross | fe06c63 | 2017-02-23 17:48:08 -0800 | [diff] [blame] | 39 |     SPFoo* foo = new SPFoo(&isDeleted); | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 40 |     ASSERT_EQ(0, foo->getStrongCount()); | 
 | 41 |     ASSERT_FALSE(isDeleted) << "Already deleted...?"; | 
| Colin Cross | fe06c63 | 2017-02-23 17:48:08 -0800 | [diff] [blame] | 42 |     sp<SPFoo> sp1(foo); | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 43 |     ASSERT_EQ(1, foo->getStrongCount()); | 
 | 44 |     { | 
| Colin Cross | fe06c63 | 2017-02-23 17:48:08 -0800 | [diff] [blame] | 45 |         sp<SPFoo> sp2 = std::move(sp1); | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 46 |         ASSERT_EQ(1, foo->getStrongCount()) << "std::move failed, incremented refcnt"; | 
 | 47 |         ASSERT_EQ(nullptr, sp1.get()) << "std::move failed, sp1 is still valid"; | 
 | 48 |         // The strong count isn't increasing, let's double check the old object | 
 | 49 |         // is properly reset and doesn't early delete | 
 | 50 |         sp1 = std::move(sp2); | 
 | 51 |     } | 
 | 52 |     ASSERT_FALSE(isDeleted) << "deleted too early! still has a reference!"; | 
 | 53 |     { | 
 | 54 |         // Now let's double check it deletes on time | 
| Colin Cross | fe06c63 | 2017-02-23 17:48:08 -0800 | [diff] [blame] | 55 |         sp<SPFoo> sp2 = std::move(sp1); | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 56 |     } | 
 | 57 |     ASSERT_TRUE(isDeleted) << "foo was leaked!"; | 
 | 58 | } | 
| Steven Moreland | 306f8b5 | 2019-12-19 16:32:00 -0800 | [diff] [blame] | 59 |  | 
 | 60 | TEST(StrongPointer, NullptrComparison) { | 
 | 61 |     sp<SPFoo> foo; | 
 | 62 |     ASSERT_EQ(foo, nullptr); | 
 | 63 |     ASSERT_EQ(nullptr, foo); | 
 | 64 | } | 
 | 65 |  | 
 | 66 | TEST(StrongPointer, PointerComparison) { | 
 | 67 |     bool isDeleted; | 
 | 68 |     sp<SPFoo> foo = new SPFoo(&isDeleted); | 
 | 69 |     ASSERT_EQ(foo.get(), foo); | 
 | 70 |     ASSERT_EQ(foo, foo.get()); | 
 | 71 |     ASSERT_NE(nullptr, foo); | 
 | 72 |     ASSERT_NE(foo, nullptr); | 
 | 73 | } |