blob: f33af44732de0105dce0c4a7db793ee1a5af1fa5 [file]
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "androidfw/ArenaAllocator.h"
#include <utility>
#include <gtest/gtest.h>
namespace android {
TEST(ArenaAllocatorTest, Allocate_Alignment) {
ArenaAllocator arena;
// Allocate odd size payloads with different alignment requests
uint8_t* p1 = arena.Allocate(1, alignof(uint16_t));
EXPECT_NE(p1, nullptr);
EXPECT_EQ(reinterpret_cast<uintptr_t>(p1) % alignof(uint16_t), 0u);
uint8_t* p2 = arena.Allocate(3, alignof(uint32_t));
EXPECT_NE(p2, nullptr);
EXPECT_EQ(reinterpret_cast<uintptr_t>(p2) % alignof(uint32_t), 0u);
uint8_t* p3 = arena.Allocate(7, alignof(uint64_t));
EXPECT_NE(p3, nullptr);
EXPECT_EQ(reinterpret_cast<uintptr_t>(p3) % alignof(uint64_t), 0u);
}
TEST(ArenaAllocatorTest, Allocate_BlockGrowthSequence) {
ArenaAllocator arena;
EXPECT_EQ(arena.block_count(), 0u);
// Allocate 500 bytes (fits in initial 1 KB block)
arena.Allocate(500);
EXPECT_EQ(arena.block_count(), 1u);
// Allocate 600 more bytes (exceeds 1 KB, triggers 4 KB block allocation)
arena.Allocate(600);
EXPECT_EQ(arena.block_count(), 2u);
// Fill up the 4 KB block and trigger the next 16 KB capped block
arena.Allocate(4000);
EXPECT_EQ(arena.block_count(), 3u);
}
TEST(ArenaAllocatorTest, Allocate_OversizedPayload) {
ArenaAllocator arena;
// Allocate a single payload larger than the 16 KB capped block size
constexpr size_t kLargeSize = 32768; // 32 KB
uint8_t* ptr = arena.Allocate(kLargeSize);
EXPECT_NE(ptr, nullptr);
EXPECT_EQ(arena.block_count(), 1u);
}
TEST(ArenaAllocatorTest, Reset_ClearsBlocksAndResetsGrowth) {
ArenaAllocator arena;
arena.Allocate(1200);
arena.Allocate(5000);
EXPECT_GE(arena.block_count(), 2u);
arena.Reset();
EXPECT_EQ(arena.block_count(), 0u);
// Next allocation should start back with 1 KB block
arena.Allocate(200);
EXPECT_EQ(arena.block_count(), 1u);
}
TEST(ArenaAllocatorTest, MoveConstructor_And_MoveAssignment) {
ArenaAllocator arena;
uint8_t* p1 = arena.Allocate(500);
*p1 = 42;
// Test move construction
ArenaAllocator moved_arena(std::move(arena));
EXPECT_EQ(arena.block_count(), 0u);
EXPECT_EQ(moved_arena.block_count(), 1u);
// Verify memory content preserved
uint8_t* p2 = moved_arena.Allocate(100);
EXPECT_NE(p2, nullptr);
// Test move assignment
ArenaAllocator assigned_arena;
assigned_arena = std::move(moved_arena);
EXPECT_EQ(moved_arena.block_count(), 0u);
EXPECT_EQ(assigned_arena.block_count(), 1u);
// Test self-move assignment
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wself-move"
assigned_arena = std::move(assigned_arena);
#pragma clang diagnostic pop
EXPECT_EQ(assigned_arena.block_count(), 1u);
EXPECT_EQ(*p1, 42);
}
} // namespace android