PR libstdc++/83306 make filesystem_error no-throw copyable
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / filesystem_error / copy.cc
blobf085f9d6794a99063c43bb3f57a27c8072c3dd1e
1 // Copyright (C) 2018 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.
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-options "-std=gnu++17 -lstdc++fs" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-filesystem-ts "" }
22 #include <filesystem>
23 #include <testsuite_hooks.h>
25 using std::filesystem::filesystem_error;
27 // PR libstdc++/83306
28 static_assert(std::is_nothrow_copy_constructible_v<filesystem_error>);
29 static_assert(std::is_nothrow_copy_assignable_v<filesystem_error>);
31 void
32 test01()
34 const char* const str = "error test";
35 const std::error_code ec = make_error_code(std::errc::is_a_directory);
36 const filesystem_error e1(str, ec);
37 auto e2 = e1;
38 VERIFY( e2.path1().empty() );
39 VERIFY( e2.path2().empty() );
40 VERIFY( std::string_view(e2.what()).find(str) != std::string_view::npos );
41 VERIFY( e2.code() == ec );
43 const filesystem_error e3(str, "test/path/one", ec);
44 auto e4 = e3;
45 VERIFY( e4.path1() == "test/path/one" );
46 VERIFY( e4.path2().empty() );
47 VERIFY( std::string_view(e4.what()).find(str) != std::string_view::npos );
48 VERIFY( e2.code() == ec );
50 const filesystem_error e5(str, "test/path/one", "/test/path/two", ec);
51 auto e6 = e5;
52 VERIFY( e6.path1() == "test/path/one" );
53 VERIFY( e6.path2() == "/test/path/two" );
54 VERIFY( std::string_view(e6.what()).find(str) != std::string_view::npos );
55 VERIFY( e2.code() == ec );
58 void
59 test02()
61 const char* const str = "error test";
62 const std::error_code ec = make_error_code(std::errc::is_a_directory);
63 const filesystem_error e1(str, ec);
64 filesystem_error e2("", {});
65 e2 = e1;
66 VERIFY( e2.path1().empty() );
67 VERIFY( e2.path2().empty() );
68 VERIFY( std::string_view(e2.what()).find(str) != std::string_view::npos );
69 VERIFY( e2.code() == ec );
71 const filesystem_error e3(str, "test/path/one", ec);
72 filesystem_error e4("", {});
73 e4 = e3;
74 VERIFY( e4.path1() == "test/path/one" );
75 VERIFY( e4.path2().empty() );
76 VERIFY( std::string_view(e4.what()).find(str) != std::string_view::npos );
77 VERIFY( e2.code() == ec );
79 const filesystem_error e5(str, "test/path/one", "/test/path/two", ec);
80 filesystem_error e6("", {});
81 e6 = e5;
82 VERIFY( e6.path1() == "test/path/one" );
83 VERIFY( e6.path2() == "/test/path/two" );
84 VERIFY( std::string_view(e6.what()).find(str) != std::string_view::npos );
85 VERIFY( e2.code() == ec );
88 void
89 test03()
91 filesystem_error e("test", std::error_code());
92 VERIFY( e.path1().empty() );
93 VERIFY( e.path2().empty() );
94 auto e2 = std::move(e);
95 // Observers must still be usable on moved-from object:
96 VERIFY( e.path1().empty() );
97 VERIFY( e.path2().empty() );
98 VERIFY( e.what() != nullptr );
99 e2 = std::move(e);
100 VERIFY( e.path1().empty() );
101 VERIFY( e.path2().empty() );
102 VERIFY( e.what() != nullptr );
106 main()
108 test01();
109 test02();
110 test03();