1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03, c++11, c++14
10 // UNSUPPORTED: availability-filesystem-missing
11
12 // <filesystem>
13
14 // class path
15
16 // path lexically_normal() const;
17
18 #include <filesystem>
19 #include <string>
20
21 #include "../../path_helper.h"
22 #include "count_new.h"
23 #include "test_macros.h"
24 #include "assert_macros.h"
25 #include "concat_macros.h"
26 namespace fs = std::filesystem;
27
main(int,char **)28 int main(int, char**) {
29 // clang-format off
30 struct {
31 std::string input;
32 std::string expect;
33 } TestCases[] = {
34 {"", ""},
35 {"/a/b/c", "/a/b/c"},
36 {"/a/b//c", "/a/b/c"},
37 {"foo/./bar/..", "foo/"},
38 {"foo/.///bar/../", "foo/"},
39 {"/a/b/", "/a/b/"},
40 {"a/b", "a/b"},
41 {"a/b/.", "a/b/"},
42 {"a/b/./", "a/b/"},
43 {"a/..", "."},
44 {".", "."},
45 {"./", "."},
46 {"./.", "."},
47 {"./..", ".."},
48 {"..", ".."},
49 {"../..", "../.."},
50 {"/../", "/"},
51 {"/../..", "/"},
52 {"/../../", "/"},
53 {"..", ".."},
54 {"../", ".."},
55 {"/a/b/c/../", "/a/b/"},
56 {"/a/b/./", "/a/b/"},
57 {"/a/b/c/../d", "/a/b/d"},
58 {"/a/b/c/../d/", "/a/b/d/"},
59 #ifdef _WIN32
60 {"//a/", "//a/"},
61 {"//a/b/", "//a/b/"},
62 {"//a/b/.", "//a/b/"},
63 {"//a/..", "//a/"},
64 #else
65 {"//a/", "/a/"},
66 {"//a/b/", "/a/b/"},
67 {"//a/b/.", "/a/b/"},
68 {"//a/..", "/"},
69 #endif
70 ///===---------------------------------------------------------------===//
71 /// Tests specifically for the clauses under [fs.path.generic]p6
72 ///===---------------------------------------------------------------===//
73 // p1: If the path is empty, stop.
74 {"", ""},
75 // p2: Replace each slash character in the root-name with a preferred
76 // separator.
77 {"NO_ROOT_NAME_ON_LINUX", "NO_ROOT_NAME_ON_LINUX"},
78 // p3: Replace each directory-separator with a preferred-separator.
79 // [ Note: The generic pathname grammar ([fs.path.generic]) defines
80 // directory-separator as one or more slashes and preferred-separators.
81 // - end note ]
82 {"/", "/"},
83 {"//", "/"},
84 {"///", "/"},
85 {"a/b", "a/b"},
86 {"a//b", "a/b"},
87 {"a///b", "a/b"},
88 {"a/b/", "a/b/"},
89 {"a/b//", "a/b/"},
90 {"a/b///", "a/b/"},
91 {"///a////b//////", "/a/b/"},
92 // p4: Remove each dot filename and any immediately following directory
93 // separators
94 {"foo/.", "foo/"},
95 {"foo/./bar/.", "foo/bar/"},
96 {"./foo/././bar/./", "foo/bar/"},
97 {".///foo//.////./bar/.///", "foo/bar/"},
98 // p5: As long as any appear, remove a non-dot-dot filename immediately
99 // followed by a directory-separator and a dot-dot filename, along with
100 // any immediately following directory separator.
101 {"foo/..", "."},
102 {"foo/../", "."},
103 {"foo/bar/..", "foo/"},
104 {"foo/bar/../", "foo/"},
105 {"foo/bar/../..", "."},
106 {"foo/bar/../../", "."},
107 {"foo/bar/baz/../..", "foo/"},
108 {"foo/bar/baz/../../", "foo/"},
109 {"foo/bar/./..", "foo/"},
110 {"foo/bar/./../", "foo/"},
111 // p6: If there is a root-directory, remove all dot-dot filenames and any
112 // directory-separators immediately following them. [ Note: These dot-dot
113 // filenames attempt to refer to nonexistent parent directories. - end note ]
114 {"/..", "/"},
115 {"/../", "/"},
116 {"/foo/../..", "/"},
117 {"/../foo", "/foo"},
118 {"/../foo/../..", "/"},
119 // p7: If the last filename is dot-dot, remove any trailing
120 // directory-separator.
121 {"../", ".."},
122 {"../../", "../.."},
123 {"foo/../bar/../..///", ".."},
124 {"foo/../bar/..//..///../", "../.."},
125 // p8: If the path is empty, add a dot
126 {".", "."},
127 {"./", "."},
128 {"foo/..", "."}
129 };
130 // clang-format on
131 for (auto& TC : TestCases) {
132 fs::path p(TC.input);
133 const fs::path output = p.lexically_normal();
134 fs::path expect(TC.expect);
135 expect.make_preferred();
136
137 TEST_REQUIRE(
138 PathEq(output, expect),
139 TEST_WRITE_CONCATENATED("Input: ", TC.input, "\nExpected: ", expect.string(), "\nOutput: ", output.string()));
140 }
141 return 0;
142 }
143