1 /* Unit testing for outcomes 2 (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (9 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(works_outcome_constexpr, "Tests that outcome works as intended in a constexpr evaluation context") 35 { 36 using namespace BOOST_OUTCOME_V2_NAMESPACE; 37 38 static_assert(std::is_literal_type<result<int, void, void>>::value, "result<int, void, void> is not a literal type!"); 39 static_assert(std::is_literal_type<outcome<int, void, void>>::value, "outcome<int, void, void> is not a literal type!"); 40 41 // Unfortunately result<T> can never be a literal type as error_code can never be literal 42 // 43 // It can however be trivially destructible as error_code is trivially destructible. That 44 // makes possible lots of compiler optimisations 45 static_assert(std::is_trivially_destructible<result<int>>::value, "result<int> is not trivially destructible!"); 46 static_assert(std::is_trivially_destructible<result<void>>::value, "result<void> is not trivially destructible!"); 47 48 // outcome<T> default has no trivial operations, but if configured it can become so 49 static_assert(std::is_trivially_destructible<outcome<int, boost::system::error_code, void>>::value, "outcome<int, boost::system::error_code, void> is not trivially destructible!"); 50 51 { 52 // Test compatible results can be constructed from one another 53 constexpr result<int, long> g(in_place_type<int>, 5); 54 constexpr result<long, int> g2(g); 55 static_assert(g.has_value(), ""); 56 static_assert(!g.has_error(), ""); 57 static_assert(g.assume_value() == 5, ""); // value() with UDT E won't compile 58 static_assert(g2.has_value(), ""); 59 static_assert(!g2.has_error(), ""); 60 static_assert(g2.assume_value() == 5, ""); // value() with UDT E won't compile 61 constexpr result<void, int> g3(in_place_type<void>); 62 constexpr result<long, int> g4(g3); 63 constexpr result<int, void> g5(in_place_type<void>); 64 constexpr result<long, int> g6(g5); 65 (void) g4; 66 (void) g6; 67 68 // Test void 69 constexpr result<void, int> h(in_place_type<void>); 70 static_assert(h.has_value(), ""); 71 constexpr result<int, void> h2(in_place_type<void>); 72 static_assert(!h2.has_value(), ""); 73 static_assert(h2.has_error(), ""); 74 75 // Test const 76 constexpr result<const int, void> i(5); 77 constexpr result<const int, void> i2(i); 78 (void) i2; 79 } 80 { 81 // Test compatible outcomes can be constructed from one another 82 constexpr outcome<int, long, char *> g(in_place_type<int>, 5); 83 constexpr outcome<long, int, const char *> g2(g); 84 static_assert(g.has_value(), ""); 85 static_assert(!g.has_error(), ""); 86 static_assert(!g.has_exception(), ""); 87 static_assert(g.assume_value() == 5, ""); // value() with UDT E won't compile 88 static_assert(g2.has_value(), ""); 89 static_assert(!g2.has_error(), ""); 90 static_assert(!g2.has_exception(), ""); 91 static_assert(g2.assume_value() == 5, ""); // value() with UDT E won't compile 92 constexpr outcome<void, int, char *> g3(in_place_type<void>); 93 constexpr outcome<long, int, const char *> g4(g3); 94 constexpr outcome<int, void, char *> g5(in_place_type<void>); 95 constexpr outcome<long, int, const char *> g6(g5); 96 (void) g4; 97 (void) g6; 98 } 99 } 100