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