Unittests/Support/PathV2: Add FileSystem tests.
[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
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16 using namespace llvm::sys;
17
18 namespace {
19
20 TEST(Support, Path) {
21   SmallVector<StringRef, 40> paths;
22   paths.push_back("");
23   paths.push_back(".");
24   paths.push_back("..");
25   paths.push_back("foo");
26   paths.push_back("/");
27   paths.push_back("/foo");
28   paths.push_back("foo/");
29   paths.push_back("/foo/");
30   paths.push_back("foo/bar");
31   paths.push_back("/foo/bar");
32   paths.push_back("//net");
33   paths.push_back("//net/foo");
34   paths.push_back("///foo///");
35   paths.push_back("///foo///bar");
36   paths.push_back("/.");
37   paths.push_back("./");
38   paths.push_back("/..");
39   paths.push_back("../");
40   paths.push_back("foo/.");
41   paths.push_back("foo/..");
42   paths.push_back("foo/./");
43   paths.push_back("foo/./bar");
44   paths.push_back("foo/..");
45   paths.push_back("foo/../");
46   paths.push_back("foo/../bar");
47   paths.push_back("c:");
48   paths.push_back("c:/");
49   paths.push_back("c:foo");
50   paths.push_back("c:/foo");
51   paths.push_back("c:foo/");
52   paths.push_back("c:/foo/");
53   paths.push_back("c:/foo/bar");
54   paths.push_back("prn:");
55   paths.push_back("c:\\");
56   paths.push_back("c:foo");
57   paths.push_back("c:\\foo");
58   paths.push_back("c:foo\\");
59   paths.push_back("c:\\foo\\");
60   paths.push_back("c:\\foo/");
61   paths.push_back("c:/foo\\bar");
62
63   for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
64                                                   e = paths.end();
65                                                   i != e;
66                                                   ++i) {
67     for (sys::path::const_iterator ci = sys::path::begin(*i),
68                                    ce = sys::path::end(*i);
69                                    ci != ce;
70                                    ++ci) {
71       ASSERT_FALSE(ci->empty());
72     }
73
74 #if 0 // Valgrind is whining about this.
75     outs() << "    Reverse Iteration: [";
76     for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
77                                      ce = sys::path::rend(*i);
78                                      ci != ce;
79                                      ++ci) {
80       outs() << *ci << ',';
81     }
82     outs() << "]\n";
83 #endif
84
85     bool      bres;
86     StringRef sfres;
87     ASSERT_FALSE(path::has_root_path(*i, bres));
88     ASSERT_FALSE(path::root_path(*i, sfres));
89     ASSERT_FALSE(path::has_root_name(*i, bres));
90     ASSERT_FALSE(path::root_name(*i, sfres));
91     ASSERT_FALSE(path::has_root_directory(*i, bres));
92     ASSERT_FALSE(path::root_directory(*i, sfres));
93     ASSERT_FALSE(path::has_parent_path(*i, bres));
94     ASSERT_FALSE(path::parent_path(*i, sfres));
95     ASSERT_FALSE(path::has_filename(*i, bres));
96     ASSERT_FALSE(path::filename(*i, sfres));
97     ASSERT_FALSE(path::has_stem(*i, bres));
98     ASSERT_FALSE(path::stem(*i, sfres));
99     ASSERT_FALSE(path::has_extension(*i, bres));
100     ASSERT_FALSE(path::extension(*i, sfres));
101     ASSERT_FALSE(path::is_absolute(*i, bres));
102     ASSERT_FALSE(path::is_relative(*i, bres));
103
104     SmallString<16> temp_store;
105     temp_store = *i;
106     ASSERT_FALSE(path::make_absolute(temp_store));
107     temp_store = *i;
108     ASSERT_FALSE(path::remove_filename(temp_store));
109
110     temp_store = *i;
111     ASSERT_FALSE(path::replace_extension(temp_store, "ext"));
112     StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
113     ASSERT_FALSE(path::stem(filename, stem));
114     ASSERT_FALSE(path::extension(filename, ext));
115     EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
116
117     ASSERT_FALSE(path::native(*i, temp_store));
118
119     outs().flush();
120   }
121
122   // Create a temp file.
123   int FileDescriptor;
124   SmallString<64> TempPath;
125   ASSERT_FALSE(fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
126
127   // Make sure it exists.
128   bool TempFileExists;
129   ASSERT_FALSE(sys::fs::exists(Twine(TempPath), TempFileExists));
130   EXPECT_TRUE(TempFileExists);
131
132   // Create another temp tile.
133   int FD2;
134   SmallString<64> TempPath2;
135   ASSERT_FALSE(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
136   ASSERT_NE(TempPath.str(), TempPath2.str());
137
138   // Try to copy the first to the second.
139   EXPECT_EQ(fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
140
141   ::close(FD2);
142   // Try again with the proper options.
143   ASSERT_FALSE(fs::copy_file(Twine(TempPath), Twine(TempPath2),
144                              fs::copy_option::overwrite_if_exists));
145   // Remove Temp2.
146   ASSERT_FALSE(fs::remove(Twine(TempPath2), TempFileExists));
147   EXPECT_TRUE(TempFileExists);
148
149   // Make sure Temp2 doesn't exist.
150   ASSERT_FALSE(fs::exists(Twine(TempPath2), TempFileExists));
151   EXPECT_FALSE(TempFileExists);
152
153   // Create a hard link to Temp1.
154   ASSERT_FALSE(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
155   bool equal;
156   ASSERT_FALSE(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
157   EXPECT_TRUE(equal);
158
159   // Remove Temp1.
160   ::close(FileDescriptor);
161   ASSERT_FALSE(fs::remove(Twine(TempPath), TempFileExists));
162   EXPECT_TRUE(TempFileExists);
163
164   // Remove the hard link.
165   ASSERT_FALSE(fs::remove(Twine(TempPath2), TempFileExists));
166   EXPECT_TRUE(TempFileExists);
167
168   // Make sure Temp1 doesn't exist.
169   ASSERT_FALSE(fs::exists(Twine(TempPath), TempFileExists));
170   EXPECT_FALSE(TempFileExists);
171 }
172
173 } // anonymous namespace