| 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 | // { dg-require-filesystem-ts "" } |
| 20 | |
| 21 | #include <filesystem> |
| 22 | #include <testsuite_hooks.h> |
| 23 | #include <testsuite_fs.h> |
| 24 | |
| 25 | using std::filesystem::proximate; |
| 26 | using __gnu_test::compare_paths; |
| 27 | |
| 28 | // Normalize directory-separators |
| 29 | std::string operator""_norm(const char* s, std::size_t n) |
| 30 | { |
| 31 | std::string str(s, n); |
| 32 | #if defined(__MINGW32__) || defined(__MINGW64__) |
| 33 | for (auto& c : str) |
| 34 | if (c == '/') |
| 35 | c = '\\'; |
| 36 | #endif |
| 37 | return str; |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | test01() |
| 42 | { |
| 43 | compare_paths( proximate("/a/d", "/a/b/c"), "../../d"_norm ); |
| 44 | compare_paths( proximate("/a/b/c", "/a/d"), "../b/c"_norm ); |
| 45 | compare_paths( proximate("a/b/c", "a"), "b/c"_norm ); |
| 46 | compare_paths( proximate("a/b/c", "a/b/c/x/y"), "../.."_norm ); |
| 47 | compare_paths( proximate("a/b/c", "a/b/c"), "." ); |
| 48 | compare_paths( proximate("a/b", "c/d"), "../../a/b"_norm ); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | test02() |
| 53 | { |
| 54 | const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); |
| 55 | std::error_code ec = bad_ec; |
| 56 | compare_paths( proximate("/a/d", "/a/b/c", ec), "../../d"_norm ); |
| 57 | VERIFY( !ec ); |
| 58 | ec = bad_ec; |
| 59 | compare_paths( proximate("/a/b/c", "/a/d", ec), "../b/c"_norm ); |
| 60 | VERIFY( !ec ); |
| 61 | ec = bad_ec; |
| 62 | compare_paths( proximate("a/b/c", "a", ec), "b/c"_norm ); |
| 63 | VERIFY( !ec ); |
| 64 | ec = bad_ec; |
| 65 | compare_paths( proximate("a/b/c", "a/b/c/x/y", ec), "../.."_norm ); |
| 66 | VERIFY( !ec ); |
| 67 | ec = bad_ec; |
| 68 | compare_paths( proximate("a/b/c", "a/b/c", ec), "." ); |
| 69 | VERIFY( !ec ); |
| 70 | ec = bad_ec; |
| 71 | compare_paths( proximate("a/b", "c/d", ec), "../../a/b"_norm ); |
| 72 | VERIFY( !ec ); |
| 73 | } |
| 74 | |
| 75 | int |
| 76 | main() |
| 77 | { |
| 78 | test01(); |
| 79 | test02(); |
| 80 | } |