libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

filesystem_error.members.pass.cpp (3061B)


      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // UNSUPPORTED: c++98, c++03
     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_include.hpp"
     25 #include <type_traits>
     26 #include <cassert>
     27 
     28 #include "test_macros.h"
     29 
     30 
     31 void test_constructors() {
     32   using namespace fs;
     33 
     34   // The string returned by "filesystem_error::what() must contain runtime_error::what()
     35   const std::string what_arg = "Hello World";
     36   const std::string what_contains = std::runtime_error(what_arg).what();
     37   assert(what_contains.find(what_arg) != std::string::npos);
     38   auto CheckWhat = [what_contains](filesystem_error const& e) {
     39     std::string s = e.what();
     40     assert(s.find(what_contains) != std::string::npos);
     41   };
     42 
     43   std::error_code ec = std::make_error_code(std::errc::file_exists);
     44   const path p1("foo");
     45   const path p2("bar");
     46 
     47   // filesystem_error(const string& what_arg, error_code ec);
     48   {
     49     ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, ec));
     50     filesystem_error e(what_arg, ec);
     51     CheckWhat(e);
     52     assert(e.code() == ec);
     53     assert(e.path1().empty() && e.path2().empty());
     54   }
     55   // filesystem_error(const string& what_arg, const path&, error_code ec);
     56   {
     57     ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, ec));
     58     filesystem_error e(what_arg, p1, ec);
     59     CheckWhat(e);
     60     assert(e.code() == ec);
     61     assert(e.path1() == p1);
     62     assert(e.path2().empty());
     63   }
     64   // filesystem_error(const string& what_arg, const path&, const path&, error_code ec);
     65   {
     66     ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, p2, ec));
     67     filesystem_error e(what_arg, p1, p2, ec);
     68     CheckWhat(e);
     69     assert(e.code() == ec);
     70     assert(e.path1() == p1);
     71     assert(e.path2() == p2);
     72   }
     73 }
     74 
     75 void test_signatures()
     76 {
     77   using namespace fs;
     78   const path p;
     79   std::error_code ec;
     80   const filesystem_error e("lala", ec);
     81   // const path& path1() const noexcept;
     82   {
     83     ASSERT_SAME_TYPE(path const&, decltype(e.path1()));
     84     ASSERT_NOEXCEPT(e.path1());
     85   }
     86   // const path& path2() const noexcept
     87   {
     88     ASSERT_SAME_TYPE(path const&, decltype(e.path2()));
     89     ASSERT_NOEXCEPT(e.path2());
     90   }
     91   // const char* what() const noexcept
     92   {
     93     ASSERT_SAME_TYPE(const char*, decltype(e.what()));
     94     ASSERT_NOEXCEPT(e.what());
     95   }
     96 }
     97 
     98 int main() {
     99   static_assert(std::is_base_of<std::system_error, fs::filesystem_error>::value, "");
    100   test_constructors();
    101   test_signatures();
    102 }