]> git.ipfire.org Git - thirdparty/gcc.git/blame - 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
CommitLineData
8d9254fc 1// Copyright (C) 2017-2020 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
2b522535 18// { dg-options "-std=gnu++17" }
641cb5a6 19// { dg-do run { target c++17 } }
641cb5a6
JW
20
21#include <filesystem>
eeb517d3 22#include <testsuite_fs.h>
641cb5a6
JW
23#include <testsuite_hooks.h>
24
25using std::filesystem::path;
dd35da2c
JW
26
27void
28compare_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}
641cb5a6
JW
37
38void
39test01()
40{
41 // C++17 [fs.path.gen] p2
eeb517d3
JW
42 compare_paths( path("foo/./bar/..").lexically_normal(), "foo/" );
43 compare_paths( path("foo/.///bar/../").lexically_normal(), "foo/" );
641cb5a6
JW
44}
45
46void
47test02()
48{
eeb517d3
JW
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(), "/" );
50e248f0
JW
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" );
eeb517d3
JW
62}
63
64void
65test03()
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/" },
eeb517d3 81 {"/foo/.." , "/" },
dd35da2c
JW
82 {"/foo/../.." , "/" },
83 {"/foo/bar/.." , "/foo/" },
84 {"/foo/bar/../.." , "/" },
85 {"/foo/bar/baz/../../.." , "/" }, // PR libstdc++/87116
eeb517d3
JW
86
87 {"/." , "/" },
88 {"/./" , "/" },
89 {"/./." , "/" },
90 {"/././" , "/" },
91 {"/././." , "/" },
92
93 {"./" , "." },
94 {"./." , "." },
95 {"././" , "." },
96 {"././." , "." },
97 {"./././" , "." },
98 {"./././." , "." },
99
100 {"foo/.." , "." },
101 {"foo/../" , "." },
102 {"foo/../.." , ".." },
dd35da2c 103 {"foo/../../..", "../.." },
eeb517d3
JW
104
105 // with root name (OS-dependent):
106#if defined(_WIN32) && !defined(__CYGWIN__)
dd35da2c 107 {"C:bar/.." , "C:" },
eeb517d3
JW
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" },
73d968d9
JW
127 {"../.." , "../.." },
128 {"../../." , "../.." },
eeb517d3
JW
129 {".././../." , "../.." },
130 {".././.././" , "../.." },
131 {"/.." , "/" },
132 };
133 for (auto& test : testcases)
134 compare_paths( path(test.input).lexically_normal(), test.normalized );
641cb5a6
JW
135}
136
dd35da2c
JW
137void
138test04()
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
641cb5a6
JW
179int
180main()
181{
182 test01();
183 test02();
eeb517d3 184 test03();
dd35da2c 185 test04();
641cb5a6 186}