]>
Commit | Line | Data |
---|---|---|
a5544970 | 1 | // Copyright (C) 2017-2019 Free Software Foundation, Inc. |
641cb5a6 JW |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | // { dg-options "-std=gnu++17 -lstdc++fs" } | |
19 | // { dg-do run { target c++17 } } | |
20 | // { dg-require-filesystem-ts "" } | |
21 | ||
22 | #include <filesystem> | |
eeb517d3 | 23 | #include <testsuite_fs.h> |
641cb5a6 JW |
24 | #include <testsuite_hooks.h> |
25 | ||
26 | using std::filesystem::path; | |
dd35da2c JW |
27 | |
28 | void | |
29 | compare_paths(path p, std::string expected) | |
30 | { | |
31 | #if defined(_WIN32) && !defined(__CYGWIN__) | |
32 | for (auto& c : expected) | |
33 | if (c == '/') | |
34 | c = '\\'; | |
35 | #endif | |
36 | __gnu_test::compare_paths(p, expected); | |
37 | } | |
641cb5a6 JW |
38 | |
39 | void | |
40 | test01() | |
41 | { | |
42 | // C++17 [fs.path.gen] p2 | |
eeb517d3 JW |
43 | compare_paths( path("foo/./bar/..").lexically_normal(), "foo/" ); |
44 | compare_paths( path("foo/.///bar/../").lexically_normal(), "foo/" ); | |
641cb5a6 JW |
45 | } |
46 | ||
47 | void | |
48 | test02() | |
49 | { | |
eeb517d3 JW |
50 | compare_paths( path("foo/../bar").lexically_normal(), "bar" ); |
51 | compare_paths( path("../foo/../bar").lexically_normal(), "../bar" ); | |
52 | compare_paths( path("foo/../").lexically_normal(), "." ); | |
53 | compare_paths( path("../../").lexically_normal(), "../.." ); | |
54 | compare_paths( path("../").lexically_normal(), ".." ); | |
55 | compare_paths( path("./").lexically_normal(), "." ); | |
56 | compare_paths( path().lexically_normal(), "" ); | |
57 | ||
58 | compare_paths( path("/..").lexically_normal(), "/" ); | |
50e248f0 JW |
59 | |
60 | // PR libstdc++/82777 | |
61 | compare_paths( path("./a/b/c/../.././b/c").lexically_normal(), "a/b/c" ); | |
62 | compare_paths( path("/a/b/c/../.././b/c").lexically_normal(), "/a/b/c" ); | |
eeb517d3 JW |
63 | } |
64 | ||
65 | void | |
66 | test03() | |
67 | { | |
68 | struct | |
69 | { | |
70 | const char* input; | |
71 | const char* normalized; | |
72 | } testcases[] = { | |
73 | {"" , "" }, | |
74 | {"." , "." }, | |
75 | {".." , ".." }, | |
76 | {"/" , "/" }, | |
77 | {"//" , "//" }, | |
78 | ||
79 | {"/foo" , "/foo" }, | |
80 | {"/foo/" , "/foo/" }, | |
81 | {"/foo/." , "/foo/" }, | |
eeb517d3 | 82 | {"/foo/.." , "/" }, |
dd35da2c JW |
83 | {"/foo/../.." , "/" }, |
84 | {"/foo/bar/.." , "/foo/" }, | |
85 | {"/foo/bar/../.." , "/" }, | |
86 | {"/foo/bar/baz/../../.." , "/" }, // PR libstdc++/87116 | |
eeb517d3 JW |
87 | |
88 | {"/." , "/" }, | |
89 | {"/./" , "/" }, | |
90 | {"/./." , "/" }, | |
91 | {"/././" , "/" }, | |
92 | {"/././." , "/" }, | |
93 | ||
94 | {"./" , "." }, | |
95 | {"./." , "." }, | |
96 | {"././" , "." }, | |
97 | {"././." , "." }, | |
98 | {"./././" , "." }, | |
99 | {"./././." , "." }, | |
100 | ||
101 | {"foo/.." , "." }, | |
102 | {"foo/../" , "." }, | |
103 | {"foo/../.." , ".." }, | |
dd35da2c | 104 | {"foo/../../..", "../.." }, |
eeb517d3 JW |
105 | |
106 | // with root name (OS-dependent): | |
107 | #if defined(_WIN32) && !defined(__CYGWIN__) | |
dd35da2c | 108 | {"C:bar/.." , "C:" }, |
eeb517d3 JW |
109 | #else |
110 | {"C:bar/.." , "." }, | |
111 | #endif | |
112 | {"C:/bar/.." , "C:/" }, | |
113 | {"C:" , "C:" }, | |
114 | #ifdef __CYGWIN__ | |
115 | {"//host/bar/.." , "//host/" }, | |
116 | {"//host" , "//host" }, | |
117 | #else | |
118 | {"//host/bar/.." , "/host/" }, | |
119 | {"//host" , "/host" }, | |
120 | #endif | |
121 | ||
122 | // a few others: | |
123 | {"foo/../foo/.." , "." }, | |
124 | {"foo/../foo/../.." , ".." }, | |
125 | {"../foo/../foo/.." , ".." }, | |
126 | {"../.f/../f" , "../f" }, | |
127 | {"../f/../.f" , "../.f" }, | |
128 | {".././../." , "../.." }, | |
129 | {".././.././" , "../.." }, | |
130 | {"/.." , "/" }, | |
131 | }; | |
132 | for (auto& test : testcases) | |
133 | compare_paths( path(test.input).lexically_normal(), test.normalized ); | |
641cb5a6 JW |
134 | } |
135 | ||
dd35da2c JW |
136 | void |
137 | test04() | |
138 | { | |
139 | // PR libstdc++/87116 | |
140 | path p = "a/b/c"; | |
141 | compare_paths( (p/"../..").lexically_normal(), "a/" ); | |
142 | ||
143 | p = "a/b/c/d/e"; | |
144 | compare_paths( (p/"..").lexically_normal(), "a/b/c/d/" ); | |
145 | compare_paths( (p/"../..").lexically_normal(), "a/b/c/" ); | |
146 | compare_paths( (p/"../../..").lexically_normal(), "a/b/" ); | |
147 | compare_paths( (p/"../../../..").lexically_normal(), "a/" ); | |
148 | compare_paths( (p/"../../../../..").lexically_normal(), "." ); | |
149 | compare_paths( (p/"../../../../../..").lexically_normal(), ".." ); | |
150 | ||
151 | p = "/a/b/c/d/e"; | |
152 | compare_paths( (p/"..").lexically_normal(), "/a/b/c/d/" ); | |
153 | compare_paths( (p/"../..").lexically_normal(), "/a/b/c/" ); | |
154 | compare_paths( (p/"../../..").lexically_normal(), "/a/b/" ); | |
155 | compare_paths( (p/"../../../..").lexically_normal(), "/a/" ); | |
156 | compare_paths( (p/"../../../../..").lexically_normal(), "/" ); | |
157 | compare_paths( (p/"../../../../../..").lexically_normal(), "/" ); | |
158 | ||
159 | #if defined(_WIN32) && !defined(__CYGWIN__) | |
160 | p = "A:b/c/d/e"; | |
161 | compare_paths( (p/"..").lexically_normal(), "A:b/c/d/" ); | |
162 | compare_paths( (p/"../..").lexically_normal(), "A:b/c/" ); | |
163 | compare_paths( (p/"../../..").lexically_normal(), "A:b/" ); | |
164 | compare_paths( (p/"../../../..").lexically_normal(), "A:" ); | |
165 | compare_paths( (p/"../../../../..").lexically_normal(), "A:.." ); | |
166 | compare_paths( (p/"../../../../../..").lexically_normal(), "A:../.." ); | |
167 | ||
168 | p = "A:/b/c/d/e"; | |
169 | compare_paths( (p/"..").lexically_normal(), "A:/b/c/d/" ); | |
170 | compare_paths( (p/"../..").lexically_normal(), "A:/b/c/" ); | |
171 | compare_paths( (p/"../../..").lexically_normal(), "A:/b/" ); | |
172 | compare_paths( (p/"../../../..").lexically_normal(), "A:/" ); | |
173 | compare_paths( (p/"../../../../..").lexically_normal(), "A:/" ); | |
174 | compare_paths( (p/"../../../../../..").lexically_normal(), "A:/" ); | |
175 | #endif | |
176 | } | |
177 | ||
641cb5a6 JW |
178 | int |
179 | main() | |
180 | { | |
181 | test01(); | |
182 | test02(); | |
eeb517d3 | 183 | test03(); |
dd35da2c | 184 | test04(); |
641cb5a6 | 185 | } |