| 1 |
// Copyright (C) 2017-2025 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-do run { target c++17 } } |
| 19 |
|
| 20 |
#include <filesystem> |
| 21 |
#include <testsuite_fs.h> |
| 22 |
#include <testsuite_hooks.h> |
| 23 |
|
| 24 |
using std::filesystem::path; |
| 25 |
|
| 26 |
void |
| 27 |
compare_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 |
} |
| 36 |
|
| 37 |
void |
| 38 |
test01() |
| 39 |
{ |
| 40 |
// C++17 [fs.path.gen] p2 |
| 41 |
compare_paths( path("foo/./bar/..").lexically_normal(), "foo/" ); |
| 42 |
compare_paths( path("foo/.///bar/../").lexically_normal(), "foo/" ); |
| 43 |
} |
| 44 |
|
| 45 |
void |
| 46 |
test02() |
| 47 |
{ |
| 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(), "/" ); |
| 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" ); |
| 61 |
} |
| 62 |
|
| 63 |
void |
| 64 |
test03() |
| 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/" }, |
| 80 |
{"/foo/.." , "/" }, |
| 81 |
{"/foo/../.." , "/" }, |
| 82 |
{"/foo/bar/.." , "/foo/" }, |
| 83 |
{"/foo/bar/../.." , "/" }, |
| 84 |
{"/foo/bar/baz/../../.." , "/" }, // PR libstdc++/87116 |
| 85 |
|
| 86 |
{"/." , "/" }, |
| 87 |
{"/./" , "/" }, |
| 88 |
{"/./." , "/" }, |
| 89 |
{"/././" , "/" }, |
| 90 |
{"/././." , "/" }, |
| 91 |
|
| 92 |
{"./" , "." }, |
| 93 |
{"./." , "." }, |
| 94 |
{"././" , "." }, |
| 95 |
{"././." , "." }, |
| 96 |
{"./././" , "." }, |
| 97 |
{"./././." , "." }, |
| 98 |
|
| 99 |
{"foo/.." , "." }, |
| 100 |
{"foo/../" , "." }, |
| 101 |
{"foo/../.." , ".." }, |
| 102 |
{"foo/../../..", "../.." }, |
| 103 |
|
| 104 |
// with root name (OS-dependent): |
| 105 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 106 |
{"C:bar/.." , "C:" }, |
| 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" }, |
| 126 |
{"../.." , "../.." }, |
| 127 |
{"../../." , "../.." }, |
| 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 |
} |