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
//===- GCFactoryListTraitsTest.cpp ----------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "GCFactoryListTraitsTest.h"
 
using namespace mcld;
using namespace mcldtest;
 
// Constructor can do set-up work for all test here.
GCFactoryListTraitsTest::GCFactoryListTraitsTest() {
  // Allocate the nodes.
  m_pNodesAlloc = new Node* [10];
#define ALLOCATE_NODE(i) m_pNodesAlloc[(i)] = m_NodeFactory.produce(i);
  ALLOCATE_NODE(0);
  ALLOCATE_NODE(1);
  ALLOCATE_NODE(2);
  ALLOCATE_NODE(3);
  ALLOCATE_NODE(4);
  ALLOCATE_NODE(5);
  ALLOCATE_NODE(6);
  ALLOCATE_NODE(7);
  ALLOCATE_NODE(8);
  ALLOCATE_NODE(9);
#undef ALLOCATE_NODE
}
 
// Destructor can do clean-up work that doesn't throw exceptions here.
GCFactoryListTraitsTest::~GCFactoryListTraitsTest() {
}
 
// SetUp() will be called immediately before each test.
void GCFactoryListTraitsTest::SetUp() {
  // Reset the node value and (re)insert into the iplist.
  for (unsigned i = 0; i < 10; i++) {
    m_pNodesAlloc[i]->setValue(m_pNodesAlloc[i]->getInitialValue());
    m_pNodeList.push_back(m_pNodesAlloc[i]);
  }
}
 
// TearDown() will be called immediately after each test.
void GCFactoryListTraitsTest::TearDown() {
  // Erasing of llvm::iplist won't destroy the allocation of the nodes managed
  // by the GCFactory (i.e., NodeFactory.)
  m_pNodeList.clear();
}
 
//==========================================================================//
// Testcases
//
 
#define CHECK_NODE_VALUE(v_)           \
  do {                                 \
    ASSERT_TRUE(v_ == it->getValue()); \
    it++;                              \
  } while (false)
 
#define CHECK_LIST_VALUE(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) \
  do {                                                            \
    llvm::iplist<Node>::const_iterator it = m_pNodeList.begin();  \
    CHECK_NODE_VALUE(v1);                                         \
    CHECK_NODE_VALUE(v2);                                         \
    CHECK_NODE_VALUE(v3);                                         \
    CHECK_NODE_VALUE(v4);                                         \
    CHECK_NODE_VALUE(v5);                                         \
    CHECK_NODE_VALUE(v6);                                         \
    CHECK_NODE_VALUE(v7);                                         \
    CHECK_NODE_VALUE(v8);                                         \
    CHECK_NODE_VALUE(v9);                                         \
    CHECK_NODE_VALUE(v10);                                        \
  } while (false)
 
TEST_F(GCFactoryListTraitsTest, Basic) {
  ASSERT_TRUE(10 == m_pNodeList.size());
  CHECK_LIST_VALUE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
}
 
TEST_F(GCFactoryListTraitsTest, BasicAgain) {
  ASSERT_TRUE(10 == m_pNodeList.size());
  CHECK_LIST_VALUE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
}
 
TEST_F(GCFactoryListTraitsTest, Clear) {
  m_pNodeList.clear();
  ASSERT_TRUE(0 == m_pNodeList.size());
}
 
TEST_F(GCFactoryListTraitsTest, PushThenPop) {
  Node* NewNode = m_NodeFactory.produce(11);
  m_pNodeList.push_back(NewNode);
  ASSERT_TRUE(11 == m_pNodeList.size());
  m_pNodeList.pop_back();
  ASSERT_TRUE(10 == m_pNodeList.size());
}
 
TEST_F(GCFactoryListTraitsTest, CodeIterator) {
  // to test whether there's compilation error for const template
  for (llvm::iplist<Node>::const_iterator I = m_pNodeList.begin(),
                                          E = m_pNodeList.end();
       I != E;
       I++)
    I->getValue();
}
 
TEST_F(GCFactoryListTraitsTest, Empty) {
  ASSERT_FALSE(m_pNodeList.empty());
  m_pNodeList.clear();
  ASSERT_TRUE(m_pNodeList.empty());
}
 
TEST_F(GCFactoryListTraitsTest, EraseAndSize) {
  ASSERT_FALSE(m_pNodeList.empty());
  m_pNodeList.erase(m_pNodeList.begin());
  m_pNodeList.erase(m_pNodeList.begin());
  ASSERT_TRUE(m_pNodeList.size() == 8);
}
 
#undef CHECK_LIST_VALUE
#undef CHECK_NODE_VALUE