1 /*
2 * Copyright (C) 2017 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 <cstring>
18
19 #include "gtest/gtest.h"
20
21 #include "chre/util/unique_ptr.h"
22
23 using chre::MakeUnique;
24 using chre::MakeUniqueZeroFill;
25 using chre::UniquePtr;
26
27 struct Value {
ValueValue28 Value(int value) : value(value) {
29 constructionCounter++;
30 }
31
~ValueValue32 ~Value() {
33 constructionCounter--;
34 }
35
operator =Value36 Value &operator=(Value &&other) {
37 value = other.value;
38 return *this;
39 }
40
41 int value;
42 static int constructionCounter;
43 };
44
45 int Value::constructionCounter = 0;
46
TEST(UniquePtr,Construct)47 TEST(UniquePtr, Construct) {
48 UniquePtr<Value> myInt = MakeUnique<Value>(0xcafe);
49 ASSERT_FALSE(myInt.isNull());
50 EXPECT_EQ(myInt.get()->value, 0xcafe);
51 EXPECT_EQ(myInt->value, 0xcafe);
52 EXPECT_EQ((*myInt).value, 0xcafe);
53 EXPECT_EQ(myInt[0].value, 0xcafe);
54 }
55
56 struct BigArray {
57 int x[2048];
58 };
59
TEST(UniquePtr,MakeUniqueZeroFill)60 TEST(UniquePtr, MakeUniqueZeroFill) {
61 BigArray baseline = {};
62 auto myArray = MakeUniqueZeroFill<BigArray>();
63 ASSERT_FALSE(myArray.isNull());
64 // Note that this doesn't actually test things properly, because we don't
65 // guarantee that malloc is not already giving us zeroed out memory. To
66 // properly do it, we could inject the allocator, but this function is simple
67 // enough that it's not really worth the effort.
68 EXPECT_EQ(std::memcmp(&baseline, myArray.get(), sizeof(baseline)), 0);
69 }
70
TEST(UniquePtr,MoveConstruct)71 TEST(UniquePtr, MoveConstruct) {
72 UniquePtr<Value> myInt = MakeUnique<Value>(0xcafe);
73 ASSERT_FALSE(myInt.isNull());
74 Value *value = myInt.get();
75
76 UniquePtr<Value> moved(std::move(myInt));
77 EXPECT_EQ(moved.get(), value);
78 EXPECT_EQ(myInt.get(), nullptr);
79 }
80
TEST(UniquePtr,Move)81 TEST(UniquePtr, Move) {
82 Value::constructionCounter = 0;
83
84 {
85 UniquePtr<Value> myInt = MakeUnique<Value>(0xcafe);
86 ASSERT_FALSE(myInt.isNull());
87 EXPECT_EQ(Value::constructionCounter, 1);
88
89 UniquePtr<Value> myMovedInt = MakeUnique<Value>(0);
90 ASSERT_FALSE(myMovedInt.isNull());
91 EXPECT_EQ(Value::constructionCounter, 2);
92 myMovedInt = std::move(myInt);
93 ASSERT_FALSE(myMovedInt.isNull());
94 ASSERT_TRUE(myInt.isNull());
95 EXPECT_EQ(myMovedInt.get()->value, 0xcafe);
96 }
97
98 EXPECT_EQ(Value::constructionCounter, 0);
99 }
100
TEST(UniquePtr,Release)101 TEST(UniquePtr, Release) {
102 Value::constructionCounter = 0;
103
104 Value *value1, *value2;
105 {
106 UniquePtr<Value> myInt = MakeUnique<Value>(0xcafe);
107 ASSERT_FALSE(myInt.isNull());
108 EXPECT_EQ(Value::constructionCounter, 1);
109 value1 = myInt.get();
110 EXPECT_NE(value1, nullptr);
111 value2 = myInt.release();
112 EXPECT_EQ(value1, value2);
113 EXPECT_EQ(myInt.get(), nullptr);
114 EXPECT_TRUE(myInt.isNull());
115 }
116
117 EXPECT_EQ(Value::constructionCounter, 1);
118 EXPECT_EQ(value2->value, 0xcafe);
119 value2->~Value();
120 chre::memoryFree(value2);
121 }
122
TEST(UniquePtr,Reset)123 TEST(UniquePtr, Reset) {
124 Value::constructionCounter = 0;
125
126 {
127 UniquePtr<Value> myInt = MakeUnique<Value>(0xcafe);
128 EXPECT_EQ(myInt.get()->value, 0xcafe);
129 EXPECT_EQ(Value::constructionCounter, 1);
130 myInt.reset(nullptr);
131 EXPECT_EQ(myInt.get(), nullptr);
132 EXPECT_EQ(Value::constructionCounter, 0);
133
134 myInt = MakeUnique<Value>(0xcafe);
135 UniquePtr<Value> myInt2 = MakeUnique<Value>(0xface);
136 EXPECT_EQ(Value::constructionCounter, 2);
137 myInt.reset(myInt2.release());
138 EXPECT_EQ(Value::constructionCounter, 1);
139 EXPECT_EQ(myInt.get()->value, 0xface);
140 EXPECT_EQ(myInt2.get(), nullptr);
141
142 myInt.reset();
143 EXPECT_EQ(myInt.get(), nullptr);
144 }
145
146 EXPECT_EQ(Value::constructionCounter, 0);
147 }
148
TEST(UniquePtr,EqualityOperator)149 TEST(UniquePtr, EqualityOperator) {
150 Value::constructionCounter = 0;
151
152 {
153 UniquePtr<Value> myInt = MakeUnique<Value>(0xcafe);
154 EXPECT_TRUE(myInt != nullptr);
155
156 myInt.reset();
157 EXPECT_TRUE(myInt == nullptr);
158 }
159
160 EXPECT_EQ(Value::constructionCounter, 0);
161 }
162
TEST(UniquePtr,OverAlignedTest)163 TEST(UniquePtr, OverAlignedTest) {
164 // Explicitly overaligned structure larger than std::max_align_t.
165 struct alignas(32) OverAlignedStruct {
166 uint32_t x[32];
167 };
168 static_assert(alignof(OverAlignedStruct) > alignof(std::max_align_t));
169
170 UniquePtr<OverAlignedStruct> ptr = MakeUnique<OverAlignedStruct>();
171 ASSERT_EQ(reinterpret_cast<uintptr_t>(ptr.get()) % alignof(OverAlignedStruct),
172 0);
173 }