filesystem_error.members.pass.cpp 3.0 KB

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