]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/filesystem/path/generation/normal.cc
c++/contracts: ICE in build_contract_condition_function [PR116490]
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / path / generation / normal.cc
CommitLineData
a945c346 1// Copyright (C) 2017-2024 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
641cb5a6 18// { dg-do run { target c++17 } }
641cb5a6
JW
19
20#include <filesystem>
eeb517d3 21#include <testsuite_fs.h>
641cb5a6
JW
22#include <testsuite_hooks.h>
23
24using std::filesystem::path;
dd35da2c
JW
25
26void
27compare_paths(path p, std::string expected)
28{
29#if defined(_WIN32) && !defined(__CYGWIN__)
30 for (auto& c : expected)
31 if (c == '/')
32 c = '\\';
33#endif
34 __gnu_test::compare_paths(p, expected);
35}
641cb5a6
JW
36
37void
38test01()
39{
40 // C++17 [fs.path.gen] p2
eeb517d3
JW
41 compare_paths( path("foo/./bar/..").lexically_normal(), "foo/" );
42 compare_paths( path("foo/.///bar/../").lexically_normal(), "foo/" );
641cb5a6
JW
43}
44
45void
46test02()
47{
eeb517d3
JW
48 compare_paths( path("foo/../bar").lexically_normal(), "bar" );
49 compare_paths( path("../foo/../bar").lexically_normal(), "../bar" );
50 compare_paths( path("foo/../").lexically_normal(), "." );
51 compare_paths( path("../../").lexically_normal(), "../.." );
52 compare_paths( path("../").lexically_normal(), ".." );
53 compare_paths( path("./").lexically_normal(), "." );
54 compare_paths( path().lexically_normal(), "" );
55
56 compare_paths( path("/..").lexically_normal(), "/" );
50e248f0
JW
57
58 // PR libstdc++/82777
59 compare_paths( path("./a/b/c/../.././b/c").lexically_normal(), "a/b/c" );
60 compare_paths( path("/a/b/c/../.././b/c").lexically_normal(), "/a/b/c" );
eeb517d3
JW
61}
62
63void
64test03()
65{
66 struct
67 {
68 const char* input;
69 const char* normalized;
70 } testcases[] = {
71 {"" , "" },
72 {"." , "." },
73 {".." , ".." },
74 {"/" , "/" },
75 {"//" , "//" },
76
77 {"/foo" , "/foo" },
78 {"/foo/" , "/foo/" },
79 {"/foo/." , "/foo/" },
eeb517d3 80 {"/foo/.." , "/" },
dd35da2c
JW
81 {"/foo/../.." , "/" },
82 {"/foo/bar/.." , "/foo/" },
83 {"/foo/bar/../.." , "/" },
84 {"/foo/bar/baz/../../.." , "/" }, // PR libstdc++/87116
eeb517d3
JW
85
86 {"/." , "/" },
87 {"/./" , "/" },
88 {"/./." , "/" },
89 {"/././" , "/" },
90 {"/././." , "/" },
91
92 {"./" , "." },
93 {"./." , "." },
94 {"././" , "." },
95 {"././." , "." },
96 {"./././" , "." },
97 {"./././." , "." },
98
99 {"foo/.." , "." },
100 {"foo/../" , "." },
101 {"foo/../.." , ".." },
dd35da2c 102 {"foo/../../..", "../.." },
eeb517d3
JW
103
104 // with root name (OS-dependent):
105#if defined(_WIN32) && !defined(__CYGWIN__)
dd35da2c 106 {"C:bar/.." , "C:" },
eeb517d3
JW
107#else
108 {"C:bar/.." , "." },
109#endif
110 {"C:/bar/.." , "C:/" },
111 {"C:" , "C:" },
112#ifdef __CYGWIN__
113 {"//host/bar/.." , "//host/" },
114 {"//host" , "//host" },
115#else
116 {"//host/bar/.." , "/host/" },
117 {"//host" , "/host" },
118#endif
119
120 // a few others:
121 {"foo/../foo/.." , "." },
122 {"foo/../foo/../.." , ".." },
123 {"../foo/../foo/.." , ".." },
124 {"../.f/../f" , "../f" },
125 {"../f/../.f" , "../.f" },
73d968d9
JW
126 {"../.." , "../.." },
127 {"../../." , "../.." },
eeb517d3
JW
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
136void
137test04()
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
178int
179main()
180{
181 test01();
182 test02();
eeb517d3 183 test03();
dd35da2c 184 test04();
641cb5a6 185}