diff options
author | Reid Kleckner <reid@kleckner.net> | 2009-07-23 18:34:13 +0000 |
---|---|---|
committer | Reid Kleckner <reid@kleckner.net> | 2009-07-23 18:34:13 +0000 |
commit | 8f51a62b41a425f7fe262ff20cee835129ecc072 (patch) | |
tree | e5ef9fec8d79b405ae7e438f74dcc1f6fbbbf85c /unittests/Support/AllocatorTest.cpp | |
parent | d3d9d66dd211d1267e764c7294876d9a227f04ca (diff) |
Re-committing changes from r76825 to BumpPtrAllocator with a fix and tests for
an off-by-one error.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76891 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Support/AllocatorTest.cpp')
-rw-r--r-- | unittests/Support/AllocatorTest.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/unittests/Support/AllocatorTest.cpp b/unittests/Support/AllocatorTest.cpp index e69de29bb2..cc3296a8d0 100644 --- a/unittests/Support/AllocatorTest.cpp +++ b/unittests/Support/AllocatorTest.cpp @@ -0,0 +1,95 @@ +//===- llvm/unittest/Support/AllocatorTest.cpp - BumpPtrAllocator tests ---===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/Allocator.h" + +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +TEST(AllocatorTest, Basics) { + BumpPtrAllocator Alloc; + int *a = (int*)Alloc.Allocate(sizeof(int), 0); + int *b = (int*)Alloc.Allocate(sizeof(int) * 10, 0); + int *c = (int*)Alloc.Allocate(sizeof(int), 0); + *a = 1; + b[0] = 2; + b[9] = 2; + *c = 3; + EXPECT_EQ(1, *a); + EXPECT_EQ(2, b[0]); + EXPECT_EQ(2, b[9]); + EXPECT_EQ(3, *c); + EXPECT_EQ(1U, Alloc.GetNumSlabs()); +} + +// Allocate enough bytes to create three slabs. +TEST(AllocatorTest, ThreeSlabs) { + BumpPtrAllocator Alloc(4096, 4096); + Alloc.Allocate(3000, 0); + EXPECT_EQ(1U, Alloc.GetNumSlabs()); + Alloc.Allocate(3000, 0); + EXPECT_EQ(2U, Alloc.GetNumSlabs()); + Alloc.Allocate(3000, 0); + EXPECT_EQ(3U, Alloc.GetNumSlabs()); +} + +// Allocate enough bytes to create two slabs, reset the allocator, and do it +// again. +TEST(AllocatorTest, TestReset) { + BumpPtrAllocator Alloc(4096, 4096); + Alloc.Allocate(3000, 0); + EXPECT_EQ(1U, Alloc.GetNumSlabs()); + Alloc.Allocate(3000, 0); + EXPECT_EQ(2U, Alloc.GetNumSlabs()); + Alloc.Reset(); + EXPECT_EQ(1U, Alloc.GetNumSlabs()); + Alloc.Allocate(3000, 0); + EXPECT_EQ(1U, Alloc.GetNumSlabs()); + Alloc.Allocate(3000, 0); + EXPECT_EQ(2U, Alloc.GetNumSlabs()); +} + +// Test some allocations at varying alignments. +TEST(AllocatorTest, TestAlignment) { + BumpPtrAllocator Alloc; + uintptr_t a; + a = (uintptr_t)Alloc.Allocate(1, 2); + EXPECT_EQ(0U, a & 1); + a = (uintptr_t)Alloc.Allocate(1, 4); + EXPECT_EQ(0U, a & 3); + a = (uintptr_t)Alloc.Allocate(1, 8); + EXPECT_EQ(0U, a & 7); + a = (uintptr_t)Alloc.Allocate(1, 16); + EXPECT_EQ(0U, a & 15); + a = (uintptr_t)Alloc.Allocate(1, 32); + EXPECT_EQ(0U, a & 31); + a = (uintptr_t)Alloc.Allocate(1, 64); + EXPECT_EQ(0U, a & 63); + a = (uintptr_t)Alloc.Allocate(1, 128); + EXPECT_EQ(0U, a & 127); +} + +// Test allocating just over the slab size. This tests a bug where before the +// allocator incorrectly calculated the buffer end pointer. +TEST(AllocatorTest, TestOverflow) { + BumpPtrAllocator Alloc(4096, 4096); + + // Fill the slab right up until the end pointer. + Alloc.Allocate(4096 - sizeof(MemSlab), 0); + EXPECT_EQ(1U, Alloc.GetNumSlabs()); + + // If we dont't allocate a new slab, then we will have overflowed. + Alloc.Allocate(1, 0); + EXPECT_EQ(2U, Alloc.GetNumSlabs()); +} + +} // anonymous namespace |