blob: 3f50e708e646a9be8e61a7bf0d1a6698ac8e930b [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. Spencer1d389622010-12-01 06:03:33 +000016#define TEST_OUT(func, result) outs() << " " #func ": " << result << '\n';
17
18#define TEST_PATH_(header, func, funcname, output) \
19 header; \
20 if (error_code ec = sys::path::func) \
21 ASSERT_FALSE(ec.message().c_str()); \
22 TEST_OUT(funcname, output)
23
24#define TEST_PATH(func, ipath, res) TEST_PATH_(, func(ipath, res), func, res);
25
26#define TEST_PATH_SMALLVEC(func, ipath, inout) \
27 TEST_PATH_(inout = ipath, func(inout), func, inout)
28
29#define TEST_PATH_SMALLVEC_P(func, ipath, inout, param) \
30 TEST_PATH_(inout = ipath, func(inout, param), func, inout)
31
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +000032namespace {
33
Michael J. Spencer013d15a2010-11-29 22:29:04 +000034TEST(Support, Path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000035 SmallVector<StringRef, 40> paths;
36 paths.push_back("");
37 paths.push_back(".");
38 paths.push_back("..");
39 paths.push_back("foo");
40 paths.push_back("/");
41 paths.push_back("/foo");
42 paths.push_back("foo/");
43 paths.push_back("/foo/");
44 paths.push_back("foo/bar");
45 paths.push_back("/foo/bar");
46 paths.push_back("//net");
47 paths.push_back("//net/foo");
48 paths.push_back("///foo///");
49 paths.push_back("///foo///bar");
50 paths.push_back("/.");
51 paths.push_back("./");
52 paths.push_back("/..");
53 paths.push_back("../");
54 paths.push_back("foo/.");
55 paths.push_back("foo/..");
56 paths.push_back("foo/./");
57 paths.push_back("foo/./bar");
58 paths.push_back("foo/..");
59 paths.push_back("foo/../");
60 paths.push_back("foo/../bar");
61 paths.push_back("c:");
62 paths.push_back("c:/");
63 paths.push_back("c:foo");
64 paths.push_back("c:/foo");
65 paths.push_back("c:foo/");
66 paths.push_back("c:/foo/");
67 paths.push_back("c:/foo/bar");
68 paths.push_back("prn:");
69 paths.push_back("c:\\");
70 paths.push_back("c:foo");
71 paths.push_back("c:\\foo");
72 paths.push_back("c:foo\\");
73 paths.push_back("c:\\foo\\");
74 paths.push_back("c:\\foo/");
75 paths.push_back("c:/foo\\bar");
76
77 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
78 e = paths.end();
79 i != e;
80 ++i) {
81 outs() << *i << " =>\n Iteration: [";
82 for (sys::path::const_iterator ci = sys::path::begin(*i),
83 ce = sys::path::end(*i);
84 ci != ce;
85 ++ci) {
86 outs() << *ci << ',';
87 }
88 outs() << "]\n";
89
Michael J. Spencera42cf732010-11-30 23:28:07 +000090 outs() << " Reverse Iteration: [";
91 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
92 ce = sys::path::rend(*i);
93 ci != ce;
94 ++ci) {
95 outs() << *ci << ',';
96 }
97 outs() << "]\n";
98
Michael J. Spencerae180082010-12-01 06:03:50 +000099 bool bres;
Michael J. Spencer1d389622010-12-01 06:03:33 +0000100 StringRef sfres;
Michael J. Spencerae180082010-12-01 06:03:50 +0000101 TEST_PATH(has_root_path, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000102 TEST_PATH(root_path, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000103 TEST_PATH(has_root_name, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000104 TEST_PATH(root_name, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000105 TEST_PATH(has_root_directory, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000106 TEST_PATH(root_directory, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000107 TEST_PATH(has_parent_path, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000108 TEST_PATH(parent_path, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000109 TEST_PATH(has_filename, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000110 TEST_PATH(filename, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000111 TEST_PATH(has_stem, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000112 TEST_PATH(stem, *i, sfres);
Michael J. Spencerae180082010-12-01 06:03:50 +0000113 TEST_PATH(has_extension, *i, bres);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000114 TEST_PATH(extension, *i, sfres);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000115
Michael J. Spencer1d389622010-12-01 06:03:33 +0000116 SmallString<16> temp_store;
117 TEST_PATH_SMALLVEC(make_absolute, *i, temp_store);
118 TEST_PATH_SMALLVEC(remove_filename, *i, temp_store);
119
120 TEST_PATH_SMALLVEC_P(replace_extension, *i, temp_store, "ext");
121 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
122 TEST_PATH(stem, filename, stem);
123 TEST_PATH(extension, filename, ext);
124 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
125
126 TEST_PATH_(, native(*i, temp_store), native, temp_store);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000127
128 outs().flush();
129 }
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +0000130}
131
132} // anonymous namespace