1 //===- LinearAllocatorTest.cpp --------------------------------------------===//
2 //
3 // The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "LinearAllocatorTest.h"
10 #include <mcld/Support/Allocators.h>
11
12 using namespace mcld;
13 using namespace mcldtest;
14
15
16 // Constructor can do set-up work for all test here.
LinearAllocatorTest()17 LinearAllocatorTest::LinearAllocatorTest()
18 {
19 // create testee. modify it if need
20 m_pTestee = new LinearAllocator<Data, CHUNK_SIZE>();
21 }
22
23 // Destructor can do clean-up work that doesn't throw exceptions here.
~LinearAllocatorTest()24 LinearAllocatorTest::~LinearAllocatorTest()
25 {
26 delete m_pTestee;
27 }
28
29 // SetUp() will be called immediately before each test.
SetUp()30 void LinearAllocatorTest::SetUp()
31 {
32 }
33
34 // TearDown() will be called immediately after each test.
TearDown()35 void LinearAllocatorTest::TearDown()
36 {
37 }
38
39 //==========================================================================//
40 // Testcases
41 //
TEST_F(LinearAllocatorTest,allocateN)42 TEST_F( LinearAllocatorTest, allocateN ) {
43 Data* pointer = m_pTestee->allocate(10);
44 ASSERT_FALSE(0 == pointer);
45 ASSERT_EQ(CHUNK_SIZE, m_pTestee->max_size());
46 ASSERT_FALSE(m_pTestee->empty());
47 }
48
TEST_F(LinearAllocatorTest,allocate)49 TEST_F( LinearAllocatorTest, allocate ) {
50 Data* pointer = m_pTestee->allocate();
51 ASSERT_FALSE(0 == pointer);
52 ASSERT_EQ(CHUNK_SIZE, m_pTestee->max_size());
53 ASSERT_FALSE(m_pTestee->empty());
54 }
55
TEST_F(LinearAllocatorTest,allocateOver)56 TEST_F( LinearAllocatorTest, allocateOver ) {
57 Data* pointer = m_pTestee->allocate(CHUNK_SIZE+1);
58 ASSERT_TRUE(0 == pointer);
59 ASSERT_TRUE(0 == m_pTestee->max_size());
60 ASSERT_TRUE(m_pTestee->empty());
61 }
62
TEST_F(LinearAllocatorTest,alloc_construct)63 TEST_F( LinearAllocatorTest, alloc_construct ) {
64 Data* pointer = m_pTestee->allocate();
65 m_pTestee->construct(pointer);
66 ASSERT_TRUE(1 == pointer->one);
67 ASSERT_TRUE(2 == pointer->two);
68 ASSERT_TRUE(3 == pointer->three);
69 ASSERT_TRUE(4 == pointer->four);
70 }
71
TEST_F(LinearAllocatorTest,alloc_constructCopy)72 TEST_F( LinearAllocatorTest, alloc_constructCopy ) {
73 Data* pointer = m_pTestee->allocate();
74 Data data(7, 7, 7, 7);
75 m_pTestee->construct(pointer, data);
76
77 ASSERT_TRUE(7 == pointer->one);
78 ASSERT_TRUE(7 == pointer->two);
79 ASSERT_TRUE(7 == pointer->three);
80 ASSERT_TRUE(7 == pointer->four);
81 }
82
TEST_F(LinearAllocatorTest,allocN_construct)83 TEST_F( LinearAllocatorTest, allocN_construct ) {
84 Data* pointer = m_pTestee->allocate(10);
85 m_pTestee->construct(pointer);
86 ASSERT_TRUE(1 == pointer->one);
87 ASSERT_TRUE(2 == pointer->two);
88 ASSERT_TRUE(3 == pointer->three);
89 ASSERT_TRUE(4 == pointer->four);
90 }
91
TEST_F(LinearAllocatorTest,allocN_constructCopy)92 TEST_F( LinearAllocatorTest, allocN_constructCopy ) {
93 Data* pointer = m_pTestee->allocate(10);
94 Data data(7, 7, 7, 7);
95 m_pTestee->construct(pointer, data);
96
97 ASSERT_TRUE(7 == pointer->one);
98 ASSERT_TRUE(7 == pointer->two);
99 ASSERT_TRUE(7 == pointer->three);
100 ASSERT_TRUE(7 == pointer->four);
101 }
102
TEST_F(LinearAllocatorTest,multi_alloc_ctor_iterate)103 TEST_F( LinearAllocatorTest, multi_alloc_ctor_iterate ) {
104 for (int i=0; i<101; ++i) {
105 Data* pointer = m_pTestee->allocate();
106 m_pTestee->construct(pointer);
107 pointer->one = i;
108 }
109 /**
110 Alloc::iterator data, dEnd = m_pTestee->end();
111 int counter = 0;
112 for (data=m_pTestee->begin(); data!=dEnd; ++data) {
113 ASSERT_EQ(counter, (*data).one);
114 ++counter;
115 }
116 **/
117 }
118
TEST_F(LinearAllocatorTest,multi_allocN_ctor_iterate)119 TEST_F( LinearAllocatorTest, multi_allocN_ctor_iterate ) {
120 int counter = 0;
121 for (int i=0; i<10000; ++i) {
122 Data* pointer = m_pTestee->allocate(10);
123 for (int j=0; j<10; ++j) {
124 m_pTestee->construct(pointer);
125 pointer->one = counter;
126 ++pointer;
127 ++counter;
128 }
129 }
130 /**
131 Alloc::iterator data, dEnd = m_pTestee->end();
132 counter = 0;
133 for (data=m_pTestee->begin(); data!=dEnd; ++data) {
134 ASSERT_EQ(counter, (*data).one);
135 ++counter;
136 }
137 **/
138 }
139
140