blob: 0f6b2494c237660363ff3b922d3baf1e143ae36c [file] [log] [blame]
John Reckb5bc4542015-04-23 15:51:55 -07001/*
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 Craik76ace112015-10-29 12:46:19 -070020#include <unit_tests/TestUtils.h>
21
John Reckb5bc4542015-04-23 15:51:55 -070022using namespace android;
23using namespace android::uirenderer;
24
25struct SimplePair {
26 int one = 1;
27 int two = 2;
28};
29
John Reckb5bc4542015-04-23 15:51:55 -070030TEST(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
45TEST(LinearAllocator, dtor) {
Chris Craik76ace112015-10-29 12:46:19 -070046 int destroyed[10] = { 0 };
John Reckb5bc4542015-04-23 15:51:55 -070047 {
48 LinearAllocator la;
49 for (int i = 0; i < 5; i++) {
Chris Craik76ace112015-10-29 12:46:19 -070050 la.alloc<TestUtils::SignalingDtor>()->setSignal(destroyed + i);
John Reckb5bc4542015-04-23 15:51:55 -070051 la.alloc<SimplePair>();
52 }
53 la.alloc(100);
54 for (int i = 0; i < 5; i++) {
Chris Craik76ace112015-10-29 12:46:19 -070055 auto sd = new (la) TestUtils::SignalingDtor(destroyed + 5 + i);
John Reckb5bc4542015-04-23 15:51:55 -070056 la.autoDestroy(sd);
57 new (la) SimplePair();
58 }
59 la.alloc(100);
60 for (int i = 0; i < 10; i++) {
Chris Craik76ace112015-10-29 12:46:19 -070061 EXPECT_EQ(0, destroyed[i]);
John Reckb5bc4542015-04-23 15:51:55 -070062 }
63 }
64 for (int i = 0; i < 10; i++) {
Chris Craik76ace112015-10-29 12:46:19 -070065 EXPECT_EQ(1, destroyed[i]);
John Reckb5bc4542015-04-23 15:51:55 -070066 }
67}
68
69TEST(LinearAllocator, rewind) {
Chris Craik76ace112015-10-29 12:46:19 -070070 int destroyed = 0;
John Reckb5bc4542015-04-23 15:51:55 -070071 {
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 Craik76ace112015-10-29 12:46:19 -070078 auto sigdtor = la.alloc<TestUtils::SignalingDtor>();
John Reckb5bc4542015-04-23 15:51:55 -070079 sigdtor->setSignal(&destroyed);
Chris Craik76ace112015-10-29 12:46:19 -070080 EXPECT_EQ(0, destroyed);
John Reckb5bc4542015-04-23 15:51:55 -070081 EXPECT_LE(emptySize, la.usedSize());
82 la.rewindIfLastAlloc(sigdtor);
Chris Craik76ace112015-10-29 12:46:19 -070083 EXPECT_EQ(1, destroyed);
John Reckb5bc4542015-04-23 15:51:55 -070084 EXPECT_EQ(emptySize, la.usedSize());
John Reckb5bc4542015-04-23 15:51:55 -070085 }
86 // Checking for a double-destroy case
Chris Craik76ace112015-10-29 12:46:19 -070087 EXPECT_EQ(1, destroyed);
John Reckb5bc4542015-04-23 15:51:55 -070088}
Chris Craik81a1d2a2015-10-15 17:13:00 -070089
90TEST(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}