blob: d19bf4a2efc8ff5f94704bfeff2f5b77f6e0c86b [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. Spencerf2ca4cb2010-11-24 19:20:05 +000018namespace {
19
Michael J. Spencer013d15a2010-11-29 22:29:04 +000020TEST(Support, Path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000021 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) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000067 for (sys::path::const_iterator ci = sys::path::begin(*i),
68 ce = sys::path::end(*i);
69 ci != ce;
70 ++ci) {
Michael J. Spencer371716c2010-12-03 02:22:34 +000071 ASSERT_FALSE(ci->empty());
Michael J. Spencerdffde992010-11-29 22:28:51 +000072 }
Michael J. Spencerdffde992010-11-29 22:28:51 +000073
Michael J. Spencer506e5792010-12-01 22:28:42 +000074#if 0 // Valgrind is whining about this.
Michael J. Spencera42cf732010-11-30 23:28:07 +000075 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";
Michael J. Spencer506e5792010-12-01 22:28:42 +000083#endif
Michael J. Spencera42cf732010-11-30 23:28:07 +000084
Michael J. Spencerae180082010-12-01 06:03:50 +000085 bool bres;
Michael J. Spencer1d389622010-12-01 06:03:33 +000086 StringRef sfres;
Michael J. Spencer371716c2010-12-03 02:22:34 +000087 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));
Michael J. Spencerdffde992010-11-29 22:28:51 +0000103
Michael J. Spencer1d389622010-12-01 06:03:33 +0000104 SmallString<16> temp_store;
Michael J. Spencer371716c2010-12-03 02:22:34 +0000105 temp_store = *i;
106 ASSERT_FALSE(path::make_absolute(temp_store));
107 temp_store = *i;
108 ASSERT_FALSE(path::remove_filename(temp_store));
Michael J. Spencer1d389622010-12-01 06:03:33 +0000109
Michael J. Spencer371716c2010-12-03 02:22:34 +0000110 temp_store = *i;
111 ASSERT_FALSE(path::replace_extension(temp_store, "ext"));
Michael J. Spencer1d389622010-12-01 06:03:33 +0000112 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
Michael J. Spencer371716c2010-12-03 02:22:34 +0000113 ASSERT_FALSE(path::stem(filename, stem));
114 ASSERT_FALSE(path::extension(filename, ext));
Michael J. Spencer1d389622010-12-01 06:03:33 +0000115 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
116
Michael J. Spencer371716c2010-12-03 02:22:34 +0000117 ASSERT_FALSE(path::native(*i, temp_store));
Michael J. Spencerdffde992010-11-29 22:28:51 +0000118
119 outs().flush();
120 }
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000121
122 int FileDescriptor;
123 SmallString<64> TempPath;
Michael J. Spencer371716c2010-12-03 02:22:34 +0000124 ASSERT_FALSE(fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000125
126 bool TempFileExists;
127 ASSERT_FALSE(sys::fs::exists(Twine(TempPath), TempFileExists));
128 EXPECT_TRUE(TempFileExists);
129
130 ::close(FileDescriptor);
Benjamin Kramer6d6d16a2010-12-03 12:33:32 +0000131 ::remove(TempPath.c_str());
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000132
133 ASSERT_FALSE(fs::exists(Twine(TempPath), TempFileExists));
Benjamin Kramer6d6d16a2010-12-03 12:33:32 +0000134 EXPECT_FALSE(TempFileExists);
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +0000135}
136
137} // anonymous namespace