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 "util/BigBuffer.h"
18 
19 #include "test/Test.h"
20 
21 using ::testing::NotNull;
22 
23 namespace aapt {
24 
TEST(BigBufferTest,AllocateSingleBlock)25 TEST(BigBufferTest, AllocateSingleBlock) {
26   BigBuffer buffer(4);
27 
28   EXPECT_THAT(buffer.NextBlock<char>(2), NotNull());
29   EXPECT_EQ(2u, buffer.size());
30 }
31 
TEST(BigBufferTest,ReturnSameBlockIfNextAllocationFits)32 TEST(BigBufferTest, ReturnSameBlockIfNextAllocationFits) {
33   BigBuffer buffer(16);
34 
35   char* b1 = buffer.NextBlock<char>(8);
36   EXPECT_THAT(b1, NotNull());
37 
38   char* b2 = buffer.NextBlock<char>(4);
39   EXPECT_THAT(b2, NotNull());
40 
41   EXPECT_EQ(b1 + 8, b2);
42 }
43 
TEST(BigBufferTest,AllocateExactSizeBlockIfLargerThanBlockSize)44 TEST(BigBufferTest, AllocateExactSizeBlockIfLargerThanBlockSize) {
45   BigBuffer buffer(16);
46 
47   EXPECT_THAT(buffer.NextBlock<char>(32), NotNull());
48   EXPECT_EQ(32u, buffer.size());
49 }
50 
TEST(BigBufferTest,AppendAndMoveBlock)51 TEST(BigBufferTest, AppendAndMoveBlock) {
52   BigBuffer buffer(16);
53 
54   uint32_t* b1 = buffer.NextBlock<uint32_t>();
55   ASSERT_THAT(b1, NotNull());
56   *b1 = 33;
57 
58   {
59     BigBuffer buffer2(16);
60     b1 = buffer2.NextBlock<uint32_t>();
61     ASSERT_THAT(b1, NotNull());
62     *b1 = 44;
63 
64     buffer.AppendBuffer(std::move(buffer2));
65     EXPECT_EQ(0u, buffer2.size());
66     EXPECT_EQ(buffer2.begin(), buffer2.end());
67   }
68 
69   EXPECT_EQ(2 * sizeof(uint32_t), buffer.size());
70 
71   auto b = buffer.begin();
72   ASSERT_NE(b, buffer.end());
73   ASSERT_EQ(sizeof(uint32_t), b->size);
74   ASSERT_EQ(33u, *reinterpret_cast<uint32_t*>(b->buffer.get()));
75   ++b;
76 
77   ASSERT_NE(b, buffer.end());
78   ASSERT_EQ(sizeof(uint32_t), b->size);
79   ASSERT_EQ(44u, *reinterpret_cast<uint32_t*>(b->buffer.get()));
80   ++b;
81 
82   ASSERT_EQ(b, buffer.end());
83 }
84 
TEST(BigBufferTest,PadAndAlignProperly)85 TEST(BigBufferTest, PadAndAlignProperly) {
86   BigBuffer buffer(16);
87 
88   ASSERT_THAT(buffer.NextBlock<char>(2), NotNull());
89   ASSERT_EQ(2u, buffer.size());
90   buffer.Pad(2);
91   ASSERT_EQ(4u, buffer.size());
92   buffer.Align4();
93   ASSERT_EQ(4u, buffer.size());
94   buffer.Pad(2);
95   ASSERT_EQ(6u, buffer.size());
96   buffer.Align4();
97   ASSERT_EQ(8u, buffer.size());
98 }
99 
100 }  // namespace aapt
101