1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/FileSystem.h"
11 #include "llvm/Support/PathV2.h"
13 #include "gtest/gtest.h"
16 using namespace llvm::sys;
18 #define TEST_OUT(func, result) outs() << " " #func ": " << result << '\n';
20 #define TEST_PATH_(header, func, funcname, output) \
22 if (error_code ec = sys::path::func) \
23 ASSERT_FALSE(ec.message().c_str()); \
24 TEST_OUT(funcname, output)
26 #define TEST_PATH(func, ipath, res) TEST_PATH_(;, func(ipath, res), func, res);
28 #define TEST_PATH_SMALLVEC(func, ipath, inout) \
29 TEST_PATH_(inout = ipath, func(inout), func, inout)
31 #define TEST_PATH_SMALLVEC_P(func, ipath, inout, param) \
32 TEST_PATH_(inout = ipath, func(inout, param), func, inout)
37 SmallVector<StringRef, 40> paths;
40 paths.push_back("..");
41 paths.push_back("foo");
43 paths.push_back("/foo");
44 paths.push_back("foo/");
45 paths.push_back("/foo/");
46 paths.push_back("foo/bar");
47 paths.push_back("/foo/bar");
48 paths.push_back("//net");
49 paths.push_back("//net/foo");
50 paths.push_back("///foo///");
51 paths.push_back("///foo///bar");
52 paths.push_back("/.");
53 paths.push_back("./");
54 paths.push_back("/..");
55 paths.push_back("../");
56 paths.push_back("foo/.");
57 paths.push_back("foo/..");
58 paths.push_back("foo/./");
59 paths.push_back("foo/./bar");
60 paths.push_back("foo/..");
61 paths.push_back("foo/../");
62 paths.push_back("foo/../bar");
63 paths.push_back("c:");
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/bar");
70 paths.push_back("prn:");
71 paths.push_back("c:\\");
72 paths.push_back("c:foo");
73 paths.push_back("c:\\foo");
74 paths.push_back("c:foo\\");
75 paths.push_back("c:\\foo\\");
76 paths.push_back("c:\\foo/");
77 paths.push_back("c:/foo\\bar");
79 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
83 outs() << *i << " =>\n Iteration: [";
84 for (sys::path::const_iterator ci = sys::path::begin(*i),
85 ce = sys::path::end(*i);
92 #if 0 // Valgrind is whining about this.
93 outs() << " Reverse Iteration: [";
94 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
95 ce = sys::path::rend(*i);
105 TEST_PATH(has_root_path, *i, bres);
106 TEST_PATH(root_path, *i, sfres);
107 TEST_PATH(has_root_name, *i, bres);
108 TEST_PATH(root_name, *i, sfres);
109 TEST_PATH(has_root_directory, *i, bres);
110 TEST_PATH(root_directory, *i, sfres);
111 TEST_PATH(has_parent_path, *i, bres);
112 TEST_PATH(parent_path, *i, sfres);
113 TEST_PATH(has_filename, *i, bres);
114 TEST_PATH(filename, *i, sfres);
115 TEST_PATH(has_stem, *i, bres);
116 TEST_PATH(stem, *i, sfres);
117 TEST_PATH(has_extension, *i, bres);
118 TEST_PATH(extension, *i, sfres);
119 TEST_PATH(is_absolute, *i, bres);
120 TEST_PATH(is_relative, *i, bres);
122 SmallString<16> temp_store;
123 TEST_PATH_SMALLVEC(make_absolute, *i, temp_store);
124 TEST_PATH_SMALLVEC(remove_filename, *i, temp_store);
126 TEST_PATH_SMALLVEC_P(replace_extension, *i, temp_store, "ext");
127 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
128 TEST_PATH(stem, filename, stem);
129 TEST_PATH(extension, filename, ext);
130 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
132 TEST_PATH_(;, native(*i, temp_store), native, temp_store);
138 SmallString<64> TempPath;
139 if (error_code ec = sys::fs::unique_file("%%-%%-%%-%%.temp",
140 FileDescriptor, TempPath))
141 ASSERT_FALSE(ec.message().c_str());
144 ASSERT_FALSE(sys::fs::exists(Twine(TempPath), TempFileExists));
145 EXPECT_TRUE(TempFileExists);
147 ::close(FileDescriptor);
148 ::remove(TempPath.begin());
150 ASSERT_FALSE(fs::exists(Twine(TempPath), TempFileExists));
151 // FIXME: This is returning true on some systems...
152 // EXPECT_FALSE(TempFileExists);
155 } // anonymous namespace