1//===- GCFactoryListTraitsTest.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 "GCFactoryListTraitsTest.h"
10
11using namespace mcld;
12using namespace mcldtest;
13
14// Constructor can do set-up work for all test here.
15GCFactoryListTraitsTest::GCFactoryListTraitsTest() {
16  // Allocate the nodes.
17  m_pNodesAlloc = new Node* [10];
18#define ALLOCATE_NODE(i) m_pNodesAlloc[(i)] = m_NodeFactory.produce(i);
19  ALLOCATE_NODE(0);
20  ALLOCATE_NODE(1);
21  ALLOCATE_NODE(2);
22  ALLOCATE_NODE(3);
23  ALLOCATE_NODE(4);
24  ALLOCATE_NODE(5);
25  ALLOCATE_NODE(6);
26  ALLOCATE_NODE(7);
27  ALLOCATE_NODE(8);
28  ALLOCATE_NODE(9);
29#undef ALLOCATE_NODE
30}
31
32// Destructor can do clean-up work that doesn't throw exceptions here.
33GCFactoryListTraitsTest::~GCFactoryListTraitsTest() {
34}
35
36// SetUp() will be called immediately before each test.
37void GCFactoryListTraitsTest::SetUp() {
38  // Reset the node value and (re)insert into the iplist.
39  for (unsigned i = 0; i < 10; i++) {
40    m_pNodesAlloc[i]->setValue(m_pNodesAlloc[i]->getInitialValue());
41    m_pNodeList.push_back(m_pNodesAlloc[i]);
42  }
43}
44
45// TearDown() will be called immediately after each test.
46void GCFactoryListTraitsTest::TearDown() {
47  // Erasing of llvm::iplist won't destroy the allocation of the nodes managed
48  // by the GCFactory (i.e., NodeFactory.)
49  m_pNodeList.clear();
50}
51
52//==========================================================================//
53// Testcases
54//
55
56#define CHECK_NODE_VALUE(v_)           \
57  do {                                 \
58    ASSERT_TRUE(v_ == it->getValue()); \
59    it++;                              \
60  } while (false)
61
62#define CHECK_LIST_VALUE(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) \
63  do {                                                            \
64    llvm::iplist<Node>::const_iterator it = m_pNodeList.begin();  \
65    CHECK_NODE_VALUE(v1);                                         \
66    CHECK_NODE_VALUE(v2);                                         \
67    CHECK_NODE_VALUE(v3);                                         \
68    CHECK_NODE_VALUE(v4);                                         \
69    CHECK_NODE_VALUE(v5);                                         \
70    CHECK_NODE_VALUE(v6);                                         \
71    CHECK_NODE_VALUE(v7);                                         \
72    CHECK_NODE_VALUE(v8);                                         \
73    CHECK_NODE_VALUE(v9);                                         \
74    CHECK_NODE_VALUE(v10);                                        \
75  } while (false)
76
77TEST_F(GCFactoryListTraitsTest, Basic) {
78  ASSERT_TRUE(10 == m_pNodeList.size());
79  CHECK_LIST_VALUE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
80}
81
82TEST_F(GCFactoryListTraitsTest, BasicAgain) {
83  ASSERT_TRUE(10 == m_pNodeList.size());
84  CHECK_LIST_VALUE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
85}
86
87TEST_F(GCFactoryListTraitsTest, Clear) {
88  m_pNodeList.clear();
89  ASSERT_TRUE(0 == m_pNodeList.size());
90}
91
92TEST_F(GCFactoryListTraitsTest, PushThenPop) {
93  Node* NewNode = m_NodeFactory.produce(11);
94  m_pNodeList.push_back(NewNode);
95  ASSERT_TRUE(11 == m_pNodeList.size());
96  m_pNodeList.pop_back();
97  ASSERT_TRUE(10 == m_pNodeList.size());
98}
99
100TEST_F(GCFactoryListTraitsTest, CodeIterator) {
101  // to test whether there's compilation error for const template
102  for (llvm::iplist<Node>::const_iterator I = m_pNodeList.begin(),
103                                          E = m_pNodeList.end();
104       I != E;
105       I++)
106    I->getValue();
107}
108
109TEST_F(GCFactoryListTraitsTest, Empty) {
110  ASSERT_FALSE(m_pNodeList.empty());
111  m_pNodeList.clear();
112  ASSERT_TRUE(m_pNodeList.empty());
113}
114
115TEST_F(GCFactoryListTraitsTest, EraseAndSize) {
116  ASSERT_FALSE(m_pNodeList.empty());
117  m_pNodeList.erase(m_pNodeList.begin());
118  m_pNodeList.erase(m_pNodeList.begin());
119  ASSERT_TRUE(m_pNodeList.size() == 8);
120}
121
122#undef CHECK_LIST_VALUE
123#undef CHECK_NODE_VALUE
124