udts.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Unit testing for outcomes
  2. (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (7 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 _MSC_VER
  25. #pragma warning(disable : 4702) // unreachable code
  26. #endif
  27. #include <boost/outcome/outcome.hpp>
  28. #include <boost/test/unit_test.hpp>
  29. #include <boost/test/unit_test_monitor.hpp>
  30. BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_udts, "Tests that the outcome works as intended with user-defined types")
  31. {
  32. using namespace BOOST_OUTCOME_V2_NAMESPACE;
  33. // No default constructor, no copy/move, no assignment
  34. {
  35. struct udt
  36. {
  37. int a;
  38. explicit udt(int _a)
  39. : a(_a)
  40. {
  41. }
  42. udt() = delete;
  43. udt(const udt &) = delete;
  44. udt(udt &&) = delete;
  45. udt &operator=(const udt &) = delete;
  46. udt &operator=(udt &&) = delete;
  47. ~udt() = default;
  48. };
  49. outcome<udt> foo(in_place_type<udt>, 5);
  50. BOOST_CHECK(5 == foo.value().a);
  51. }
  52. #ifndef BOOST_NO_EXCEPTIONS
  53. // Emplace construct, throws during move and copy
  54. {
  55. struct udt
  56. {
  57. std::string a;
  58. explicit udt(std::string _a)
  59. : a(std::move(_a))
  60. {
  61. }
  62. udt() = delete;
  63. udt(const udt & /*unused*/) { throw std::logic_error("copy"); }
  64. udt(udt && /*unused*/) noexcept(false) { throw std::logic_error("move"); } // NOLINT
  65. udt &operator=(const udt & /*unused*/) { throw std::logic_error("copy"); }
  66. udt &operator=(udt && /*unused*/) noexcept(false) { throw std::logic_error("move"); } // NOLINT
  67. ~udt() { a.clear(); }
  68. };
  69. static_assert(!std::is_default_constructible<udt>::value, "udt is default constructible");
  70. static_assert(std::is_copy_constructible<udt>::value, "udt is not copy constructible");
  71. static_assert(std::is_move_constructible<udt>::value, "udt is not move constructible");
  72. static_assert(!std::is_default_constructible<outcome<udt>>::value, "outcome<udt> is default constructible");
  73. static_assert(std::is_copy_constructible<outcome<udt>>::value, "outcome<udt> is not copy constructible");
  74. static_assert(std::is_move_constructible<outcome<udt>>::value, "outcome<udt> is not move constructible");
  75. // Emplace constructs
  76. outcome<udt> foo(in_place_type<udt>, "niall");
  77. BOOST_CHECK("niall" == foo.value().a);
  78. try
  79. {
  80. auto foo2(foo); // NOLINT
  81. BOOST_CHECK(false);
  82. }
  83. catch(const std::logic_error &e)
  84. {
  85. BOOST_CHECK(!strcmp(e.what(), "copy"));
  86. }
  87. catch(...)
  88. {
  89. BOOST_CHECK(false);
  90. }
  91. BOOST_CHECK("niall" == foo.value().a);
  92. try
  93. {
  94. auto foo2(std::move(foo));
  95. BOOST_CHECK(false);
  96. }
  97. catch(const std::logic_error &e)
  98. {
  99. BOOST_CHECK(!strcmp(e.what(), "move"));
  100. }
  101. catch(...)
  102. {
  103. BOOST_CHECK(false);
  104. }
  105. BOOST_CHECK("niall" == foo.value().a); // NOLINT
  106. // Does throwing during copy assignment work?
  107. {
  108. outcome<udt> foo2(in_place_type<udt>, "douglas");
  109. try
  110. {
  111. foo2 = foo;
  112. BOOST_CHECK(false);
  113. }
  114. catch(const std::logic_error &e)
  115. {
  116. BOOST_CHECK(!strcmp(e.what(), "copy"));
  117. BOOST_CHECK(foo2.value().a == "douglas");
  118. }
  119. catch(...)
  120. {
  121. BOOST_CHECK(false);
  122. }
  123. BOOST_CHECK("niall" == foo.value().a);
  124. }
  125. // Does throwing during move assignment work?
  126. {
  127. outcome<udt> foo2(in_place_type<udt>, "douglas");
  128. try
  129. {
  130. foo2 = std::move(foo);
  131. BOOST_CHECK(false);
  132. }
  133. catch(const std::logic_error &e)
  134. {
  135. BOOST_CHECK(!strcmp(e.what(), "move"));
  136. BOOST_CHECK(foo2.value().a == "douglas");
  137. }
  138. catch(...)
  139. {
  140. BOOST_CHECK(false);
  141. }
  142. BOOST_CHECK("niall" == foo.value().a); // NOLINT
  143. }
  144. }
  145. #endif
  146. }