| 1 | // { dg-do run { target c++17 } } |
| 2 | |
| 3 | // Copyright (C) 2014-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.8.4.9 path decomposition [fs.path.decompose] |
| 21 | |
| 22 | #include <filesystem> |
| 23 | #include <testsuite_hooks.h> |
| 24 | #include <testsuite_fs.h> |
| 25 | |
| 26 | using std::filesystem::path; |
| 27 | |
| 28 | void |
| 29 | test01() |
| 30 | { |
| 31 | path p0; |
| 32 | VERIFY( p0.parent_path() == p0 ); |
| 33 | path p1 = "foo"; |
| 34 | VERIFY( p1.parent_path() == p0 ); |
| 35 | path p2 = "foo/bar"; |
| 36 | VERIFY( p2.parent_path() == p1 ); |
| 37 | path p3 = "/foo/bar"; |
| 38 | VERIFY( p3.parent_path() == path("/foo") ); |
| 39 | VERIFY( p3.parent_path().parent_path() == path("/") ); |
| 40 | VERIFY( p3.parent_path().parent_path().parent_path() == path("/") ); |
| 41 | path p4 = "/"; |
| 42 | VERIFY( p4.parent_path() == p4 ); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | test02() |
| 47 | { |
| 48 | for (const path p : __gnu_test::test_paths) |
| 49 | { |
| 50 | if (p.begin() == p.end()) |
| 51 | continue; |
| 52 | if (p.has_relative_path()) |
| 53 | { |
| 54 | path pp; |
| 55 | for (auto i = p.begin(), end = --p.end(); i != end; ++i) |
| 56 | { |
| 57 | pp /= *i; |
| 58 | } |
| 59 | VERIFY( p.parent_path() == pp ); |
| 60 | } |
| 61 | else |
| 62 | VERIFY( p.parent_path() == p ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | test03() |
| 68 | { |
| 69 | const std::string narrow = "there/are/no/wrong/turns/only/unexpected/paths"; |
| 70 | const path::string_type s(narrow.begin(), narrow.end()); |
| 71 | const auto s1 = s.substr(0, s.length() - 6); // remove "/paths" |
| 72 | const auto s2 = s1.substr(0, s1.length() - 16); // remove "/only/..." |
| 73 | |
| 74 | // PR libstdc++/99805 |
| 75 | path p = path::string_type(s); |
| 76 | auto pp = p.parent_path(); |
| 77 | VERIFY( pp.native() == s1 ); |
| 78 | pp = pp.parent_path().parent_path(); |
| 79 | VERIFY( pp.native() == s2 ); |
| 80 | |
| 81 | path from_lval(s); |
| 82 | pp = from_lval.parent_path(); |
| 83 | VERIFY( pp.native() == s1 ); |
| 84 | pp = pp.parent_path().parent_path(); |
| 85 | VERIFY( pp.native() == s2 ); |
| 86 | } |
| 87 | |
| 88 | int |
| 89 | main() |
| 90 | { |
| 91 | test01(); |
| 92 | test02(); |
| 93 | test03(); |
| 94 | } |