LinearAllocatorTest.cpp revision 87f34658dec9097d987d254a990ea7f311bfc95f
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
12using namespace mcld;
13using namespace mcldtest;
14
15
16// Constructor can do set-up work for all test here.
17LinearAllocatorTest::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.
24LinearAllocatorTest::~LinearAllocatorTest()
25{
26	delete m_pTestee;
27}
28
29// SetUp() will be called immediately before each test.
30void LinearAllocatorTest::SetUp()
31{
32}
33
34// TearDown() will be called immediately after each test.
35void LinearAllocatorTest::TearDown()
36{
37}
38
39//==========================================================================//
40// Testcases
41//
42TEST_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
49TEST_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
56TEST_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
63TEST_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
72TEST_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
83TEST_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
92TEST_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
103TEST_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
119TEST_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