constructors.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <catch2/catch.hpp>
  2. #include <tl/expected.hpp>
  3. #include <type_traits>
  4. #include <vector>
  5. #include <string>
  6. struct takes_init_and_variadic {
  7. std::vector<int> v;
  8. std::tuple<int, int> t;
  9. template <class... Args>
  10. takes_init_and_variadic(std::initializer_list<int> l, Args &&... args)
  11. : v(l), t(std::forward<Args>(args)...) {}
  12. };
  13. TEST_CASE("Constructors", "[constructors]") {
  14. {
  15. tl::expected<int,int> e;
  16. REQUIRE(e);
  17. REQUIRE(e == 0);
  18. }
  19. {
  20. tl::expected<int,int> e = tl::make_unexpected(0);
  21. REQUIRE(!e);
  22. REQUIRE(e.error() == 0);
  23. }
  24. {
  25. tl::expected<int,int> e (tl::unexpect, 0);
  26. REQUIRE(!e);
  27. REQUIRE(e.error() == 0);
  28. }
  29. {
  30. tl::expected<int,int> e (tl::in_place, 42);
  31. REQUIRE(e);
  32. REQUIRE(e == 42);
  33. }
  34. {
  35. tl::expected<std::vector<int>,int> e (tl::in_place, {0,1});
  36. REQUIRE(e);
  37. REQUIRE((*e)[0] == 0);
  38. REQUIRE((*e)[1] == 1);
  39. }
  40. {
  41. tl::expected<std::tuple<int,int>,int> e (tl::in_place, 0, 1);
  42. REQUIRE(e);
  43. REQUIRE(std::get<0>(*e) == 0);
  44. REQUIRE(std::get<1>(*e) == 1);
  45. }
  46. {
  47. tl::expected<takes_init_and_variadic,int> e (tl::in_place, {0,1}, 2, 3);
  48. REQUIRE(e);
  49. REQUIRE(e->v[0] == 0);
  50. REQUIRE(e->v[1] == 1);
  51. REQUIRE(std::get<0>(e->t) == 2);
  52. REQUIRE(std::get<1>(e->t) == 3);
  53. }
  54. {
  55. tl::expected<int, int> e;
  56. REQUIRE(std::is_default_constructible<decltype(e)>::value);
  57. REQUIRE(std::is_copy_constructible<decltype(e)>::value);
  58. REQUIRE(std::is_move_constructible<decltype(e)>::value);
  59. REQUIRE(std::is_copy_assignable<decltype(e)>::value);
  60. REQUIRE(std::is_move_assignable<decltype(e)>::value);
  61. REQUIRE(TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value);
  62. REQUIRE(TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value);
  63. # if !defined(TL_EXPECTED_GCC49)
  64. REQUIRE(std::is_trivially_move_constructible<decltype(e)>::value);
  65. REQUIRE(std::is_trivially_move_assignable<decltype(e)>::value);
  66. # endif
  67. }
  68. {
  69. tl::expected<int, std::string> e;
  70. REQUIRE(std::is_default_constructible<decltype(e)>::value);
  71. REQUIRE(std::is_copy_constructible<decltype(e)>::value);
  72. REQUIRE(std::is_move_constructible<decltype(e)>::value);
  73. REQUIRE(std::is_copy_assignable<decltype(e)>::value);
  74. REQUIRE(std::is_move_assignable<decltype(e)>::value);
  75. REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value);
  76. REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value);
  77. # if !defined(TL_EXPECTED_GCC49)
  78. REQUIRE(!std::is_trivially_move_constructible<decltype(e)>::value);
  79. REQUIRE(!std::is_trivially_move_assignable<decltype(e)>::value);
  80. # endif
  81. }
  82. {
  83. tl::expected<std::string, int> e;
  84. REQUIRE(std::is_default_constructible<decltype(e)>::value);
  85. REQUIRE(std::is_copy_constructible<decltype(e)>::value);
  86. REQUIRE(std::is_move_constructible<decltype(e)>::value);
  87. REQUIRE(std::is_copy_assignable<decltype(e)>::value);
  88. REQUIRE(std::is_move_assignable<decltype(e)>::value);
  89. REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value);
  90. REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value);
  91. # if !defined(TL_EXPECTED_GCC49)
  92. REQUIRE(!std::is_trivially_move_constructible<decltype(e)>::value);
  93. REQUIRE(!std::is_trivially_move_assignable<decltype(e)>::value);
  94. # endif
  95. }
  96. {
  97. tl::expected<std::string, std::string> e;
  98. REQUIRE(std::is_default_constructible<decltype(e)>::value);
  99. REQUIRE(std::is_copy_constructible<decltype(e)>::value);
  100. REQUIRE(std::is_move_constructible<decltype(e)>::value);
  101. REQUIRE(std::is_copy_assignable<decltype(e)>::value);
  102. REQUIRE(std::is_move_assignable<decltype(e)>::value);
  103. REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value);
  104. REQUIRE(!TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value);
  105. # if !defined(TL_EXPECTED_GCC49)
  106. REQUIRE(!std::is_trivially_move_constructible<decltype(e)>::value);
  107. REQUIRE(!std::is_trivially_move_assignable<decltype(e)>::value);
  108. # endif
  109. }
  110. {
  111. tl::expected<void,int> e;
  112. REQUIRE(e);
  113. }
  114. {
  115. tl::expected<void,int> e (tl::unexpect, 42);
  116. REQUIRE(!e);
  117. REQUIRE(e.error() == 42);
  118. }
  119. }