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