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