1 // Copyright (C) 2018-2024 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++17 } }
19 // { dg-require-filesystem-ts "" }
22 #include <testsuite_hooks.h>
24 using std::filesystem::filesystem_error
;
26 // The COW std::string does not have noexcept copies, because copying a
27 // "leaked" string can throw (and for fully-dynamic strings, copying the
28 // empty rep can also throw).
29 // That's OK, because we know that the std::string in the std::runtime_error
30 // or std::logic_error base class won't be leaked (and usually won't be empty).
31 // The is_nothrow_xxx type traits don't know that though, so we can only
32 // check them for the cxx11 ABI, which uses __cow_string, which has noexcept
34 #if _GLIBCXX_USE_CXX11_ABI
36 static_assert(std::is_nothrow_copy_constructible_v
<filesystem_error
>);
37 static_assert(std::is_nothrow_copy_assignable_v
<filesystem_error
>);
43 const char* const str
= "error test";
44 const std::error_code ec
= make_error_code(std::errc::is_a_directory
);
45 const filesystem_error
e1(str
, ec
);
47 VERIFY( e2
.path1().empty() );
48 VERIFY( e2
.path2().empty() );
49 VERIFY( std::string_view(e2
.what()).find(str
) != std::string_view::npos
);
50 VERIFY( e2
.code() == ec
);
52 const filesystem_error
e3(str
, "test/path/one", ec
);
54 VERIFY( e4
.path1() == "test/path/one" );
55 VERIFY( e4
.path2().empty() );
56 VERIFY( std::string_view(e4
.what()).find(str
) != std::string_view::npos
);
57 VERIFY( e2
.code() == ec
);
59 const filesystem_error
e5(str
, "test/path/one", "/test/path/two", ec
);
61 VERIFY( e6
.path1() == "test/path/one" );
62 VERIFY( e6
.path2() == "/test/path/two" );
63 VERIFY( std::string_view(e6
.what()).find(str
) != std::string_view::npos
);
64 VERIFY( e2
.code() == ec
);
70 const char* const str
= "error test";
71 const std::error_code ec
= make_error_code(std::errc::is_a_directory
);
72 const filesystem_error
e1(str
, ec
);
73 filesystem_error
e2("", {});
75 VERIFY( e2
.path1().empty() );
76 VERIFY( e2
.path2().empty() );
77 VERIFY( std::string_view(e2
.what()).find(str
) != std::string_view::npos
);
78 VERIFY( e2
.code() == ec
);
80 const filesystem_error
e3(str
, "test/path/one", ec
);
81 filesystem_error
e4("", {});
83 VERIFY( e4
.path1() == "test/path/one" );
84 VERIFY( e4
.path2().empty() );
85 VERIFY( std::string_view(e4
.what()).find(str
) != std::string_view::npos
);
86 VERIFY( e2
.code() == ec
);
88 const filesystem_error
e5(str
, "test/path/one", "/test/path/two", ec
);
89 filesystem_error
e6("", {});
91 VERIFY( e6
.path1() == "test/path/one" );
92 VERIFY( e6
.path2() == "/test/path/two" );
93 VERIFY( std::string_view(e6
.what()).find(str
) != std::string_view::npos
);
94 VERIFY( e2
.code() == ec
);
100 filesystem_error
e("test", std::error_code());
101 VERIFY( e
.path1().empty() );
102 VERIFY( e
.path2().empty() );
103 auto e2
= std::move(e
);
104 // Observers must still be usable on moved-from object:
105 VERIFY( e
.path1().empty() );
106 VERIFY( e
.path2().empty() );
107 VERIFY( e
.what() != nullptr );
109 VERIFY( e
.path1().empty() );
110 VERIFY( e
.path2().empty() );
111 VERIFY( e
.what() != nullptr );