index.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014-present
  4. // Copyright Gonzalo Brito Gadeschi 2017
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. #include <vector>
  13. #include <range/v3/algorithm/equal.hpp>
  14. #include <range/v3/view/c_str.hpp>
  15. #include <range/v3/view/iota.hpp>
  16. #include <range/v3/core.hpp>
  17. #include "../simple_test.hpp"
  18. int main()
  19. {
  20. {
  21. std::vector<int> vi{1,2,3,4};
  22. CHECK(ranges::index(vi, 0) == 1);
  23. CHECK(ranges::index(vi, 1) == 2);
  24. CHECK(ranges::index(vi, 2) == 3);
  25. CHECK(ranges::index(vi, 3) == 4);
  26. CHECK(ranges::at(vi, 0) == 1);
  27. CHECK(ranges::at(vi, 1) == 2);
  28. CHECK(ranges::at(vi, 2) == 3);
  29. CHECK(ranges::at(vi, 3) == 4);
  30. try
  31. {
  32. ranges::at(vi, 4);
  33. CHECK(false);
  34. }
  35. catch(std::out_of_range const& e)
  36. {
  37. CHECK(ranges::equal(ranges::views::c_str(e.what()),
  38. ranges::views::c_str("ranges::at")));
  39. }
  40. try
  41. {
  42. ranges::at(vi, -1);
  43. CHECK(false);
  44. }
  45. catch(std::out_of_range const& e)
  46. {
  47. CHECK(ranges::equal(ranges::views::c_str(e.what()),
  48. ranges::views::c_str("ranges::at")));
  49. }
  50. auto viv = ranges::make_subrange(vi.begin(), vi.end());
  51. CHECK(viv.at(0) == 1);
  52. CHECK(viv.at(1) == 2);
  53. CHECK(viv.at(2) == 3);
  54. CHECK(viv.at(3) == 4);
  55. try
  56. {
  57. viv.at(4);
  58. CHECK(false);
  59. }
  60. catch(std::out_of_range const& e)
  61. {
  62. CHECK(ranges::equal(ranges::views::c_str(e.what()),
  63. ranges::views::c_str("view_interface::at")));
  64. }
  65. try
  66. {
  67. viv.at(-1);
  68. CHECK(false);
  69. }
  70. catch(std::out_of_range const& e)
  71. {
  72. CHECK(ranges::equal(ranges::views::c_str(e.what()),
  73. ranges::views::c_str("view_interface::at")));
  74. }
  75. const auto cviv = viv;
  76. CHECK(cviv.at(0) == 1);
  77. CHECK(cviv.at(1) == 2);
  78. CHECK(cviv.at(2) == 3);
  79. CHECK(cviv.at(3) == 4);
  80. try
  81. {
  82. cviv.at(4);
  83. CHECK(false);
  84. }
  85. catch(std::out_of_range const& e)
  86. {
  87. CHECK(ranges::equal(ranges::views::c_str(e.what()),
  88. ranges::views::c_str("view_interface::at")));
  89. }
  90. try
  91. {
  92. cviv.at(-1);
  93. CHECK(false);
  94. }
  95. catch(std::out_of_range const& e)
  96. {
  97. CHECK(ranges::equal(ranges::views::c_str(e.what()),
  98. ranges::views::c_str("view_interface::at")));
  99. }
  100. }
  101. {
  102. auto rng = ranges::views::ints(std::int64_t{0}, std::numeric_limits<std::int64_t>::max());
  103. CHECK(ranges::index(rng, std::numeric_limits<std::int64_t>::max() - 1) ==
  104. std::numeric_limits<std::int64_t>::max() - 1);
  105. CHECK(ranges::at(rng, std::numeric_limits<std::int64_t>::max() - 1) ==
  106. std::numeric_limits<std::int64_t>::max() - 1);
  107. }
  108. #if RANGES_CXX_CONSTEXPR >= RANGES_CXX_CONSTEXPR_14
  109. {
  110. constexpr int vi[4] = {1, 2, 3, 4};
  111. constexpr int vi0 = ranges::index(vi, 0);
  112. static_assert(vi0 == 1, "");
  113. constexpr int vi1 = ranges::at(vi, 1);
  114. static_assert(vi1 == 2, "");
  115. }
  116. #endif
  117. return ::test_result();
  118. }