blob: ecf818b5d458d38b366731e70adf54d32e5cc2f2 [file] [log] [blame]
Michael J. Spencer013d15a2010-11-29 22:29:04 +00001//===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +00002//
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
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000010#include "llvm/Support/FileSystem.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000011#include "llvm/Support/PathV2.h"
Michael J. Spencer861ef4b2010-11-24 19:20:28 +000012
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +000013#include "gtest/gtest.h"
14
Michael J. Spencerdffde992010-11-29 22:28:51 +000015using namespace llvm;
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000016using namespace llvm::sys;
Michael J. Spencerdffde992010-11-29 22:28:51 +000017
Michael J. Spencer1d389622010-12-01 06:03:33 +000018#define TEST_OUT(func, result) outs() << " " #func ": " << result << '\n';
19
20#define TEST_PATH_(header, func, funcname, output) \
21 header; \
22 if (error_code ec = sys::path::func) \
23 ASSERT_FALSE(ec.message().c_str()); \
24 TEST_OUT(funcname, output)
25
Michael J. Spencereb21e3c2010-12-01 19:23:49 +000026#define TEST_PATH(func, ipath, res) TEST_PATH_(;, func(ipath, res), func, res);
Michael J. Spencer1d389622010-12-01 06:03:33 +000027
28#define TEST_PATH_SMALLVEC(func, ipath, inout) \
29 TEST_PATH_(inout = ipath, func(inout), func, inout)
30
31#define TEST_PATH_SMALLVEC_P(func, ipath, inout, param) \
32 TEST_PATH_(inout = ipath, func(inout, param), func, inout)
33
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +000034namespace {
35
Michael J. Spencer013d15a2010-11-29 22:29:04 +000036TEST(Support, Path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000037 SmallVector<StringRef, 40> paths;
38 paths.push_back("");
39 paths.push_back(".");
40 paths.push_back("..");
41 paths.push_back("foo");
42 paths.push_back("/");
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");
78
79 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
80 e = paths.end();
81 i != e;
82 ++i) {
83 outs() << *i << " =>\n Iteration: [";
84 for (sys::path::const_iterator ci = sys::path::begin(*i),
85 ce = sys::path::end(*i);
86 ci != ce;
87 ++ci) {
88 outs() << *ci << ',';
89 }
90 outs() << "]\n";
91
Michael J. Spencer506e5792010-12-01 22:28:42 +000092#if 0 // Valgrind is whining about this.
Michael J. Spencera42cf732010-11-30 23:28:07 +000093 outs() << " Reverse Iteration: [";
94 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
95 ce = sys::path::rend(*i);
96 ci != ce;
97 ++ci) {
98 outs() << *ci << ',';
99 }
100 outs() << "]\n";
Michael J. Spencer506e5792010-12-01 22:28:42 +0000101#endif
Michael J. Spencera42cf732010-11-30 23:28:07 +0000102
Michael J. Spencerae180082010-12-01 06:03:50 +0000103 bool bres;
Michael J. Spencer1d389622010-12-01 06:03:33 +0000104 StringRef sfres;
Michael J. Spencerae180082010-12-01 06:03:50 +0000105 TEST_PATH(has_root_path, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000106 TEST_PATH(root_path, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000107 TEST_PATH(has_root_name, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000108 TEST_PATH(root_name, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000109 TEST_PATH(has_root_directory, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000110 TEST_PATH(root_directory, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000111 TEST_PATH(has_parent_path, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000112 TEST_PATH(parent_path, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000113 TEST_PATH(has_filename, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000114 TEST_PATH(filename, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000115 TEST_PATH(has_stem, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000116 TEST_PATH(stem, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000117 TEST_PATH(has_extension, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000118 TEST_PATH(extension, *i, sfres);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000119 TEST_PATH(is_absolute, *i, bres);
120 TEST_PATH(is_relative, *i, bres);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000121
Michael J. Spencer1d389622010-12-01 06:03:33 +0000122 SmallString<16> temp_store;
123 TEST_PATH_SMALLVEC(make_absolute, *i, temp_store);
124 TEST_PATH_SMALLVEC(remove_filename, *i, temp_store);
125
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());
131
Michael J. Spencereb21e3c2010-12-01 19:23:49 +0000132 TEST_PATH_(;, native(*i, temp_store), native, temp_store);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000133
134 outs().flush();
135 }
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000136
137 int FileDescriptor;
138 SmallString<64> TempPath;
139 if (error_code ec = sys::fs::unique_file("%%-%%-%%-%%.temp",
140 FileDescriptor, TempPath))
141 ASSERT_FALSE(ec.message().c_str());
142
143 bool TempFileExists;
144 ASSERT_FALSE(sys::fs::exists(Twine(TempPath), TempFileExists));
145 EXPECT_TRUE(TempFileExists);
146
147 ::close(FileDescriptor);
148 ::remove(TempPath.begin());
149
150 ASSERT_FALSE(fs::exists(Twine(TempPath), TempFileExists));
151 EXPECT_FALSE(TempFileExists);
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +0000152}
153
154} // anonymous namespace