diff options
author | John Reck | 2015-11-02 18:14:15 -0600 |
---|---|---|
committer | android-build-merger | 2015-11-02 18:14:15 -0600 |
commit | dc7694eac5288d1808e0c9602ad1c1a2e1780dbf (patch) | |
tree | 38c17d093cac88761e027f67f61a48437b328256 /libutils/tests | |
parent | dd321c169bb1df0f98f0b5bffbbafa938b4ebc83 (diff) | |
parent | a573d6b18686266667c8efd9d7291ecd28efa67c (diff) | |
download | platform-system-core-dc7694eac5288d1808e0c9602ad1c1a2e1780dbf.tar.gz platform-system-core-dc7694eac5288d1808e0c9602ad1c1a2e1780dbf.tar.xz platform-system-core-dc7694eac5288d1808e0c9602ad1c1a2e1780dbf.zip |
Merge "Teach sp<> how to std::move" am: 518d043c86 am: bb12b91ae1
am: a573d6b186
* commit 'a573d6b18686266667c8efd9d7291ecd28efa67c':
Teach sp<> how to std::move
Diffstat (limited to 'libutils/tests')
-rw-r--r-- | libutils/tests/Android.mk | 1 | ||||
-rw-r--r-- | libutils/tests/StrongPointer_test.cpp | 58 |
2 files changed, 59 insertions, 0 deletions
diff --git a/libutils/tests/Android.mk b/libutils/tests/Android.mk index 47415ab05..8f07f1ad1 100644 --- a/libutils/tests/Android.mk +++ b/libutils/tests/Android.mk | |||
@@ -27,6 +27,7 @@ LOCAL_SRC_FILES := \ | |||
27 | Looper_test.cpp \ | 27 | Looper_test.cpp \ |
28 | LruCache_test.cpp \ | 28 | LruCache_test.cpp \ |
29 | String8_test.cpp \ | 29 | String8_test.cpp \ |
30 | StrongPointer_test.cpp \ | ||
30 | Unicode_test.cpp \ | 31 | Unicode_test.cpp \ |
31 | Vector_test.cpp \ | 32 | Vector_test.cpp \ |
32 | 33 | ||
diff --git a/libutils/tests/StrongPointer_test.cpp b/libutils/tests/StrongPointer_test.cpp new file mode 100644 index 000000000..f46d6d140 --- /dev/null +++ b/libutils/tests/StrongPointer_test.cpp | |||
@@ -0,0 +1,58 @@ | |||
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 | |||
24 | class Foo : public LightRefBase<Foo> { | ||
25 | public: | ||
26 | Foo(bool* deleted_check) : mDeleted(deleted_check) { | ||
27 | *mDeleted = false; | ||
28 | } | ||
29 | |||
30 | ~Foo() { | ||
31 | *mDeleted = true; | ||
32 | } | ||
33 | private: | ||
34 | bool* mDeleted; | ||
35 | }; | ||
36 | |||
37 | TEST(StrongPointer, move) { | ||
38 | bool isDeleted; | ||
39 | Foo* foo = new Foo(&isDeleted); | ||
40 | ASSERT_EQ(0, foo->getStrongCount()); | ||
41 | ASSERT_FALSE(isDeleted) << "Already deleted...?"; | ||
42 | sp<Foo> sp1(foo); | ||
43 | ASSERT_EQ(1, foo->getStrongCount()); | ||
44 | { | ||
45 | sp<Foo> sp2 = std::move(sp1); | ||
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 | ||
55 | sp<Foo> sp2 = std::move(sp1); | ||
56 | } | ||
57 | ASSERT_TRUE(isDeleted) << "foo was leaked!"; | ||
58 | } | ||