| 1 | // { dg-do run { target c++17 } } |
| 2 | // { dg-require-filesystem-ts "" } |
| 3 | |
| 4 | // Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 5 | // |
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
| 7 | // software; you can redistribute it and/or modify it under the |
| 8 | // terms of the GNU General Public License as published by the |
| 9 | // Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | |
| 12 | // This library is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this library; see the file COPYING3. If not see |
| 19 | // <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | // C++17 30.10.14.1 Absolute [fs.op.absolute] |
| 22 | |
| 23 | #include <filesystem> |
| 24 | #include <testsuite_fs.h> |
| 25 | #include <testsuite_hooks.h> |
| 26 | |
| 27 | using std::filesystem::path; |
| 28 | |
| 29 | void |
| 30 | test01() |
| 31 | { |
| 32 | for (const path p : __gnu_test::test_paths) |
| 33 | { |
| 34 | std::error_code ec; |
| 35 | path abs = absolute(p, ec); |
| 36 | VERIFY( ec || abs.is_absolute() ); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | test02() |
| 42 | { |
| 43 | std::error_code ec = make_error_code(std::errc::invalid_argument); |
| 44 | path root = __gnu_test::root_path(); |
| 45 | VERIFY( absolute(root) == root ); |
| 46 | VERIFY( absolute(root, ec) == root && !ec ); |
| 47 | VERIFY( absolute(path{}, ec).empty() && ec ); |
| 48 | |
| 49 | #if defined(__MINGW32__) || defined(__MINGW64__) |
| 50 | path p1("/"); |
| 51 | VERIFY( absolute(p1) != p1 ); |
| 52 | path p2("/foo"); |
| 53 | VERIFY( absolute(p2) != p2 ); |
| 54 | path p3("foo"); |
| 55 | VERIFY( absolute(p3) != p3 ); |
| 56 | path p4("C:\\"); |
| 57 | VERIFY( absolute(p4) == p4 ); |
| 58 | #else |
| 59 | path p1("/"); |
| 60 | VERIFY( absolute(p1) == p1 ); |
| 61 | path p2("/foo"); |
| 62 | VERIFY( absolute(p2) == p2 ); |
| 63 | path p3("foo"); |
| 64 | VERIFY( absolute(p3) != p3 ); |
| 65 | VERIFY( absolute(p3) == (std::filesystem::current_path()/p3) ); |
| 66 | #endif |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | test03() |
| 71 | { |
| 72 | // PR libstdc++/90299 |
| 73 | const path p = __gnu_test::nonexistent_path(); |
| 74 | std::error_code ec; |
| 75 | const path pabs = absolute(p, ec); |
| 76 | VERIFY( !ec ); |
| 77 | VERIFY( pabs.is_absolute() ); |
| 78 | |
| 79 | const path pabs2 = absolute(p); |
| 80 | VERIFY( pabs2 == pabs ); |
| 81 | |
| 82 | const path eabs = absolute(path{}, ec); |
| 83 | VERIFY( ec == std::errc::invalid_argument ); |
| 84 | VERIFY( eabs.empty() ); |
| 85 | |
| 86 | try { |
| 87 | (void) absolute(path{}); |
| 88 | VERIFY( false ); |
| 89 | } catch (const std::filesystem::filesystem_error& e) { |
| 90 | VERIFY( e.code() == std::errc::invalid_argument ); |
| 91 | VERIFY( e.path1().empty() ); |
| 92 | VERIFY( e.path2().empty() ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | int |
| 97 | main() |
| 98 | { |
| 99 | test01(); |
| 100 | test02(); |
| 101 | test03(); |
| 102 | } |