Home | History | Annotate | Download | only in unittests
      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.
     15 GCFactoryListTraitsTest::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.
     34 GCFactoryListTraitsTest::~GCFactoryListTraitsTest()
     35 {
     36 }
     37 
     38 // SetUp() will be called immediately before each test.
     39 void 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.
     49 void 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_TRUE(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 
     79 TEST_F( GCFactoryListTraitsTest, Basic) {
     80   ASSERT_TRUE(10 == m_pNodeList.size());
     81   CHECK_LIST_VALUE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
     82 }
     83 
     84 TEST_F( GCFactoryListTraitsTest, BasicAgain) {
     85   ASSERT_TRUE(10 == m_pNodeList.size());
     86   CHECK_LIST_VALUE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
     87 }
     88 
     89 TEST_F( GCFactoryListTraitsTest, Clear) {
     90   m_pNodeList.clear();
     91   ASSERT_TRUE(0 == m_pNodeList.size());
     92 }
     93 
     94 TEST_F( GCFactoryListTraitsTest, PushThenPop) {
     95   Node *NewNode = m_NodeFactory.produce(11);
     96   m_pNodeList.push_back(NewNode);
     97   ASSERT_TRUE(11 == m_pNodeList.size());
     98   m_pNodeList.pop_back();
     99   ASSERT_TRUE(10 == m_pNodeList.size());
    100 }
    101 
    102 TEST_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 
    109 TEST_F( GCFactoryListTraitsTest, Empty) {
    110   ASSERT_FALSE(m_pNodeList.empty());
    111   m_pNodeList.clear();
    112   ASSERT_TRUE(m_pNodeList.empty());
    113 }
    114 
    115 TEST_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