EnumerateTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <array>
  17. #include <string>
  18. #include <vector>
  19. #include <folly/Range.h>
  20. #include <folly/container/Enumerate.h>
  21. #include <folly/portability/GTest.h>
  22. namespace {
  23. template <class T>
  24. struct IsConstReference {
  25. constexpr static bool value = false;
  26. };
  27. template <class T>
  28. struct IsConstReference<const T&> {
  29. constexpr static bool value = true;
  30. };
  31. } // namespace
  32. #define ENUMERATE_TEST_BASIC(DECL, NAME) \
  33. TEST(Enumerate, NAME) { \
  34. std::vector<std::string> v = {"abc", "a", "ab"}; \
  35. size_t i = 0; \
  36. for (DECL it : folly::enumerate(v)) { \
  37. EXPECT_EQ(it.index, i); \
  38. EXPECT_EQ(*it, v[i]); \
  39. EXPECT_EQ(it->size(), v[i].size()); \
  40. \
  41. /* Test mutability. */ \
  42. std::string newValue = "x"; \
  43. *it = newValue; \
  44. EXPECT_EQ(newValue, v[i]); \
  45. \
  46. ++i; \
  47. } \
  48. \
  49. EXPECT_EQ(i, v.size()); \
  50. }
  51. ENUMERATE_TEST_BASIC(auto, Basic)
  52. ENUMERATE_TEST_BASIC(auto&&, BasicRRef)
  53. #undef ENUMERATE_TEST_BASIC
  54. #define ENUMERATE_TEST_BASIC_CONST(DECL, NAME) \
  55. TEST(Enumerate, NAME) { \
  56. std::vector<std::string> v = {"abc", "a", "ab"}; \
  57. size_t i = 0; \
  58. for (DECL it : folly::enumerate(v)) { \
  59. static_assert( \
  60. IsConstReference<decltype(*it)>::value, "Const enumeration"); \
  61. EXPECT_EQ(it.index, i); \
  62. EXPECT_EQ(*it, v[i]); \
  63. EXPECT_EQ(it->size(), v[i].size()); \
  64. ++i; \
  65. } \
  66. \
  67. EXPECT_EQ(i, v.size()); \
  68. }
  69. ENUMERATE_TEST_BASIC_CONST(const auto, BasicConst)
  70. ENUMERATE_TEST_BASIC_CONST(const auto&, BasicConstRef)
  71. ENUMERATE_TEST_BASIC_CONST(const auto&&, BasicConstRRef)
  72. #undef ENUMERATE_TEST_BASIC_CONST
  73. TEST(Enumerate, Temporary) {
  74. std::vector<std::string> v = {"abc", "a", "ab"};
  75. size_t i = 0;
  76. for (auto&& it : folly::enumerate(decltype(v)(v))) { // Copy v.
  77. EXPECT_EQ(it.index, i);
  78. EXPECT_EQ(*it, v[i]);
  79. EXPECT_EQ(it->size(), v[i].size());
  80. ++i;
  81. }
  82. EXPECT_EQ(i, v.size());
  83. }
  84. TEST(Enumerate, BasicConstArg) {
  85. const std::vector<std::string> v = {"abc", "a", "ab"};
  86. size_t i = 0;
  87. for (auto&& it : folly::enumerate(v)) {
  88. static_assert(
  89. IsConstReference<decltype(*it)>::value, "Enumerating a const vector");
  90. EXPECT_EQ(it.index, i);
  91. EXPECT_EQ(*it, v[i]);
  92. EXPECT_EQ(it->size(), v[i].size());
  93. ++i;
  94. }
  95. EXPECT_EQ(i, v.size());
  96. }
  97. TEST(Enumerate, TemporaryConstEnumerate) {
  98. std::vector<std::string> v = {"abc", "a", "ab"};
  99. size_t i = 0;
  100. for (const auto&& it : folly::enumerate(decltype(v)(v))) { // Copy v.
  101. static_assert(IsConstReference<decltype(*it)>::value, "Const enumeration");
  102. EXPECT_EQ(it.index, i);
  103. EXPECT_EQ(*it, v[i]);
  104. EXPECT_EQ(it->size(), v[i].size());
  105. ++i;
  106. }
  107. EXPECT_EQ(i, v.size());
  108. }
  109. TEST(Enumerate, RangeSupport) {
  110. std::vector<std::string> v = {"abc", "a", "ab"};
  111. size_t i = 0;
  112. for (const auto&& it : folly::enumerate(folly::range(v))) {
  113. EXPECT_EQ(it.index, i);
  114. EXPECT_EQ(*it, v[i]);
  115. EXPECT_EQ(it->size(), v[i].size());
  116. ++i;
  117. }
  118. EXPECT_EQ(i, v.size());
  119. }
  120. TEST(Enumerate, EmptyRange) {
  121. std::vector<std::string> v;
  122. for (auto&& it : folly::enumerate(v)) {
  123. (void)it; // Silence warnings.
  124. ADD_FAILURE();
  125. }
  126. }
  127. class CStringRange {
  128. const char* cstr;
  129. public:
  130. struct Sentinel {};
  131. explicit CStringRange(const char* cstr_) : cstr(cstr_) {}
  132. const char* begin() const {
  133. return cstr;
  134. }
  135. Sentinel end() const {
  136. return Sentinel{};
  137. }
  138. };
  139. bool operator==(const char* c, CStringRange::Sentinel) {
  140. return *c == 0;
  141. }
  142. TEST(Enumerate, Cpp17Support) {
  143. std::array<char, 5> test = {"test"};
  144. // Can't use range based for loop until C++17, so test manually
  145. // Equivalent to:
  146. // for (const auto&& it : folly::enumerate(CStringRange{test.data()})) { ... }
  147. {
  148. auto&& enumerate = folly::enumerate(CStringRange{test.data()});
  149. auto begin = enumerate.begin();
  150. auto end = enumerate.end();
  151. for (; begin != end; ++begin) {
  152. const auto&& it = *begin;
  153. ASSERT_LT(it.index, test.size());
  154. EXPECT_EQ(*it, test[it.index]);
  155. }
  156. }
  157. }