1 /*
2 * Copyright (C) 2021 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 "chre/util/system/shared_ptr.h"
20
21 namespace {
22
23 class TestBase : public chre::RefBase<TestBase> {
24 public:
TestBase()25 TestBase() {}
TestBase(int value)26 TestBase(int value) : value(value) {}
27
~TestBase()28 ~TestBase() {
29 destructorCount++;
30 }
31
32 int value = 0;
33 static int destructorCount;
34 };
35
36 int TestBase::destructorCount = 0;
37
38 struct BigArray : public chre::RefBase<BigArray> {
39 int x[2048];
40 };
41
42 class SharedPtrTest : public testing::Test {
43 public:
SetUp()44 void SetUp() override {
45 TestBase::destructorCount = 0;
46 }
47 };
48
49 } // namespace
50
TEST_F(SharedPtrTest,IsNull)51 TEST_F(SharedPtrTest, IsNull) {
52 chre::SharedPtr<TestBase> ptr;
53
54 EXPECT_TRUE(ptr.isNull());
55 }
56
TEST_F(SharedPtrTest,IsNotNull)57 TEST_F(SharedPtrTest, IsNotNull) {
58 chre::SharedPtr<TestBase> ptr = chre::MakeShared<TestBase>();
59
60 EXPECT_FALSE(ptr.isNull());
61 }
62
TEST_F(SharedPtrTest,MoveConstructor)63 TEST_F(SharedPtrTest, MoveConstructor) {
64 chre::SharedPtr<TestBase> ptr = chre::MakeShared<TestBase>();
65
66 chre::SharedPtr<TestBase> movedPtr(std::move(ptr));
67
68 EXPECT_TRUE(ptr.isNull());
69 EXPECT_FALSE(movedPtr.isNull());
70 }
71
TEST_F(SharedPtrTest,CopyConstructor)72 TEST_F(SharedPtrTest, CopyConstructor) {
73 chre::SharedPtr<TestBase> ptr = chre::MakeShared<TestBase>();
74
75 chre::SharedPtr<TestBase> copiedPtr(ptr);
76
77 EXPECT_FALSE(ptr.isNull());
78 EXPECT_FALSE(copiedPtr.isNull());
79 }
80
TEST_F(SharedPtrTest,MoveAssignment)81 TEST_F(SharedPtrTest, MoveAssignment) {
82 chre::SharedPtr<TestBase> ptr = chre::MakeShared<TestBase>();
83
84 chre::SharedPtr<TestBase> movedPtr = std::move(ptr);
85
86 EXPECT_TRUE(ptr.isNull());
87 EXPECT_FALSE(movedPtr.isNull());
88 }
89
TEST_F(SharedPtrTest,CopiedAssignment)90 TEST_F(SharedPtrTest, CopiedAssignment) {
91 chre::SharedPtr<TestBase> ptr = chre::MakeShared<TestBase>();
92
93 chre::SharedPtr<TestBase> copiedPtr = ptr;
94
95 EXPECT_FALSE(ptr.isNull());
96 EXPECT_FALSE(copiedPtr.isNull());
97 }
98
TEST_F(SharedPtrTest,Get)99 TEST_F(SharedPtrTest, Get) {
100 int specialVal = 0xdeadbeef;
101 chre::SharedPtr<TestBase> ptr = chre::MakeShared<TestBase>(specialVal);
102
103 EXPECT_EQ(specialVal, ptr.get()->value);
104 }
105
TEST_F(SharedPtrTest,Reset)106 TEST_F(SharedPtrTest, Reset) {
107 chre::SharedPtr<TestBase> ptr = chre::MakeShared<TestBase>();
108 chre::SharedPtr<TestBase> ptr2 = chre::MakeShared<TestBase>();
109
110 EXPECT_NE(ptr, ptr2);
111
112 ptr2.reset(ptr.get());
113 EXPECT_EQ(ptr, ptr2);
114
115 ptr.reset();
116 EXPECT_TRUE(ptr.isNull());
117 }
118
TEST_F(SharedPtrTest,MemoryReleased)119 TEST_F(SharedPtrTest, MemoryReleased) {
120 chre::SharedPtr<TestBase> ptr = chre::MakeShared<TestBase>();
121 chre::SharedPtr<TestBase> copiedPtr = ptr;
122
123 ptr.~SharedPtr();
124 EXPECT_EQ(0, TestBase::destructorCount);
125
126 copiedPtr.~SharedPtr();
127 EXPECT_EQ(1, TestBase::destructorCount);
128 }
129
TEST_F(SharedPtrTest,MakeSharedZeroFill)130 TEST_F(SharedPtrTest, MakeSharedZeroFill) {
131 BigArray baseline = {};
132 auto myArray = chre::MakeSharedZeroFill<BigArray>();
133 ASSERT_FALSE(myArray.isNull());
134 // Note that this doesn't actually test things properly, because we don't
135 // guarantee that malloc is not already giving us zeroed out memory. To
136 // properly do it, we could inject the allocator, but this function is simple
137 // enough that it's not really worth the effort.
138 EXPECT_EQ(std::memcmp(baseline.x, myArray.get()->x, sizeof(baseline.x)), 0);
139 }
140