experimental-core-outcome-status.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Unit testing for outcomes
  2. (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (4 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/experimental/status_outcome.hpp>
  25. #define BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND std
  26. template <class T, class S = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::system_code, class P = BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::exception_ptr> using outcome = BOOST_OUTCOME_V2_NAMESPACE::experimental::status_outcome<T, S, P>;
  27. using BOOST_OUTCOME_V2_NAMESPACE::in_place_type;
  28. #include <boost/test/unit_test.hpp>
  29. #include <boost/test/unit_test_monitor.hpp>
  30. #include <iostream>
  31. #ifdef _MSC_VER
  32. #pragma warning(disable : 4702) // unreachable code
  33. #endif
  34. BOOST_OUTCOME_AUTO_TEST_CASE(works_status_code_outcome, "Tests that the outcome with status_code works as intended")
  35. {
  36. using namespace BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE;
  37. { // errored int
  38. outcome<int> m(generic_code{errc::bad_address});
  39. BOOST_CHECK(!m);
  40. BOOST_CHECK(!m.has_value());
  41. BOOST_CHECK(m.has_error());
  42. BOOST_CHECK(!m.has_exception());
  43. BOOST_CHECK_THROW(m.value(), status_error<void>);
  44. BOOST_CHECK_NO_THROW(m.error());
  45. BOOST_CHECK_THROW(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m.failure()), generic_error);
  46. }
  47. { // errored void
  48. outcome<void> m(generic_code{errc::bad_address});
  49. BOOST_CHECK(!m);
  50. BOOST_CHECK(!m.has_value());
  51. BOOST_CHECK(m.has_error());
  52. BOOST_CHECK(!m.has_exception());
  53. BOOST_CHECK_THROW(([&m]() -> void { return m.value(); }()), generic_error);
  54. BOOST_CHECK_NO_THROW(m.error());
  55. BOOST_CHECK_THROW(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m.failure()), generic_error);
  56. }
  57. { // valued int
  58. outcome<int> m(5);
  59. BOOST_CHECK(m);
  60. BOOST_CHECK(m.has_value());
  61. BOOST_CHECK(!m.has_error());
  62. BOOST_CHECK(!m.has_exception());
  63. BOOST_CHECK(m.value() == 5);
  64. m.value() = 6;
  65. BOOST_CHECK(m.value() == 6);
  66. BOOST_CHECK(!m.failure());
  67. }
  68. { // moves do not clear state
  69. outcome<std::string> m("niall");
  70. BOOST_CHECK(m);
  71. BOOST_CHECK(m.has_value());
  72. BOOST_CHECK(!m.has_error());
  73. BOOST_CHECK(!m.has_exception());
  74. BOOST_CHECK(m.value() == "niall");
  75. m.value() = "NIALL";
  76. BOOST_CHECK(m.value() == "NIALL");
  77. auto temp(std::move(m).value());
  78. BOOST_CHECK(temp == "NIALL");
  79. BOOST_CHECK(m.value().empty()); // NOLINT
  80. }
  81. { // valued void
  82. outcome<void> m(in_place_type<void>);
  83. BOOST_CHECK(m);
  84. BOOST_CHECK(m.has_value());
  85. BOOST_CHECK(!m.has_error());
  86. BOOST_CHECK(!m.has_exception());
  87. BOOST_CHECK_NO_THROW(m.value()); // works, but type returned is unusable
  88. BOOST_CHECK(!m.failure());
  89. }
  90. { // errored
  91. error ec(errc::no_link);
  92. outcome<int> m(ec.clone());
  93. BOOST_CHECK(!m);
  94. BOOST_CHECK(!m.has_value());
  95. BOOST_CHECK(m.has_error());
  96. BOOST_CHECK(!m.has_exception());
  97. BOOST_CHECK_THROW(m.value(), generic_error);
  98. BOOST_CHECK(m.error() == ec);
  99. #ifndef BOOST_NO_EXCEPTIONS
  100. BOOST_CHECK(m.failure());
  101. try
  102. {
  103. BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m.failure());
  104. }
  105. catch(const generic_error &ex)
  106. {
  107. BOOST_CHECK(ex.code() == ec);
  108. BOOST_CHECK(ex.code().value() == errc::no_link);
  109. }
  110. #endif
  111. }
  112. #if !defined(__APPLE__) || defined(__cpp_exceptions)
  113. { // excepted
  114. BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::error_code ec(5, BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_category());
  115. auto e = BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::make_exception_ptr(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error(ec)); // NOLINT
  116. outcome<int> m(e);
  117. BOOST_CHECK(!m);
  118. BOOST_CHECK(!m.has_value());
  119. BOOST_CHECK(!m.has_error());
  120. BOOST_CHECK(m.has_exception());
  121. BOOST_CHECK_THROW(m.value(), BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error);
  122. BOOST_CHECK(m.exception() == e);
  123. #ifndef BOOST_NO_EXCEPTIONS
  124. BOOST_CHECK(m.failure());
  125. try
  126. {
  127. BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m.failure());
  128. }
  129. catch(const BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error &ex)
  130. {
  131. BOOST_CHECK(ex.code() == ec);
  132. BOOST_CHECK(ex.code().value() == 5);
  133. }
  134. #endif
  135. }
  136. { // custom error type
  137. struct Foo
  138. {
  139. };
  140. auto e = BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::make_exception_ptr(Foo());
  141. outcome<int> m(e);
  142. BOOST_CHECK(!m);
  143. BOOST_CHECK(!m.has_value());
  144. BOOST_CHECK(!m.has_error());
  145. BOOST_CHECK(m.has_exception());
  146. BOOST_CHECK_THROW(m.value(), Foo);
  147. BOOST_CHECK(m.exception() == e);
  148. }
  149. { // outcome<void, void> should work
  150. BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::error_code ec(5, BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_category());
  151. auto e = BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::make_exception_ptr(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error(ec));
  152. outcome<void, void> m(e);
  153. BOOST_CHECK(!m);
  154. BOOST_CHECK(!m.has_value());
  155. BOOST_CHECK(!m.has_error());
  156. BOOST_CHECK(m.has_exception());
  157. }
  158. #endif
  159. {
  160. outcome<int> a(5);
  161. outcome<int> b(generic_code{errc::invalid_argument});
  162. std::cout << sizeof(a) << std::endl; // 40 bytes
  163. a.assume_value();
  164. b.assume_error();
  165. #ifndef BOOST_NO_EXCEPTIONS
  166. try
  167. {
  168. b.value();
  169. std::cerr << "fail" << std::endl;
  170. std::terminate();
  171. }
  172. catch(const generic_error & /*unused*/)
  173. {
  174. }
  175. #endif
  176. static_assert(!std::is_default_constructible<decltype(a)>::value, "");
  177. static_assert(!std::is_nothrow_default_constructible<decltype(a)>::value, "");
  178. static_assert(!std::is_copy_constructible<decltype(a)>::value, "");
  179. static_assert(!std::is_trivially_copy_constructible<decltype(a)>::value, "");
  180. static_assert(!std::is_nothrow_copy_constructible<decltype(a)>::value, "");
  181. static_assert(!std::is_copy_assignable<decltype(a)>::value, "");
  182. static_assert(!std::is_trivially_copy_assignable<decltype(a)>::value, "");
  183. static_assert(!std::is_nothrow_copy_assignable<decltype(a)>::value, "");
  184. static_assert(!std::is_trivially_destructible<decltype(a)>::value, "");
  185. static_assert(std::is_nothrow_destructible<decltype(a)>::value, "");
  186. // Test void compiles
  187. outcome<void> c(in_place_type<void>);
  188. // Test int, void compiles
  189. outcome<int, void> d(in_place_type<BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::exception_ptr>);
  190. }
  191. {
  192. // Can only be constructed via multiple args
  193. struct udt3
  194. {
  195. udt3() = delete;
  196. udt3(udt3 &&) = delete;
  197. udt3(const udt3 &) = delete;
  198. udt3 &operator=(udt3 &&) = delete;
  199. udt3 &operator=(const udt3 &) = delete;
  200. explicit udt3(int /*unused*/, const char * /*unused*/, std::nullptr_t /*unused*/) {}
  201. ~udt3() = default;
  202. };
  203. // Test a udt which can only be constructed in place compiles
  204. outcome<udt3> g(in_place_type<udt3>, 5, static_cast<const char *>("niall"), nullptr);
  205. // Does converting inplace construction also work?
  206. outcome<udt3> h(5, static_cast<const char *>("niall"), nullptr);
  207. BOOST_CHECK(h.has_value());
  208. }
  209. }