optional_test_conversions_from_U.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (C) 2014 Andrzej Krzemienski.
  2. //
  3. // Use, modification, and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/lib/optional for documentation.
  8. //
  9. // You are welcome to contact the author at: akrzemi1@gmail.com
  10. #include "boost/optional/optional.hpp"
  11. #ifdef __BORLANDC__
  12. #pragma hdrstop
  13. #endif
  14. #include "boost/core/lightweight_test.hpp"
  15. using boost::optional;
  16. // testing types:
  17. // X is convertible to Y
  18. // ADeriv is convertible to ABase
  19. struct X
  20. {
  21. int val;
  22. explicit X(int v) : val(v) {}
  23. };
  24. struct Y
  25. {
  26. int yval;
  27. Y(X const& x) : yval(x.val) {}
  28. friend bool operator==(Y const& l, Y const& r) { return l.yval == r.yval; }
  29. };
  30. struct ABase
  31. {
  32. int val;
  33. explicit ABase(int v) : val(v) {}
  34. friend bool operator==(ABase const& l, ABase const& r) { return l.val == r.val; }
  35. };
  36. struct ADeriv : ABase
  37. {
  38. explicit ADeriv(int v) : ABase(v) {}
  39. };
  40. template <typename T, typename U>
  41. void test_convert_optional_U_to_optional_T_for()
  42. {
  43. #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
  44. {
  45. optional<U> ou(U(8));
  46. optional<T> ot1(ou);
  47. BOOST_TEST(ot1);
  48. BOOST_TEST(*ot1 == T(*ou));
  49. }
  50. #endif
  51. #ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
  52. {
  53. optional<U> ou(U(8));
  54. optional<T> ot2;
  55. ot2 = ou;
  56. BOOST_TEST(ot2);
  57. BOOST_TEST(*ot2 == T(*ou));
  58. }
  59. #endif
  60. }
  61. void test_convert_optional_U_to_optional_T()
  62. {
  63. test_convert_optional_U_to_optional_T_for<Y, X>();
  64. test_convert_optional_U_to_optional_T_for<ABase, ADeriv>();
  65. test_convert_optional_U_to_optional_T_for<long, short>();
  66. test_convert_optional_U_to_optional_T_for<double, float>();
  67. }
  68. template <typename T, typename U>
  69. void test_convert_U_to_optional_T_for()
  70. {
  71. U u(8);
  72. optional<T> ot1(u);
  73. BOOST_TEST(ot1);
  74. BOOST_TEST(*ot1 == T(u));
  75. optional<T> ot2;
  76. ot2 = u;
  77. BOOST_TEST(ot2);
  78. BOOST_TEST(*ot2 == T(u));
  79. }
  80. void test_convert_U_to_optional_T()
  81. {
  82. test_convert_U_to_optional_T_for<Y, X>();
  83. test_convert_U_to_optional_T_for<ABase, ADeriv>();
  84. test_convert_U_to_optional_T_for<long, short>();
  85. test_convert_U_to_optional_T_for<double, float>();
  86. }
  87. int main()
  88. {
  89. test_convert_optional_U_to_optional_T();
  90. test_convert_U_to_optional_T();
  91. return boost::report_errors();
  92. }