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