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: c++03
10 
11 // <filesystem>
12 
13 // class filesystem_error
14 
15 // filesystem_error(const string& what_arg, error_code ec);
16 // filesystem_error(const string& what_arg, const path& p1, error_code ec);
17 // filesystem_error(const string& what_arg, const path& p1, const path& p2, error_code ec);
18 // const std::error_code& code() const;
19 // const char* what() const noexcept;
20 // const path& path1() const noexcept;
21 // const path& path2() const noexcept;
22 
23 #include "filesystem_include.h"
24 #include <cassert>
25 #include <string>
26 #include <system_error>
27 #include <type_traits>
28 
29 #include "test_macros.h"
30 
31 
test_constructors()32 void test_constructors() {
33   using namespace fs;
34 
35   // The string returned by "filesystem_error::what() must contain runtime_error::what()
36   const std::string what_arg = "Hello World";
37   const std::string what_contains = std::runtime_error(what_arg).what();
38   assert(what_contains.find(what_arg) != std::string::npos);
39   auto CheckWhat = [what_contains](filesystem_error const& e) {
40     std::string s = e.what();
41     assert(s.find(what_contains) != std::string::npos);
42   };
43 
44   std::error_code ec = std::make_error_code(std::errc::file_exists);
45   const path p1("foo");
46   const path p2("bar");
47 
48   // filesystem_error(const string& what_arg, error_code ec);
49   {
50     ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, ec));
51     filesystem_error e(what_arg, ec);
52     CheckWhat(e);
53     assert(e.code() == ec);
54     assert(e.path1().empty() && e.path2().empty());
55   }
56   // filesystem_error(const string& what_arg, const path&, error_code ec);
57   {
58     ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, ec));
59     filesystem_error e(what_arg, p1, ec);
60     CheckWhat(e);
61     assert(e.code() == ec);
62     assert(e.path1() == p1);
63     assert(e.path2().empty());
64   }
65   // filesystem_error(const string& what_arg, const path&, const path&, error_code ec);
66   {
67     ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, p2, ec));
68     filesystem_error e(what_arg, p1, p2, ec);
69     CheckWhat(e);
70     assert(e.code() == ec);
71     assert(e.path1() == p1);
72     assert(e.path2() == p2);
73   }
74 }
75 
test_signatures()76 void test_signatures()
77 {
78   using namespace fs;
79   const path p;
80   std::error_code ec;
81   const filesystem_error e("lala", ec);
82   // const path& path1() const noexcept;
83   {
84     ASSERT_SAME_TYPE(path const&, decltype(e.path1()));
85     ASSERT_NOEXCEPT(e.path1());
86   }
87   // const path& path2() const noexcept
88   {
89     ASSERT_SAME_TYPE(path const&, decltype(e.path2()));
90     ASSERT_NOEXCEPT(e.path2());
91   }
92   // const char* what() const noexcept
93   {
94     ASSERT_SAME_TYPE(const char*, decltype(e.what()));
95     ASSERT_NOEXCEPT(e.what());
96   }
97 }
98 
main(int,char **)99 int main(int, char**) {
100   static_assert(std::is_base_of<std::system_error, fs::filesystem_error>::value, "");
101   test_constructors();
102   test_signatures();
103 
104   return 0;
105 }
106