proxygen
ForeachTest.cpp File Reference
#include <folly/container/Foreach.h>
#include <array>
#include <initializer_list>
#include <iterator>
#include <list>
#include <map>
#include <string>
#include <tuple>
#include <vector>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Classes

class  folly::test::TestRValueConstruct
 
class  folly::test::TestAdlIterable
 
class  folly::test::TestBothIndexingAndIter
 
class  folly::test::TestBothIndexingAndIter::Iterator
 

Namespaces

 folly
 —— Concurrent Priority Queue Implementation ——
 
 folly::test
 

Functions

auto folly::test::begin (TestAdlIterable &instance)
 
auto folly::test::begin (const TestAdlIterable &instance)
 
auto folly::test::end (TestAdlIterable &instance)
 
auto folly::test::end (const TestAdlIterable &instance)
 
 TEST (Foreach, ForEachFunctionBasic)
 
 TEST (Foreach, ForEachFunctionBasicRuntimeOneArg)
 
 TEST (Foreach, ForEachFunctionBasicRuntimeTwoArg)
 
 TEST (Foreach, ForEachFunctionBasicRuntimeThreeArg)
 
 TEST (Foreach, ForEachFunctionBasicTupleOneArg)
 
 TEST (Foreach, ForEachFunctionBasicTupleTwoArg)
 
 TEST (Foreach, ForEachFunctionBreakRuntimeOneArg)
 
 TEST (Foreach, ForEachFunctionBreakRuntimeTwoArg)
 
 TEST (Foreach, ForEachFunctionBreakRuntimeThreeArg)
 
 TEST (Foreach, ForEachFunctionBreakTupleOneArg)
 
 TEST (Foreach, ForEachFunctionBreakTupleTwoArg)
 
 TEST (Foreach, ForEachFunctionArray)
 
 TEST (Foreach, ForEachFunctionInitializerListBasic)
 
 TEST (Foreach, ForEachFunctionTestForward)
 
 TEST (Foreach, ForEachFunctionAdlIterable)
 
 TEST (ForEach, FetchRandomAccessIterator)
 
 TEST (ForEach, FetchIndexing)
 
 TEST (ForEach, FetchTuple)
 
 TEST (ForEach, FetchTestPreferIterator)
 
 TEST (Foreach, ForEachRvalue)
 
 TEST (Foreach, ForEachNested)
 
 TEST (Foreach, ForEachKV)
 
 TEST (Foreach, ForEachKVBreak)
 
 TEST (Foreach, ForEachKvWithMultiMap)
 
 TEST (Foreach, ForEachEnumerate)
 
 TEST (Foreach, ForEachEnumerateBreak)
 
 TEST (Foreach, ForEachRangeR)
 

Function Documentation

TEST ( Foreach  ,
ForEachFunctionBasic   
)

Definition at line 104 of file ForeachTest.cpp.

References EXPECT_TRUE, folly::for_each(), std::tr1::make_tuple(), and folly::range().

104  {
105  auto range = std::make_tuple(1, 2, 3);
106  auto result_range = std::vector<int>{};
107  auto correct_result_range = std::vector<int>{1, 2, 3};
108 
109  folly::for_each(range, [&](auto ele) { result_range.push_back(ele); });
110 
111  EXPECT_TRUE(std::equal(
112  result_range.begin(), result_range.end(), correct_result_range.begin()));
113 }
tuple make_tuple()
Definition: gtest-tuple.h:675
Gen range(Value begin, Value end)
Definition: Base.h:467
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionBasicRuntimeOneArg   
)

Definition at line 115 of file ForeachTest.cpp.

References current, EXPECT_EQ, folly::for_each(), and folly::range().

115  {
116  auto range = std::vector<int>{1, 2, 3};
117  auto current = 0;
118  folly::for_each(range, [&](auto ele) {
119  if (current == 0) {
120  EXPECT_EQ(ele, 1);
121  } else if (current == 1) {
122  EXPECT_EQ(ele, 2);
123  } else {
124  EXPECT_EQ(ele, 3);
125  }
126  ++current;
127  });
128 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
int current
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionBasicRuntimeTwoArg   
)

Definition at line 130 of file ForeachTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, folly::for_each(), and folly::range().

130  {
131  auto range = std::vector<int>{1, 2, 3};
132  folly::for_each(range, [](auto ele, auto index) {
133  EXPECT_TRUE(index < 3);
134  if (index == 0) {
135  EXPECT_EQ(ele, 1);
136  } else if (index == 1) {
137  EXPECT_EQ(ele, 2);
138  } else if (index == 2) {
139  EXPECT_EQ(ele, 3);
140  }
141  });
142 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionBasicRuntimeThreeArg   
)

Definition at line 144 of file ForeachTest.cpp.

References EXPECT_TRUE, folly::for_each(), and folly::range().

144  {
145  auto range = std::list<int>{1, 2, 3};
146  auto result_range = std::list<int>{1, 3};
147  folly::for_each(range, [&](auto ele, auto, auto iter) {
148  if (ele == 2) {
149  range.erase(iter);
150  }
151  });
152  EXPECT_TRUE(std::equal(range.begin(), range.end(), result_range.begin()));
153 }
Gen range(Value begin, Value end)
Definition: Base.h:467
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionBasicTupleOneArg   
)

Definition at line 155 of file ForeachTest.cpp.

References current, EXPECT_EQ, folly::for_each(), std::tr1::make_tuple(), and folly::range().

155  {
156  auto range = std::make_tuple(1, 2, 3);
157  auto current = 0;
158  folly::for_each(range, [&](auto ele) {
159  if (current == 0) {
160  EXPECT_EQ(ele, 1);
161  } else if (current == 1) {
162  EXPECT_EQ(ele, 2);
163  } else {
164  EXPECT_EQ(ele, 3);
165  }
166  ++current;
167  });
168 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
tuple make_tuple()
Definition: gtest-tuple.h:675
Gen range(Value begin, Value end)
Definition: Base.h:467
int current
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionBasicTupleTwoArg   
)

Definition at line 170 of file ForeachTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, folly::for_each(), std::tr1::make_tuple(), and folly::range().

170  {
171  auto range = std::make_tuple(1, 2, 3);
172  folly::for_each(range, [](auto ele, auto index) {
173  EXPECT_TRUE(index < 3);
174  if (index == 0) {
175  EXPECT_EQ(ele, 1);
176  } else if (index == 1) {
177  EXPECT_EQ(ele, 2);
178  } else if (index == 2) {
179  EXPECT_EQ(ele, 3);
180  }
181  });
182 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
tuple make_tuple()
Definition: gtest-tuple.h:675
Gen range(Value begin, Value end)
Definition: Base.h:467
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionBreakRuntimeOneArg   
)

Definition at line 184 of file ForeachTest.cpp.

References EXPECT_EQ, folly::for_each(), folly::loop_break, folly::loop_continue, and folly::range().

184  {
185  auto range = std::vector<int>{1, 2, 3};
186  auto iterations = 0;
187  folly::for_each(range, [&](auto) {
188  ++iterations;
189  if (iterations == 1) {
190  return folly::loop_break;
191  }
192  return folly::loop_continue;
193  });
194  EXPECT_EQ(iterations, 1);
195 }
constexpr auto loop_continue
Definition: Foreach.h:98
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
constexpr auto loop_break
Definition: Foreach.h:97
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionBreakRuntimeTwoArg   
)

Definition at line 197 of file ForeachTest.cpp.

References EXPECT_EQ, folly::for_each(), folly::loop_break, folly::loop_continue, and folly::range().

197  {
198  auto range = std::vector<int>{1, 2, 3};
199  auto iterations = 0;
200  folly::for_each(range, [&](auto, auto index) {
201  ++iterations;
202  if (index == 1) {
203  return folly::loop_break;
204  }
205  return folly::loop_continue;
206  });
207  EXPECT_EQ(iterations, 2);
208 }
constexpr auto loop_continue
Definition: Foreach.h:98
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
constexpr auto loop_break
Definition: Foreach.h:97
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionBreakRuntimeThreeArg   
)

Definition at line 210 of file ForeachTest.cpp.

References EXPECT_EQ, folly::for_each(), folly::loop_break, folly::loop_continue, and folly::range().

210  {
211  auto range = std::vector<int>{1, 2, 3};
212  auto iterations = 0;
213  folly::for_each(range, [&](auto, auto index, auto) {
214  ++iterations;
215  if (index == 1) {
216  return folly::loop_break;
217  }
218  return folly::loop_continue;
219  });
220  EXPECT_EQ(iterations, 2);
221 }
constexpr auto loop_continue
Definition: Foreach.h:98
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
constexpr auto loop_break
Definition: Foreach.h:97
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionBreakTupleOneArg   
)

Definition at line 223 of file ForeachTest.cpp.

References EXPECT_EQ, folly::for_each(), folly::loop_break, folly::loop_continue, and folly::range().

223  {
224  auto range = std::vector<int>{1, 2, 3};
225  auto iterations = 0;
226  folly::for_each(range, [&](auto) {
227  ++iterations;
228  if (iterations == 1) {
229  return folly::loop_break;
230  }
231  return folly::loop_continue;
232  });
233  EXPECT_EQ(iterations, 1);
234 }
constexpr auto loop_continue
Definition: Foreach.h:98
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
constexpr auto loop_break
Definition: Foreach.h:97
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionBreakTupleTwoArg   
)

Definition at line 236 of file ForeachTest.cpp.

References EXPECT_EQ, folly::for_each(), folly::loop_break, folly::loop_continue, and folly::range().

236  {
237  auto range = std::vector<int>{1, 2, 3};
238  auto iterations = 0;
239  folly::for_each(range, [&](auto, auto index) {
240  ++iterations;
241  if (index == 1) {
242  return folly::loop_break;
243  }
244  return folly::loop_continue;
245  });
246  EXPECT_EQ(iterations, 2);
247 }
constexpr auto loop_continue
Definition: Foreach.h:98
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
constexpr auto loop_break
Definition: Foreach.h:97
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionArray   
)

Definition at line 249 of file ForeachTest.cpp.

References EXPECT_EQ, folly::for_each(), folly::loop_break, folly::loop_continue, and folly::range().

249  {
250  auto range = std::array<int, 3>{{1, 2, 3}};
251  auto iterations = 0;
252  folly::for_each(range, [&](auto, auto index) {
253  ++iterations;
254  if (index == 1) {
255  return folly::loop_break;
256  }
257  return folly::loop_continue;
258  });
259  EXPECT_EQ(iterations, 2);
260 }
constexpr auto loop_continue
Definition: Foreach.h:98
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
constexpr auto loop_break
Definition: Foreach.h:97
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionInitializerListBasic   
)

Definition at line 262 of file ForeachTest.cpp.

References folly::for_each().

262  {
263  folly::for_each(std::initializer_list<int>{1, 2, 3}, [](auto ele) { ++ele; });
264 }
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionTestForward   
)

Definition at line 266 of file ForeachTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, folly::for_each(), std::tr1::make_tuple(), and folly::gen::move.

266  {
268  auto range_one = std::vector<TestRValueConstruct>{};
269  range_one.resize(3);
270 
271  folly::for_each(std::move(range_one), [](auto ele) {
272  EXPECT_FALSE(ele.constructed_from_rvalue);
273  });
274 
276  std::make_tuple(TestRValueConstruct{}, TestRValueConstruct{}),
277  [](auto ele) { EXPECT_TRUE(ele.constructed_from_rvalue); });
278 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
tuple make_tuple()
Definition: gtest-tuple.h:675
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( Foreach  ,
ForEachFunctionAdlIterable   
)

Definition at line 280 of file ForeachTest.cpp.

References EXPECT_EQ, folly::for_each(), and folly::range().

280  {
281  auto range = test::TestAdlIterable{};
282  auto iterations = 0;
283  folly::for_each(range, [&](auto ele, auto index) {
284  ++iterations;
285  EXPECT_EQ(ele, index);
286  });
287  EXPECT_EQ(iterations, 4);
288 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
TEST ( ForEach  ,
FetchRandomAccessIterator   
)

Definition at line 290 of file ForeachTest.cpp.

References EXPECT_EQ, and folly::fetch().

290  {
291  auto vec = std::vector<int>{1, 2, 3};
292  auto& second = folly::fetch(vec, 1);
293  EXPECT_EQ(second, 2);
294  second = 3;
295  EXPECT_EQ(second, 3);
296 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Definition: Traits.h:588
decltype(auto) FOLLY_CPP14_CONSTEXPR fetch(Sequence &&sequence, Index &&index)
Definition: Foreach-inl.h:322
TEST ( ForEach  ,
FetchIndexing   
)

Definition at line 298 of file ForeachTest.cpp.

References EXPECT_EQ, and folly::fetch().

298  {
299  auto mp = std::map<int, int>{{1, 2}};
300  auto& ele = folly::fetch(mp, 1);
301  EXPECT_EQ(ele, 2);
302  ele = 3;
303  EXPECT_EQ(ele, 3);
304 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
decltype(auto) FOLLY_CPP14_CONSTEXPR fetch(Sequence &&sequence, Index &&index)
Definition: Foreach-inl.h:322
TEST ( ForEach  ,
FetchTuple   
)

Definition at line 306 of file ForeachTest.cpp.

References EXPECT_EQ, folly::fetch(), and std::tr1::make_tuple().

306  {
307  auto mp = std::make_tuple(1, 2, 3);
308  auto& ele = folly::fetch(mp, std::integral_constant<int, 1>{});
309  EXPECT_EQ(ele, 2);
310  ele = 3;
311  EXPECT_EQ(ele, 3);
312 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
tuple make_tuple()
Definition: gtest-tuple.h:675
decltype(auto) FOLLY_CPP14_CONSTEXPR fetch(Sequence &&sequence, Index &&index)
Definition: Foreach-inl.h:322
TEST ( ForEach  ,
FetchTestPreferIterator   
)

Definition at line 314 of file ForeachTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, folly::fetch(), and folly::range().

314  {
316  auto& ele = folly::fetch(range, 0);
317  EXPECT_TRUE(range.called_begin);
318  EXPECT_EQ(ele, 0);
319  ele = 2;
320  EXPECT_EQ(folly::fetch(range, 0), 2);
321 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
decltype(auto) FOLLY_CPP14_CONSTEXPR fetch(Sequence &&sequence, Index &&index)
Definition: Foreach-inl.h:322
TEST ( Foreach  ,
ForEachRvalue   
)

Definition at line 323 of file ForeachTest.cpp.

References EXPECT_EQ, FOR_EACH, FOR_EACH_R, and string.

323  {
324  const char* const hello = "hello";
325  int n = 0;
326  FOR_EACH (it, std::string(hello)) { ++n; }
327  EXPECT_EQ(strlen(hello), n);
328  FOR_EACH_R (it, std::string(hello)) {
329  --n;
330  EXPECT_EQ(hello[n], *it);
331  }
332  EXPECT_EQ(0, n);
333 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define FOR_EACH_R(i, c)
Definition: Foreach.h:156
#define FOR_EACH(i, c)
Definition: Foreach.h:143
const char * string
Definition: Conv.cpp:212
TEST ( Foreach  ,
ForEachNested   
)

Definition at line 335 of file ForeachTest.cpp.

References EXPECT_EQ, FOR_EACH, i, and string.

335  {
336  const std::string hello = "hello";
337  size_t n = 0;
338  FOR_EACH (i, hello) {
339  FOR_EACH (j, hello) { ++n; }
340  }
341  auto len = hello.size();
342  EXPECT_EQ(len * len, n);
343 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define FOR_EACH(i, c)
Definition: Foreach.h:143
const char * string
Definition: Conv.cpp:212
TEST ( Foreach  ,
ForEachKV   
)

Definition at line 345 of file ForeachTest.cpp.

References EXPECT_EQ, FOR_EACH_KV, string, testMap(), folly::value(), and values().

345  {
346  std::map<std::string, int> testMap;
347  testMap["abc"] = 1;
348  testMap["def"] = 2;
349  std::string keys = "";
350  int values = 0;
351  int numEntries = 0;
352  FOR_EACH_KV (key, value, testMap) {
353  keys += key;
354  values += value;
355  ++numEntries;
356  }
357  EXPECT_EQ("abcdef", keys);
358  EXPECT_EQ(3, values);
359  EXPECT_EQ(2, numEntries);
360 }
#define FOR_EACH_KV(k, v, c)
Definition: Foreach.h:197
void testMap()
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static const char *const value
Definition: Conv.cpp:50
const char * string
Definition: Conv.cpp:212
std::vector< int > values(1'000)
TEST ( Foreach  ,
ForEachKVBreak   
)

Definition at line 362 of file ForeachTest.cpp.

References EXPECT_EQ, FOR_EACH_KV, string, testMap(), folly::value(), and values().

362  {
363  std::map<std::string, int> testMap;
364  testMap["abc"] = 1;
365  testMap["def"] = 2;
366  std::string keys = "";
367  int values = 0;
368  int numEntries = 0;
369  FOR_EACH_KV (key, value, testMap) {
370  keys += key;
371  values += value;
372  ++numEntries;
373  break;
374  }
375  EXPECT_EQ("abc", keys);
376  EXPECT_EQ(1, values);
377  EXPECT_EQ(1, numEntries);
378 }
#define FOR_EACH_KV(k, v, c)
Definition: Foreach.h:197
void testMap()
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static const char *const value
Definition: Conv.cpp:50
const char * string
Definition: Conv.cpp:212
std::vector< int > values(1'000)
TEST ( Foreach  ,
ForEachKvWithMultiMap   
)

Definition at line 380 of file ForeachTest.cpp.

References EXPECT_EQ, FOR_EACH_KV, string, testMap(), folly::value(), and values().

380  {
381  std::multimap<std::string, int> testMap;
382  testMap.insert(std::make_pair("abc", 1));
383  testMap.insert(std::make_pair("abc", 2));
384  testMap.insert(std::make_pair("def", 3));
385  std::string keys = "";
386  int values = 0;
387  int numEntries = 0;
388  FOR_EACH_KV (key, value, testMap) {
389  keys += key;
390  values += value;
391  ++numEntries;
392  }
393  EXPECT_EQ("abcabcdef", keys);
394  EXPECT_EQ(6, values);
395  EXPECT_EQ(3, numEntries);
396 }
#define FOR_EACH_KV(k, v, c)
Definition: Foreach.h:197
void testMap()
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static const char *const value
Definition: Conv.cpp:50
const char * string
Definition: Conv.cpp:212
std::vector< int > values(1'000)
TEST ( Foreach  ,
ForEachEnumerate   
)

Definition at line 398 of file ForeachTest.cpp.

References EXPECT_EQ, and FOR_EACH_ENUMERATE.

398  {
399  std::vector<int> vv;
400  int sumAA = 0;
401  int sumIter = 0;
402  int numIterations = 0;
403  FOR_EACH_ENUMERATE (aa, iter, vv) {
404  sumAA += aa;
405  sumIter += *iter;
406  ++numIterations;
407  }
408  EXPECT_EQ(sumAA, 0);
409  EXPECT_EQ(sumIter, 0);
410  EXPECT_EQ(numIterations, 0);
411 
412  vv.push_back(1);
413  vv.push_back(3);
414  vv.push_back(5);
415  FOR_EACH_ENUMERATE (aa, iter, vv) {
416  sumAA += aa;
417  sumIter += *iter;
418  ++numIterations;
419  }
420  EXPECT_EQ(sumAA, 3); // 0 + 1 + 2
421  EXPECT_EQ(sumIter, 9); // 1 + 3 + 5
422  EXPECT_EQ(numIterations, 3);
423 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define FOR_EACH_ENUMERATE(count, i, c)
Definition: Foreach.h:170
TEST ( Foreach  ,
ForEachEnumerateBreak   
)

Definition at line 425 of file ForeachTest.cpp.

References EXPECT_EQ, and FOR_EACH_ENUMERATE.

425  {
426  std::vector<int> vv;
427  int sumAA = 0;
428  int sumIter = 0;
429  int numIterations = 0;
430  vv.push_back(1);
431  vv.push_back(2);
432  vv.push_back(4);
433  vv.push_back(8);
434  FOR_EACH_ENUMERATE (aa, iter, vv) {
435  sumAA += aa;
436  sumIter += *iter;
437  ++numIterations;
438  if (aa == 1) {
439  break;
440  }
441  }
442  EXPECT_EQ(sumAA, 1); // 0 + 1
443  EXPECT_EQ(sumIter, 3); // 1 + 2
444  EXPECT_EQ(numIterations, 2);
445 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define FOR_EACH_ENUMERATE(count, i, c)
Definition: Foreach.h:170
TEST ( Foreach  ,
ForEachRangeR   
)

Definition at line 447 of file ForeachTest.cpp.

References EXPECT_EQ, FOR_EACH_RANGE_R, i, and sum().

447  {
448  int sum = 0;
449 
450  FOR_EACH_RANGE_R (i, 0, 0) { sum += i; }
451  EXPECT_EQ(0, sum);
452 
453  FOR_EACH_RANGE_R (i, 0, -1) { sum += i; }
454  EXPECT_EQ(0, sum);
455 
456  FOR_EACH_RANGE_R (i, 0, 5) { sum += i; }
457  EXPECT_EQ(10, sum);
458 
459  std::list<int> lst = {0, 1, 2, 3, 4};
460  sum = 0;
461  FOR_EACH_RANGE_R (i, lst.begin(), lst.end()) { sum += *i; }
462  EXPECT_EQ(10, sum);
463 }
std::atomic< int64_t > sum(0)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define FOR_EACH_RANGE_R(i, begin, end)
Definition: Foreach.h:324