blob: 70cf213f1917a69de59a9fcc85dc60874bb14dec [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. Spencer1f6efa32010-11-29 18:16:10 +000010#include "llvm/Support/PathV2.h"
Michael J. Spencer861ef4b2010-11-24 19:20:28 +000011
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +000012#include "gtest/gtest.h"
13
Michael J. Spencerdffde992010-11-29 22:28:51 +000014using namespace llvm;
15
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +000016namespace {
17
Michael J. Spencer013d15a2010-11-29 22:29:04 +000018TEST(Support, Path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000019 SmallVector<StringRef, 40> paths;
20 paths.push_back("");
21 paths.push_back(".");
22 paths.push_back("..");
23 paths.push_back("foo");
24 paths.push_back("/");
25 paths.push_back("/foo");
26 paths.push_back("foo/");
27 paths.push_back("/foo/");
28 paths.push_back("foo/bar");
29 paths.push_back("/foo/bar");
30 paths.push_back("//net");
31 paths.push_back("//net/foo");
32 paths.push_back("///foo///");
33 paths.push_back("///foo///bar");
34 paths.push_back("/.");
35 paths.push_back("./");
36 paths.push_back("/..");
37 paths.push_back("../");
38 paths.push_back("foo/.");
39 paths.push_back("foo/..");
40 paths.push_back("foo/./");
41 paths.push_back("foo/./bar");
42 paths.push_back("foo/..");
43 paths.push_back("foo/../");
44 paths.push_back("foo/../bar");
45 paths.push_back("c:");
46 paths.push_back("c:/");
47 paths.push_back("c:foo");
48 paths.push_back("c:/foo");
49 paths.push_back("c:foo/");
50 paths.push_back("c:/foo/");
51 paths.push_back("c:/foo/bar");
52 paths.push_back("prn:");
53 paths.push_back("c:\\");
54 paths.push_back("c:foo");
55 paths.push_back("c:\\foo");
56 paths.push_back("c:foo\\");
57 paths.push_back("c:\\foo\\");
58 paths.push_back("c:\\foo/");
59 paths.push_back("c:/foo\\bar");
60
61 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
62 e = paths.end();
63 i != e;
64 ++i) {
65 outs() << *i << " =>\n Iteration: [";
66 for (sys::path::const_iterator ci = sys::path::begin(*i),
67 ce = sys::path::end(*i);
68 ci != ce;
69 ++ci) {
70 outs() << *ci << ',';
71 }
72 outs() << "]\n";
73
Michael J. Spencera42cf732010-11-30 23:28:07 +000074 outs() << " Reverse Iteration: [";
75 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
76 ce = sys::path::rend(*i);
77 ci != ce;
78 ++ci) {
79 outs() << *ci << ',';
80 }
81 outs() << "]\n";
82
Michael J. Spencerdffde992010-11-29 22:28:51 +000083 StringRef res;
84 SmallString<16> temp_store;
Michael J. Spencer1f986402010-11-29 23:35:49 +000085 if (error_code ec = sys::path::root_path(*i, res))
86 ASSERT_FALSE(ec.message().c_str());
Michael J. Spencerdffde992010-11-29 22:28:51 +000087 outs() << " root_path: " << res << '\n';
Michael J. Spencer1f986402010-11-29 23:35:49 +000088 if (error_code ec = sys::path::root_name(*i, res))
89 ASSERT_FALSE(ec.message().c_str());
Michael J. Spencerdffde992010-11-29 22:28:51 +000090 outs() << " root_name: " << res << '\n';
Michael J. Spencer1f986402010-11-29 23:35:49 +000091 if (error_code ec = sys::path::root_directory(*i, res))
92 ASSERT_FALSE(ec.message().c_str());
Michael J. Spencerdffde992010-11-29 22:28:51 +000093 outs() << " root_directory: " << res << '\n';
Michael J. Spencera42cf732010-11-30 23:28:07 +000094 if (error_code ec = sys::path::parent_path(*i, res))
95 ASSERT_FALSE(ec.message().c_str());
96 outs() << " parent_path: " << res << '\n';
Michael J. Spencera9793552010-12-01 03:18:17 +000097 if (error_code ec = sys::path::filename(*i, res))
98 ASSERT_FALSE(ec.message().c_str());
99 outs() << " filename: " << res << '\n';
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000100 if (error_code ec = sys::path::stem(*i, res))
101 ASSERT_FALSE(ec.message().c_str());
102 outs() << " stem: " << res << '\n';
Michael J. Spencerdffde992010-11-29 22:28:51 +0000103
104 temp_store = *i;
Michael J. Spencer1f986402010-11-29 23:35:49 +0000105 if (error_code ec = sys::path::make_absolute(temp_store))
106 ASSERT_FALSE(ec.message().c_str());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000107 outs() << " make_absolute: " << temp_store << '\n';
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000108 temp_store = *i;
109 if (error_code ec = sys::path::remove_filename(temp_store))
110 ASSERT_FALSE(ec.message().c_str());
111 outs() << " remove_filename: " << temp_store << '\n';
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000112 temp_store = *i;
113 if (error_code ec = sys::path::replace_extension(temp_store, "ext"))
114 ASSERT_FALSE(ec.message().c_str());
115 outs() << " replace_extension: " << temp_store << '\n';
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000116 if (error_code ec = sys::path::stem(
117 StringRef(temp_store.begin(), temp_store.size()), res))
118 ASSERT_FALSE(ec.message().c_str());
119 outs() << " stem: " << res << '\n';
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000120 if (error_code ec = sys::path::native(*i, temp_store))
121 ASSERT_FALSE(ec.message().c_str());
122 outs() << " native: " << temp_store << '\n';
Michael J. Spencerdffde992010-11-29 22:28:51 +0000123
124 outs().flush();
125 }
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +0000126}
127
128} // anonymous namespace