issue0010.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Unit testing for outcomes
  2. (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (9 commits)
  3. Boost Software License - Version 1.0 - August 17th, 2003
  4. Permission is hereby granted, free of charge, to any person or organization
  5. obtaining a copy of the software and accompanying documentation covered by
  6. this license (the "Software") to use, reproduce, display, distribute,
  7. execute, and transmit the Software, and to prepare derivative works of the
  8. Software, and to permit third-parties to whom the Software is furnished to
  9. do so, all subject to the following:
  10. The copyright notices in the Software and this entire statement, including
  11. the above license grant, this restriction and the following disclaimer,
  12. must be included in all copies of the Software, in whole or in part, and
  13. all derivative works of the Software, unless such copies or derivative
  14. works are solely in the form of machine-executable object code generated by
  15. a source language processor.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  19. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  20. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <boost/outcome/outcome.hpp>
  25. #include <boost/test/unit_test.hpp>
  26. #include <boost/test/unit_test_monitor.hpp>
  27. BOOST_OUTCOME_AUTO_TEST_CASE(issues_0010_test, "Expected's operator->(), operator*() and .error() throw exceptions when they should not")
  28. {
  29. using namespace BOOST_OUTCOME_V2_NAMESPACE;
  30. const char *a = "hi", *b = "bye";
  31. struct udt1 // NOLINT
  32. {
  33. const char *_v{nullptr};
  34. udt1() = default;
  35. constexpr explicit udt1(const char *v) noexcept : _v(v) {}
  36. constexpr udt1(udt1 &&o) noexcept : _v(o._v) { o._v = nullptr; }
  37. udt1(const udt1 &) = default;
  38. constexpr udt1 &operator=(udt1 &&o) noexcept
  39. {
  40. _v = o._v;
  41. o._v = nullptr;
  42. return *this;
  43. }
  44. udt1 &operator=(const udt1 &) = delete;
  45. constexpr const char *operator*() const noexcept { return _v; }
  46. };
  47. struct udt2 // NOLINT
  48. {
  49. const char *_v{nullptr};
  50. udt2() = default;
  51. constexpr explicit udt2(const char *v) noexcept : _v(v) {}
  52. constexpr udt2(udt2 &&o) noexcept : _v(o._v) { o._v = nullptr; }
  53. udt2(const udt2 &) = default;
  54. constexpr udt2 &operator=(udt2 &&o) noexcept
  55. {
  56. _v = o._v;
  57. o._v = nullptr;
  58. return *this;
  59. }
  60. udt1 &operator=(const udt1 &) = delete;
  61. constexpr const char *operator*() const noexcept { return _v; }
  62. };
  63. result<udt1, udt2> p(udt1{a});
  64. result<udt1, udt2> n(udt2{b});
  65. // State check
  66. BOOST_CHECK(p.has_value());
  67. BOOST_CHECK(!n.has_value());
  68. // These should behave as expected (!)
  69. // BOOST_CHECK_NO_THROW(p.value());
  70. // BOOST_CHECK_NO_THROW(n.value());
  71. // And state is not destroyed
  72. BOOST_CHECK(p.has_value() && *p.assume_value() == a);
  73. BOOST_CHECK(!n.has_value() && *n.assume_error() == b);
  74. // LEWG Expected provides rvalue ref semantics for operator*(), error() and error_or()
  75. udt1 a1(std::move(p.assume_value()));
  76. BOOST_CHECK(*a1 == a);
  77. BOOST_CHECK(*p.assume_value() == nullptr);
  78. udt2 e2(std::move(n).assume_error());
  79. BOOST_CHECK(*e2 == b);
  80. BOOST_CHECK(*n.assume_error() == nullptr); // NOLINT
  81. }