filesystem_error.members.pass.cpp 3.0 KB

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