path.decompose.pass.cpp revision 6e9a694dce70319e60dbdfb09cf055bacb4c948e
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// UNSUPPORTED: c++98, c++03
11
12// <experimental/filesystem>
13
14// class path
15
16// 8.4.9 path decomposition [path.decompose]
17//------------------------------------------
18// path root_name() const;
19// path root_directory() const;
20// path root_path() const;
21// path relative_path() const;
22// path parent_path() const;
23// path filename() const;
24// path stem() const;
25// path extension() const;
26//-------------------------------
27// 8.4.10 path query [path.query]
28//-------------------------------
29// bool empty() const noexcept;
30// bool has_root_path() const;
31// bool has_root_name() const;
32// bool has_root_directory() const;
33// bool has_relative_path() const;
34// bool has_parent_path() const;
35// bool has_filename() const;
36// bool has_stem() const;
37// bool has_extension() const;
38// bool is_absolute() const;
39// bool is_relative() const;
40//-------------------------------
41// 8.5 path iterators [path.itr]
42//-------------------------------
43// iterator begin() const;
44// iterator end() const;
45
46
47#include <experimental/filesystem>
48#include <type_traits>
49#include <vector>
50#include <cassert>
51
52#include "test_macros.h"
53#include "test_iterators.h"
54#include "count_new.hpp"
55#include "filesystem_test_helper.hpp"
56
57template <class It>
58std::reverse_iterator<It> mkRev(It it) {
59  return std::reverse_iterator<It>(it);
60}
61
62
63namespace fs = std::experimental::filesystem;
64struct PathDecomposeTestcase
65{
66    std::string raw;
67    std::vector<std::string> elements;
68    std::string root_path;
69    std::string root_name;
70    std::string root_directory;
71    std::string relative_path;
72    std::string parent_path;
73    std::string filename;
74};
75
76const PathDecomposeTestcase PathTestCases[] =
77  {
78      {"", {}, "", "", "", "", "", ""}
79    , {".", {"."}, "", "", "", ".", "", "."}
80    , {"..", {".."}, "", "", "", "..", "", ".."}
81    , {"foo", {"foo"}, "", "", "", "foo", "", "foo"}
82    , {"/", {"/"}, "/", "", "/", "", "", "/"}
83    , {"/foo", {"/", "foo"}, "/", "", "/", "foo", "/", "foo"}
84    , {"foo/", {"foo", "."}, "", "", "", "foo/", "foo", "."}
85    , {"/foo/", {"/", "foo", "."}, "/", "", "/", "foo/", "/foo", "."}
86    , {"foo/bar", {"foo","bar"}, "",  "", "",  "foo/bar", "foo", "bar"}
87    , {"/foo//bar", {"/","foo","bar"}, "/", "", "/", "foo/bar", "/foo", "bar"}
88    , {"//net", {"//net"}, "//net", "//net", "", "", "", "//net"}
89    , {"//net/foo", {"//net", "/", "foo"}, "//net/", "//net", "/", "foo", "//net/", "foo"}
90    , {"///foo///", {"/", "foo", "."}, "/", "", "/", "foo///", "///foo", "."}
91    , {"///foo///bar", {"/", "foo", "bar"}, "/", "", "/", "foo///bar", "///foo", "bar"}
92    , {"/.", {"/", "."}, "/", "", "/", ".", "/", "."}
93    , {"./", {".", "."}, "", "", "", "./", ".", "."}
94    , {"/..", {"/", ".."}, "/", "", "/", "..", "/", ".."}
95    , {"../", {"..", "."}, "", "", "", "../", "..", "."}
96    , {"foo/.", {"foo", "."}, "", "", "", "foo/.", "foo", "."}
97    , {"foo/..", {"foo", ".."}, "", "", "", "foo/..", "foo", ".."}
98    , {"foo/./", {"foo", ".", "."}, "", "", "", "foo/./", "foo/.", "."}
99    , {"foo/./bar", {"foo", ".", "bar"}, "", "", "", "foo/./bar", "foo/.", "bar"}
100    , {"foo/../", {"foo", "..", "."}, "", "", "", "foo/../", "foo/..", "."}
101    , {"foo/../bar", {"foo", "..", "bar"}, "", "", "", "foo/../bar", "foo/..", "bar"}
102    , {"c:", {"c:"}, "", "", "", "c:", "", "c:"}
103    , {"c:/", {"c:", "."}, "", "", "", "c:/", "c:", "."}
104    , {"c:foo", {"c:foo"}, "", "", "", "c:foo", "", "c:foo"}
105    , {"c:/foo", {"c:", "foo"}, "", "", "", "c:/foo", "c:", "foo"}
106    , {"c:foo/", {"c:foo", "."}, "", "", "", "c:foo/", "c:foo", "."}
107    , {"c:/foo/", {"c:", "foo", "."}, "", "", "", "c:/foo/",  "c:/foo", "."}
108    , {"c:/foo/bar", {"c:", "foo", "bar"}, "", "", "", "c:/foo/bar", "c:/foo", "bar"}
109    , {"prn:", {"prn:"}, "", "", "", "prn:", "", "prn:"}
110    , {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"}
111    , {"c:\\foo", {"c:\\foo"}, "", "", "", "c:\\foo", "", "c:\\foo"}
112    , {"c:foo\\", {"c:foo\\"}, "", "", "", "c:foo\\", "", "c:foo\\"}
113    , {"c:\\foo\\", {"c:\\foo\\"}, "", "", "", "c:\\foo\\", "", "c:\\foo\\"}
114    , {"c:\\foo/",  {"c:\\foo", "."}, "", "", "", "c:\\foo/", "c:\\foo", "."}
115    , {"c:/foo\\bar", {"c:", "foo\\bar"}, "", "", "", "c:/foo\\bar", "c:", "foo\\bar"}
116    , {"//", {"//"}, "//", "//", "", "", "", "//"}
117  };
118
119void decompPathTest()
120{
121  using namespace fs;
122  for (auto const & TC : PathTestCases) {
123    path p(TC.raw);
124    assert(p == TC.raw);
125
126    assert(p.root_path() == TC.root_path);
127    assert(p.has_root_path() !=  TC.root_path.empty());
128
129    assert(p.root_name() == TC.root_name);
130    assert(p.has_root_name() !=  TC.root_name.empty());
131
132    assert(p.root_directory() == TC.root_directory);
133    assert(p.has_root_directory() !=  TC.root_directory.empty());
134
135    assert(p.relative_path() == TC.relative_path);
136    assert(p.has_relative_path() !=  TC.relative_path.empty());
137
138    assert(p.parent_path() == TC.parent_path);
139    assert(p.has_parent_path() !=  TC.parent_path.empty());
140
141    assert(p.filename() == TC.filename);
142    assert(p.has_filename() !=  TC.filename.empty());
143
144    assert(p.is_absolute() == p.has_root_directory());
145    assert(p.is_relative() !=  p.is_absolute());
146
147    assert(checkCollectionsEqual(p.begin(), p.end(),
148                                 TC.elements.begin(), TC.elements.end()));
149    // check backwards
150    assert(checkCollectionsEqual(mkRev(p.end()), mkRev(p.begin()),
151                                 TC.elements.rbegin(), TC.elements.rend()));
152  }
153}
154
155
156struct FilenameDecompTestcase
157{
158  std::string raw;
159  std::string filename;
160  std::string stem;
161  std::string extension;
162};
163
164const FilenameDecompTestcase FilenameTestCases[] =
165{
166    {"", "", "", ""}
167  , {".", ".", ".", ""}
168  , {"..", "..", "..", ""}
169  , {"/", "/", "/", ""}
170  , {"foo", "foo", "foo", ""}
171  , {"/foo/bar.txt", "bar.txt", "bar", ".txt"}
172  , {"foo..txt", "foo..txt", "foo.", ".txt"}
173};
174
175
176void decompFilenameTest()
177{
178  using namespace fs;
179  for (auto const & TC : FilenameTestCases) {
180    path p(TC.raw);
181    assert(p == TC.raw);
182
183    assert(p.filename() == TC.filename);
184    assert(p.has_filename() != TC.filename.empty());
185
186    assert(p.stem() == TC.stem);
187    assert(p.has_stem() != TC.stem.empty());
188
189    assert(p.extension() == TC.extension);
190    assert(p.has_extension() != TC.extension.empty());
191  }
192}
193
194int main()
195{
196  decompPathTest();
197  decompFilenameTest();
198}
199