1 /* Unit testing for outcomes 2 (C) 2013-2020 Niall Douglas <http://www.nedproductions.biz/> (7 commits) 3 4 5 Boost Software License - Version 1.0 - August 17th, 2003 6 7 Permission is hereby granted, free of charge, to any person or organization 8 obtaining a copy of the software and accompanying documentation covered by 9 this license (the "Software") to use, reproduce, display, distribute, 10 execute, and transmit the Software, and to prepare derivative works of the 11 Software, and to permit third-parties to whom the Software is furnished to 12 do so, all subject to the following: 13 14 The copyright notices in the Software and this entire statement, including 15 the above license grant, this restriction and the following disclaimer, 16 must be included in all copies of the Software, in whole or in part, and 17 all derivative works of the Software, unless such copies or derivative 18 works are solely in the form of machine-executable object code generated by 19 a source language processor. 20 21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 DEALINGS IN THE SOFTWARE. 28 */ 29 30 #ifdef _MSC_VER 31 #pragma warning(disable : 4702) // unreachable code 32 #endif 33 34 #include <boost/outcome/outcome.hpp> 35 #include <boost/test/unit_test.hpp> 36 #include <boost/test/unit_test_monitor.hpp> 37 38 BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_udts, "Tests that the outcome works as intended with user-defined types") 39 { 40 using namespace BOOST_OUTCOME_V2_NAMESPACE; 41 // No default constructor, no copy/move, no assignment 42 { 43 struct udt 44 { 45 int a; udtudt46 explicit udt(int _a) 47 : a(_a) 48 { 49 } 50 udt() = delete; 51 udt(const udt &) = delete; 52 udt(udt &&) = delete; 53 udt &operator=(const udt &) = delete; 54 udt &operator=(udt &&) = delete; 55 ~udt() = default; 56 }; 57 outcome<udt> foo(in_place_type<udt>, 5); 58 BOOST_CHECK(5 == foo.value().a); 59 } 60 #ifndef BOOST_NO_EXCEPTIONS 61 // Emplace construct, throws during move and copy 62 { 63 struct udt 64 { 65 std::string a; udtudt66 explicit udt(std::string _a) 67 : a(std::move(_a)) 68 { 69 } 70 udt() = delete; udtudt71 udt(const udt & /*unused*/) { throw std::logic_error("copy"); } udtudt72 udt(udt && /*unused*/) noexcept(false) { throw std::logic_error("move"); } // NOLINT operator =udt73 udt &operator=(const udt & /*unused*/) { throw std::logic_error("copy"); } operator =udt74 udt &operator=(udt && /*unused*/) noexcept(false) { throw std::logic_error("move"); } // NOLINT ~udtudt75 ~udt() { a.clear(); } 76 }; 77 static_assert(!std::is_default_constructible<udt>::value, "udt is default constructible"); 78 static_assert(std::is_copy_constructible<udt>::value, "udt is not copy constructible"); 79 static_assert(std::is_move_constructible<udt>::value, "udt is not move constructible"); 80 static_assert(!std::is_default_constructible<outcome<udt>>::value, "outcome<udt> is default constructible"); 81 static_assert(std::is_copy_constructible<outcome<udt>>::value, "outcome<udt> is not copy constructible"); 82 static_assert(std::is_move_constructible<outcome<udt>>::value, "outcome<udt> is not move constructible"); 83 // Emplace constructs 84 outcome<udt> foo(in_place_type<udt>, "niall"); 85 BOOST_CHECK("niall" == foo.value().a); 86 try 87 { 88 auto foo2(foo); // NOLINT 89 BOOST_CHECK(false); 90 } 91 catch(const std::logic_error &e) 92 { 93 BOOST_CHECK(!strcmp(e.what(), "copy")); 94 } 95 catch(...) 96 { 97 BOOST_CHECK(false); 98 } 99 BOOST_CHECK("niall" == foo.value().a); 100 try 101 { 102 auto foo2(std::move(foo)); 103 BOOST_CHECK(false); 104 } 105 catch(const std::logic_error &e) 106 { 107 BOOST_CHECK(!strcmp(e.what(), "move")); 108 } 109 catch(...) 110 { 111 BOOST_CHECK(false); 112 } 113 BOOST_CHECK("niall" == foo.value().a); // NOLINT 114 // Does throwing during copy assignment work? 115 { 116 outcome<udt> foo2(in_place_type<udt>, "douglas"); 117 try 118 { 119 foo2 = foo; 120 BOOST_CHECK(false); 121 } 122 catch(const std::logic_error &e) 123 { 124 BOOST_CHECK(!strcmp(e.what(), "copy")); 125 BOOST_CHECK(foo2.value().a == "douglas"); 126 } 127 catch(...) 128 { 129 BOOST_CHECK(false); 130 } 131 BOOST_CHECK("niall" == foo.value().a); 132 } 133 // Does throwing during move assignment work? 134 { 135 outcome<udt> foo2(in_place_type<udt>, "douglas"); 136 try 137 { 138 foo2 = std::move(foo); 139 BOOST_CHECK(false); 140 } 141 catch(const std::logic_error &e) 142 { 143 BOOST_CHECK(!strcmp(e.what(), "move")); 144 BOOST_CHECK(foo2.value().a == "douglas"); 145 } 146 catch(...) 147 { 148 BOOST_CHECK(false); 149 } 150 BOOST_CHECK("niall" == foo.value().a); // NOLINT 151 } 152 } 153 #endif 154 } 155