filesystem_error.members.pass.cpp 3.0 KB

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