| 1 |
// Copyright (C) 2018-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 |
|
| 24 |
using std::filesystem::filesystem_error; |
| 25 |
|
| 26 |
bool contains(std::string_view what_str, std::string_view expected) |
| 27 |
{ |
| 28 |
return what_str.find(expected) != std::string_view::npos; |
| 29 |
} |
| 30 |
|
| 31 |
void |
| 32 |
test01() |
| 33 |
{ |
| 34 |
const char* const str = "error test"; |
| 35 |
const std::error_code ec = make_error_code(std::errc::is_a_directory); |
| 36 |
const std::filesystem::path p1 = "test/path/one"; |
| 37 |
const std::filesystem::path p2 = "/test/path/two"; |
| 38 |
std::system_error syserr(ec, str); |
| 39 |
|
| 40 |
const filesystem_error e1(str, ec); |
| 41 |
VERIFY( contains(e1.what(), syserr.what()) ); |
| 42 |
VERIFY( !contains(e1.what(), "[]") ); // no "empty path" in the string |
| 43 |
VERIFY( e1.path1().empty() ); |
| 44 |
VERIFY( e1.path2().empty() ); |
| 45 |
VERIFY( e1.code() == ec ); |
| 46 |
|
| 47 |
const filesystem_error e2(str, p1, ec); |
| 48 |
VERIFY( e2.path1() == p1 ); |
| 49 |
VERIFY( e2.path2().empty() ); |
| 50 |
VERIFY( contains(e2.what(), syserr.what()) ); |
| 51 |
VERIFY( contains(e2.what(), p1.string()) ); |
| 52 |
VERIFY( !contains(e2.what(), "[]") ); |
| 53 |
VERIFY( e2.code() == ec ); |
| 54 |
|
| 55 |
const filesystem_error e3(str, std::filesystem::path{}, ec); |
| 56 |
VERIFY( e3.path1().empty() ); |
| 57 |
VERIFY( e3.path2().empty() ); |
| 58 |
VERIFY( contains(e3.what(), syserr.what()) ); |
| 59 |
VERIFY( contains(e3.what(), "[]") ); |
| 60 |
VERIFY( !contains(e3.what(), "[] []") ); |
| 61 |
VERIFY( e3.code() == ec ); |
| 62 |
|
| 63 |
const filesystem_error e4(str, p1, p2, ec); |
| 64 |
VERIFY( e4.path1() == p1 ); |
| 65 |
VERIFY( e4.path2() == p2 ); |
| 66 |
VERIFY( contains(e4.what(), syserr.what()) ); |
| 67 |
VERIFY( contains(e4.what(), p1.string()) ); |
| 68 |
VERIFY( contains(e4.what(), p2.string()) ); |
| 69 |
VERIFY( !contains(e4.what(), "[]") ); |
| 70 |
VERIFY( e4.code() == ec ); |
| 71 |
|
| 72 |
const filesystem_error e5(str, p1, std::filesystem::path{}, ec); |
| 73 |
VERIFY( e5.path1() == p1 ); |
| 74 |
VERIFY( e5.path2().empty() ); |
| 75 |
VERIFY( contains(e5.what(), syserr.what()) ); |
| 76 |
VERIFY( contains(e5.what(), p1.string()) ); |
| 77 |
VERIFY( contains(e5.what(), "[]") ); |
| 78 |
VERIFY( e5.code() == ec ); |
| 79 |
|
| 80 |
const filesystem_error e6(str, std::filesystem::path{}, p2, ec); |
| 81 |
VERIFY( e6.path1().empty() ); |
| 82 |
VERIFY( e6.path2() == p2 ); |
| 83 |
VERIFY( contains(e6.what(), syserr.what()) ); |
| 84 |
VERIFY( contains(e6.what(), "[]") ); |
| 85 |
VERIFY( contains(e6.what(), p2.string()) ); |
| 86 |
VERIFY( e6.code() == ec ); |
| 87 |
} |
| 88 |
|
| 89 |
int |
| 90 |
main() |
| 91 |
{ |
| 92 |
test01(); |
| 93 |
} |