Path.cpp revision 6d6d16a5fc054fb3f7032297d0a15abb659be7ef
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
15using namespace llvm;
16using namespace llvm::sys;
17
18namespace {
19
20TEST(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  int FileDescriptor;
123  SmallString<64> TempPath;
124  ASSERT_FALSE(fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
125
126  bool TempFileExists;
127  ASSERT_FALSE(sys::fs::exists(Twine(TempPath), TempFileExists));
128  EXPECT_TRUE(TempFileExists);
129
130  ::close(FileDescriptor);
131  ::remove(TempPath.c_str());
132
133  ASSERT_FALSE(fs::exists(Twine(TempPath), TempFileExists));
134  EXPECT_FALSE(TempFileExists);
135}
136
137} // anonymous namespace
138