Path: blob/main_old/src/common/PoolAlloc_unittest.cpp
1693 views
//1// Copyright 2019 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// PoolAlloc_unittest:6// Tests of the PoolAlloc class7//89#include <gtest/gtest.h>1011#include "common/PoolAlloc.h"1213namespace angle14{15// Verify the public interface of PoolAllocator class16TEST(PoolAllocatorTest, Interface)17{18size_t numBytes = 1024;19constexpr uint32_t kTestValue = 0xbaadbeef;20// Create a default pool allocator and allocate from it21PoolAllocator poolAllocator;22void *allocation = poolAllocator.allocate(numBytes);23// Verify non-zero ptr returned24EXPECT_NE(nullptr, allocation);25// Write to allocation to check later26uint32_t *writePtr = static_cast<uint32_t *>(allocation);27*writePtr = kTestValue;28// Test push and creating a new allocation29poolAllocator.push();30allocation = poolAllocator.allocate(numBytes);31EXPECT_NE(nullptr, allocation);32// Make an allocation that spans multiple pages33allocation = poolAllocator.allocate(10 * 1024);34// pop previous two allocations35poolAllocator.pop();36// Verify first allocation still has data37EXPECT_EQ(kTestValue, *writePtr);38// Make a bunch of allocations39for (uint32_t i = 0; i < 1000; ++i)40{41numBytes = (rand() % (1024 * 4)) + 1;42allocation = poolAllocator.allocate(numBytes);43EXPECT_NE(nullptr, allocation);44// Write data into full allocation. In debug case if we45// overwrite any other allocation we get error.46memset(allocation, 0xb8, numBytes);47}48// Free everything49poolAllocator.popAll();50}5152#if !defined(ANGLE_POOL_ALLOC_GUARD_BLOCKS)53// Verify allocations are correctly aligned for different alignments54class PoolAllocatorAlignmentTest : public testing::TestWithParam<int>55{};5657TEST_P(PoolAllocatorAlignmentTest, Alignment)58{59int alignment = GetParam();60// Create a pool allocator to allocate from61PoolAllocator poolAllocator(4096, alignment);62// Test a number of allocation sizes for each alignment63for (uint32_t i = 0; i < 100; ++i)64{65// Vary the allocation size around 4k to hit some multi-page allocations66void *allocation = poolAllocator.allocate((rand() % (1024 * 4)) + 1);67// Verify alignment of allocation matches expected default68EXPECT_EQ(0u, (reinterpret_cast<std::uintptr_t>(allocation) % alignment));69}70}7172INSTANTIATE_TEST_SUITE_P(,73PoolAllocatorAlignmentTest,74testing::Values(2, 4, 8, 16, 32, 64, 128),75testing::PrintToStringParamName());76#endif77} // namespace angle787980