Use the GTest portability headers
[folly.git] / folly / test / ForeachTest.cpp
1 /*
2  * Copyright 2016 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
17 #include <folly/Foreach.h>
18
19 #include <folly/portability/GTest.h>
20
21 #include <map>
22 #include <string>
23 #include <vector>
24 #include <list>
25
26 using namespace folly;
27 using namespace folly::detail;
28
29 TEST(Foreach, ForEachRvalue) {
30   const char* const hello = "hello";
31   int n = 0;
32   FOR_EACH(it, std::string(hello)) {
33     ++n;
34   }
35   EXPECT_EQ(strlen(hello), n);
36   FOR_EACH_R(it, std::string(hello)) {
37     --n;
38     EXPECT_EQ(hello[n], *it);
39   }
40   EXPECT_EQ(0, n);
41 }
42
43 TEST(Foreach, ForEachKV) {
44   std::map<std::string, int> testMap;
45   testMap["abc"] = 1;
46   testMap["def"] = 2;
47   std::string keys = "";
48   int values = 0;
49   int numEntries = 0;
50   FOR_EACH_KV (key, value, testMap) {
51     keys += key;
52     values += value;
53     ++numEntries;
54   }
55   EXPECT_EQ("abcdef", keys);
56   EXPECT_EQ(3, values);
57   EXPECT_EQ(2, numEntries);
58 }
59
60 TEST(Foreach, ForEachKVBreak) {
61   std::map<std::string, int> testMap;
62   testMap["abc"] = 1;
63   testMap["def"] = 2;
64   std::string keys = "";
65   int values = 0;
66   int numEntries = 0;
67   FOR_EACH_KV (key, value, testMap) {
68     keys += key;
69     values += value;
70     ++numEntries;
71     break;
72   }
73   EXPECT_EQ("abc", keys);
74   EXPECT_EQ(1, values);
75   EXPECT_EQ(1, numEntries);
76 }
77
78 TEST(Foreach, ForEachKvWithMultiMap) {
79   std::multimap<std::string, int> testMap;
80   testMap.insert(std::make_pair("abc", 1));
81   testMap.insert(std::make_pair("abc", 2));
82   testMap.insert(std::make_pair("def", 3));
83   std::string keys = "";
84   int values = 0;
85   int numEntries = 0;
86   FOR_EACH_KV (key, value, testMap) {
87     keys += key;
88     values += value;
89     ++numEntries;
90   }
91   EXPECT_EQ("abcabcdef", keys);
92   EXPECT_EQ(6, values);
93   EXPECT_EQ(3, numEntries);
94 }
95
96 TEST(Foreach, ForEachEnumerate) {
97   std::vector<int> vv;
98   int sumAA = 0;
99   int sumIter = 0;
100   int numIterations = 0;
101   FOR_EACH_ENUMERATE(aa, iter, vv) {
102     sumAA += aa;
103     sumIter += *iter;
104     ++numIterations;
105   }
106   EXPECT_EQ(sumAA, 0);
107   EXPECT_EQ(sumIter, 0);
108   EXPECT_EQ(numIterations, 0);
109
110   vv.push_back(1);
111   vv.push_back(3);
112   vv.push_back(5);
113   FOR_EACH_ENUMERATE(aa, iter, vv) {
114     sumAA += aa;
115     sumIter += *iter;
116     ++numIterations;
117   }
118   EXPECT_EQ(sumAA, 3);   // 0 + 1 + 2
119   EXPECT_EQ(sumIter, 9); // 1 + 3 + 5
120   EXPECT_EQ(numIterations, 3);
121 }
122
123 TEST(Foreach, ForEachEnumerateBreak) {
124   std::vector<int> vv;
125   int sumAA = 0;
126   int sumIter = 0;
127   int numIterations = 0;
128   vv.push_back(1);
129   vv.push_back(2);
130   vv.push_back(4);
131   vv.push_back(8);
132   FOR_EACH_ENUMERATE(aa, iter, vv) {
133     sumAA += aa;
134     sumIter += *iter;
135     ++numIterations;
136     if (aa == 1) break;
137   }
138   EXPECT_EQ(sumAA, 1);   // 0 + 1
139   EXPECT_EQ(sumIter, 3); // 1 + 2
140   EXPECT_EQ(numIterations, 2);
141 }
142
143 TEST(Foreach, ForEachRangeR) {
144   int sum = 0;
145
146   FOR_EACH_RANGE_R (i, 0, 0) {
147     sum += i;
148   }
149   EXPECT_EQ(0, sum);
150
151   FOR_EACH_RANGE_R (i, 0, -1) {
152     sum += i;
153   }
154   EXPECT_EQ(0, sum);
155
156   FOR_EACH_RANGE_R (i, 0, 5) {
157     sum += i;
158   }
159   EXPECT_EQ(10, sum);
160
161   std::list<int> lst = { 0, 1, 2, 3, 4 };
162   sum = 0;
163   FOR_EACH_RANGE_R (i, lst.begin(), lst.end()) {
164     sum += *i;
165   }
166   EXPECT_EQ(10, sum);
167 }