• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
57 namespace fs = std::experimental::filesystem;
58 struct PathDecomposeTestcase
59 {
60     std::string raw;
61     std::vector<std::string> elements;
62     std::string root_path;
63     std::string root_name;
64     std::string root_directory;
65     std::string relative_path;
66     std::string parent_path;
67     std::string filename;
68 };
69 
70 const PathDecomposeTestcase PathTestCases[] =
71   {
72       {"", {}, "", "", "", "", "", ""}
73     , {".", {"."}, "", "", "", ".", "", "."}
74     , {"..", {".."}, "", "", "", "..", "", ".."}
75     , {"foo", {"foo"}, "", "", "", "foo", "", "foo"}
76     , {"/", {"/"}, "/", "", "/", "", "", "/"}
77     , {"/foo", {"/", "foo"}, "/", "", "/", "foo", "/", "foo"}
78     , {"foo/", {"foo", "."}, "", "", "", "foo/", "foo", "."}
79     , {"/foo/", {"/", "foo", "."}, "/", "", "/", "foo/", "/foo", "."}
80     , {"foo/bar", {"foo","bar"}, "",  "", "",  "foo/bar", "foo", "bar"}
81     , {"/foo//bar", {"/","foo","bar"}, "/", "", "/", "foo/bar", "/foo", "bar"}
82     , {"//net", {"//net"}, "//net", "//net", "", "", "", "//net"}
83     , {"//net/foo", {"//net", "/", "foo"}, "//net/", "//net", "/", "foo", "//net/", "foo"}
84     , {"///foo///", {"/", "foo", "."}, "/", "", "/", "foo///", "///foo", "."}
85     , {"///foo///bar", {"/", "foo", "bar"}, "/", "", "/", "foo///bar", "///foo", "bar"}
86     , {"/.", {"/", "."}, "/", "", "/", ".", "/", "."}
87     , {"./", {".", "."}, "", "", "", "./", ".", "."}
88     , {"/..", {"/", ".."}, "/", "", "/", "..", "/", ".."}
89     , {"../", {"..", "."}, "", "", "", "../", "..", "."}
90     , {"foo/.", {"foo", "."}, "", "", "", "foo/.", "foo", "."}
91     , {"foo/..", {"foo", ".."}, "", "", "", "foo/..", "foo", ".."}
92     , {"foo/./", {"foo", ".", "."}, "", "", "", "foo/./", "foo/.", "."}
93     , {"foo/./bar", {"foo", ".", "bar"}, "", "", "", "foo/./bar", "foo/.", "bar"}
94     , {"foo/../", {"foo", "..", "."}, "", "", "", "foo/../", "foo/..", "."}
95     , {"foo/../bar", {"foo", "..", "bar"}, "", "", "", "foo/../bar", "foo/..", "bar"}
96     , {"c:", {"c:"}, "", "", "", "c:", "", "c:"}
97     , {"c:/", {"c:", "."}, "", "", "", "c:/", "c:", "."}
98     , {"c:foo", {"c:foo"}, "", "", "", "c:foo", "", "c:foo"}
99     , {"c:/foo", {"c:", "foo"}, "", "", "", "c:/foo", "c:", "foo"}
100     , {"c:foo/", {"c:foo", "."}, "", "", "", "c:foo/", "c:foo", "."}
101     , {"c:/foo/", {"c:", "foo", "."}, "", "", "", "c:/foo/",  "c:/foo", "."}
102     , {"c:/foo/bar", {"c:", "foo", "bar"}, "", "", "", "c:/foo/bar", "c:/foo", "bar"}
103     , {"prn:", {"prn:"}, "", "", "", "prn:", "", "prn:"}
104     , {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"}
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/",  {"c:\\foo", "."}, "", "", "", "c:\\foo/", "c:\\foo", "."}
109     , {"c:/foo\\bar", {"c:", "foo\\bar"}, "", "", "", "c:/foo\\bar", "c:", "foo\\bar"}
110     , {"//", {"//"}, "//", "//", "", "", "", "//"}
111   };
112 
decompPathTest()113 void decompPathTest()
114 {
115   using namespace fs;
116   for (auto const & TC : PathTestCases) {
117     path p(TC.raw);
118     assert(p == TC.raw);
119 
120     assert(p.root_path() == TC.root_path);
121     assert(p.has_root_path() !=  TC.root_path.empty());
122 
123     assert(p.root_name() == TC.root_name);
124     assert(p.has_root_name() !=  TC.root_name.empty());
125 
126     assert(p.root_directory() == TC.root_directory);
127     assert(p.has_root_directory() !=  TC.root_directory.empty());
128 
129     assert(p.relative_path() == TC.relative_path);
130     assert(p.has_relative_path() !=  TC.relative_path.empty());
131 
132     assert(p.parent_path() == TC.parent_path);
133     assert(p.has_parent_path() !=  TC.parent_path.empty());
134 
135     assert(p.filename() == TC.filename);
136     assert(p.has_filename() !=  TC.filename.empty());
137 
138     assert(p.is_absolute() == p.has_root_directory());
139     assert(p.is_relative() !=  p.is_absolute());
140 
141     assert(checkCollectionsEqual(p.begin(), p.end(),
142                                  TC.elements.begin(), TC.elements.end()));
143     // check backwards
144 
145     std::vector<fs::path> Parts;
146     for (auto it = p.end(); it != p.begin(); )
147       Parts.push_back(*--it);
148     assert(checkCollectionsEqual(Parts.begin(), Parts.end(),
149                                  TC.elements.rbegin(), TC.elements.rend()));
150   }
151 }
152 
153 
154 struct FilenameDecompTestcase
155 {
156   std::string raw;
157   std::string filename;
158   std::string stem;
159   std::string extension;
160 };
161 
162 const FilenameDecompTestcase FilenameTestCases[] =
163 {
164     {"", "", "", ""}
165   , {".", ".", ".", ""}
166   , {"..", "..", "..", ""}
167   , {"/", "/", "/", ""}
168   , {"foo", "foo", "foo", ""}
169   , {"/foo/bar.txt", "bar.txt", "bar", ".txt"}
170   , {"foo..txt", "foo..txt", "foo.", ".txt"}
171 };
172 
173 
decompFilenameTest()174 void decompFilenameTest()
175 {
176   using namespace fs;
177   for (auto const & TC : FilenameTestCases) {
178     path p(TC.raw);
179     assert(p == TC.raw);
180     ASSERT_NOEXCEPT(p.empty());
181 
182     assert(p.filename() == TC.filename);
183     assert(p.has_filename() != TC.filename.empty());
184 
185     assert(p.stem() == TC.stem);
186     assert(p.has_stem() != TC.stem.empty());
187 
188     assert(p.extension() == TC.extension);
189     assert(p.has_extension() != TC.extension.empty());
190   }
191 }
192 
main()193 int main()
194 {
195   decompPathTest();
196   decompFilenameTest();
197 }
198