| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 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/LinearAllocator.h> |
| 19 | |
| Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 20 | #include <unit_tests/TestUtils.h> |
| 21 | |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 22 | using namespace android; |
| 23 | using namespace android::uirenderer; |
| 24 | |
| 25 | struct SimplePair { |
| 26 | int one = 1; |
| 27 | int two = 2; |
| 28 | }; |
| 29 | |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 30 | TEST(LinearAllocator, alloc) { |
| 31 | LinearAllocator la; |
| 32 | EXPECT_EQ(0u, la.usedSize()); |
| 33 | la.alloc(64); |
| 34 | // There's some internal tracking as well as padding |
| 35 | // so the usedSize isn't strictly defined |
| 36 | EXPECT_LE(64u, la.usedSize()); |
| 37 | EXPECT_GT(80u, la.usedSize()); |
| 38 | auto pair = la.alloc<SimplePair>(); |
| 39 | EXPECT_LE(64u + sizeof(SimplePair), la.usedSize()); |
| 40 | EXPECT_GT(80u + sizeof(SimplePair), la.usedSize()); |
| 41 | EXPECT_EQ(1, pair->one); |
| 42 | EXPECT_EQ(2, pair->two); |
| 43 | } |
| 44 | |
| 45 | TEST(LinearAllocator, dtor) { |
| Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 46 | int destroyed[10] = { 0 }; |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 47 | { |
| 48 | LinearAllocator la; |
| 49 | for (int i = 0; i < 5; i++) { |
| Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 50 | la.alloc<TestUtils::SignalingDtor>()->setSignal(destroyed + i); |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 51 | la.alloc<SimplePair>(); |
| 52 | } |
| 53 | la.alloc(100); |
| 54 | for (int i = 0; i < 5; i++) { |
| Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 55 | auto sd = new (la) TestUtils::SignalingDtor(destroyed + 5 + i); |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 56 | la.autoDestroy(sd); |
| 57 | new (la) SimplePair(); |
| 58 | } |
| 59 | la.alloc(100); |
| 60 | for (int i = 0; i < 10; i++) { |
| Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 61 | EXPECT_EQ(0, destroyed[i]); |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | for (int i = 0; i < 10; i++) { |
| Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 65 | EXPECT_EQ(1, destroyed[i]); |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | TEST(LinearAllocator, rewind) { |
| Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 70 | int destroyed = 0; |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 71 | { |
| 72 | LinearAllocator la; |
| 73 | auto addr = la.alloc(100); |
| 74 | EXPECT_LE(100u, la.usedSize()); |
| 75 | la.rewindIfLastAlloc(addr, 100); |
| 76 | EXPECT_GT(16u, la.usedSize()); |
| 77 | size_t emptySize = la.usedSize(); |
| Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 78 | auto sigdtor = la.alloc<TestUtils::SignalingDtor>(); |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 79 | sigdtor->setSignal(&destroyed); |
| Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 80 | EXPECT_EQ(0, destroyed); |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 81 | EXPECT_LE(emptySize, la.usedSize()); |
| 82 | la.rewindIfLastAlloc(sigdtor); |
| Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 83 | EXPECT_EQ(1, destroyed); |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 84 | EXPECT_EQ(emptySize, la.usedSize()); |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 85 | } |
| 86 | // Checking for a double-destroy case |
| Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 87 | EXPECT_EQ(1, destroyed); |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 88 | } |
| Chris Craik | 81a1d2a | 2015-10-15 17:13:00 -0700 | [diff] [blame] | 89 | |
| 90 | TEST(LinearStdAllocator, simpleAllocate) { |
| 91 | LinearAllocator la; |
| 92 | LinearStdAllocator<void*> stdAllocator(la); |
| 93 | |
| 94 | std::vector<char, LinearStdAllocator<char> > v(stdAllocator); |
| 95 | v.push_back(0); |
| 96 | char* initialLocation = &v[0]; |
| 97 | v.push_back(10); |
| 98 | v.push_back(20); |
| 99 | v.push_back(30); |
| 100 | |
| 101 | // expect to have allocated (since no space reserved), so [0] will have moved to |
| 102 | // slightly further down in the same LinearAllocator page |
| 103 | EXPECT_LT(initialLocation, &v[0]); |
| 104 | EXPECT_GT(initialLocation + 20, &v[0]); |
| 105 | |
| 106 | // expect to have allocated again inserting 4 more entries |
| 107 | char* lastLocation = &v[0]; |
| 108 | v.push_back(40); |
| 109 | v.push_back(50); |
| 110 | v.push_back(60); |
| 111 | v.push_back(70); |
| 112 | |
| 113 | EXPECT_LT(lastLocation, &v[0]); |
| 114 | EXPECT_GT(lastLocation + 20, &v[0]); |
| 115 | |
| 116 | } |