| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: availability-filesystem-missing |
| 10 | // UNSUPPORTED: c++03, c++11, c++14 |
| 11 | |
| 12 | // <filesystem> |
| 13 | |
| 14 | // class filesystem_error |
| 15 | |
| 16 | // filesystem_error(const string& what_arg, error_code ec); |
| 17 | // filesystem_error(const string& what_arg, const path& p1, error_code ec); |
| 18 | // filesystem_error(const string& what_arg, const path& p1, const path& p2, error_code ec); |
| 19 | // const std::error_code& code() const; |
| 20 | // const char* what() const noexcept; |
| 21 | // const path& path1() const noexcept; |
| 22 | // const path& path2() const noexcept; |
| 23 | |
| 24 | #include <filesystem> |
| 25 | #include <cassert> |
| 26 | #include <string> |
| 27 | #include <system_error> |
| 28 | #include <type_traits> |
| 29 | |
| 30 | #include "test_macros.h" |
| 31 | namespace fs = std::filesystem; |
| 32 | |
| 33 | void test_constructors() { |
| 34 | using namespace fs; |
| 35 | |
| 36 | { |
| 37 | // The string returned by "filesystem_error::what() must contain runtime_error::what().c_str() |
| 38 | const std::string what_arg = "Hello World" ; |
| 39 | const std::string what_contains = std::runtime_error(what_arg).what(); |
| 40 | assert(what_contains.find(what_arg) != std::string::npos); |
| 41 | auto CheckWhat = [what_contains](filesystem_error const& e) { |
| 42 | std::string s = e.what(); |
| 43 | assert(s.find(what_contains.c_str()) != std::string::npos); |
| 44 | }; |
| 45 | |
| 46 | std::error_code ec = std::make_error_code(e: std::errc::file_exists); |
| 47 | const path p1("foo" ); |
| 48 | const path p2("bar" ); |
| 49 | |
| 50 | // filesystem_error(const string& what_arg, error_code ec); |
| 51 | { |
| 52 | ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, ec)); |
| 53 | filesystem_error e(what_arg, ec); |
| 54 | CheckWhat(e); |
| 55 | assert(e.code() == ec); |
| 56 | assert(e.path1().empty() && e.path2().empty()); |
| 57 | } |
| 58 | // filesystem_error(const string& what_arg, const path&, error_code ec); |
| 59 | { |
| 60 | ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, ec)); |
| 61 | filesystem_error e(what_arg, p1, ec); |
| 62 | CheckWhat(e); |
| 63 | assert(e.code() == ec); |
| 64 | assert(e.path1() == p1); |
| 65 | assert(e.path2().empty()); |
| 66 | } |
| 67 | // filesystem_error(const string& what_arg, const path&, const path&, error_code ec); |
| 68 | { |
| 69 | ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, p2, ec)); |
| 70 | filesystem_error e(what_arg, p1, p2, ec); |
| 71 | CheckWhat(e); |
| 72 | assert(e.code() == ec); |
| 73 | assert(e.path1() == p1); |
| 74 | assert(e.path2() == p2); |
| 75 | } |
| 76 | } |
| 77 | { |
| 78 | // The string returned by "filesystem_error::what() must contain runtime_error::what().c_str() |
| 79 | const std::string what_arg("test LWG3112 with and With embedded \0 character" ); |
| 80 | const std::string what_contains = std::runtime_error(what_arg).what(); |
| 81 | assert(what_contains.find(what_arg) != std::string::npos); |
| 82 | auto CheckWhat = [what_contains](filesystem_error const& e) { |
| 83 | std::string s = e.what(); |
| 84 | assert(s.find(what_contains.c_str()) != std::string::npos); |
| 85 | }; |
| 86 | |
| 87 | std::error_code ec = std::make_error_code(e: std::errc::file_exists); |
| 88 | const path p1("foo" ); |
| 89 | const path p2("bar" ); |
| 90 | |
| 91 | // filesystem_error(const string& what_arg, error_code ec); |
| 92 | { |
| 93 | ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, ec)); |
| 94 | filesystem_error e(what_arg, ec); |
| 95 | CheckWhat(e); |
| 96 | assert(e.code() == ec); |
| 97 | assert(e.path1().empty() && e.path2().empty()); |
| 98 | } |
| 99 | // filesystem_error(const string& what_arg, const path&, error_code ec); |
| 100 | { |
| 101 | ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, ec)); |
| 102 | filesystem_error e(what_arg, p1, ec); |
| 103 | CheckWhat(e); |
| 104 | assert(e.code() == ec); |
| 105 | assert(e.path1() == p1); |
| 106 | assert(e.path2().empty()); |
| 107 | } |
| 108 | // filesystem_error(const string& what_arg, const path&, const path&, error_code ec); |
| 109 | { |
| 110 | ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, p2, ec)); |
| 111 | filesystem_error e(what_arg, p1, p2, ec); |
| 112 | CheckWhat(e); |
| 113 | assert(e.code() == ec); |
| 114 | assert(e.path1() == p1); |
| 115 | assert(e.path2() == p2); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void test_signatures() { |
| 121 | using namespace fs; |
| 122 | const path p; |
| 123 | std::error_code ec; |
| 124 | const filesystem_error e("lala" , ec); |
| 125 | // const path& path1() const noexcept; |
| 126 | { |
| 127 | ASSERT_SAME_TYPE(path const&, decltype(e.path1())); |
| 128 | ASSERT_NOEXCEPT(e.path1()); |
| 129 | } |
| 130 | // const path& path2() const noexcept |
| 131 | { |
| 132 | ASSERT_SAME_TYPE(path const&, decltype(e.path2())); |
| 133 | ASSERT_NOEXCEPT(e.path2()); |
| 134 | } |
| 135 | // const char* what() const noexcept |
| 136 | { |
| 137 | ASSERT_SAME_TYPE(const char*, decltype(e.what())); |
| 138 | ASSERT_NOEXCEPT(e.what()); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | int main(int, char**) { |
| 143 | static_assert(std::is_base_of<std::system_error, fs::filesystem_error>::value, "" ); |
| 144 | test_constructors(); |
| 145 | test_signatures(); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |