optional_test_ref_to_val.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2016 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:
  10. // akrzemi1@gmail.com
  11. #include "boost/optional/optional.hpp"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #include "boost/core/lightweight_test.hpp"
  16. using boost::optional;
  17. using boost::none;
  18. struct Value
  19. {
  20. int val;
  21. explicit Value(int v) : val(v) {}
  22. };
  23. int val(int const& i)
  24. {
  25. return i;
  26. }
  27. int val(Value const& v)
  28. {
  29. return v.val;
  30. }
  31. template <typename Tref>
  32. optional<Tref&> make_opt_ref(Tref& v)
  33. {
  34. return optional<Tref&>(v);
  35. }
  36. template <typename Tval, typename Tref>
  37. void test_construct_from_optional_ref()
  38. {
  39. Tref v1 (1), v2 (2);
  40. optional<Tref&> opt_ref0;
  41. optional<Tref&> opt_ref1 (v1);
  42. optional<Tval> opt_val0 (opt_ref0);
  43. optional<Tval> opt_val1 (opt_ref1);
  44. optional<Tval> opt_val2 (make_opt_ref(v2));
  45. BOOST_TEST (!opt_val0);
  46. BOOST_TEST (opt_val1);
  47. BOOST_TEST (opt_val2);
  48. BOOST_TEST_EQ (1, val(*opt_val1));
  49. BOOST_TEST_EQ (2, val(*opt_val2));
  50. BOOST_TEST (boost::addressof(*opt_val1) != boost::addressof(v1));
  51. BOOST_TEST (boost::addressof(*opt_val2) != boost::addressof(v2));
  52. }
  53. template <typename Tval, typename Tref>
  54. void test_assign_from_optional_ref()
  55. {
  56. Tref v1 (1), v2 (2);
  57. optional<Tref&> opt_ref0;
  58. optional<Tref&> opt_ref1 (v1);
  59. optional<Tval> opt_val0;
  60. optional<Tval> opt_val1;
  61. optional<Tval> opt_val2;
  62. opt_val0 = opt_ref0;
  63. opt_val1 = opt_ref1;
  64. opt_val2 = make_opt_ref(v2);
  65. BOOST_TEST (!opt_val0);
  66. BOOST_TEST (opt_val1);
  67. BOOST_TEST (opt_val2);
  68. BOOST_TEST_EQ (1, val(*opt_val1));
  69. BOOST_TEST_EQ (2, val(*opt_val2));
  70. BOOST_TEST (boost::addressof(*opt_val1) != boost::addressof(v1));
  71. BOOST_TEST (boost::addressof(*opt_val2) != boost::addressof(v2));
  72. }
  73. int main()
  74. {
  75. test_construct_from_optional_ref<int, int>();
  76. test_construct_from_optional_ref<int, int const>();
  77. test_construct_from_optional_ref<int const, int const>();
  78. test_construct_from_optional_ref<int const, int>();
  79. test_construct_from_optional_ref<Value, Value>();
  80. test_construct_from_optional_ref<Value, Value const>();
  81. test_construct_from_optional_ref<Value const, Value const>();
  82. test_construct_from_optional_ref<Value const, Value>();
  83. test_assign_from_optional_ref<int, int>();
  84. test_assign_from_optional_ref<int, int const>();
  85. test_assign_from_optional_ref<Value, Value>();
  86. test_assign_from_optional_ref<Value, Value const>();
  87. return boost::report_errors();
  88. }