blob: 382d21e208460b2968d19dc4850db6574692bbe9 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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
Jeremy Meyer56f36e82022-05-20 20:35:42 +000017#include "androidfw/BigBuffer.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018
Jeremy Meyer56f36e82022-05-20 20:35:42 +000019#include "gmock/gmock.h"
20#include "gtest/gtest.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021
Adam Lesinskia45893a2017-05-30 15:19:02 -070022using ::testing::NotNull;
23
Jeremy Meyer56f36e82022-05-20 20:35:42 +000024namespace android {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025
26TEST(BigBufferTest, AllocateSingleBlock) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027 BigBuffer buffer(4);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028
Adam Lesinskia45893a2017-05-30 15:19:02 -070029 EXPECT_THAT(buffer.NextBlock<char>(2), NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 EXPECT_EQ(2u, buffer.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031}
32
33TEST(BigBufferTest, ReturnSameBlockIfNextAllocationFits) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 BigBuffer buffer(16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 char* b1 = buffer.NextBlock<char>(8);
Adam Lesinskia45893a2017-05-30 15:19:02 -070037 EXPECT_THAT(b1, NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 char* b2 = buffer.NextBlock<char>(4);
Adam Lesinskia45893a2017-05-30 15:19:02 -070040 EXPECT_THAT(b2, NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 EXPECT_EQ(b1 + 8, b2);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043}
44
45TEST(BigBufferTest, AllocateExactSizeBlockIfLargerThanBlockSize) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 BigBuffer buffer(16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinskia45893a2017-05-30 15:19:02 -070048 EXPECT_THAT(buffer.NextBlock<char>(32), NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 EXPECT_EQ(32u, buffer.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050}
51
52TEST(BigBufferTest, AppendAndMoveBlock) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 BigBuffer buffer(16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 uint32_t* b1 = buffer.NextBlock<uint32_t>();
Adam Lesinskia45893a2017-05-30 15:19:02 -070056 ASSERT_THAT(b1, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 *b1 = 33;
58
59 {
60 BigBuffer buffer2(16);
61 b1 = buffer2.NextBlock<uint32_t>();
Adam Lesinskia45893a2017-05-30 15:19:02 -070062 ASSERT_THAT(b1, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 *b1 = 44;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 buffer.AppendBuffer(std::move(buffer2));
Jeremy Meyer56f36e82022-05-20 20:35:42 +000066 EXPECT_EQ(0u, buffer2.size()); // NOLINT
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 EXPECT_EQ(buffer2.begin(), buffer2.end());
68 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 EXPECT_EQ(2 * sizeof(uint32_t), buffer.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 auto b = buffer.begin();
73 ASSERT_NE(b, buffer.end());
74 ASSERT_EQ(sizeof(uint32_t), b->size);
75 ASSERT_EQ(33u, *reinterpret_cast<uint32_t*>(b->buffer.get()));
76 ++b;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 ASSERT_NE(b, buffer.end());
79 ASSERT_EQ(sizeof(uint32_t), b->size);
80 ASSERT_EQ(44u, *reinterpret_cast<uint32_t*>(b->buffer.get()));
81 ++b;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 ASSERT_EQ(b, buffer.end());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084}
85
86TEST(BigBufferTest, PadAndAlignProperly) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 BigBuffer buffer(16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088
Adam Lesinskia45893a2017-05-30 15:19:02 -070089 ASSERT_THAT(buffer.NextBlock<char>(2), NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 ASSERT_EQ(2u, buffer.size());
91 buffer.Pad(2);
92 ASSERT_EQ(4u, buffer.size());
93 buffer.Align4();
94 ASSERT_EQ(4u, buffer.size());
95 buffer.Pad(2);
96 ASSERT_EQ(6u, buffer.size());
97 buffer.Align4();
98 ASSERT_EQ(8u, buffer.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099}
100
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000101} // namespace android