ranges_unittest.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/ranges/ranges.h"
  5. #include <array>
  6. #include <initializer_list>
  7. #include <vector>
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace base {
  10. namespace {
  11. // Test struct with free function overloads for begin and end. Tests whether the
  12. // free functions are found.
  13. struct S {
  14. std::vector<int> v;
  15. };
  16. auto begin(const S& s) {
  17. return s.v.begin();
  18. }
  19. auto end(const S& s) {
  20. return s.v.end();
  21. }
  22. // Test struct with both member and free function overloads for begin and end.
  23. // Tests whether the member function is preferred.
  24. struct T {
  25. constexpr int begin() const { return 1; }
  26. constexpr int end() const { return 1; }
  27. };
  28. constexpr int begin(const T& t) {
  29. return 2;
  30. }
  31. constexpr int end(const T& t) {
  32. return 2;
  33. }
  34. // constexpr utility to generate a std::array. Ensures that a mutable array can
  35. // be used in a constexpr context.
  36. template <size_t N>
  37. constexpr std::array<int, N> GenerateArray() {
  38. std::array<int, N> arr{};
  39. int i = 0;
  40. for (auto* it = ranges::begin(arr); it != ranges::end(arr); ++it) {
  41. *it = i++;
  42. }
  43. return arr;
  44. }
  45. } // namespace
  46. TEST(RangesTest, BeginPrefersMember) {
  47. constexpr T t;
  48. static_assert(ranges::begin(t) == 1, "");
  49. }
  50. TEST(RangesTest, BeginConstexprContainers) {
  51. int arr[1]{};
  52. static_assert(arr == ranges::begin(arr), "");
  53. static constexpr std::initializer_list<int> il = {1, 2, 3};
  54. static_assert(il.begin() == ranges::begin(il), "");
  55. static constexpr std::array<int, 3> array = {1, 2, 3};
  56. static_assert(&array[0] == ranges::begin(array), "");
  57. }
  58. TEST(RangesTest, BeginRegularContainers) {
  59. std::vector<int> vec;
  60. S s;
  61. EXPECT_EQ(vec.begin(), ranges::begin(vec));
  62. EXPECT_EQ(s.v.begin(), ranges::begin(s));
  63. }
  64. TEST(RangesTest, EndPrefersMember) {
  65. constexpr T t;
  66. static_assert(ranges::end(t) == 1, "");
  67. }
  68. TEST(RangesTest, EndConstexprContainers) {
  69. int arr[1]{};
  70. static_assert(arr + 1 == ranges::end(arr), "");
  71. static constexpr std::initializer_list<int> il = {1, 2, 3};
  72. static_assert(il.end() == ranges::end(il), "");
  73. static constexpr std::array<int, 3> array = {1, 2, 3};
  74. static_assert(&array[0] + 3 == ranges::end(array), "");
  75. }
  76. TEST(RangesTest, EndRegularContainers) {
  77. std::vector<int> vec;
  78. S s;
  79. EXPECT_EQ(vec.end(), ranges::end(vec));
  80. EXPECT_EQ(s.v.end(), ranges::end(s));
  81. }
  82. TEST(RangesTest, BeginEndStdArray) {
  83. static constexpr std::array<int, 0> null_array = GenerateArray<0>();
  84. static_assert(ranges::begin(null_array) == ranges::end(null_array), "");
  85. static constexpr std::array<int, 3> array = GenerateArray<3>();
  86. static_assert(array[0] == 0, "");
  87. static_assert(array[1] == 1, "");
  88. static_assert(array[2] == 2, "");
  89. }
  90. } // namespace base