1 /* Unit testing for outcomes 2 (C) 2013-2020 Niall Douglas <http://www.nedproductions.biz/> (6 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 #include <boost/outcome/outcome.hpp> 31 #include <boost/test/unit_test.hpp> 32 #include <boost/test/unit_test_monitor.hpp> 33 34 BOOST_OUTCOME_AUTO_TEST_CASE(issues_0061_result, "result<T1, E1> does not compare to incompatible result<T2, E2>") 35 { 36 using namespace BOOST_OUTCOME_V2_NAMESPACE; 37 struct udt1 38 { 39 const char *_v{nullptr}; 40 udt1() = default; udt1udt141 constexpr udt1(const char *v) noexcept : _v(v) {} // NOLINT 42 udt1(udt1 &&o) = delete; 43 udt1(const udt1 &) = delete; 44 udt1 &operator=(udt1 &&o) = delete; 45 udt1 &operator=(const udt1 &) = delete; 46 ~udt1() = default; operator *udt147 constexpr const char *operator*() const noexcept { return _v; } 48 }; 49 struct udt2 50 { 51 const char *_v{nullptr}; 52 udt2() = default; udt2udt253 constexpr explicit udt2(const char *v) noexcept : _v(v) {} 54 udt2(udt2 &&o) = delete; 55 udt2(const udt2 &) = delete; 56 udt2 &operator=(udt2 &&o) = delete; 57 udt2 &operator=(const udt2 &) = delete; 58 ~udt2() = default; operator *udt259 constexpr const char *operator*() const noexcept { return _v; } operator ==udt260 bool operator==(const udt1 &o) const noexcept { return _v == *o; } operator !=udt261 bool operator!=(const udt1 &o) const noexcept { return _v != *o; } 62 }; 63 using result1 = result<int, udt1>; 64 using result2 = result<int, udt2>; 65 66 result1 a(5); 67 result2 b(5); 68 BOOST_CHECK(b == a); // udt2 will compare to udt1 69 BOOST_CHECK(!(b != a)); // udt2 will compare to udt1 70 71 result<void> c = success(); 72 result<void> d = success(); 73 BOOST_CHECK(c == d); 74 BOOST_CHECK(!(c != d)); 75 76 BOOST_CHECK(a == success()); 77 BOOST_CHECK(success() == a); 78 BOOST_CHECK(b != failure("foo")); 79 BOOST_CHECK(failure("foo") != b); 80 } 81 82 BOOST_OUTCOME_AUTO_TEST_CASE(issues_0061_outcome, "outcome<T1, E1, P1> does not compare to incompatible outcome<T2, E2, P2>") 83 { 84 using namespace BOOST_OUTCOME_V2_NAMESPACE; 85 struct udt1 86 { 87 const char *_v{nullptr}; 88 udt1() = default; udt1udt189 constexpr udt1(const char *v) noexcept : _v(v) {} // NOLINT 90 udt1(udt1 &&o) = delete; 91 udt1(const udt1 &) = delete; 92 udt1 &operator=(udt1 &&o) = delete; 93 udt1 &operator=(const udt1 &) = delete; 94 ~udt1() = default; operator *udt195 constexpr const char *operator*() const noexcept { return _v; } 96 }; 97 struct udt2 98 { 99 const char *_v{nullptr}; 100 udt2() = default; udt2udt2101 constexpr explicit udt2(const char *v) noexcept : _v(v) {} 102 udt2(udt2 &&o) = delete; 103 udt2(const udt2 &) = delete; 104 udt2 &operator=(udt2 &&o) = delete; 105 udt2 &operator=(const udt2 &) = delete; 106 ~udt2() = default; operator *udt2107 constexpr const char *operator*() const noexcept { return _v; } operator ==udt2108 bool operator==(const udt1 &o) const noexcept { return _v == *o; } operator !=udt2109 bool operator!=(const udt1 &o) const noexcept { return _v != *o; } 110 }; 111 using outcome1 = outcome<int, udt1>; 112 using outcome2 = outcome<int, udt2>; 113 114 outcome1 a(5), _a(6); 115 outcome2 b(5); 116 BOOST_CHECK(b == a); // udt2 will compare to udt1 117 BOOST_CHECK(!(b != a)); // udt2 will compare to udt1 118 119 outcome<void> c = success(); 120 outcome<void> d = success(); 121 BOOST_CHECK(c == d); 122 BOOST_CHECK(!(c != d)); 123 124 BOOST_CHECK(a == success()); 125 BOOST_CHECK(success() == a); 126 BOOST_CHECK(b != failure("foo")); 127 BOOST_CHECK(failure("foo") != b); 128 } 129