]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/filesystem/filesystem_error/copy.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / filesystem_error / copy.cc
1 // Copyright (C) 2018-2024 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 // 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
33 // copies.
34 #if _GLIBCXX_USE_CXX11_ABI
35 // PR libstdc++/83306
36 static_assert(std::is_nothrow_copy_constructible_v<filesystem_error>);
37 static_assert(std::is_nothrow_copy_assignable_v<filesystem_error>);
38 #endif
39
40 void
41 test01()
42 {
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);
46 auto e2 = e1;
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 );
51
52 const filesystem_error e3(str, "test/path/one", ec);
53 auto e4 = e3;
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 );
58
59 const filesystem_error e5(str, "test/path/one", "/test/path/two", ec);
60 auto e6 = e5;
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 );
65 }
66
67 void
68 test02()
69 {
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("", {});
74 e2 = e1;
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 );
79
80 const filesystem_error e3(str, "test/path/one", ec);
81 filesystem_error e4("", {});
82 e4 = e3;
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 );
87
88 const filesystem_error e5(str, "test/path/one", "/test/path/two", ec);
89 filesystem_error e6("", {});
90 e6 = e5;
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 );
95 }
96
97 void
98 test03()
99 {
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 );
108 e2 = std::move(e);
109 VERIFY( e.path1().empty() );
110 VERIFY( e.path2().empty() );
111 VERIFY( e.what() != nullptr );
112 }
113
114 int
115 main()
116 {
117 test01();
118 test02();
119 test03();
120 }