core-outcome.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Unit testing for outcomes
  2. (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (18 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. #include <iostream>
  28. #ifdef _MSC_VER
  29. #pragma warning(disable : 4702) // unreachable code
  30. #endif
  31. BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome, "Tests that the outcome works as intended")
  32. {
  33. using namespace BOOST_OUTCOME_V2_NAMESPACE;
  34. static_assert(std::is_constructible<outcome<long>, int>::value, "Sanity check that monad can be constructed from a value_type");
  35. static_assert(!std::is_constructible<outcome<outcome<long>>, int>::value, "Sanity check that outer monad can be constructed from an inner monad's value_type");
  36. static_assert(!std::is_constructible<outcome<outcome<outcome<long>>>, int>::value, "Sanity check that outer monad can be constructed from an inner inner monad's value_type");
  37. static_assert(!std::is_constructible<outcome<outcome<outcome<outcome<long>>>>, int>::value, "Sanity check that outer monad can be constructed from an inner inner monad's value_type");
  38. static_assert(std::is_constructible<outcome<int>, outcome<long>>::value, "Sanity check that compatible monads can be constructed from one another");
  39. static_assert(std::is_constructible<outcome<outcome<int>>, outcome<long>>::value, "Sanity check that outer monad can be constructed from a compatible monad");
  40. static_assert(!std::is_constructible<outcome<outcome<outcome<int>>>, outcome<long>>::value, "Sanity check that outer monad can be constructed from a compatible monad up to two nestings deep");
  41. static_assert(!std::is_constructible<outcome<outcome<outcome<outcome<int>>>>, outcome<long>>::value, "Sanity check that outer monad can be constructed from a compatible monad three or more nestings deep");
  42. static_assert(!std::is_constructible<outcome<std::string>, outcome<int>>::value, "Sanity check that incompatible monads cannot be constructed from one another");
  43. static_assert(std::is_constructible<outcome<int>, outcome<void>>::value, "Sanity check that all monads can be constructed from a void monad");
  44. static_assert(std::is_constructible<outcome<outcome<int>>, outcome<void>>::value, "Sanity check that outer monad can be constructed from a compatible monad");
  45. static_assert(std::is_constructible<outcome<outcome<outcome<int>>>, outcome<void>>::value, "Sanity check that outer monad can be constructed from a compatible monad up to two nestings deep");
  46. static_assert(!std::is_constructible<outcome<void>, outcome<int>>::value, "Sanity check that incompatible monads cannot be constructed from one another");
  47. static_assert(std::is_void<result<void>::value_type>::value, "Sanity check that result<void> has a void value_type");
  48. static_assert(std::is_void<result<void, void>::error_type>::value, "Sanity check that result<void, void> has a void error_type");
  49. // static_assert(std::is_void<outcome<void, void, void>::exception_type>::value, "Sanity check that outcome<void, void, void> has a void exception_type");
  50. static_assert(std::is_same<outcome<int>::value_type, int>::value, "Sanity check that outcome<int> has a int value_type");
  51. static_assert(std::is_same<outcome<int>::error_type, boost::system::error_code>::value, "Sanity check that outcome<int> has a error_code error_type");
  52. static_assert(std::is_same<outcome<int>::exception_type, boost::exception_ptr>::value, "Sanity check that outcome<int> has a exception_ptr exception_type");
  53. { // errored int
  54. outcome<int> m(boost::system::errc::bad_address);
  55. BOOST_CHECK(!m);
  56. BOOST_CHECK(!m.has_value());
  57. BOOST_CHECK(m.has_error());
  58. BOOST_CHECK(!m.has_exception());
  59. BOOST_CHECK_THROW(m.value(), boost::system::system_error);
  60. BOOST_CHECK_NO_THROW(m.error());
  61. BOOST_CHECK_THROW(m.exception(), bad_outcome_access);
  62. BOOST_CHECK_THROW(boost::rethrow_exception(m.failure()), boost::system::system_error);
  63. }
  64. { // errored void
  65. outcome<void> m(boost::system::errc::bad_address);
  66. BOOST_CHECK(!m);
  67. BOOST_CHECK(!m.has_value());
  68. BOOST_CHECK(m.has_error());
  69. BOOST_CHECK(!m.has_exception());
  70. BOOST_CHECK_THROW(([&m]() -> void { return m.value(); }()), boost::system::system_error);
  71. BOOST_CHECK_NO_THROW(m.error());
  72. BOOST_CHECK_THROW(m.exception(), bad_outcome_access);
  73. BOOST_CHECK_THROW(boost::rethrow_exception(m.failure()), boost::system::system_error);
  74. }
  75. { // valued int
  76. outcome<int> m(5);
  77. BOOST_CHECK(m);
  78. BOOST_CHECK(m.has_value());
  79. BOOST_CHECK(!m.has_error());
  80. BOOST_CHECK(!m.has_exception());
  81. BOOST_CHECK(m.value() == 5);
  82. m.value() = 6;
  83. BOOST_CHECK(m.value() == 6);
  84. BOOST_CHECK_THROW(m.error(), bad_outcome_access);
  85. BOOST_CHECK_THROW(m.exception(), bad_outcome_access);
  86. BOOST_CHECK(!m.failure());
  87. }
  88. { // moves do not clear state
  89. outcome<std::string> m("niall");
  90. BOOST_CHECK(m);
  91. BOOST_CHECK(m.has_value());
  92. BOOST_CHECK(!m.has_error());
  93. BOOST_CHECK(!m.has_exception());
  94. BOOST_CHECK(m.value() == "niall");
  95. m.value() = "NIALL";
  96. BOOST_CHECK(m.value() == "NIALL");
  97. auto temp(std::move(m).value());
  98. BOOST_CHECK(temp == "NIALL");
  99. BOOST_CHECK(m.value().empty()); // NOLINT
  100. }
  101. { // valued void
  102. outcome<void> m(in_place_type<void>);
  103. BOOST_CHECK(m);
  104. BOOST_CHECK(m.has_value());
  105. BOOST_CHECK(!m.has_error());
  106. BOOST_CHECK(!m.has_exception());
  107. BOOST_CHECK_NO_THROW(m.value()); // works, but type returned is unusable
  108. BOOST_CHECK_THROW(m.error(), bad_outcome_access);
  109. BOOST_CHECK_THROW(m.exception(), bad_outcome_access);
  110. BOOST_CHECK(!m.failure());
  111. }
  112. { // errored
  113. boost::system::error_code ec(5, boost::system::system_category());
  114. outcome<int> m(ec);
  115. BOOST_CHECK(!m);
  116. BOOST_CHECK(!m.has_value());
  117. BOOST_CHECK(m.has_error());
  118. BOOST_CHECK(!m.has_exception());
  119. BOOST_CHECK_THROW(m.value(), boost::system::system_error);
  120. BOOST_CHECK(m.error() == ec);
  121. BOOST_CHECK_THROW(m.exception(), bad_outcome_access);
  122. #ifndef BOOST_NO_EXCEPTIONS
  123. BOOST_CHECK(m.failure());
  124. try
  125. {
  126. boost::rethrow_exception(m.failure());
  127. }
  128. catch(const boost::system::system_error &ex)
  129. {
  130. BOOST_CHECK(ex.code() == ec);
  131. BOOST_CHECK(ex.code().value() == 5);
  132. }
  133. #endif
  134. }
  135. #if !defined(__APPLE__) || defined(__cpp_exceptions)
  136. { // excepted
  137. boost::system::error_code ec(5, boost::system::system_category());
  138. auto e = boost::copy_exception(boost::system::system_error(ec)); // NOLINT
  139. outcome<int> m(e);
  140. BOOST_CHECK(!m);
  141. BOOST_CHECK(!m.has_value());
  142. BOOST_CHECK(!m.has_error());
  143. BOOST_CHECK(m.has_exception());
  144. BOOST_CHECK_THROW(m.value(), boost::system::system_error);
  145. BOOST_CHECK_THROW(m.error(), bad_outcome_access);
  146. BOOST_CHECK(m.exception() == e);
  147. #ifndef BOOST_NO_EXCEPTIONS
  148. BOOST_CHECK(m.failure());
  149. try
  150. {
  151. boost::rethrow_exception(m.failure());
  152. }
  153. catch(const boost::system::system_error &ex)
  154. {
  155. BOOST_CHECK(ex.code() == ec);
  156. BOOST_CHECK(ex.code().value() == 5);
  157. }
  158. #endif
  159. }
  160. { // custom error type
  161. struct Foo
  162. {
  163. };
  164. auto e = boost::copy_exception(Foo());
  165. outcome<int> m(e);
  166. BOOST_CHECK(!m);
  167. BOOST_CHECK(!m.has_value());
  168. BOOST_CHECK(!m.has_error());
  169. BOOST_CHECK(m.has_exception());
  170. BOOST_CHECK_THROW(m.value(), Foo);
  171. BOOST_CHECK_THROW(m.error(), bad_outcome_access);
  172. BOOST_CHECK(m.exception() == e);
  173. }
  174. { // outcome<void, void> should work
  175. boost::system::error_code ec(5, boost::system::system_category());
  176. auto e = boost::copy_exception(boost::system::system_error(ec));
  177. outcome<void, void> m(e);
  178. BOOST_CHECK(!m);
  179. BOOST_CHECK(!m.has_value());
  180. BOOST_CHECK(!m.has_error());
  181. BOOST_CHECK(m.has_exception());
  182. }
  183. #endif
  184. {
  185. outcome<int> a(5);
  186. outcome<int> b(make_error_code(boost::system::errc::invalid_argument));
  187. std::cout << sizeof(a) << std::endl; // 40 bytes
  188. a.assume_value();
  189. b.assume_error();
  190. #ifndef BOOST_NO_EXCEPTIONS
  191. try
  192. {
  193. b.value();
  194. std::cerr << "fail" << std::endl;
  195. std::terminate();
  196. }
  197. catch(const boost::system::system_error & /*unused*/)
  198. {
  199. }
  200. #endif
  201. static_assert(!std::is_default_constructible<decltype(a)>::value, "");
  202. static_assert(!std::is_nothrow_default_constructible<decltype(a)>::value, "");
  203. static_assert(std::is_copy_constructible<decltype(a)>::value, "");
  204. static_assert(!std::is_trivially_copy_constructible<decltype(a)>::value, "");
  205. static_assert(std::is_nothrow_copy_constructible<decltype(a)>::value, "");
  206. static_assert(std::is_copy_assignable<decltype(a)>::value, "");
  207. static_assert(!std::is_trivially_copy_assignable<decltype(a)>::value, "");
  208. static_assert(std::is_nothrow_copy_assignable<decltype(a)>::value, "");
  209. static_assert(!std::is_trivially_destructible<decltype(a)>::value, "");
  210. static_assert(std::is_nothrow_destructible<decltype(a)>::value, "");
  211. // Test void compiles
  212. outcome<void> c(in_place_type<void>);
  213. outcome<void> c2(c);
  214. (void) c2;
  215. // Test int, void compiles
  216. outcome<int, void> d(in_place_type<boost::exception_ptr>);
  217. }
  218. {
  219. // Can only be constructed via multiple args
  220. struct udt3
  221. {
  222. udt3() = delete;
  223. udt3(udt3 &&) = delete;
  224. udt3(const udt3 &) = delete;
  225. udt3 &operator=(udt3 &&) = delete;
  226. udt3 &operator=(const udt3 &) = delete;
  227. explicit udt3(int /*unused*/, const char * /*unused*/, std::nullptr_t /*unused*/) {}
  228. ~udt3() = default;
  229. };
  230. // Test a udt which can only be constructed in place compiles
  231. outcome<udt3> g(in_place_type<udt3>, 5, static_cast<const char *>("niall"), nullptr);
  232. // Does converting inplace construction also work?
  233. outcome<udt3> h(5, static_cast<const char *>("niall"), nullptr);
  234. outcome<udt3> i(ENOMEM, boost::system::generic_category());
  235. BOOST_CHECK(h.has_value());
  236. BOOST_CHECK(i.has_error());
  237. }
  238. }