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