| 1 | /* Unit testing for outcomes |
| 2 | (C) 2013-2024 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.hpp> |
| 31 | #include <boost/test/unit_test.hpp> |
| 32 | #include <boost/test/unit_test_monitor.hpp> |
| 33 | |
| 34 | #if __cplusplus >= 201700 || _HAS_CXX17 |
| 35 | // Match LiteralType, even on C++ 17 and later |
| 36 | template <class T> struct is_literal_type |
| 37 | { |
| 38 | static constexpr bool value = // |
| 39 | std::is_void<T>::value // |
| 40 | || std::is_scalar<T>::value // |
| 41 | || std::is_reference<T>::value // |
| 42 | // leave out is_array for simplicity |
| 43 | || (std::is_class<T>::value && std::is_trivially_destructible<T>::value |
| 44 | // how does one detect if a type has at least one constexpr constructor without Reflection??? |
| 45 | ) // leave out union for simplicity |
| 46 | ; |
| 47 | }; |
| 48 | #else |
| 49 | template <class T> using is_literal_type = std::is_literal_type<T>; |
| 50 | #endif |
| 51 | |
| 52 | BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_constexpr, "Tests that outcome works as intended in a constexpr evaluation context" ) |
| 53 | { |
| 54 | using namespace BOOST_OUTCOME_V2_NAMESPACE; |
| 55 | |
| 56 | static_assert(is_literal_type<result<int, void, void>>::value, "result<int, void, void> is not a literal type!" ); |
| 57 | static_assert(is_literal_type<outcome<int, void, void>>::value, "outcome<int, void, void> is not a literal type!" ); |
| 58 | |
| 59 | // Unfortunately result<T> can never be a literal type as error_code can never be literal |
| 60 | // |
| 61 | // It can however be trivially destructible as error_code is trivially destructible. That |
| 62 | // makes possible lots of compiler optimisations |
| 63 | static_assert(std::is_trivially_destructible<result<int>>::value, "result<int> is not trivially destructible!" ); |
| 64 | static_assert(std::is_trivially_destructible<result<void>>::value, "result<void> is not trivially destructible!" ); |
| 65 | |
| 66 | // outcome<T> default has no trivial operations, but if configured it can become so |
| 67 | static_assert(std::is_trivially_destructible<outcome<int, boost::system::error_code, void>>::value, |
| 68 | "outcome<int, boost::system::error_code, void> is not trivially destructible!" ); |
| 69 | |
| 70 | { |
| 71 | // Test compatible results can be constructed from one another |
| 72 | constexpr result<int, long> g(in_place_type<int>, 5); |
| 73 | constexpr result<long, int> g2(g); |
| 74 | static_assert(g.has_value(), "" ); |
| 75 | static_assert(!g.has_error(), "" ); |
| 76 | static_assert(g.assume_value() == 5, "" ); // value() with UDT E won't compile |
| 77 | static_assert(g2.has_value(), "" ); |
| 78 | static_assert(!g2.has_error(), "" ); |
| 79 | static_assert(g2.assume_value() == 5, "" ); // value() with UDT E won't compile |
| 80 | constexpr result<void, int> g3(in_place_type<void>); |
| 81 | constexpr result<long, int> g4(g3); |
| 82 | constexpr result<int, void> g5(in_place_type<void>); |
| 83 | constexpr result<long, int> g6(g5); |
| 84 | (void) g4; |
| 85 | (void) g6; |
| 86 | |
| 87 | // Test void |
| 88 | constexpr result<void, int> h(in_place_type<void>); |
| 89 | static_assert(h.has_value(), "" ); |
| 90 | constexpr result<int, void> h2(in_place_type<void>); |
| 91 | static_assert(!h2.has_value(), "" ); |
| 92 | static_assert(h2.has_error(), "" ); |
| 93 | |
| 94 | // Test const |
| 95 | constexpr result<const int, void> i(5); |
| 96 | constexpr result<const int, void> i2(i); |
| 97 | (void) i2; |
| 98 | } |
| 99 | { |
| 100 | // Test compatible outcomes can be constructed from one another |
| 101 | constexpr outcome<int, long, char *> g(in_place_type<int>, 5); |
| 102 | constexpr outcome<long, int, const char *> g2(g); |
| 103 | static_assert(g.has_value(), "" ); |
| 104 | static_assert(!g.has_error(), "" ); |
| 105 | static_assert(!g.has_exception(), "" ); |
| 106 | static_assert(g.assume_value() == 5, "" ); // value() with UDT E won't compile |
| 107 | static_assert(g2.has_value(), "" ); |
| 108 | static_assert(!g2.has_error(), "" ); |
| 109 | static_assert(!g2.has_exception(), "" ); |
| 110 | static_assert(g2.assume_value() == 5, "" ); // value() with UDT E won't compile |
| 111 | constexpr outcome<void, int, char *> g3(in_place_type<void>); |
| 112 | constexpr outcome<long, int, const char *> g4(g3); |
| 113 | constexpr outcome<int, void, char *> g5(in_place_type<void>); |
| 114 | constexpr outcome<long, int, const char *> g6(g5); |
| 115 | (void) g4; |
| 116 | (void) g6; |
| 117 | } |
| 118 | } |
| 119 | |