Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 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 | |
| 11 | using namespace mcld; |
| 12 | using namespace mcldtest; |
| 13 | |
| 14 | // Constructor can do set-up work for all test here. |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 15 | GCFactoryListTraitsTest::GCFactoryListTraitsTest() { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 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. |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 33 | GCFactoryListTraitsTest::~GCFactoryListTraitsTest() { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // SetUp() will be called immediately before each test. |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 37 | void GCFactoryListTraitsTest::SetUp() { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 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. |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 46 | void GCFactoryListTraitsTest::TearDown() { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 56 | #define CHECK_NODE_VALUE(v_) \ |
| 57 | do { \ |
| 58 | ASSERT_TRUE(v_ == it->getValue()); \ |
| 59 | it++; \ |
| 60 | } while (false) |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 61 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 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) |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 76 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 77 | TEST_F(GCFactoryListTraitsTest, Basic) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 78 | ASSERT_TRUE(10 == m_pNodeList.size()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 79 | CHECK_LIST_VALUE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 80 | } |
| 81 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 82 | TEST_F(GCFactoryListTraitsTest, BasicAgain) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 83 | ASSERT_TRUE(10 == m_pNodeList.size()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 84 | CHECK_LIST_VALUE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 85 | } |
| 86 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 87 | TEST_F(GCFactoryListTraitsTest, Clear) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 88 | m_pNodeList.clear(); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 89 | ASSERT_TRUE(0 == m_pNodeList.size()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 92 | TEST_F(GCFactoryListTraitsTest, PushThenPop) { |
| 93 | Node* NewNode = m_NodeFactory.produce(11); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 94 | m_pNodeList.push_back(NewNode); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 95 | ASSERT_TRUE(11 == m_pNodeList.size()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 96 | m_pNodeList.pop_back(); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 97 | ASSERT_TRUE(10 == m_pNodeList.size()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 100 | TEST_F(GCFactoryListTraitsTest, CodeIterator) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 101 | // to test whether there's compilation error for const template |
| 102 | for (llvm::iplist<Node>::const_iterator I = m_pNodeList.begin(), |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 103 | E = m_pNodeList.end(); |
| 104 | I != E; |
| 105 | I++) |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 106 | I->getValue(); |
| 107 | } |
| 108 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 109 | TEST_F(GCFactoryListTraitsTest, Empty) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 110 | ASSERT_FALSE(m_pNodeList.empty()); |
| 111 | m_pNodeList.clear(); |
| 112 | ASSERT_TRUE(m_pNodeList.empty()); |
| 113 | } |
| 114 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 115 | TEST_F(GCFactoryListTraitsTest, EraseAndSize) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 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 |