add missing include to ThreadId.h
[folly.git] / folly / test / ForeachTest.cpp
1 /*
2  * Copyright 2017 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, ForEachNested) {
44   const std::string hello = "hello";
45   size_t n = 0;
46   FOR_EACH(i, hello) {
47     FOR_EACH(j, hello) {
48       ++n;
49     }
50   }
51   auto len = hello.size();
52   EXPECT_EQ(len * len, n);
53 }
54
55 TEST(Foreach, ForEachKV) {
56   std::map<std::string, int> testMap;
57   testMap["abc"] = 1;
58   testMap["def"] = 2;
59   std::string keys = "";
60   int values = 0;
61   int numEntries = 0;
62   FOR_EACH_KV (key, value, testMap) {
63     keys += key;
64     values += value;
65     ++numEntries;
66   }
67   EXPECT_EQ("abcdef", keys);
68   EXPECT_EQ(3, values);
69   EXPECT_EQ(2, numEntries);
70 }
71
72 TEST(Foreach, ForEachKVBreak) {
73   std::map<std::string, int> testMap;
74   testMap["abc"] = 1;
75   testMap["def"] = 2;
76   std::string keys = "";
77   int values = 0;
78   int numEntries = 0;
79   FOR_EACH_KV (key, value, testMap) {
80     keys += key;
81     values += value;
82     ++numEntries;
83     break;
84   }
85   EXPECT_EQ("abc", keys);
86   EXPECT_EQ(1, values);
87   EXPECT_EQ(1, numEntries);
88 }
89
90 TEST(Foreach, ForEachKvWithMultiMap) {
91   std::multimap<std::string, int> testMap;
92   testMap.insert(std::make_pair("abc", 1));
93   testMap.insert(std::make_pair("abc", 2));
94   testMap.insert(std::make_pair("def", 3));
95   std::string keys = "";
96   int values = 0;
97   int numEntries = 0;
98   FOR_EACH_KV (key, value, testMap) {
99     keys += key;
100     values += value;
101     ++numEntries;
102   }
103   EXPECT_EQ("abcabcdef", keys);
104   EXPECT_EQ(6, values);
105   EXPECT_EQ(3, numEntries);
106 }
107
108 TEST(Foreach, ForEachEnumerate) {
109   std::vector<int> vv;
110   int sumAA = 0;
111   int sumIter = 0;
112   int numIterations = 0;
113   FOR_EACH_ENUMERATE(aa, iter, vv) {
114     sumAA += aa;
115     sumIter += *iter;
116     ++numIterations;
117   }
118   EXPECT_EQ(sumAA, 0);
119   EXPECT_EQ(sumIter, 0);
120   EXPECT_EQ(numIterations, 0);
121
122   vv.push_back(1);
123   vv.push_back(3);
124   vv.push_back(5);
125   FOR_EACH_ENUMERATE(aa, iter, vv) {
126     sumAA += aa;
127     sumIter += *iter;
128     ++numIterations;
129   }
130   EXPECT_EQ(sumAA, 3);   // 0 + 1 + 2
131   EXPECT_EQ(sumIter, 9); // 1 + 3 + 5
132   EXPECT_EQ(numIterations, 3);
133 }
134
135 TEST(Foreach, ForEachEnumerateBreak) {
136   std::vector<int> vv;
137   int sumAA = 0;
138   int sumIter = 0;
139   int numIterations = 0;
140   vv.push_back(1);
141   vv.push_back(2);
142   vv.push_back(4);
143   vv.push_back(8);
144   FOR_EACH_ENUMERATE(aa, iter, vv) {
145     sumAA += aa;
146     sumIter += *iter;
147     ++numIterations;
148     if (aa == 1) break;
149   }
150   EXPECT_EQ(sumAA, 1);   // 0 + 1
151   EXPECT_EQ(sumIter, 3); // 1 + 2
152   EXPECT_EQ(numIterations, 2);
153 }
154
155 TEST(Foreach, ForEachRangeR) {
156   int sum = 0;
157
158   FOR_EACH_RANGE_R (i, 0, 0) {
159     sum += i;
160   }
161   EXPECT_EQ(0, sum);
162
163   FOR_EACH_RANGE_R (i, 0, -1) {
164     sum += i;
165   }
166   EXPECT_EQ(0, sum);
167
168   FOR_EACH_RANGE_R (i, 0, 5) {
169     sum += i;
170   }
171   EXPECT_EQ(10, sum);
172
173   std::list<int> lst = { 0, 1, 2, 3, 4 };
174   sum = 0;
175   FOR_EACH_RANGE_R (i, lst.begin(), lst.end()) {
176     sum += *i;
177   }
178   EXPECT_EQ(10, sum);
179 }