2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <folly/Enumerate.h>
21 #include <folly/Range.h>
22 #include <gtest/gtest.h>
24 TEST(Enumerate, Basic) {
25 std::vector<std::string> v = {"abc", "a", "ab"};
27 for (auto it : folly::enumerate(v)) {
28 EXPECT_EQ(it.index, i);
30 EXPECT_EQ(it->size(), v[i].size());
33 std::string newValue = "x";
35 EXPECT_EQ(newValue, v[i]);
40 EXPECT_EQ(i, v.size());
43 TEST(Enumerate, Temporary) {
44 std::vector<std::string> v = {"abc", "a", "ab"};
46 for (auto it : folly::enumerate(decltype(v)(v))) { // Copy v.
47 EXPECT_EQ(it.index, i);
49 EXPECT_EQ(it->size(), v[i].size());
53 EXPECT_EQ(i, v.size());
57 struct IsConstReference {
58 constexpr static bool value = false;
61 struct IsConstReference<const T&> {
62 constexpr static bool value = true;
65 TEST(Enumerate, BasicConstArg) {
66 const std::vector<std::string> v = {"abc", "a", "ab"};
68 for (auto it : folly::enumerate(v)) {
70 IsConstReference<decltype(*it)>::value, "Enumerating a const vector");
71 EXPECT_EQ(it.index, i);
73 EXPECT_EQ(it->size(), v[i].size());
77 EXPECT_EQ(i, v.size());
80 TEST(Enumerate, BasicConstEnumerate) {
81 std::vector<std::string> v = {"abc", "a", "ab"};
83 for (const auto it : folly::enumerate(v)) {
84 static_assert(IsConstReference<decltype(*it)>::value, "Const enumeration");
85 EXPECT_EQ(it.index, i);
87 EXPECT_EQ(it->size(), v[i].size());
91 EXPECT_EQ(i, v.size());
94 TEST(Enumerate, TemporaryConstEnumerate) {
95 std::vector<std::string> v = {"abc", "a", "ab"};
97 for (const auto it : folly::enumerate(decltype(v)(v))) { // Copy v.
98 static_assert(IsConstReference<decltype(*it)>::value, "Const enumeration");
99 EXPECT_EQ(it.index, i);
100 EXPECT_EQ(*it, v[i]);
101 EXPECT_EQ(it->size(), v[i].size());
105 EXPECT_EQ(i, v.size());
108 TEST(Enumerate, RangeSupport) {
109 std::vector<std::string> v = {"abc", "a", "ab"};
111 for (const auto it : folly::enumerate(folly::range(v))) {
112 EXPECT_EQ(it.index, i);
113 EXPECT_EQ(*it, v[i]);
114 EXPECT_EQ(it->size(), v[i].size());
118 EXPECT_EQ(i, v.size());
121 TEST(Enumerate, EmptyRange) {
122 std::vector<std::string> v;
123 for (auto it : folly::enumerate(v)) {
124 (void)it; // Silence warnings.