constexpr.cpp 4.6 KB

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