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