Home | History | Annotate | Download | only in unittests
      1 //===- InputTreeTest.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 "InputTreeTest.h"
     10 
     11 #include <vector>
     12 #include <iostream>
     13 
     14 #include "mcld/InputTree.h"
     15 #include "mcld/MC/InputFactory.h"
     16 #include "mcld/LinkerConfig.h"
     17 #include "mcld/MC/InputBuilder.h"
     18 #include "mcld/MC/FileAction.h"
     19 #include "mcld/MC/CommandAction.h"
     20 
     21 using namespace mcld;
     22 using namespace mcld::test;
     23 
     24 // Constructor can do set-up work for all test here.
     25 InputTreeTest::InputTreeTest() : m_MemFactory(10), m_ContextFactory(4) {
     26   // create testee. modify it if need
     27   m_pConfig = new mcld::LinkerConfig("arm-none-linux-gnueabi");
     28   m_pAlloc = new mcld::InputFactory(10, *m_pConfig);
     29   m_pBuilder = new mcld::InputBuilder(
     30       *m_pConfig, *m_pAlloc, m_ContextFactory, m_MemFactory, false);
     31   m_pTestee = new mcld::InputTree();
     32   m_pBuilder->setCurrentTree(*m_pTestee);
     33 }
     34 
     35 // Destructor can do clean-up work that doesn't throw exceptions here.
     36 InputTreeTest::~InputTreeTest() {
     37   delete m_pTestee;
     38   delete m_pAlloc;
     39   delete m_pBuilder;
     40   delete m_pConfig;
     41 }
     42 
     43 // SetUp() will be called immediately before each test.
     44 void InputTreeTest::SetUp() {
     45 }
     46 
     47 // TearDown() will be called immediately after each test.
     48 void InputTreeTest::TearDown() {
     49 }
     50 
     51 //===----------------------------------------------------------------------===//
     52 // Testcases
     53 //
     54 TEST_F(InputTreeTest, Basic_operation) {
     55   std::vector<InputAction*> actions;
     56 
     57   size_t position = 0;
     58   actions.push_back(new StartGroupAction(position++));
     59   actions.push_back(new InputFileAction(position++, "path1"));
     60   actions.push_back(new EndGroupAction(position++));
     61 
     62   std::vector<InputAction*>::iterator action;
     63   for (action = actions.begin(); action != actions.end(); ++action) {
     64     (*action)->activate(*m_pBuilder);
     65     delete *action;
     66   }
     67 
     68   InputTree::iterator node = m_pTestee->root();
     69   InputTree::const_iterator const_node = node;
     70   --node;
     71   --const_node;
     72 
     73   ASSERT_TRUE(isGroup(node));
     74   ASSERT_TRUE(isGroup(const_node));
     75   ASSERT_FALSE(m_pAlloc->empty());
     76   ASSERT_TRUE(1 == m_pAlloc->size());
     77 
     78   --node;
     79 
     80   m_pTestee->enterGroup(node, InputTree::Downward);
     81 
     82   InputTree::const_iterator const_node2 = node;
     83 
     84   ASSERT_FALSE(node.isRoot());
     85 
     86   ASSERT_FALSE(isGroup(node));
     87   ASSERT_FALSE(isGroup(const_node2));
     88   ASSERT_FALSE(m_pAlloc->empty());
     89   ASSERT_FALSE(m_pAlloc->size() == 0);
     90 
     91   ASSERT_TRUE(m_pTestee->size() == 3);
     92 }
     93 
     94 TEST_F(InputTreeTest, forLoop_TEST) {
     95   InputTree::iterator node = m_pTestee->root();
     96 
     97   Input* input = m_pAlloc->produce("FileSpec", "path1");
     98   m_pTestee->insert<InputTree::Inclusive>(node, *input);
     99   InputTree::const_iterator const_node = node;
    100   --node;
    101 
    102   for (int i = 0; i < 100; ++i) {
    103     Input* input = m_pAlloc->produce("FileSpec", "path1");
    104     m_pTestee->insert<InputTree::Inclusive>(node, *input);
    105     ++node;
    106   }
    107 
    108   m_pTestee->enterGroup(node, InputTree::Downward);
    109   --node;
    110 
    111   ASSERT_FALSE(node.isRoot());
    112   ASSERT_TRUE(isGroup(node));
    113   ASSERT_FALSE(m_pAlloc->empty());
    114   ASSERT_FALSE(m_pAlloc->size() == 100);
    115 
    116   ASSERT_TRUE(m_pTestee->size() == 102);
    117 }
    118 
    119 TEST_F(InputTreeTest, Nesting_Case) {
    120   InputTree::iterator node = m_pTestee->root();
    121 
    122   for (int i = 0; i < 50; ++i) {
    123     m_pTestee->enterGroup(node, InputTree::Downward);
    124     --node;
    125 
    126     Input* input = m_pAlloc->produce("FileSpec", "path1");
    127     m_pTestee->insert(node, InputTree::Afterward, *input);
    128     ++node;
    129   }
    130 
    131   ASSERT_FALSE(node.isRoot());
    132   ASSERT_FALSE(isGroup(node));
    133   ASSERT_FALSE(m_pAlloc->empty());
    134   ASSERT_TRUE(m_pAlloc->size() == 50);
    135   ASSERT_TRUE(m_pTestee->size() == 100);
    136 }
    137 
    138 TEST_F(InputTreeTest, DFSIterator_BasicTraversal) {
    139   InputTree::iterator node = m_pTestee->root();
    140   Input* input = m_pAlloc->produce("111", "/");
    141   m_pTestee->insert<InputTree::Inclusive>(node, *input);
    142   node.move<InputTree::Inclusive>();
    143 
    144   input = m_pAlloc->produce("10", "/");
    145   m_pTestee->insert<InputTree::Positional>(node, *input);
    146   m_pTestee->enterGroup<InputTree::Inclusive>(node);
    147   node.move<InputTree::Inclusive>();
    148 
    149   input = m_pAlloc->produce("7", "/");
    150   m_pTestee->insert<InputTree::Inclusive>(node, *input);
    151   input = m_pAlloc->produce("8", "/");
    152   m_pTestee->insert<InputTree::Positional>(node, *input);
    153 
    154   InputTree::dfs_iterator dfs_it = m_pTestee->dfs_begin();
    155   InputTree::dfs_iterator dfs_end = m_pTestee->dfs_end();
    156   ASSERT_STREQ("111", (*dfs_it)->name().c_str());
    157   ++dfs_it;
    158   ASSERT_STREQ("7", (**dfs_it).name().c_str());
    159   ++dfs_it;
    160   ASSERT_STREQ("8", (**dfs_it).name().c_str());
    161   ++dfs_it;
    162   ASSERT_STREQ("10", (**dfs_it).name().c_str());
    163   ++dfs_it;
    164   ASSERT_TRUE(dfs_it == dfs_end);
    165 }
    166