core-result.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* Unit testing for outcomes
  2. (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (30 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. #ifdef TESTING_WG21_EXPERIMENTAL_RESULT
  25. #include <boost/outcome/experimental/result.hpp>
  26. #define BOOST_OUTCOME_AUTO_TEST_CASE(...) BOOST_AUTO_TEST_CASE(__VA_ARGS__)
  27. #else
  28. #include <boost/outcome/result.hpp>
  29. #endif
  30. #include <boost/test/unit_test.hpp>
  31. #include <boost/test/unit_test_monitor.hpp>
  32. #include <iostream>
  33. #ifndef BOOST_NO_EXCEPTIONS
  34. // Custom error type with payload
  35. struct payload
  36. {
  37. boost::system::error_code ec;
  38. const char *str{nullptr};
  39. payload() = default;
  40. payload(boost::system::errc::errc_t _ec, const char *_str)
  41. : ec(make_error_code(_ec))
  42. , str(_str)
  43. {
  44. }
  45. };
  46. struct payload_exception : std::runtime_error
  47. {
  48. explicit payload_exception(const char *what)
  49. : std::runtime_error(what)
  50. {
  51. }
  52. };
  53. inline const boost::system::error_code &make_error_code(const payload &p)
  54. {
  55. return p.ec;
  56. }
  57. inline void outcome_throw_as_system_error_with_payload(const payload &p)
  58. {
  59. throw payload_exception(p.str);
  60. }
  61. #endif
  62. BOOST_OUTCOME_AUTO_TEST_CASE(works_result, "Tests that the result works as intended")
  63. {
  64. #ifdef TESTING_WG21_EXPERIMENTAL_RESULT
  65. using namespace std::experimental;
  66. using std::in_place_type;
  67. #else
  68. using namespace BOOST_OUTCOME_V2_NAMESPACE;
  69. #endif
  70. static_assert(std::is_constructible<result<long>, int>::value, "Sanity check that monad can be constructed from a value_type");
  71. static_assert(!std::is_constructible<result<result<long>>, int>::value, "Sanity check that outer monad can be constructed from an inner monad's value_type");
  72. static_assert(!std::is_constructible<result<result<result<long>>>, int>::value, "Sanity check that outer monad can be constructed from an inner inner monad's value_type");
  73. static_assert(!std::is_constructible<result<result<result<result<long>>>>, int>::value, "Sanity check that outer monad can be constructed from an inner inner monad's value_type");
  74. static_assert(std::is_constructible<result<int>, result<long>>::value, "Sanity check that compatible monads can be constructed from one another");
  75. static_assert(std::is_constructible<result<result<int>>, result<long>>::value, "Sanity check that outer monad can be constructed from a compatible monad");
  76. static_assert(!std::is_constructible<result<result<result<int>>>, result<long>>::value, "Sanity check that outer monad can be constructed from a compatible monad up to two nestings deep");
  77. static_assert(!std::is_constructible<result<result<result<result<int>>>>, result<long>>::value, "Sanity check that outer monad can be constructed from a compatible monad three or more nestings deep");
  78. static_assert(!std::is_constructible<result<std::string>, result<int>>::value, "Sanity check that incompatible monads cannot be constructed from one another");
  79. #ifndef TESTING_WG21_EXPERIMENTAL_RESULT
  80. static_assert(std::is_constructible<result<int>, result<void>>::value, "Sanity check that all monads can be constructed from a void monad");
  81. static_assert(std::is_constructible<result<result<int>>, result<void>>::value, "Sanity check that outer monad can be constructed from a compatible monad");
  82. static_assert(std::is_constructible<result<result<result<int>>>, result<void>>::value, "Sanity check that outer monad can be constructed from a compatible monad up to two nestings deep");
  83. static_assert(!std::is_constructible<result<void>, result<int>>::value, "Sanity check that incompatible monads cannot be constructed from one another");
  84. #endif
  85. static_assert(std::is_void<result<void>::value_type>::value, "Sanity check that result<void> has a void value_type");
  86. #ifndef TESTING_WG21_EXPERIMENTAL_RESULT
  87. static_assert(std::is_void<result<void, void>::error_type>::value, "Sanity check that result<void, void> has a void error_type");
  88. #endif
  89. static_assert(std::is_same<result<int>::value_type, int>::value, "Sanity check that result<int> has a int value_type");
  90. static_assert(std::is_same<result<int>::error_type, boost::system::error_code>::value, "Sanity check that result<int> has a error_code error_type");
  91. { // errored int
  92. result<int> m(boost::system::errc::bad_address);
  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(), boost::system::system_error);
  98. BOOST_CHECK_NO_THROW(m.error());
  99. }
  100. { // errored void
  101. result<void> m(boost::system::errc::bad_address);
  102. BOOST_CHECK(!m);
  103. BOOST_CHECK(!m.has_value());
  104. BOOST_CHECK(m.has_error());
  105. // BOOST_CHECK(!m.has_exception());
  106. #ifndef TESTING_WG21_EXPERIMENTAL_RESULT
  107. BOOST_CHECK_THROW(([&m]() -> void { return m.value(); }()), boost::system::system_error);
  108. #endif
  109. BOOST_CHECK_NO_THROW(m.error());
  110. }
  111. { // valued int
  112. result<int> m(5);
  113. BOOST_CHECK(m);
  114. BOOST_CHECK(m.has_value());
  115. BOOST_CHECK(!m.has_error());
  116. // BOOST_CHECK(!m.has_exception());
  117. BOOST_CHECK(m.value() == 5);
  118. m.value() = 6;
  119. BOOST_CHECK(m.value() == 6);
  120. BOOST_CHECK_THROW(m.error(), bad_result_access);
  121. }
  122. { // valued bool
  123. result<bool> m(false);
  124. BOOST_CHECK(m);
  125. BOOST_CHECK(m.has_value());
  126. BOOST_CHECK(!m.has_error());
  127. // BOOST_CHECK(!m.has_exception());
  128. BOOST_CHECK(m.value() == false);
  129. m.value() = true;
  130. BOOST_CHECK(m.value() == true);
  131. BOOST_CHECK_THROW(m.error(), bad_result_access);
  132. }
  133. { // moves do not clear state
  134. result<std::string> m("niall");
  135. BOOST_CHECK(m);
  136. BOOST_CHECK(m.has_value());
  137. BOOST_CHECK(!m.has_error());
  138. // BOOST_CHECK(!m.has_exception());
  139. BOOST_CHECK(m.value() == "niall");
  140. m.value() = "NIALL";
  141. BOOST_CHECK(m.value() == "NIALL");
  142. auto temp(std::move(m).value());
  143. BOOST_CHECK(temp == "NIALL");
  144. BOOST_CHECK(m.value().empty()); // NOLINT
  145. }
  146. { // valued void
  147. result<void> m(in_place_type<void>);
  148. BOOST_CHECK(m);
  149. BOOST_CHECK(m.has_value());
  150. BOOST_CHECK(!m.has_error());
  151. // BOOST_CHECK(!m.has_exception());
  152. BOOST_CHECK_NO_THROW(m.value()); // works, but type returned is unusable
  153. BOOST_CHECK_THROW(m.error(), bad_result_access);
  154. }
  155. { // errored
  156. boost::system::error_code ec(5, boost::system::system_category());
  157. result<int> m(ec);
  158. BOOST_CHECK(!m);
  159. BOOST_CHECK(!m.has_value());
  160. BOOST_CHECK(m.has_error());
  161. // BOOST_CHECK(!m.has_exception());
  162. BOOST_CHECK_THROW(m.value(), boost::system::system_error);
  163. BOOST_CHECK(m.error() == ec);
  164. }
  165. #if !defined(__APPLE__) || defined(__cpp_exceptions)
  166. { // errored, custom
  167. boost::system::error_code ec(5, boost::system::system_category());
  168. auto e = boost::copy_exception(boost::system::system_error(ec)); // NOLINT
  169. result<int, boost::exception_ptr> m(e);
  170. BOOST_CHECK(!m);
  171. BOOST_CHECK(!m.has_value());
  172. BOOST_CHECK(m.has_error());
  173. // BOOST_CHECK(!m.has_exception());
  174. BOOST_CHECK_THROW(m.value(), boost::system::system_error);
  175. BOOST_CHECK(m.error() == e);
  176. }
  177. #endif
  178. #ifndef TESTING_WG21_EXPERIMENTAL_RESULT
  179. { // custom error type
  180. struct Foo
  181. {
  182. };
  183. result<int, Foo> m(in_place_type<Foo>);
  184. BOOST_CHECK(!m);
  185. BOOST_CHECK(!m.has_value());
  186. BOOST_CHECK(m.has_error());
  187. // BOOST_CHECK(!m.has_exception());
  188. // BOOST_CHECK_NO_THROW(m.value());
  189. // BOOST_CHECK_NO_THROW(m.error());
  190. }
  191. if(false) // NOLINT
  192. { // void, void is permitted, but is not constructible
  193. result<void, void> *m = nullptr;
  194. m->value();
  195. m->error();
  196. }
  197. #endif
  198. {
  199. // Deliberately define non-trivial operations
  200. struct udt
  201. {
  202. int _v{0};
  203. udt() = default;
  204. udt(udt &&o) noexcept : _v(o._v) {}
  205. udt(const udt &o) // NOLINT
  206. : _v(o._v)
  207. {
  208. }
  209. udt &operator=(udt &&o) noexcept
  210. {
  211. _v = o._v;
  212. return *this;
  213. }
  214. udt &operator=(const udt &o) // NOLINT
  215. {
  216. _v = o._v;
  217. return *this;
  218. }
  219. ~udt() { _v = 0; }
  220. };
  221. // No default construction, no copy nor move
  222. struct udt2
  223. {
  224. udt2() = delete;
  225. udt2(udt2 &&) = delete;
  226. udt2(const udt2 &) = delete;
  227. udt2 &operator=(udt2 &&) = delete;
  228. udt2 &operator=(const udt2 &) = delete;
  229. explicit udt2(int /*unused*/) {}
  230. ~udt2() = default;
  231. };
  232. // Can only be constructed via multiple args
  233. struct udt3
  234. {
  235. udt3() = delete;
  236. udt3(udt3 &&) = delete;
  237. udt3(const udt3 &) = delete;
  238. udt3 &operator=(udt3 &&) = delete;
  239. udt3 &operator=(const udt3 &) = delete;
  240. explicit udt3(int /*unused*/, const char * /*unused*/, std::nullptr_t /*unused*/) {}
  241. ~udt3() = default;
  242. };
  243. result<int> a(5);
  244. result<int> b(make_error_code(boost::system::errc::invalid_argument));
  245. std::cout << sizeof(a) << std::endl; // 32 bytes
  246. if(false) // NOLINT
  247. {
  248. b.assume_value();
  249. a.assume_error();
  250. }
  251. #ifndef BOOST_NO_EXCEPTIONS
  252. try
  253. {
  254. b.value();
  255. std::cerr << "fail" << std::endl;
  256. std::terminate();
  257. }
  258. catch(const boost::system::system_error & /*unused*/)
  259. {
  260. }
  261. #endif
  262. static_assert(!std::is_default_constructible<decltype(a)>::value, "");
  263. static_assert(!std::is_nothrow_default_constructible<decltype(a)>::value, "");
  264. static_assert(std::is_copy_constructible<decltype(a)>::value, "");
  265. // Quality of implementation of std::optional is poor :(
  266. #ifndef TESTING_WG21_EXPERIMENTAL_RESULT
  267. static_assert(std::is_trivially_copy_constructible<decltype(a)>::value, "");
  268. static_assert(std::is_nothrow_copy_constructible<decltype(a)>::value, "");
  269. static_assert(std::is_copy_assignable<decltype(a)>::value, "");
  270. static_assert(std::is_trivially_copy_assignable<decltype(a)>::value, "");
  271. static_assert(std::is_nothrow_copy_assignable<decltype(a)>::value, "");
  272. #endif
  273. static_assert(std::is_trivially_destructible<decltype(a)>::value, "");
  274. static_assert(std::is_nothrow_destructible<decltype(a)>::value, "");
  275. // Test void compiles
  276. result<void> c(in_place_type<void>);
  277. result<void> c2(c);
  278. (void) c2;
  279. // Test a standard udt compiles
  280. result<udt> d(in_place_type<udt>);
  281. result<udt> d2(d);
  282. static_assert(!std::is_default_constructible<decltype(d)>::value, "");
  283. static_assert(!std::is_nothrow_default_constructible<decltype(d)>::value, "");
  284. static_assert(std::is_copy_constructible<decltype(d)>::value, "");
  285. static_assert(!std::is_trivially_copy_constructible<decltype(d)>::value, "");
  286. static_assert(!std::is_nothrow_copy_constructible<decltype(d)>::value, "");
  287. static_assert(std::is_copy_assignable<decltype(d)>::value, "");
  288. static_assert(!std::is_trivially_copy_assignable<decltype(d)>::value, "");
  289. static_assert(!std::is_nothrow_copy_assignable<decltype(d)>::value, "");
  290. static_assert(std::is_move_assignable<decltype(d)>::value, "");
  291. static_assert(!std::is_trivially_move_assignable<decltype(d)>::value, "");
  292. static_assert(std::is_nothrow_move_assignable<decltype(d)>::value, "");
  293. static_assert(!std::is_trivially_destructible<decltype(d)>::value, "");
  294. static_assert(std::is_nothrow_destructible<decltype(d)>::value, "");
  295. // Test a highly pathological udt compiles
  296. result<udt2> e(in_place_type<udt2>, 5);
  297. // result<udt2> e2(e);
  298. static_assert(!std::is_default_constructible<decltype(e)>::value, "");
  299. static_assert(!std::is_nothrow_default_constructible<decltype(e)>::value, "");
  300. static_assert(!std::is_copy_constructible<decltype(e)>::value, "");
  301. static_assert(!std::is_trivially_copy_constructible<decltype(e)>::value, "");
  302. static_assert(!std::is_nothrow_copy_constructible<decltype(e)>::value, "");
  303. static_assert(!std::is_copy_assignable<decltype(e)>::value, "");
  304. static_assert(!std::is_trivially_copy_assignable<decltype(e)>::value, "");
  305. static_assert(!std::is_nothrow_copy_assignable<decltype(e)>::value, "");
  306. static_assert(!std::is_move_assignable<decltype(e)>::value, "");
  307. static_assert(!std::is_trivially_move_assignable<decltype(e)>::value, "");
  308. static_assert(!std::is_nothrow_move_assignable<decltype(e)>::value, "");
  309. // Test a udt which can only be constructed in place compiles
  310. result<udt3> g(in_place_type<udt3>, 5, static_cast<const char *>("niall"), nullptr);
  311. // Does converting inplace construction also work?
  312. result<udt3> h(5, static_cast<const char *>("niall"), nullptr);
  313. result<udt3> i(ENOMEM, boost::system::generic_category());
  314. BOOST_CHECK(h.has_value());
  315. BOOST_CHECK(i.has_error());
  316. }
  317. // Test direct use of error code enum works
  318. {
  319. constexpr result<int, boost::system::errc::errc_t> a(5), b(boost::system::errc::invalid_argument);
  320. static_assert(a.value() == 5, "a is not 5");
  321. static_assert(b.error() == boost::system::errc::invalid_argument, "b is not errored");
  322. BOOST_CHECK_THROW(b.value(), boost::system::system_error);
  323. }
  324. #ifndef TESTING_WG21_EXPERIMENTAL_RESULT
  325. #ifndef BOOST_NO_EXCEPTIONS
  326. // Test payload facility
  327. {
  328. const char *niall = "niall";
  329. result<int, payload> b{boost::system::errc::invalid_argument, niall};
  330. try
  331. {
  332. b.value();
  333. BOOST_CHECK(false);
  334. }
  335. catch(const payload_exception &e)
  336. {
  337. BOOST_CHECK(!strcmp(e.what(), niall));
  338. }
  339. catch(...)
  340. {
  341. BOOST_CHECK(false);
  342. }
  343. }
  344. #endif
  345. #endif
  346. }