132d43950b3199a53f27e3f2f6804e2f687f4422
[oota-llvm.git] / unittests / Support / Path.cpp
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 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Support/FileSystem.h"
11 #include "llvm/Support/PathV2.h"
12 #include "llvm/Support/ErrorHandling.h"
13
14 #include "gtest/gtest.h"
15
16 using namespace llvm;
17 using namespace llvm::sys;
18
19 #define ASSERT_NO_ERROR(x) \
20   if (error_code ec = x) { \
21     SmallString<128> Message; \
22     GTEST_FATAL_FAILURE_((Twine(#x) + ": did not return errc::success.\n" + \
23                          "error message: " + \
24                       x.message()).toNullTerminatedStringRef(Message).data()); \
25   } else {}
26
27 namespace {
28
29 TEST(Support, Path) {
30   SmallVector<StringRef, 40> paths;
31   paths.push_back("");
32   paths.push_back(".");
33   paths.push_back("..");
34   paths.push_back("foo");
35   paths.push_back("/");
36   paths.push_back("/foo");
37   paths.push_back("foo/");
38   paths.push_back("/foo/");
39   paths.push_back("foo/bar");
40   paths.push_back("/foo/bar");
41   paths.push_back("//net");
42   paths.push_back("//net/foo");
43   paths.push_back("///foo///");
44   paths.push_back("///foo///bar");
45   paths.push_back("/.");
46   paths.push_back("./");
47   paths.push_back("/..");
48   paths.push_back("../");
49   paths.push_back("foo/.");
50   paths.push_back("foo/..");
51   paths.push_back("foo/./");
52   paths.push_back("foo/./bar");
53   paths.push_back("foo/..");
54   paths.push_back("foo/../");
55   paths.push_back("foo/../bar");
56   paths.push_back("c:");
57   paths.push_back("c:/");
58   paths.push_back("c:foo");
59   paths.push_back("c:/foo");
60   paths.push_back("c:foo/");
61   paths.push_back("c:/foo/");
62   paths.push_back("c:/foo/bar");
63   paths.push_back("prn:");
64   paths.push_back("c:\\");
65   paths.push_back("c:foo");
66   paths.push_back("c:\\foo");
67   paths.push_back("c:foo\\");
68   paths.push_back("c:\\foo\\");
69   paths.push_back("c:\\foo/");
70   paths.push_back("c:/foo\\bar");
71
72   for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
73                                                   e = paths.end();
74                                                   i != e;
75                                                   ++i) {
76     for (sys::path::const_iterator ci = sys::path::begin(*i),
77                                    ce = sys::path::end(*i);
78                                    ci != ce;
79                                    ++ci) {
80       ASSERT_FALSE(ci->empty());
81     }
82
83 #if 0 // Valgrind is whining about this.
84     outs() << "    Reverse Iteration: [";
85     for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
86                                      ce = sys::path::rend(*i);
87                                      ci != ce;
88                                      ++ci) {
89       outs() << *ci << ',';
90     }
91     outs() << "]\n";
92 #endif
93
94     path::has_root_path(*i);
95     path::root_path(*i);
96     path::has_root_name(*i);
97     path::root_name(*i);
98     path::has_root_directory(*i);
99     path::root_directory(*i);
100     path::has_parent_path(*i);
101     path::parent_path(*i);
102     path::has_filename(*i);
103     path::filename(*i);
104     path::has_stem(*i);
105     path::stem(*i);
106     path::has_extension(*i);
107     path::extension(*i);
108     path::is_absolute(*i);
109     path::is_relative(*i);
110
111     SmallString<128> temp_store;
112     temp_store = *i;
113     ASSERT_NO_ERROR(fs::make_absolute(temp_store));
114     temp_store = *i;
115     path::remove_filename(temp_store);
116
117     temp_store = *i;
118     path::replace_extension(temp_store, "ext");
119     StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
120     stem = path::stem(filename);
121     ext  = path::extension(filename);
122     EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
123
124     path::native(*i, temp_store);
125   }
126 }
127
128 class FileSystemTest : public testing::Test {
129 protected:
130   /// Unique temporary directory in which all created filesystem entities must
131   /// be placed. It is recursively removed at the end of each test.
132   SmallString<128> TestDirectory;
133
134   virtual void SetUp() {
135     /*int fd;
136     ASSERT_NO_ERROR(
137       fs::unique_file("%%-%%-%%-%%/test-directory.anchor", fd, TestDirectory));
138     // We don't care about this specific file.
139     ::close(fd);*/
140   }
141
142   virtual void TearDown() {
143     /*uint32_t removed;
144     ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));*/
145   }
146 };
147
148 TEST_F(FileSystemTest, TempFiles) {
149   // Create a temp file.
150   int FileDescriptor;
151   SmallString<64> TempPath;
152   ASSERT_NO_ERROR(
153     fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
154
155   // Make sure it exists.
156   bool TempFileExists;
157   ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
158   EXPECT_TRUE(TempFileExists);
159
160   // Create another temp tile.
161   int FD2;
162   SmallString<64> TempPath2;
163   ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
164   ASSERT_NE(TempPath.str(), TempPath2.str());
165
166   // Try to copy the first to the second.
167   EXPECT_EQ(
168     fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
169
170   ::close(FD2);
171   // Try again with the proper options.
172   ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
173                                 fs::copy_option::overwrite_if_exists));
174   // Remove Temp2.
175   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
176   EXPECT_TRUE(TempFileExists);
177
178   // Make sure Temp2 doesn't exist.
179   ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
180   EXPECT_FALSE(TempFileExists);
181
182   // Create a hard link to Temp1.
183   ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
184   bool equal;
185   ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
186   EXPECT_TRUE(equal);
187
188   // Remove Temp1.
189   ::close(FileDescriptor);
190   ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
191   EXPECT_TRUE(TempFileExists);
192
193   // Remove the hard link.
194   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
195   EXPECT_TRUE(TempFileExists);
196
197   // Make sure Temp1 doesn't exist.
198   ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
199   EXPECT_FALSE(TempFileExists);
200 }
201
202 TEST_F(FileSystemTest, DirectoryIteration) {
203   error_code ec;
204   for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) {
205     if (ec) {
206       errs() << ec.message() << '\n';
207       errs().flush();
208       report_fatal_error("Directory iteration failed!");
209     }
210   }
211 }
212
213 } // anonymous namespace