keys_value.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014-present
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. #include <map>
  12. #include <string>
  13. #include <vector>
  14. #include <range/v3/core.hpp>
  15. #include <range/v3/view/iota.hpp>
  16. #include <range/v3/view/map.hpp>
  17. #include <range/v3/view/zip.hpp>
  18. #include <range/v3/utility/copy.hpp>
  19. #include <range/v3/algorithm/find.hpp>
  20. #include "../simple_test.hpp"
  21. #include "../test_utils.hpp"
  22. int main()
  23. {
  24. using namespace ranges;
  25. std::map<std::string, int> m = {
  26. {"this", 0},
  27. {"that", 1},
  28. {"other", 2}};
  29. auto keys = m | views::keys;
  30. has_type<std::string const &>(*begin(keys));
  31. CPP_assert(view_<decltype(keys)>);
  32. CPP_assert(sized_range<decltype(keys)>);
  33. CPP_assert(common_range<decltype(keys)>);
  34. CPP_assert(bidirectional_iterator<decltype(begin(keys))>);
  35. CHECK(&*begin(keys) == &m.begin()->first);
  36. ::check_equal(keys, {"other", "that", "this"});
  37. auto values = m | views::values;
  38. has_type<int &>(*begin(values));
  39. CPP_assert(sized_range<decltype(values)>);
  40. CPP_assert(common_range<decltype(values)>);
  41. CPP_assert(bidirectional_iterator<decltype(begin(values))>);
  42. CHECK(&*begin(values) == &m.begin()->second);
  43. ::check_equal(values, {2, 1, 0});
  44. {
  45. // regression test for #526
  46. auto f = detail::get_first{};
  47. CPP_assert(same_as<int, decltype(f(std::declval<std::pair<int,int>>()))>);
  48. CPP_assert(same_as<int&, decltype(f(std::declval<std::pair<int,int>&>()))>);
  49. CPP_assert(same_as<int&, decltype(f(std::declval<std::pair<int&,int&>>()))>);
  50. CPP_assert(same_as<int&, decltype(f(std::declval<std::pair<int&,int&>&>()))>);
  51. CPP_assert(same_as<int, decltype(f(std::declval<std::pair<int&&,int&&>>()))>);
  52. CPP_assert(same_as<int&, decltype(f(std::declval<std::pair<int&&,int&&>&>()))>);
  53. std::vector<int> xs = {42, 100, -1234};
  54. auto exs = views::zip(views::ints, xs);
  55. ::check_equal(views::keys(exs), {0, 1, 2});
  56. }
  57. {
  58. std::pair<int, int> const rgp[] = {{0, 2}, {1, 1}, {2, 0}};
  59. auto key_range = debug_input_view<std::pair<int, int> const>{rgp} | views::keys;
  60. check_equal(key_range, {0,1,2});
  61. auto value_range = debug_input_view<std::pair<int, int> const>{rgp} | views::values;
  62. check_equal(value_range, {2,1,0});
  63. }
  64. {
  65. auto it = find(m | views::keys, "other");
  66. CHECK(it.base()->second == 2);
  67. auto it2 = find(m | views::values, 1);
  68. CHECK(it2.base()->first == "that");
  69. }
  70. return test_result();
  71. }