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
//===- PathTest.cpp -------------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "PathTest.h"
#include "mcld/Support/FileSystem.h"
#include <string>
 
//
using namespace mcld;
using namespace mcld::sys::fs;
using namespace mcldtest;
 
// Constructor can do set-up work for all test here.
PathTest::PathTest() {
  // create testee. modify it if need
  m_pTestee = new Path();
}
 
// Destructor can do clean-up work that doesn't throw exceptions here.
PathTest::~PathTest() {
  delete m_pTestee;
}
 
// SetUp() will be called immediately before each test.
void PathTest::SetUp() {
}
 
// TearDown() will be called immediately after each test.
void PathTest::TearDown() {
}
 
//==========================================================================//
// Testcases
//
TEST_F(PathTest, should_exist) {
  std::string root(TOPDIR);
  root += "/test/lit.cfg";
  m_pTestee->assign(root);
  EXPECT_TRUE(exists(*m_pTestee));
 
  delete m_pTestee;
  m_pTestee = new Path(root);
  EXPECT_TRUE(exists(*m_pTestee));
}
 
TEST_F(PathTest, should_not_exist) {
  const std::string root = "/luck";
  m_pTestee->assign(root);
  EXPECT_FALSE(exists(*m_pTestee));
 
  delete m_pTestee;
  m_pTestee = new Path(root);
  EXPECT_FALSE(exists(*m_pTestee));
}
 
TEST_F(PathTest, should_is_directory) {
  const std::string root = "../././..";
  m_pTestee->assign(root);
  EXPECT_TRUE(exists(*m_pTestee));
  EXPECT_TRUE(is_directory(*m_pTestee));
  delete m_pTestee;
  m_pTestee = new Path(root);
  EXPECT_TRUE(exists(*m_pTestee));
  EXPECT_TRUE(is_directory(*m_pTestee));
}
 
TEST_F(PathTest, should_not_is_directory) {
  const std::string root = "/luck";
  m_pTestee->assign(root);
  EXPECT_FALSE(exists(*m_pTestee));
  EXPECT_FALSE(is_directory(*m_pTestee));
  delete m_pTestee;
  m_pTestee = new Path(root);
  EXPECT_FALSE(exists(*m_pTestee));
  EXPECT_FALSE(is_directory(*m_pTestee));
}
 
TEST_F(PathTest, should_equal) {
  const std::string root = "aaa/bbb/../../ccc/";
  m_pTestee->assign(root);
 
  Path* p2 = new Path("ccc///////");
 
  EXPECT_TRUE(*m_pTestee == *p2);
 
  delete m_pTestee;
  m_pTestee = new Path(root);
  EXPECT_TRUE(*m_pTestee == *m_pTestee);
  delete p2;
}
 
TEST_F(PathTest, should_not_equal) {
  const std::string root = "aa/";
  Path* p2 = new Path("aaa//");
  //  p2->assign(root);
  m_pTestee->assign(root);
  EXPECT_TRUE(*m_pTestee != *p2);
 
  delete m_pTestee;
  m_pTestee = new Path(root);
  EXPECT_TRUE(*m_pTestee != *p2);
  delete p2;
}
 
TEST_F(PathTest, append_success) {
  const std::string root = "aa/";
  m_pTestee->assign(root);
  m_pTestee->append("aaa");
  std::string a("aa/aaa");
  EXPECT_TRUE(m_pTestee->native() == "aa/aaa");
  delete m_pTestee;
  m_pTestee = new Path("aa/");
  m_pTestee->append("/aaa");
  EXPECT_TRUE(m_pTestee->native() == "aa/aaa");
  delete m_pTestee;
  m_pTestee = new Path("aa");
  m_pTestee->append("/aaa");
  EXPECT_TRUE(m_pTestee->native() == "aa/aaa");
  delete m_pTestee;
  m_pTestee = new Path("aa");
  m_pTestee->append("aaa");
  EXPECT_TRUE(m_pTestee->native() == "aa/aaa");
}
 
TEST_F(PathTest, should_become_generic_string) {
  m_pTestee->assign("/etc/../dev/../usr//lib//");
  EXPECT_STREQ("/usr/lib/", m_pTestee->generic_string().c_str());
}
 
TEST_F(PathTest, parent_path) {
  m_pTestee->assign("aa/bb/cc/dd");
  EXPECT_STREQ("aa/bb/cc", m_pTestee->parent_path().c_str());
  delete m_pTestee;
  m_pTestee = new Path("/aa/bb/");
  EXPECT_STREQ("/aa/bb", m_pTestee->parent_path().c_str());
  delete m_pTestee;
  m_pTestee = new Path("/aa/bb");
  EXPECT_STREQ("/aa", m_pTestee->parent_path().c_str());
  delete m_pTestee;
  m_pTestee = new Path("aa/");
  EXPECT_STREQ("aa", m_pTestee->parent_path().c_str());
  delete m_pTestee;
  m_pTestee = new Path("aa");
  EXPECT_TRUE(m_pTestee->parent_path().empty());
}
 
TEST_F(PathTest, filename) {
  m_pTestee->assign("aa/bb/cc");
  EXPECT_STREQ("cc", m_pTestee->filename().c_str());
 
  m_pTestee->assign("aa/bb/");
  EXPECT_STREQ("", m_pTestee->filename().c_str());
 
  m_pTestee->assign("aa");
  EXPECT_STREQ("aa", m_pTestee->filename().c_str());
}