When removing a function from the function set and adding it to deferred, we
[llvm.git] / unittests / Support / Path.cpp
blob1f67f13e97e45a14342d94f0ae75371c6f84ab52
1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/FileSystem.h"
11 #include "llvm/Support/PathV2.h"
12 #include "llvm/Support/ErrorHandling.h"
13 #include "llvm/Support/raw_ostream.h"
15 #include "gtest/gtest.h"
17 using namespace llvm;
18 using namespace llvm::sys;
20 #define ASSERT_NO_ERROR(x) \
21 if (error_code ASSERT_NO_ERROR_ec = x) { \
22 SmallString<128> MessageStorage; \
23 raw_svector_ostream Message(MessageStorage); \
24 Message << #x ": did not return errc::success.\n" \
25 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
26 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
27 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
28 } else {}
30 namespace {
32 TEST(Support, Path) {
33 SmallVector<StringRef, 40> paths;
34 paths.push_back("");
35 paths.push_back(".");
36 paths.push_back("..");
37 paths.push_back("foo");
38 paths.push_back("/");
39 paths.push_back("/foo");
40 paths.push_back("foo/");
41 paths.push_back("/foo/");
42 paths.push_back("foo/bar");
43 paths.push_back("/foo/bar");
44 paths.push_back("//net");
45 paths.push_back("//net/foo");
46 paths.push_back("///foo///");
47 paths.push_back("///foo///bar");
48 paths.push_back("/.");
49 paths.push_back("./");
50 paths.push_back("/..");
51 paths.push_back("../");
52 paths.push_back("foo/.");
53 paths.push_back("foo/..");
54 paths.push_back("foo/./");
55 paths.push_back("foo/./bar");
56 paths.push_back("foo/..");
57 paths.push_back("foo/../");
58 paths.push_back("foo/../bar");
59 paths.push_back("c:");
60 paths.push_back("c:/");
61 paths.push_back("c:foo");
62 paths.push_back("c:/foo");
63 paths.push_back("c:foo/");
64 paths.push_back("c:/foo/");
65 paths.push_back("c:/foo/bar");
66 paths.push_back("prn:");
67 paths.push_back("c:\\");
68 paths.push_back("c:foo");
69 paths.push_back("c:\\foo");
70 paths.push_back("c:foo\\");
71 paths.push_back("c:\\foo\\");
72 paths.push_back("c:\\foo/");
73 paths.push_back("c:/foo\\bar");
75 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
76 e = paths.end();
77 i != e;
78 ++i) {
79 for (sys::path::const_iterator ci = sys::path::begin(*i),
80 ce = sys::path::end(*i);
81 ci != ce;
82 ++ci) {
83 ASSERT_FALSE(ci->empty());
86 #if 0 // Valgrind is whining about this.
87 outs() << " Reverse Iteration: [";
88 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
89 ce = sys::path::rend(*i);
90 ci != ce;
91 ++ci) {
92 outs() << *ci << ',';
94 outs() << "]\n";
95 #endif
97 path::has_root_path(*i);
98 path::root_path(*i);
99 path::has_root_name(*i);
100 path::root_name(*i);
101 path::has_root_directory(*i);
102 path::root_directory(*i);
103 path::has_parent_path(*i);
104 path::parent_path(*i);
105 path::has_filename(*i);
106 path::filename(*i);
107 path::has_stem(*i);
108 path::stem(*i);
109 path::has_extension(*i);
110 path::extension(*i);
111 path::is_absolute(*i);
112 path::is_relative(*i);
114 SmallString<128> temp_store;
115 temp_store = *i;
116 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
117 temp_store = *i;
118 path::remove_filename(temp_store);
120 temp_store = *i;
121 path::replace_extension(temp_store, "ext");
122 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
123 stem = path::stem(filename);
124 ext = path::extension(filename);
125 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
127 path::native(*i, temp_store);
131 class FileSystemTest : public testing::Test {
132 protected:
133 /// Unique temporary directory in which all created filesystem entities must
134 /// be placed. It is recursively removed at the end of each test.
135 SmallString<128> TestDirectory;
137 virtual void SetUp() {
138 int fd;
139 ASSERT_NO_ERROR(
140 fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd,
141 TestDirectory));
142 // We don't care about this specific file.
143 ::close(fd);
144 TestDirectory = path::parent_path(TestDirectory);
145 errs() << "Test Directory: " << TestDirectory << '\n';
146 errs().flush();
149 virtual void TearDown() {
150 uint32_t removed;
151 ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
155 TEST_F(FileSystemTest, TempFiles) {
156 // Create a temp file.
157 int FileDescriptor;
158 SmallString<64> TempPath;
159 ASSERT_NO_ERROR(
160 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
162 // Make sure it exists.
163 bool TempFileExists;
164 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
165 EXPECT_TRUE(TempFileExists);
167 // Create another temp tile.
168 int FD2;
169 SmallString<64> TempPath2;
170 ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
171 ASSERT_NE(TempPath.str(), TempPath2.str());
173 // Try to copy the first to the second.
174 EXPECT_EQ(
175 fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
177 ::close(FD2);
178 // Try again with the proper options.
179 ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
180 fs::copy_option::overwrite_if_exists));
181 // Remove Temp2.
182 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
183 EXPECT_TRUE(TempFileExists);
185 // Make sure Temp2 doesn't exist.
186 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
187 EXPECT_FALSE(TempFileExists);
189 // Create a hard link to Temp1.
190 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
191 bool equal;
192 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
193 EXPECT_TRUE(equal);
195 // Remove Temp1.
196 ::close(FileDescriptor);
197 ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
198 EXPECT_TRUE(TempFileExists);
200 // Remove the hard link.
201 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
202 EXPECT_TRUE(TempFileExists);
204 // Make sure Temp1 doesn't exist.
205 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
206 EXPECT_FALSE(TempFileExists);
209 TEST_F(FileSystemTest, DirectoryIteration) {
210 error_code ec;
211 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
212 ASSERT_NO_ERROR(ec);
215 TEST_F(FileSystemTest, Magic) {
216 struct type {
217 const char *filename;
218 const char *magic_str;
219 size_t magic_str_len;
220 } types [] = {{"magic.archive", "!<arch>\x0A", 8}};
222 // Create some files filled with magic.
223 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
224 ++i) {
225 SmallString<128> file_pathname(TestDirectory);
226 path::append(file_pathname, i->filename);
227 std::string ErrMsg;
228 raw_fd_ostream file(file_pathname.c_str(), ErrMsg,
229 raw_fd_ostream::F_Binary);
230 ASSERT_FALSE(file.has_error());
231 StringRef magic(i->magic_str, i->magic_str_len);
232 file << magic;
233 file.close();
234 bool res = false;
235 ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res));
236 EXPECT_TRUE(res);
240 } // anonymous namespace