blob: 7fecb265e7a5caa84bf20e4efb60cc0250997353 [file] [log] [blame]
Shih-wei Liao5460a1f2012-03-16 22:41:16 -07001//===- 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.
Stephen Hines37b74a32014-11-26 18:48:20 -080015GCFactoryListTraitsTest::GCFactoryListTraitsTest() {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070016 // 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.
Stephen Hines37b74a32014-11-26 18:48:20 -080033GCFactoryListTraitsTest::~GCFactoryListTraitsTest() {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070034}
35
36// SetUp() will be called immediately before each test.
Stephen Hines37b74a32014-11-26 18:48:20 -080037void GCFactoryListTraitsTest::SetUp() {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070038 // 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.
Stephen Hines37b74a32014-11-26 18:48:20 -080046void GCFactoryListTraitsTest::TearDown() {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070047 // 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
Stephen Hines37b74a32014-11-26 18:48:20 -080056#define CHECK_NODE_VALUE(v_) \
57 do { \
58 ASSERT_TRUE(v_ == it->getValue()); \
59 it++; \
60 } while (false)
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070061
Stephen Hines37b74a32014-11-26 18:48:20 -080062#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)
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070076
Stephen Hines37b74a32014-11-26 18:48:20 -080077TEST_F(GCFactoryListTraitsTest, Basic) {
Shih-wei Liao22add6f2012-12-15 17:21:00 -080078 ASSERT_TRUE(10 == m_pNodeList.size());
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070079 CHECK_LIST_VALUE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
80}
81
Stephen Hines37b74a32014-11-26 18:48:20 -080082TEST_F(GCFactoryListTraitsTest, BasicAgain) {
Shih-wei Liao22add6f2012-12-15 17:21:00 -080083 ASSERT_TRUE(10 == m_pNodeList.size());
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070084 CHECK_LIST_VALUE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
85}
86
Stephen Hines37b74a32014-11-26 18:48:20 -080087TEST_F(GCFactoryListTraitsTest, Clear) {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070088 m_pNodeList.clear();
Shih-wei Liao22add6f2012-12-15 17:21:00 -080089 ASSERT_TRUE(0 == m_pNodeList.size());
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070090}
91
Stephen Hines37b74a32014-11-26 18:48:20 -080092TEST_F(GCFactoryListTraitsTest, PushThenPop) {
93 Node* NewNode = m_NodeFactory.produce(11);
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070094 m_pNodeList.push_back(NewNode);
Shih-wei Liao22add6f2012-12-15 17:21:00 -080095 ASSERT_TRUE(11 == m_pNodeList.size());
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070096 m_pNodeList.pop_back();
Shih-wei Liao22add6f2012-12-15 17:21:00 -080097 ASSERT_TRUE(10 == m_pNodeList.size());
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070098}
99
Stephen Hines37b74a32014-11-26 18:48:20 -0800100TEST_F(GCFactoryListTraitsTest, CodeIterator) {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700101 // to test whether there's compilation error for const template
102 for (llvm::iplist<Node>::const_iterator I = m_pNodeList.begin(),
Stephen Hines37b74a32014-11-26 18:48:20 -0800103 E = m_pNodeList.end();
104 I != E;
105 I++)
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700106 I->getValue();
107}
108
Stephen Hines37b74a32014-11-26 18:48:20 -0800109TEST_F(GCFactoryListTraitsTest, Empty) {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700110 ASSERT_FALSE(m_pNodeList.empty());
111 m_pNodeList.clear();
112 ASSERT_TRUE(m_pNodeList.empty());
113}
114
Stephen Hines37b74a32014-11-26 18:48:20 -0800115TEST_F(GCFactoryListTraitsTest, EraseAndSize) {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700116 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