| 1 | // { dg-do run { target c++17 } } |
| 2 | |
| 3 | // Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // You should have received a copy of the GNU General Public License along |
| 17 | // with this library; see the file COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | // C++17 30.10.7.4.7 path generic format observers [fs.path.generic.obs] |
| 21 | |
| 22 | #include <filesystem> |
| 23 | #include <testsuite_fs.h> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | using std::filesystem::path; |
| 27 | |
| 28 | void |
| 29 | test01() |
| 30 | { |
| 31 | VERIFY( path().generic_string() == "" ); |
| 32 | VERIFY( path("/").generic_string() == "/" ); |
| 33 | VERIFY( path("////").generic_string() == "/" ); |
| 34 | #ifdef __CYGWIN__ |
| 35 | VERIFY( path("//a").generic_string() == "//a" ); |
| 36 | VERIFY( path("//a/").generic_string() == "//a/" ); |
| 37 | VERIFY( path("//a//").generic_string() == "//a/" ); |
| 38 | VERIFY( path("//a/b").generic_string() == "//a/b" ); |
| 39 | VERIFY( path("//a//b").generic_string() == "//a/b" ); |
| 40 | #else |
| 41 | VERIFY( path("//a").generic_string() == "/a" ); |
| 42 | VERIFY( path("//a/").generic_string() == "/a/" ); |
| 43 | VERIFY( path("//a//").generic_string() == "/a/" ); |
| 44 | VERIFY( path("//a/b").generic_string() == "/a/b" ); |
| 45 | VERIFY( path("//a//b").generic_string() == "/a/b" ); |
| 46 | #endif |
| 47 | VERIFY( path("/a//b").generic_string() == "/a/b" ); |
| 48 | VERIFY( path("/a//b/").generic_string() == "/a/b/" ); |
| 49 | VERIFY( path("/a//b//").generic_string() == "/a/b/" ); |
| 50 | VERIFY( path("/a//b//.").generic_string() == "/a/b/." ); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | test02() |
| 55 | { |
| 56 | if constexpr (path::preferred_separator == L'\\') |
| 57 | { |
| 58 | // PR libstdc++/93244 |
| 59 | VERIFY( path("C:\\foo\\bar").generic_string() == "C:/foo/bar" ); |
| 60 | VERIFY( path("C://foo//bar").generic_string() == "C:/foo/bar" ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | test03() |
| 66 | { |
| 67 | for (path p : __gnu_test::test_paths) |
| 68 | { |
| 69 | // A path constructed from the generic format string should compare equal |
| 70 | // to the original, because they represent the same path. |
| 71 | VERIFY( path(p.generic_string()) == p ); |
| 72 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 73 | VERIFY( path(p.generic_wstring()) == p ); |
| 74 | #endif |
| 75 | VERIFY( path(p.generic_u8string()) == p ); |
| 76 | VERIFY( path(p.generic_u16string()) == p ); |
| 77 | VERIFY( path(p.generic_u32string()) == p ); |
| 78 | } |
| 79 | |
| 80 | for (path p : { "a///b//c", "///a//b//c", "a:b//c", "a://b///c" }) |
| 81 | { |
| 82 | // A path constructed from the generic format string should compare equal |
| 83 | // to the original, because they represent the same path. |
| 84 | VERIFY( path(p.generic_string()) == p ); |
| 85 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 86 | VERIFY( path(p.generic_wstring()) == p ); |
| 87 | #endif |
| 88 | VERIFY( path(p.generic_u8string()) == p ); |
| 89 | VERIFY( path(p.generic_u16string()) == p ); |
| 90 | VERIFY( path(p.generic_u32string()) == p ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | int |
| 95 | main() |
| 96 | { |
| 97 | test01(); |
| 98 | test02(); |
| 99 | test03(); |
| 100 | } |