ref_unwrapped.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Boost.Range library
  2. //
  3. // Copyright Robin Eckert 2015. Use, modification and distribution is
  4. // subject to the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. #include <boost/range/adaptor/ref_unwrapped.hpp>
  12. #define BOOST_TEST_MAIN
  13. #include <boost/test/test_tools.hpp>
  14. #include <boost/test/unit_test.hpp>
  15. #include <vector>
  16. #if !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && !defined(BOOST_NO_CXX11_RANGE_BASED_FOR) && !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  17. namespace boost
  18. {
  19. BOOST_AUTO_TEST_CASE(test_mutable)
  20. {
  21. int one = 1;
  22. int two = 2;
  23. int three = 3;
  24. std::vector<std::reference_wrapper<int>> input_values{one, two, three};
  25. const std::vector<int*> expected{&one, &two, &three};
  26. std::vector<int*> actual;
  27. for (auto&& value : input_values | adaptors::ref_unwrapped)
  28. {
  29. actual.push_back(&value);
  30. }
  31. BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(),
  32. expected.end(),
  33. actual.begin(),
  34. actual.end());
  35. }
  36. BOOST_AUTO_TEST_CASE(test_const_range)
  37. {
  38. int one = 1;
  39. int two = 2;
  40. int three = 3;
  41. const std::vector<std::reference_wrapper<int>> input_values{one, two, three};
  42. const std::vector<int*> expected{&one, &two, &three};
  43. std::vector<int*> actual;
  44. for (auto&& value : input_values | adaptors::ref_unwrapped)
  45. {
  46. actual.push_back(&value);
  47. }
  48. BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(),
  49. expected.end(),
  50. actual.begin(),
  51. actual.end());
  52. }
  53. BOOST_AUTO_TEST_CASE(test_const_reference)
  54. {
  55. const int one = 1;
  56. const int two = 2;
  57. const int three = 3;
  58. const std::vector<std::reference_wrapper<const int>> input_values{one, two, three};
  59. const std::vector<const int*> expected{&one, &two, &three};
  60. std::vector<const int*> actual;
  61. for (auto&& value : input_values | adaptors::ref_unwrapped)
  62. {
  63. actual.push_back(&value);
  64. }
  65. BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(),
  66. expected.end(),
  67. actual.begin(),
  68. actual.end());
  69. }
  70. }
  71. #else
  72. BOOST_AUTO_TEST_CASE(empty)
  73. {
  74. // C++11 only
  75. }
  76. #endif