]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/filesystem/path/generation/normal.cc
a4a2b8dd0d9f275ec2fcdb779aa286fa0f742b18
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / path / generation / normal.cc
1 // Copyright (C) 2017-2020 Free Software Foundation, Inc.
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" }
19 // { dg-do run { target c++17 } }
20
21 #include <filesystem>
22 #include <testsuite_fs.h>
23 #include <testsuite_hooks.h>
24
25 using std::filesystem::path;
26
27 void
28 compare_paths(path p, std::string expected)
29 {
30 #if defined(_WIN32) && !defined(__CYGWIN__)
31 for (auto& c : expected)
32 if (c == '/')
33 c = '\\';
34 #endif
35 __gnu_test::compare_paths(p, expected);
36 }
37
38 void
39 test01()
40 {
41 // C++17 [fs.path.gen] p2
42 compare_paths( path("foo/./bar/..").lexically_normal(), "foo/" );
43 compare_paths( path("foo/.///bar/../").lexically_normal(), "foo/" );
44 }
45
46 void
47 test02()
48 {
49 compare_paths( path("foo/../bar").lexically_normal(), "bar" );
50 compare_paths( path("../foo/../bar").lexically_normal(), "../bar" );
51 compare_paths( path("foo/../").lexically_normal(), "." );
52 compare_paths( path("../../").lexically_normal(), "../.." );
53 compare_paths( path("../").lexically_normal(), ".." );
54 compare_paths( path("./").lexically_normal(), "." );
55 compare_paths( path().lexically_normal(), "" );
56
57 compare_paths( path("/..").lexically_normal(), "/" );
58
59 // PR libstdc++/82777
60 compare_paths( path("./a/b/c/../.././b/c").lexically_normal(), "a/b/c" );
61 compare_paths( path("/a/b/c/../.././b/c").lexically_normal(), "/a/b/c" );
62 }
63
64 void
65 test03()
66 {
67 struct
68 {
69 const char* input;
70 const char* normalized;
71 } testcases[] = {
72 {"" , "" },
73 {"." , "." },
74 {".." , ".." },
75 {"/" , "/" },
76 {"//" , "//" },
77
78 {"/foo" , "/foo" },
79 {"/foo/" , "/foo/" },
80 {"/foo/." , "/foo/" },
81 {"/foo/.." , "/" },
82 {"/foo/../.." , "/" },
83 {"/foo/bar/.." , "/foo/" },
84 {"/foo/bar/../.." , "/" },
85 {"/foo/bar/baz/../../.." , "/" }, // PR libstdc++/87116
86
87 {"/." , "/" },
88 {"/./" , "/" },
89 {"/./." , "/" },
90 {"/././" , "/" },
91 {"/././." , "/" },
92
93 {"./" , "." },
94 {"./." , "." },
95 {"././" , "." },
96 {"././." , "." },
97 {"./././" , "." },
98 {"./././." , "." },
99
100 {"foo/.." , "." },
101 {"foo/../" , "." },
102 {"foo/../.." , ".." },
103 {"foo/../../..", "../.." },
104
105 // with root name (OS-dependent):
106 #if defined(_WIN32) && !defined(__CYGWIN__)
107 {"C:bar/.." , "C:" },
108 #else
109 {"C:bar/.." , "." },
110 #endif
111 {"C:/bar/.." , "C:/" },
112 {"C:" , "C:" },
113 #ifdef __CYGWIN__
114 {"//host/bar/.." , "//host/" },
115 {"//host" , "//host" },
116 #else
117 {"//host/bar/.." , "/host/" },
118 {"//host" , "/host" },
119 #endif
120
121 // a few others:
122 {"foo/../foo/.." , "." },
123 {"foo/../foo/../.." , ".." },
124 {"../foo/../foo/.." , ".." },
125 {"../.f/../f" , "../f" },
126 {"../f/../.f" , "../.f" },
127 {"../.." , "../.." },
128 {"../../." , "../.." },
129 {".././../." , "../.." },
130 {".././.././" , "../.." },
131 {"/.." , "/" },
132 };
133 for (auto& test : testcases)
134 compare_paths( path(test.input).lexically_normal(), test.normalized );
135 }
136
137 void
138 test04()
139 {
140 // PR libstdc++/87116
141 path p = "a/b/c";
142 compare_paths( (p/"../..").lexically_normal(), "a/" );
143
144 p = "a/b/c/d/e";
145 compare_paths( (p/"..").lexically_normal(), "a/b/c/d/" );
146 compare_paths( (p/"../..").lexically_normal(), "a/b/c/" );
147 compare_paths( (p/"../../..").lexically_normal(), "a/b/" );
148 compare_paths( (p/"../../../..").lexically_normal(), "a/" );
149 compare_paths( (p/"../../../../..").lexically_normal(), "." );
150 compare_paths( (p/"../../../../../..").lexically_normal(), ".." );
151
152 p = "/a/b/c/d/e";
153 compare_paths( (p/"..").lexically_normal(), "/a/b/c/d/" );
154 compare_paths( (p/"../..").lexically_normal(), "/a/b/c/" );
155 compare_paths( (p/"../../..").lexically_normal(), "/a/b/" );
156 compare_paths( (p/"../../../..").lexically_normal(), "/a/" );
157 compare_paths( (p/"../../../../..").lexically_normal(), "/" );
158 compare_paths( (p/"../../../../../..").lexically_normal(), "/" );
159
160 #if defined(_WIN32) && !defined(__CYGWIN__)
161 p = "A:b/c/d/e";
162 compare_paths( (p/"..").lexically_normal(), "A:b/c/d/" );
163 compare_paths( (p/"../..").lexically_normal(), "A:b/c/" );
164 compare_paths( (p/"../../..").lexically_normal(), "A:b/" );
165 compare_paths( (p/"../../../..").lexically_normal(), "A:" );
166 compare_paths( (p/"../../../../..").lexically_normal(), "A:.." );
167 compare_paths( (p/"../../../../../..").lexically_normal(), "A:../.." );
168
169 p = "A:/b/c/d/e";
170 compare_paths( (p/"..").lexically_normal(), "A:/b/c/d/" );
171 compare_paths( (p/"../..").lexically_normal(), "A:/b/c/" );
172 compare_paths( (p/"../../..").lexically_normal(), "A:/b/" );
173 compare_paths( (p/"../../../..").lexically_normal(), "A:/" );
174 compare_paths( (p/"../../../../..").lexically_normal(), "A:/" );
175 compare_paths( (p/"../../../../../..").lexically_normal(), "A:/" );
176 #endif
177 }
178
179 int
180 main()
181 {
182 test01();
183 test02();
184 test03();
185 test04();
186 }