]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/filesystem/path/generation/normal.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / path / generation / normal.cc
1 // Copyright (C) 2017-2019 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 -lstdc++fs" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-filesystem-ts "" }
21
22 #include <filesystem>
23 #include <testsuite_fs.h>
24 #include <testsuite_hooks.h>
25
26 using std::filesystem::path;
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 }
38
39 void
40 test01()
41 {
42 // C++17 [fs.path.gen] p2
43 compare_paths( path("foo/./bar/..").lexically_normal(), "foo/" );
44 compare_paths( path("foo/.///bar/../").lexically_normal(), "foo/" );
45 }
46
47 void
48 test02()
49 {
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(), "/" );
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" );
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/" },
82 {"/foo/.." , "/" },
83 {"/foo/../.." , "/" },
84 {"/foo/bar/.." , "/foo/" },
85 {"/foo/bar/../.." , "/" },
86 {"/foo/bar/baz/../../.." , "/" }, // PR libstdc++/87116
87
88 {"/." , "/" },
89 {"/./" , "/" },
90 {"/./." , "/" },
91 {"/././" , "/" },
92 {"/././." , "/" },
93
94 {"./" , "." },
95 {"./." , "." },
96 {"././" , "." },
97 {"././." , "." },
98 {"./././" , "." },
99 {"./././." , "." },
100
101 {"foo/.." , "." },
102 {"foo/../" , "." },
103 {"foo/../.." , ".." },
104 {"foo/../../..", "../.." },
105
106 // with root name (OS-dependent):
107 #if defined(_WIN32) && !defined(__CYGWIN__)
108 {"C:bar/.." , "C:" },
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 );
134 }
135
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
178 int
179 main()
180 {
181 test01();
182 test02();
183 test03();
184 test04();
185 }