blob: 64dcc1dad9a21c6208abd98f7159776a47785750 [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
Adam Lesinski1ab598f2015-08-14 14:26:04 -070017#include "util/BigBuffer.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include "test/Test.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020
Adam Lesinskia45893a2017-05-30 15:19:02 -070021using ::testing::NotNull;
22
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023namespace aapt {
24
25TEST(BigBufferTest, AllocateSingleBlock) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026 BigBuffer buffer(4);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027
Adam Lesinskia45893a2017-05-30 15:19:02 -070028 EXPECT_THAT(buffer.NextBlock<char>(2), NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029 EXPECT_EQ(2u, buffer.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030}
31
32TEST(BigBufferTest, ReturnSameBlockIfNextAllocationFits) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033 BigBuffer buffer(16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 char* b1 = buffer.NextBlock<char>(8);
Adam Lesinskia45893a2017-05-30 15:19:02 -070036 EXPECT_THAT(b1, NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 char* b2 = buffer.NextBlock<char>(4);
Adam Lesinskia45893a2017-05-30 15:19:02 -070039 EXPECT_THAT(b2, NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 EXPECT_EQ(b1 + 8, b2);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042}
43
44TEST(BigBufferTest, AllocateExactSizeBlockIfLargerThanBlockSize) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 BigBuffer buffer(16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046
Adam Lesinskia45893a2017-05-30 15:19:02 -070047 EXPECT_THAT(buffer.NextBlock<char>(32), NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 EXPECT_EQ(32u, buffer.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049}
50
51TEST(BigBufferTest, AppendAndMoveBlock) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 BigBuffer buffer(16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 uint32_t* b1 = buffer.NextBlock<uint32_t>();
Adam Lesinskia45893a2017-05-30 15:19:02 -070055 ASSERT_THAT(b1, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 *b1 = 33;
57
58 {
59 BigBuffer buffer2(16);
60 b1 = buffer2.NextBlock<uint32_t>();
Adam Lesinskia45893a2017-05-30 15:19:02 -070061 ASSERT_THAT(b1, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 *b1 = 44;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 buffer.AppendBuffer(std::move(buffer2));
Chih-Hung Hsieh7a616f62020-03-05 15:59:25 -080065 EXPECT_EQ(0u, buffer2.size()); // NOLINT
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 EXPECT_EQ(buffer2.begin(), buffer2.end());
67 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 EXPECT_EQ(2 * sizeof(uint32_t), buffer.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 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;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 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;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 ASSERT_EQ(b, buffer.end());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083}
84
85TEST(BigBufferTest, PadAndAlignProperly) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 BigBuffer buffer(16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
Adam Lesinskia45893a2017-05-30 15:19:02 -070088 ASSERT_THAT(buffer.NextBlock<char>(2), NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 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());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098}
99
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100} // namespace aapt