Home | History | Annotate | Download | only in unit
      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 
     20 #include <tests/common/TestUtils.h>
     21 
     22 using namespace android;
     23 using namespace android::uirenderer;
     24 
     25 struct SimplePair {
     26     int one = 1;
     27     int two = 2;
     28 };
     29 
     30 TEST(LinearAllocator, create) {
     31     LinearAllocator la;
     32     EXPECT_EQ(0u, la.usedSize());
     33     la.alloc<char>(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.create<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) {
     46     int destroyed[10] = {0};
     47     {
     48         LinearAllocator la;
     49         for (int i = 0; i < 5; i++) {
     50             la.create<TestUtils::SignalingDtor>()->setSignal(destroyed + i);
     51             la.create<SimplePair>();
     52         }
     53         la.alloc<char>(100);
     54         for (int i = 0; i < 5; i++) {
     55             la.create<TestUtils::SignalingDtor>(destroyed + 5 + i);
     56             la.create_trivial<SimplePair>();
     57         }
     58         la.alloc<char>(100);
     59         for (int i = 0; i < 10; i++) {
     60             EXPECT_EQ(0, destroyed[i]);
     61         }
     62     }
     63     for (int i = 0; i < 10; i++) {
     64         EXPECT_EQ(1, destroyed[i]);
     65     }
     66 }
     67 
     68 TEST(LinearAllocator, rewind) {
     69     int destroyed = 0;
     70     {
     71         LinearAllocator la;
     72         auto addr = la.alloc<char>(100);
     73         EXPECT_LE(100u, la.usedSize());
     74         la.rewindIfLastAlloc(addr, 100);
     75         EXPECT_GT(16u, la.usedSize());
     76         size_t emptySize = la.usedSize();
     77         auto sigdtor = la.create<TestUtils::SignalingDtor>();
     78         sigdtor->setSignal(&destroyed);
     79         EXPECT_EQ(0, destroyed);
     80         EXPECT_LE(emptySize, la.usedSize());
     81         la.rewindIfLastAlloc(sigdtor);
     82         EXPECT_EQ(1, destroyed);
     83         EXPECT_EQ(emptySize, la.usedSize());
     84     }
     85     // Checking for a double-destroy case
     86     EXPECT_EQ(1, destroyed);
     87 }
     88 
     89 TEST(LinearStdAllocator, simpleAllocate) {
     90     LinearAllocator la;
     91     LinearStdAllocator<void*> stdAllocator(la);
     92 
     93     std::vector<char, LinearStdAllocator<char> > v(stdAllocator);
     94     v.push_back(0);
     95     char* initialLocation = &v[0];
     96     v.push_back(10);
     97     v.push_back(20);
     98     v.push_back(30);
     99 
    100     // expect to have allocated (since no space reserved), so [0] will have moved to
    101     // slightly further down in the same LinearAllocator page
    102     EXPECT_LT(initialLocation, &v[0]);
    103     EXPECT_GT(initialLocation + 20, &v[0]);
    104 
    105     // expect to have allocated again inserting 4 more entries
    106     char* lastLocation = &v[0];
    107     v.push_back(40);
    108     v.push_back(50);
    109     v.push_back(60);
    110     v.push_back(70);
    111 
    112     EXPECT_LT(lastLocation, &v[0]);
    113     EXPECT_GT(lastLocation + 20, &v[0]);
    114 }
    115 
    116 TEST(LsaVector, dtorCheck) {
    117     LinearAllocator allocator;
    118     LinearStdAllocator<void*> stdAllocator(allocator);
    119 
    120     for (int size : {1, 2, 3, 500}) {
    121         int destroyed = 0;
    122         {
    123             LsaVector<std::unique_ptr<TestUtils::SignalingDtor> > vector(stdAllocator);
    124             for (int i = 0; i < size; i++) {
    125                 vector.emplace_back(new TestUtils::SignalingDtor(&destroyed));
    126             }
    127             EXPECT_EQ(0, destroyed);
    128             EXPECT_EQ(size, (int)vector.size());
    129         }
    130         EXPECT_EQ(size, destroyed);
    131     }
    132 }
    133