Parameterize the BumpPtrAllocator over a slab allocator. It defaults to using
[oota-llvm.git] / unittests / Support / AllocatorTest.cpp
1 //===- llvm/unittest/Support/AllocatorTest.cpp - BumpPtrAllocator tests ---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Support/Allocator.h"
11
12 #include "gtest/gtest.h"
13
14 using namespace llvm;
15
16 namespace {
17
18 TEST(AllocatorTest, Basics) {
19   BumpPtrAllocator Alloc;
20   int *a = (int*)Alloc.Allocate(sizeof(int), 0);
21   int *b = (int*)Alloc.Allocate(sizeof(int) * 10, 0);
22   int *c = (int*)Alloc.Allocate(sizeof(int), 0);
23   *a = 1;
24   b[0] = 2;
25   b[9] = 2;
26   *c = 3;
27   EXPECT_EQ(1, *a);
28   EXPECT_EQ(2, b[0]);
29   EXPECT_EQ(2, b[9]);
30   EXPECT_EQ(3, *c);
31   EXPECT_EQ(1U, Alloc.GetNumSlabs());
32 }
33
34 // Allocate enough bytes to create three slabs.
35 TEST(AllocatorTest, ThreeSlabs) {
36   BumpPtrAllocator Alloc(4096, 4096);
37   Alloc.Allocate(3000, 0);
38   EXPECT_EQ(1U, Alloc.GetNumSlabs());
39   Alloc.Allocate(3000, 0);
40   EXPECT_EQ(2U, Alloc.GetNumSlabs());
41   Alloc.Allocate(3000, 0);
42   EXPECT_EQ(3U, Alloc.GetNumSlabs());
43 }
44
45 // Allocate enough bytes to create two slabs, reset the allocator, and do it
46 // again.
47 TEST(AllocatorTest, TestReset) {
48   BumpPtrAllocator Alloc(4096, 4096);
49   Alloc.Allocate(3000, 0);
50   EXPECT_EQ(1U, Alloc.GetNumSlabs());
51   Alloc.Allocate(3000, 0);
52   EXPECT_EQ(2U, Alloc.GetNumSlabs());
53   Alloc.Reset();
54   EXPECT_EQ(1U, Alloc.GetNumSlabs());
55   Alloc.Allocate(3000, 0);
56   EXPECT_EQ(1U, Alloc.GetNumSlabs());
57   Alloc.Allocate(3000, 0);
58   EXPECT_EQ(2U, Alloc.GetNumSlabs());
59 }
60
61 }  // anonymous namespace