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 #include <ui/FatVector.h>
19
20 #include <tests/common/TestUtils.h>
21
22 using namespace android;
23 using namespace android::uirenderer;
24
25 template <class VectorType>
allocationIsInternal(VectorType & v)26 static bool allocationIsInternal(VectorType& v) {
27 // allocation array (from &v[0] to &v[0] + v.capacity) is
28 // located within the vector object itself
29 return (char*)(&v) <= (char*)(&v[0]) && (char*)(&v + 1) >= (char*)(&v[0] + v.capacity());
30 }
31
TEST(FatVector,baseline)32 TEST(FatVector, baseline) {
33 // Verify allocation behavior FatVector contrasts against - allocations are always external
34 std::vector<int> v;
35 for (int i = 0; i < 50; i++) {
36 v.push_back(i);
37 EXPECT_FALSE(allocationIsInternal(v));
38 }
39 }
40
TEST(FatVector,simpleAllocate)41 TEST(FatVector, simpleAllocate) {
42 FatVector<int, 4> v;
43 EXPECT_EQ(4u, v.capacity());
44
45 // can insert 4 items into internal buffer
46 for (int i = 0; i < 4; i++) {
47 v.push_back(i);
48 EXPECT_TRUE(allocationIsInternal(v));
49 }
50
51 // then will fall back to external allocation
52 for (int i = 5; i < 50; i++) {
53 v.push_back(i);
54 EXPECT_FALSE(allocationIsInternal(v));
55 }
56 }
57
TEST(FatVector,preSizeConstructor)58 TEST(FatVector, preSizeConstructor) {
59 {
60 FatVector<int, 4> v(32);
61 EXPECT_EQ(32u, v.capacity());
62 EXPECT_EQ(32u, v.size());
63 EXPECT_FALSE(allocationIsInternal(v));
64 }
65 {
66 FatVector<int, 4> v(4);
67 EXPECT_EQ(4u, v.capacity());
68 EXPECT_EQ(4u, v.size());
69 EXPECT_TRUE(allocationIsInternal(v));
70 }
71 {
72 FatVector<int, 4> v(2);
73 EXPECT_EQ(4u, v.capacity());
74 EXPECT_EQ(2u, v.size());
75 EXPECT_TRUE(allocationIsInternal(v));
76 }
77 }
78
TEST(FatVector,shrink)79 TEST(FatVector, shrink) {
80 FatVector<int, 10> v;
81 EXPECT_TRUE(allocationIsInternal(v));
82
83 // push into external alloc
84 v.resize(11);
85 EXPECT_FALSE(allocationIsInternal(v));
86
87 // shrinking back to internal alloc succeeds
88 // note that shrinking further will succeed, but is a waste
89 v.resize(10);
90 v.shrink_to_fit();
91 EXPECT_TRUE(allocationIsInternal(v));
92 }
93
TEST(FatVector,destructorInternal)94 TEST(FatVector, destructorInternal) {
95 int count = 0;
96 {
97 // push 1 into external allocation, verify destruction happens once
98 FatVector<TestUtils::SignalingDtor, 0> v;
99 v.emplace_back(&count);
100 EXPECT_FALSE(allocationIsInternal(v));
101 EXPECT_EQ(0, count) << "Destruction shouldn't have happened yet";
102 }
103 EXPECT_EQ(1, count) << "Destruction should happen exactly once";
104 }
105
TEST(FatVector,destructorExternal)106 TEST(FatVector, destructorExternal) {
107 int count = 0;
108 {
109 // push 10 into internal allocation, verify 10 destructors called
110 FatVector<TestUtils::SignalingDtor, 10> v;
111 for (int i = 0; i < 10; i++) {
112 v.emplace_back(&count);
113 EXPECT_TRUE(allocationIsInternal(v));
114 }
115 EXPECT_EQ(0, count) << "Destruction shouldn't have happened yet";
116 }
117 EXPECT_EQ(10, count) << "Destruction should happen exactly once";
118 }
119