Use the GTest portability headers
[folly.git] / folly / gen / test / BaseTest.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 <glog/logging.h>
18
19 #include <iosfwd>
20 #include <random>
21 #include <set>
22 #include <vector>
23
24 #include <folly/FBVector.h>
25 #include <folly/MapUtil.h>
26 #include <folly/Memory.h>
27 #include <folly/String.h>
28 #include <folly/dynamic.h>
29 #include <folly/experimental/TestUtil.h>
30 #include <folly/gen/Base.h>
31 #include <folly/portability/GTest.h>
32
33 using namespace folly::gen;
34 using namespace folly;
35 using std::make_tuple;
36 using std::ostream;
37 using std::pair;
38 using std::set;
39 using std::string;
40 using std::tuple;
41 using std::unique_ptr;
42 using std::vector;
43
44 #define EXPECT_SAME(A, B) \
45   static_assert(std::is_same<A, B>::value, "Mismatched: " #A ", " #B)
46 EXPECT_SAME(int&&, typename ArgumentReference<int>::type);
47 EXPECT_SAME(int&, typename ArgumentReference<int&>::type);
48 EXPECT_SAME(const int&, typename ArgumentReference<const int&>::type);
49 EXPECT_SAME(const int&, typename ArgumentReference<const int>::type);
50
51 template<typename T>
52 ostream& operator<<(ostream& os, const set<T>& values) {
53   return os << from(values);
54 }
55
56 template<typename T>
57 ostream& operator<<(ostream& os, const vector<T>& values) {
58   os << "[";
59   for (auto& value : values) {
60     if (&value != &values.front()) {
61       os << " ";
62     }
63     os << value;
64   }
65   return os << "]";
66 }
67
68 auto square = [](int x) { return x * x; };
69 auto add = [](int a, int b) { return a + b; };
70 auto multiply = [](int a, int b) { return a * b; };
71
72 auto product = foldl(1, multiply);
73
74 template<typename A, typename B>
75 ostream& operator<<(ostream& os, const pair<A, B>& pair) {
76   return os << "(" << pair.first << ", " << pair.second << ")";
77 }
78
79 TEST(Gen, Count) {
80   auto gen = seq(1, 10);
81   EXPECT_EQ(10, gen | count);
82   EXPECT_EQ(5, gen | take(5) | count);
83 }
84
85 TEST(Gen, Sum) {
86   auto gen = seq(1, 10);
87   EXPECT_EQ((1 + 10) * 10 / 2, gen | sum);
88   EXPECT_EQ((1 + 5) * 5 / 2, gen | take(5) | sum);
89 }
90
91 TEST(Gen, Foreach) {
92   auto gen = seq(1, 4);
93   int accum = 0;
94   gen | [&](int x) { accum += x; };
95   EXPECT_EQ(10, accum);
96   int accum2 = 0;
97   gen | take(3) | [&](int x) { accum2 += x; };
98   EXPECT_EQ(6, accum2);
99 }
100
101 TEST(Gen, Map) {
102   auto expected = vector<int>{4, 9, 16};
103   auto gen = from({2, 3, 4}) | map(square);
104   EXPECT_EQ((vector<int>{4, 9, 16}), gen | as<vector>());
105   EXPECT_EQ((vector<int>{4, 9}), gen | take(2) | as<vector>());
106 }
107
108 TEST(Gen, Member) {
109   struct Counter {
110     Counter(int start = 0)
111       : c(start)
112     {}
113
114     int count() const { return c; }
115     int incr() { return ++c; }
116
117     int& ref() { return c; }
118     const int& ref() const { return c; }
119    private:
120     int c;
121   };
122   auto counters = seq(1, 10) | eachAs<Counter>() | as<vector>();
123   EXPECT_EQ(10 * (1 + 10) / 2,
124             from(counters)
125           | member(&Counter::count)
126           | sum);
127   EXPECT_EQ(10 * (1 + 10) / 2,
128             from(counters)
129           | indirect
130           | member(&Counter::count)
131           | sum);
132   EXPECT_EQ(10 * (2 + 11) / 2,
133             from(counters)
134           | member(&Counter::incr)
135           | sum);
136   EXPECT_EQ(10 * (3 + 12) / 2,
137             from(counters)
138           | indirect
139           | member(&Counter::incr)
140           | sum);
141   EXPECT_EQ(10 * (3 + 12) / 2,
142             from(counters)
143           | member(&Counter::count)
144           | sum);
145
146   // type-verifications
147   auto m = empty<Counter&>();
148   auto c = empty<const Counter&>();
149   m | member(&Counter::incr) | assert_type<int&&>();
150   m | member(&Counter::count) | assert_type<int&&>();
151   m | member(&Counter::count) | assert_type<int&&>();
152   m | member<Const>(&Counter::ref) | assert_type<const int&>();
153   m | member<Mutable>(&Counter::ref) | assert_type<int&>();
154   c | member<Const>(&Counter::ref) | assert_type<const int&>();
155 }
156
157 TEST(Gen, Field) {
158   struct X {
159     X() : a(2), b(3), c(4), d(b) {}
160
161     const int a;
162     int b;
163     mutable int c;
164     int& d; // can't access this with a field pointer.
165   };
166
167   std::vector<X> xs(1);
168   EXPECT_EQ(2, from(xs)
169              | field(&X::a)
170              | sum);
171   EXPECT_EQ(3, from(xs)
172              | field(&X::b)
173              | sum);
174   EXPECT_EQ(4, from(xs)
175              | field(&X::c)
176              | sum);
177   EXPECT_EQ(2, seq(&xs[0], &xs[0])
178              | field(&X::a)
179              | sum);
180   // type-verification
181   empty<X&>() | field(&X::a) | assert_type<const int&>();
182   empty<X*>() | field(&X::a) | assert_type<const int&>();
183   empty<X&>() | field(&X::b) | assert_type<int&>();
184   empty<X*>() | field(&X::b) | assert_type<int&>();
185   empty<X&>() | field(&X::c) | assert_type<int&>();
186   empty<X*>() | field(&X::c) | assert_type<int&>();
187
188   empty<X&&>() | field(&X::a) | assert_type<const int&&>();
189   empty<X&&>() | field(&X::b) | assert_type<int&&>();
190   empty<X&&>() | field(&X::c) | assert_type<int&&>();
191   // references don't imply ownership so they're not moved
192
193   empty<const X&>() | field(&X::a) | assert_type<const int&>();
194   empty<const X*>() | field(&X::a) | assert_type<const int&>();
195   empty<const X&>() | field(&X::b) | assert_type<const int&>();
196   empty<const X*>() | field(&X::b) | assert_type<const int&>();
197   // 'mutable' has no effect on field pointers, by C++ spec
198   empty<const X&>() | field(&X::c) | assert_type<const int&>();
199   empty<const X*>() | field(&X::c) | assert_type<const int&>();
200
201   // can't form pointer-to-reference field: empty<X&>() | field(&X::d)
202 }
203
204 TEST(Gen, Seq) {
205   // cover the fenceposts of the loop unrolling
206   for (int n = 1; n < 100; ++n) {
207     EXPECT_EQ(n, seq(1, n) | count);
208     EXPECT_EQ(n + 1, seq(1) | take(n + 1) | count);
209   }
210 }
211
212 TEST(Gen, SeqWithStep) {
213   EXPECT_EQ(75, seq(5, 25, 5) | sum);
214 }
215
216 TEST(Gen, SeqWithStepArray) {
217   const std::array<int, 6> arr{{1, 2, 3, 4, 5, 6}};
218   EXPECT_EQ(9, seq(&arr[0], &arr[5], 2)
219              | map([](const int *i) { return *i; })
220              | sum);
221 }
222
223 TEST(Gen, Range) {
224   // cover the fenceposts of the loop unrolling
225   for (int n = 1; n < 100; ++n) {
226     EXPECT_EQ(gen::range(0, n) | count, n);
227   }
228 }
229
230 TEST(Gen, RangeWithStep) {
231   EXPECT_EQ(50, range(5, 25, 5) | sum);
232 }
233
234 TEST(Gen, FromIterators) {
235   vector<int> source {2, 3, 5, 7, 11};
236   auto gen = from(folly::range(source.begin() + 1, source.end() - 1));
237   EXPECT_EQ(3 * 5 * 7, gen | product);
238 }
239
240 TEST(Gen, FromMap) {
241   auto source = seq(0, 10)
242               | map([](int i) { return std::make_pair(i, i * i); })
243               | as<std::map<int, int>>();
244   auto gen = fromConst(source)
245            | map([&](const std::pair<const int, int>& p) {
246                return p.second - p.first;
247              });
248   EXPECT_EQ(330, gen | sum);
249 }
250
251 TEST(Gen, Filter) {
252   const auto expected = vector<int>{1, 2, 4, 5, 7, 8};
253   auto actual =
254       seq(1, 9)
255     | filter([](int x) { return x % 3; })
256     | as<vector<int>>();
257   EXPECT_EQ(expected, actual);
258 }
259
260 TEST(Gen, FilterDefault) {
261   {
262     // Default filter should remove 0s
263     const auto expected = vector<int>{1, 1, 2, 3};
264     auto actual =
265         from({0, 1, 1, 0, 2, 3, 0})
266       | filter()
267       | as<vector>();
268     EXPECT_EQ(expected, actual);
269   }
270   {
271     // Default filter should remove nullptrs
272     int a = 5;
273     int b = 3;
274     int c = 0;
275     const auto expected = vector<int*>{&a, &b, &c};
276     auto actual =
277         from({(int*)nullptr, &a, &b, &c, (int*)nullptr})
278       | filter()
279       | as<vector>();
280     EXPECT_EQ(expected, actual);
281   }
282   {
283     // Default filter on Optionals should remove folly::null
284     const auto expected =
285         vector<Optional<int>>{Optional<int>(5), Optional<int>(0)};
286     const auto actual =
287         from({Optional<int>(5), Optional<int>(), Optional<int>(0)})
288       | filter()
289       | as<vector>();
290     EXPECT_EQ(expected, actual);
291   }
292 }
293
294 TEST(Gen, FilterSink) {
295   auto actual
296     = seq(1, 2)
297     | map([](int x) { return vector<int>{x}; })
298     | filter([](vector<int> v) { return !v.empty(); })
299     | as<vector>();
300   EXPECT_FALSE(from(actual) | rconcat | isEmpty);
301 }
302
303 TEST(Gen, Contains) {
304   {
305     auto gen =
306         seq(1, 9)
307       | map(square);
308     EXPECT_TRUE(gen | contains(49));
309     EXPECT_FALSE(gen | contains(50));
310   }
311   {
312     auto gen =
313         seq(1) // infinite, to prove laziness
314       | map(square)
315       | eachTo<std::string>();
316
317     // std::string gen, const char* needle
318     EXPECT_TRUE(gen | take(9999) | contains("49"));
319   }
320 }
321
322 TEST(Gen, Take) {
323   {
324     auto expected = vector<int>{1, 4, 9, 16};
325     auto actual =
326       seq(1, 1000)
327       | mapped([](int x) { return x * x; })
328       | take(4)
329       | as<vector<int>>();
330     EXPECT_EQ(expected, actual);
331   }
332   {
333     auto expected = vector<int>{ 0, 1, 4, 5, 8 };
334     auto actual
335       = ((seq(0) | take(2)) +
336          (seq(4) | take(2)) +
337          (seq(8) | take(2)))
338       | take(5)
339       | as<vector>();
340     EXPECT_EQ(expected, actual);
341   }
342   {
343     auto expected = vector<int>{ 0, 1, 4, 5, 8 };
344     auto actual
345       = seq(0)
346       | mapped([](int i) {
347           return seq(i * 4) | take(2);
348         })
349       | concat
350       | take(5)
351       | as<vector>();
352     EXPECT_EQ(expected, actual);
353   }
354   {
355     int64_t limit = 5;
356     take(limit - 5);
357     EXPECT_THROW(take(limit - 6), std::invalid_argument);
358   }
359 }
360
361
362 TEST(Gen, Stride) {
363   {
364     EXPECT_THROW(stride(0), std::invalid_argument);
365   }
366   {
367     auto expected = vector<int>{1, 2, 3, 4};
368     auto actual
369       = seq(1, 4)
370       | stride(1)
371       | as<vector<int>>();
372     EXPECT_EQ(expected, actual);
373   }
374   {
375     auto expected = vector<int>{1, 3, 5, 7};
376     auto actual
377       = seq(1, 8)
378       | stride(2)
379       | as<vector<int>>();
380     EXPECT_EQ(expected, actual);
381   }
382   {
383     auto expected = vector<int>{1, 4, 7, 10};
384     auto actual
385       = seq(1, 12)
386       | stride(3)
387       | as<vector<int>>();
388     EXPECT_EQ(expected, actual);
389   }
390   {
391     auto expected = vector<int>{1, 3, 5, 7, 9, 1, 4, 7, 10};
392     auto actual
393       = ((seq(1, 10) | stride(2)) +
394          (seq(1, 10) | stride(3)))
395       | as<vector<int>>();
396     EXPECT_EQ(expected, actual);
397   }
398   EXPECT_EQ(500, seq(1) | take(1000) | stride(2) | count);
399   EXPECT_EQ(10, seq(1) | take(1000) | stride(2) | take(10) | count);
400 }
401
402 TEST(Gen, Sample) {
403   std::mt19937 rnd(42);
404
405   auto sampler =
406       seq(1, 100)
407     | sample(50, rnd);
408   std::unordered_map<int,int> hits;
409   const int kNumIters = 80;
410   for (int i = 0; i < kNumIters; i++) {
411     auto vec = sampler | as<vector<int>>();
412     EXPECT_EQ(vec.size(), 50);
413     auto uniq = fromConst(vec) | as<set<int>>();
414     EXPECT_EQ(uniq.size(), vec.size());  // sampling without replacement
415     for (auto v: vec) {
416       ++hits[v];
417     }
418   }
419
420   // In 80 separate samples of our range, we should have seen every value
421   // at least once and no value all 80 times. (The odds of either of those
422   // events is 1/2^80).
423   EXPECT_EQ(hits.size(), 100);
424   for (auto hit: hits) {
425     EXPECT_GT(hit.second, 0);
426     EXPECT_LT(hit.second, kNumIters);
427   }
428
429   auto small =
430       seq(1, 5)
431     | sample(10);
432   EXPECT_EQ((small | sum), 15);
433   EXPECT_EQ((small | take(3) | count), 3);
434 }
435
436 TEST(Gen, Skip) {
437   auto gen =
438       seq(1, 1000)
439     | mapped([](int x) { return x * x; })
440     | skip(4)
441     | take(4);
442   EXPECT_EQ((vector<int>{25, 36, 49, 64}), gen | as<vector>());
443 }
444
445 TEST(Gen, Until) {
446   {
447     auto expected = vector<int>{1, 4, 9, 16};
448     auto actual
449       = seq(1, 1000)
450       | mapped([](int x) { return x * x; })
451       | until([](int x) { return x > 20; })
452       | as<vector<int>>();
453     EXPECT_EQ(expected, actual);
454   }
455   {
456     auto expected = vector<int>{ 0, 1, 4, 5, 8 };
457     auto actual
458       = ((seq(0) | until([](int i) { return i > 1; })) +
459          (seq(4) | until([](int i) { return i > 5; })) +
460          (seq(8) | until([](int i) { return i > 9; })))
461       | until([](int i) { return i > 8; })
462       | as<vector<int>>();
463     EXPECT_EQ(expected, actual);
464   }
465   /*
466   {
467     auto expected = vector<int>{ 0, 1, 5, 6, 10 };
468     auto actual
469       = seq(0)
470       | mapped([](int i) {
471           return seq(i * 5) | until([=](int j) { return j > i * 5 + 1; });
472         })
473       | concat
474       | until([](int i) { return i > 10; })
475       | as<vector<int>>();
476     EXPECT_EQ(expected, actual);
477   }
478   */
479 }
480
481 TEST(Gen, Composed) {
482   // Operator, Operator
483   auto valuesOf =
484       filter([](Optional<int>& o) { return o.hasValue(); })
485     | map([](Optional<int>& o) -> int& { return o.value(); });
486   std::vector<Optional<int>> opts {
487     none, 4, none, 6, none
488   };
489   EXPECT_EQ(4 * 4 + 6 * 6, from(opts) | valuesOf | map(square) | sum);
490   // Operator, Sink
491   auto sumOpt = valuesOf | sum;
492   EXPECT_EQ(10, from(opts) | sumOpt);
493 }
494
495 TEST(Gen, Chain) {
496   std::vector<int> nums {2, 3, 5, 7};
497   std::map<int, int> mappings { { 3, 9}, {5, 25} };
498   auto gen = from(nums) + (from(mappings) | get<1>());
499   EXPECT_EQ(51, gen | sum);
500   EXPECT_EQ(5, gen | take(2) | sum);
501   EXPECT_EQ(26, gen | take(5) | sum);
502 }
503
504 TEST(Gen, Concat) {
505   std::vector<std::vector<int>> nums {{2, 3}, {5, 7}};
506   auto gen = from(nums) | rconcat;
507   EXPECT_EQ(17, gen | sum);
508   EXPECT_EQ(10, gen | take(3) | sum);
509 }
510
511 TEST(Gen, ConcatGen) {
512   auto gen = seq(1, 10)
513            | map([](int i) { return seq(1, i); })
514            | concat;
515   EXPECT_EQ(220, gen | sum);
516   EXPECT_EQ(10, gen | take(6) | sum);
517 }
518
519 TEST(Gen, ConcatAlt) {
520   std::vector<std::vector<int>> nums {{2, 3}, {5, 7}};
521   auto actual = from(nums)
522               | map([](std::vector<int>& v) { return from(v); })
523               | concat
524               | sum;
525   auto expected = 17;
526   EXPECT_EQ(expected, actual);
527 }
528
529 TEST(Gen, Order) {
530   auto expected = vector<int>{0, 3, 5, 6, 7, 8, 9};
531   auto actual =
532       from({8, 6, 7, 5, 3, 0, 9})
533     | order
534     | as<vector>();
535   EXPECT_EQ(expected, actual);
536 }
537
538 TEST(Gen, OrderMoved) {
539   auto expected = vector<int>{0, 9, 25, 36, 49, 64, 81};
540   auto actual =
541       from({8, 6, 7, 5, 3, 0, 9})
542     | move
543     | order
544     | map(square)
545     | as<vector>();
546   EXPECT_EQ(expected, actual);
547 }
548
549 TEST(Gen, OrderTake) {
550   auto expected = vector<int>{9, 8, 7};
551   auto actual =
552       from({8, 6, 7, 5, 3, 0, 9})
553     | orderByDescending(square)
554     | take(3)
555     | as<vector>();
556   EXPECT_EQ(expected, actual);
557 }
558
559 TEST(Gen, Distinct) {
560   auto expected = vector<int>{3, 1, 2};
561   auto actual =
562       from({3, 1, 3, 2, 1, 2, 3})
563     | distinct
564     | as<vector>();
565   EXPECT_EQ(expected, actual);
566 }
567
568 TEST(Gen, DistinctBy) {   //  0  1  4  9  6  5  6  9  4  1  0
569   auto expected = vector<int>{0, 1, 2, 3, 4, 5};
570   auto actual =
571       seq(0, 100)
572     | distinctBy([](int i) { return i * i % 10; })
573     | as<vector>();
574   EXPECT_EQ(expected, actual);
575 }
576
577 TEST(Gen, DistinctMove) {   //  0  1  4  9  6  5  6  9  4  1  0
578   auto expected = vector<int>{0, 1, 2, 3, 4, 5};
579   auto actual =
580       seq(0, 100)
581     | mapped([](int i) { return std::unique_ptr<int>(new int(i)); })
582       // see comment below about selector parameters for Distinct
583     | distinctBy([](const std::unique_ptr<int>& pi) { return *pi * *pi % 10; })
584     | mapped([](std::unique_ptr<int> pi) { return *pi; })
585     | as<vector>();
586
587   // NOTE(tjackson): the following line intentionally doesn't work:
588   //  | distinctBy([](std::unique_ptr<int> pi) { return *pi * *pi % 10; })
589   // This is because distinctBy because the selector intentionally requires a
590   // const reference.  If it required a move-reference, the value might get
591   // gutted by the selector before said value could be passed to downstream
592   // operators.
593   EXPECT_EQ(expected, actual);
594 }
595
596 TEST(Gen, DistinctInfinite) {
597   // distinct should be able to handle an infinite sequence, provided that, of
598   // of cource, is it eventually made finite before returning the result.
599   auto expected = seq(0) | take(5) | as<vector>(); // 0 1 2 3 4
600
601   auto actual =
602       seq(0)                              // 0 1 2 3 4 5 6 7 ...
603     | mapped([](int i) { return i / 2; }) // 0 0 1 1 2 2 3 3 ...
604     | distinct                            // 0 1 2 3 4 5 6 7 ...
605     | take(5)                             // 0 1 2 3 4
606     | as<vector>();
607
608   EXPECT_EQ(expected, actual);
609 }
610
611 TEST(Gen, DistinctByInfinite) {
612   // Similarly to the DistinctInfinite test case, distinct by should be able to
613   // handle infinite sequences. Note that depending on how many values we take()
614   // at the end, the sequence may infinite loop. This is fine becasue we cannot
615   // solve the halting problem.
616   auto expected = vector<int>{1, 2};
617   auto actual =
618       seq(1)                                    // 1 2 3 4 5 6 7 8 ...
619     | distinctBy([](int i) { return i % 2; })   // 1 2 (but might by infinite)
620     | take(2)                                   // 1 2
621     | as<vector>();
622   // Note that if we had take(3), this would infinite loop
623
624   EXPECT_EQ(expected, actual);
625 }
626
627 TEST(Gen, MinBy) {
628   EXPECT_EQ(7, seq(1, 10)
629              | minBy([](int i) -> double {
630                  double d = i - 6.8;
631                  return d * d;
632                })
633              | unwrap);
634 }
635
636 TEST(Gen, MaxBy) {
637   auto gen = from({"three", "eleven", "four"});
638
639   EXPECT_EQ("eleven", gen | maxBy(&strlen) | unwrap);
640 }
641
642 TEST(Gen, Min) {
643   auto odds = seq(2,10) | filter([](int i){ return i % 2; });
644
645   EXPECT_EQ(3, odds | min);
646 }
647
648 TEST(Gen, Max) {
649   auto odds = seq(2,10) | filter([](int i){ return i % 2; });
650
651   EXPECT_EQ(9, odds | max);
652 }
653
654 TEST(Gen, Append) {
655   string expected = "facebook";
656   string actual = "face";
657   from(StringPiece("book")) | appendTo(actual);
658   EXPECT_EQ(expected, actual);
659 }
660
661 TEST(Gen, FromRValue) {
662   {
663     // AFAICT The C++ Standard does not specify what happens to the rvalue
664     // reference of a std::vector when it is used as the 'other' for an rvalue
665     // constructor.  Use fbvector because we're sure its size will be zero in
666     // this case.
667     fbvector<int> v({1,2,3,4});
668     auto q1 = from(v);
669     EXPECT_EQ(v.size(), 4);  // ensure that the lvalue version was called!
670     auto expected = 1 * 2 * 3 * 4;
671     EXPECT_EQ(expected, q1 | product);
672
673     auto q2 = from(std::move(v));
674     EXPECT_EQ(v.size(), 0);  // ensure that rvalue version was called
675     EXPECT_EQ(expected, q2 | product);
676   }
677   {
678     auto expected = 7;
679     auto q = from([] {return vector<int>({3,7,5}); }());
680     EXPECT_EQ(expected, q | max);
681   }
682   {
683     for (auto size: {5, 1024, 16384, 1<<20}) {
684       auto q1 = from(vector<int>(size, 2));
685       auto q2 = from(vector<int>(size, 3));
686       // If the rvalue specialization is broken/gone, then the compiler will
687       // (disgustingly!) just store a *reference* to the temporary object,
688       // which is bad.  Try to catch this by allocating two temporary vectors
689       // of the same size, so that they'll probably use the same underlying
690       // buffer if q1's vector is destructed before q2's vector is constructed.
691       EXPECT_EQ(size * 2 + size * 3, (q1 | sum) + (q2 | sum));
692     }
693   }
694   {
695     auto q = from(set<int>{1,2,3,2,1});
696     EXPECT_EQ(q | sum, 6);
697   }
698 }
699
700 TEST(Gen, OrderBy) {
701   auto expected = vector<int>{5, 6, 4, 7, 3, 8, 2, 9, 1, 10};
702   auto actual =
703       seq(1, 10)
704     | orderBy([](int x) { return (5.1 - x) * (5.1 - x); })
705     | as<vector>();
706   EXPECT_EQ(expected, actual);
707
708   expected = seq(1, 10) | as<vector>();
709   actual =
710       from(expected)
711     | map([] (int x) { return 11 - x; })
712     | orderBy()
713     | as<vector>();
714   EXPECT_EQ(expected, actual);
715 }
716
717 TEST(Gen, Foldl) {
718   int expected = 2 * 3 * 4 * 5;
719   auto actual =
720       seq(2, 5)
721     | foldl(1, multiply);
722   EXPECT_EQ(expected, actual);
723 }
724
725 TEST(Gen, Reduce) {
726   int expected = 2 + 3 + 4 + 5;
727   auto actual = seq(2, 5) | reduce(add);
728   EXPECT_EQ(expected, actual | unwrap);
729 }
730
731 TEST(Gen, ReduceBad) {
732   auto gen = seq(1) | take(0);
733   auto actual = gen | reduce(add);
734   EXPECT_FALSE(actual); // Empty sequences are okay, they just yeild 'none'
735 }
736
737 TEST(Gen, Moves) {
738   std::vector<unique_ptr<int>> ptrs;
739   ptrs.emplace_back(new int(1));
740   EXPECT_NE(ptrs.front().get(), nullptr);
741   auto ptrs2 = from(ptrs) | move | as<vector>();
742   EXPECT_EQ(ptrs.front().get(), nullptr);
743   EXPECT_EQ(**ptrs2.data(), 1);
744 }
745
746 TEST(Gen, First) {
747   auto gen = seq(0) | filter([](int x) { return x > 3; });
748   EXPECT_EQ(4, gen | first | unwrap);
749 }
750
751 TEST(Gen, FromCopy) {
752   vector<int> v {3, 5};
753   auto src = from(v);
754   auto copy = fromCopy(v);
755   EXPECT_EQ(8, src | sum);
756   EXPECT_EQ(8, copy | sum);
757   v[1] = 7;
758   EXPECT_EQ(10, src | sum);
759   EXPECT_EQ(8, copy | sum);
760 }
761
762 TEST(Gen, Get) {
763   std::map<int, int> pairs {
764     {1, 1},
765     {2, 4},
766     {3, 9},
767     {4, 16},
768   };
769   auto pairSrc = from(pairs);
770   auto keys = pairSrc | get<0>();
771   auto values = pairSrc | get<1>();
772   EXPECT_EQ(10, keys | sum);
773   EXPECT_EQ(30, values | sum);
774   EXPECT_EQ(30, keys | map(square) | sum);
775   pairs[5] = 25;
776   EXPECT_EQ(15, keys | sum);
777   EXPECT_EQ(55, values | sum);
778
779   vector<tuple<int, int, int>> tuples {
780     make_tuple(1, 1, 1),
781     make_tuple(2, 4, 8),
782     make_tuple(3, 9, 27),
783   };
784   EXPECT_EQ(36, from(tuples) | get<2>() | sum);
785 }
786
787 TEST(Gen, notEmpty) {
788   EXPECT_TRUE(seq(0, 1) | notEmpty);
789   EXPECT_TRUE(just(1) | notEmpty);
790   EXPECT_FALSE(gen::range(0, 0) | notEmpty);
791   EXPECT_FALSE(from({1}) | take(0) | notEmpty);
792 }
793
794 TEST(Gen, isEmpty) {
795   EXPECT_FALSE(seq(0, 1) | isEmpty);
796   EXPECT_FALSE(just(1) | isEmpty);
797   EXPECT_TRUE(gen::range(0, 0) | isEmpty);
798   EXPECT_TRUE(from({1}) | take(0) | isEmpty);
799 }
800
801 TEST(Gen, Any) {
802   EXPECT_TRUE(seq(0, 10) | any([](int i) { return i == 7; }));
803   EXPECT_FALSE(seq(0, 10) | any([](int i) { return i == 11; }));
804 }
805
806 TEST(Gen, All) {
807   EXPECT_TRUE(seq(0, 10) | all([](int i) { return i < 11; }));
808   EXPECT_FALSE(seq(0, 10) | all([](int i) { return i < 5; }));
809   EXPECT_FALSE(seq(0) | take(9999) | all([](int i) { return i < 10; }));
810
811   // empty lists satisfies all
812   EXPECT_TRUE(seq(0) | take(0) | all([](int i) { return i < 50; }));
813   EXPECT_TRUE(seq(0) | take(0) | all([](int i) { return i > 50; }));
814 }
815
816 TEST(Gen, Yielders) {
817   auto gen = GENERATOR(int) {
818     for (int i = 1; i <= 5; ++i) {
819       yield(i);
820     }
821     yield(7);
822     for (int i = 3; ; ++i) {
823       yield(i * i);
824     }
825   };
826   vector<int> expected {
827     1, 2, 3, 4, 5, 7, 9, 16, 25
828   };
829   EXPECT_EQ(expected, gen | take(9) | as<vector>());
830 }
831
832 TEST(Gen, NestedYield) {
833   auto nums = GENERATOR(int) {
834     for (int i = 1; ; ++i) {
835       yield(i);
836     }
837   };
838   auto gen = GENERATOR(int) {
839     nums | take(10) | yield;
840     seq(1, 5) | [&](int i) {
841       yield(i);
842     };
843   };
844   EXPECT_EQ(70, gen | sum);
845 }
846
847 TEST(Gen, MapYielders) {
848   auto gen = seq(1, 5)
849            | map([](int n) {
850                return GENERATOR(int) {
851                  int i;
852                  for (i = 1; i < n; ++i)
853                    yield(i);
854                  for (; i >= 1; --i)
855                    yield(i);
856                };
857              })
858            | concat;
859   vector<int> expected {
860                 1,
861              1, 2, 1,
862           1, 2, 3, 2, 1,
863        1, 2, 3, 4, 3, 2, 1,
864     1, 2, 3, 4, 5, 4, 3, 2, 1,
865   };
866   EXPECT_EQ(expected, gen | as<vector>());
867 }
868
869 TEST(Gen, VirtualGen) {
870   VirtualGen<int> v(seq(1, 10));
871   EXPECT_EQ(55, v | sum);
872   v = v | map(square);
873   EXPECT_EQ(385, v | sum);
874   v = v | take(5);
875   EXPECT_EQ(55, v | sum);
876   EXPECT_EQ(30, v | take(4) | sum);
877 }
878
879
880 TEST(Gen, CustomType) {
881   struct Foo{
882     int y;
883   };
884   auto gen = from({Foo{2}, Foo{3}})
885            | map([](const Foo& f) { return f.y; });
886   EXPECT_EQ(5, gen | sum);
887 }
888
889 TEST(Gen, NoNeedlessCopies) {
890   auto gen = seq(1, 5)
891            | map([](int x) { return unique_ptr<int>(new int(x)); })
892            | map([](unique_ptr<int> p) { return p; })
893            | map([](unique_ptr<int>&& p) { return std::move(p); })
894            | map([](const unique_ptr<int>& p) { return *p; });
895   EXPECT_EQ(15, gen | sum);
896   EXPECT_EQ(6, gen | take(3) | sum);
897 }
898
899 namespace {
900
901 class TestIntSeq : public GenImpl<int, TestIntSeq> {
902  public:
903   TestIntSeq() { }
904
905   template <class Body>
906   bool apply(Body&& body) const {
907     for (int i = 1; i < 6; ++i) {
908       if (!body(i)) {
909         return false;
910       }
911     }
912     return true;
913   }
914
915   TestIntSeq(TestIntSeq&&) noexcept = default;
916   TestIntSeq& operator=(TestIntSeq&&) noexcept = default;
917   TestIntSeq(const TestIntSeq&) = delete;
918   TestIntSeq& operator=(const TestIntSeq&) = delete;
919 };
920
921 }  // namespace
922
923 TEST(Gen, NoGeneratorCopies) {
924   EXPECT_EQ(15, TestIntSeq() | sum);
925   auto x = TestIntSeq() | take(3);
926   EXPECT_EQ(6, std::move(x) | sum);
927 }
928
929 TEST(Gen, FromArray) {
930   int source[] = {2, 3, 5, 7};
931   auto gen = from(source);
932   EXPECT_EQ(2 * 3 * 5 * 7, gen | product);
933 }
934
935 TEST(Gen, FromStdArray) {
936   std::array<int,4> source {{2, 3, 5, 7}};
937   auto gen = from(source);
938   EXPECT_EQ(2 * 3 * 5 * 7, gen | product);
939 }
940
941 TEST(Gen, StringConcat) {
942   auto gen = seq(1, 10)
943            | eachTo<string>()
944            | rconcat;
945   EXPECT_EQ("12345678910", gen | as<string>());
946 }
947
948 struct CopyCounter {
949   static int alive;
950   int copies;
951   int moves;
952
953   CopyCounter() : copies(0), moves(0) {
954     ++alive;
955   }
956
957   CopyCounter(CopyCounter&& source) noexcept {
958     *this = std::move(source);
959     ++alive;
960   }
961
962   CopyCounter(const CopyCounter& source) {
963     *this = source;
964     ++alive;
965   }
966
967   ~CopyCounter() {
968     --alive;
969   }
970
971   CopyCounter& operator=(const CopyCounter& source) {
972     this->copies = source.copies + 1;
973     this->moves = source.moves;
974     return *this;
975   }
976
977   CopyCounter& operator=(CopyCounter&& source) {
978     this->copies = source.copies;
979     this->moves = source.moves + 1;
980     return *this;
981   }
982 };
983
984 int CopyCounter::alive = 0;
985
986 TEST(Gen, CopyCount) {
987   vector<CopyCounter> originals;
988   originals.emplace_back();
989   EXPECT_EQ(1, originals.size());
990   EXPECT_EQ(0, originals.back().copies);
991
992   vector<CopyCounter> copies = from(originals) | as<vector>();
993   EXPECT_EQ(1, copies.back().copies);
994   EXPECT_EQ(0, copies.back().moves);
995
996   vector<CopyCounter> moves = from(originals) | move | as<vector>();
997   EXPECT_EQ(0, moves.back().copies);
998   EXPECT_EQ(1, moves.back().moves);
999 }
1000
1001 // test dynamics with various layers of nested arrays.
1002 TEST(Gen, Dynamic) {
1003   dynamic array1 = dynamic::array(1, 2);
1004   EXPECT_EQ(dynamic(3), from(array1) | sum);
1005   dynamic array2 = {{1}, {1, 2}};
1006   EXPECT_EQ(dynamic(4), from(array2) | rconcat | sum);
1007   dynamic array3 = {{{1}}, {{1}, {1, 2}}};
1008   EXPECT_EQ(dynamic(5), from(array3) | rconcat | rconcat | sum);
1009 }
1010
1011 TEST(Gen, DynamicObject) {
1012   const dynamic obj = dynamic::object(1, 2)(3, 4);
1013   EXPECT_EQ(dynamic(4), from(obj.keys()) | sum);
1014   EXPECT_EQ(dynamic(6), from(obj.values()) | sum);
1015   EXPECT_EQ(dynamic(4), from(obj.items()) | get<0>() | sum);
1016   EXPECT_EQ(dynamic(6), from(obj.items()) | get<1>() | sum);
1017 }
1018
1019 TEST(Gen, Collect) {
1020   auto s = from({7, 6, 5, 4, 3}) | as<set<int>>();
1021   EXPECT_EQ(s.size(), 5);
1022 }
1023
1024
1025 TEST(Gen, Cycle) {
1026   {
1027     auto s = from({1, 2});
1028     EXPECT_EQ((vector<int> { 1, 2, 1, 2, 1 }),
1029               s | cycle | take(5) | as<vector>());
1030   }
1031   {
1032     auto s = from({1, 2});
1033     EXPECT_EQ((vector<int> { 1, 2, 1, 2 }),
1034               s | cycle(2) | as<vector>());
1035   }
1036   {
1037     auto s = from({1, 2, 3});
1038     EXPECT_EQ((vector<int> { 1, 2, 1, 2, 1 }),
1039               s | take(2) | cycle | take(5) | as<vector>());
1040   }
1041   {
1042     auto s = empty<int>();
1043     EXPECT_EQ((vector<int> { }),
1044               s | cycle | take(4) | as<vector>());
1045   }
1046   {
1047     int count = 3;
1048     int* pcount = &count;
1049     auto countdown = GENERATOR(int) {
1050       ASSERT_GE(*pcount, 0)
1051         << "Cycle should have stopped when it didnt' get values!";
1052       for (int i = 1; i <= *pcount; ++i) {
1053         yield(i);
1054       }
1055       --*pcount;
1056     };
1057     auto s = countdown;
1058     EXPECT_EQ((vector<int> { 1, 2, 3, 1, 2, 1}),
1059               s | cycle | take(7) | as<vector>());
1060     // take necessary as cycle returns an infinite generator
1061   }
1062 }
1063
1064 TEST(Gen, Dereference) {
1065   {
1066     const int x = 4, y = 2;
1067     auto s = from(std::initializer_list<const int*>({&x, nullptr, &y}));
1068     EXPECT_EQ(6, s | dereference | sum);
1069   }
1070   {
1071     vector<int> a { 1, 2 };
1072     vector<int> b { 3, 4 };
1073     vector<vector<int>*> pv { &a, nullptr, &b };
1074     from(pv)
1075       | dereference
1076       | [&](vector<int>& v) {
1077           v.push_back(5);
1078         };
1079     EXPECT_EQ(3, a.size());
1080     EXPECT_EQ(3, b.size());
1081     EXPECT_EQ(5, a.back());
1082     EXPECT_EQ(5, b.back());
1083   }
1084   {
1085     vector<std::map<int, int>> maps {
1086       {
1087         { 2, 31 },
1088         { 3, 41 },
1089       },
1090       {
1091         { 3, 52 },
1092         { 4, 62 },
1093       },
1094       {
1095         { 4, 73 },
1096         { 5, 83 },
1097       },
1098     };
1099     EXPECT_EQ(
1100       93,
1101       from(maps)
1102       | map([](std::map<int, int>& m) {
1103           return get_ptr(m, 3);
1104         })
1105       | dereference
1106       | sum);
1107   }
1108   {
1109     vector<unique_ptr<int>> ups;
1110     ups.emplace_back(new int(3));
1111     ups.emplace_back();
1112     ups.emplace_back(new int(7));
1113     EXPECT_EQ(10, from(ups) | dereference | sum);
1114     EXPECT_EQ(10, from(ups) | move | dereference | sum);
1115   }
1116 }
1117
1118 namespace {
1119 struct DereferenceWrapper {
1120   string data;
1121   string& operator*() & {
1122     return data;
1123   }
1124   string&& operator*() && {
1125     return std::move(data);
1126   }
1127   explicit operator bool() {
1128     return true;
1129   }
1130 };
1131 bool operator==(const DereferenceWrapper& a, const DereferenceWrapper& b) {
1132   return a.data == b.data;
1133 }
1134 void PrintTo(const DereferenceWrapper& a, std::ostream* o) {
1135   *o << "Wrapper{\"" << cEscape<string>(a.data) << "\"}";
1136 }
1137 }
1138
1139 TEST(Gen, DereferenceWithLValueRef) {
1140   auto original = vector<DereferenceWrapper>{{"foo"}, {"bar"}};
1141   auto copy = original;
1142   auto expected = vector<string>{"foo", "bar"};
1143   auto actual = from(original) | dereference | as<vector>();
1144   EXPECT_EQ(expected, actual);
1145   EXPECT_EQ(copy, original);
1146 }
1147
1148 TEST(Gen, DereferenceWithRValueRef) {
1149   auto original = vector<DereferenceWrapper>{{"foo"}, {"bar"}};
1150   auto empty = vector<DereferenceWrapper>{{}, {}};
1151   auto expected = vector<string>{"foo", "bar"};
1152   auto actual = from(original) | move | dereference | as<vector>();
1153   EXPECT_EQ(expected, actual);
1154   EXPECT_EQ(empty, original);
1155 }
1156
1157 TEST(Gen, Indirect) {
1158   vector<int> vs{1};
1159   EXPECT_EQ(&vs[0], from(vs) | indirect | first | unwrap);
1160 }
1161
1162 TEST(Gen, Guard) {
1163   using std::runtime_error;
1164   EXPECT_THROW(from({"1", "a", "3"})
1165                | eachTo<int>()
1166                | sum,
1167                runtime_error);
1168   EXPECT_EQ(4,
1169             from({"1", "a", "3"})
1170             | guard<runtime_error>([](runtime_error&, const char*) {
1171                 return true; // continue
1172               })
1173             | eachTo<int>()
1174             | sum);
1175   EXPECT_EQ(1,
1176             from({"1", "a", "3"})
1177             | guard<runtime_error>([](runtime_error&, const char*) {
1178                 return false; // break
1179               })
1180             | eachTo<int>()
1181             | sum);
1182   EXPECT_THROW(from({"1", "a", "3"})
1183                 | guard<runtime_error>([](runtime_error&, const char* v) {
1184                     if (v[0] == 'a') {
1185                       throw;
1186                     }
1187                     return true;
1188                   })
1189                | eachTo<int>()
1190                | sum,
1191                runtime_error);
1192 }
1193
1194 TEST(Gen, Batch) {
1195   EXPECT_EQ((vector<vector<int>> { {1} }),
1196             seq(1, 1) | batch(5) | as<vector>());
1197   EXPECT_EQ((vector<vector<int>> { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11} }),
1198             seq(1, 11) | batch(3) | as<vector>());
1199   EXPECT_THROW(seq(1, 1) | batch(0) | as<vector>(),
1200                std::invalid_argument);
1201 }
1202
1203 TEST(Gen, BatchMove) {
1204   auto expected = vector<vector<int>>{ {0, 1}, {2, 3}, {4} };
1205   auto actual =
1206       seq(0, 4)
1207     | mapped([](int i) { return std::unique_ptr<int>(new int(i)); })
1208     | batch(2)
1209     | mapped([](std::vector<std::unique_ptr<int>>& pVector) {
1210         std::vector<int> iVector;
1211         for (const auto& p : pVector) {
1212           iVector.push_back(*p);
1213         };
1214         return iVector;
1215       })
1216     | as<vector>();
1217   EXPECT_EQ(expected, actual);
1218 }
1219
1220 TEST(Gen, Just) {
1221   {
1222     int x = 3;
1223     auto j = just(x);
1224     EXPECT_EQ(&x, j | indirect | first | unwrap);
1225     x = 4;
1226     EXPECT_EQ(4, j | sum);
1227   }
1228   {
1229     int x = 3;
1230     const int& cx = x;
1231     auto j = just(cx);
1232     EXPECT_EQ(&x, j | indirect | first | unwrap);
1233     x = 5;
1234     EXPECT_EQ(5, j | sum);
1235   }
1236   {
1237     int x = 3;
1238     auto j = just(std::move(x));
1239     EXPECT_NE(&x, j | indirect | first | unwrap);
1240     x = 5;
1241     EXPECT_EQ(3, j | sum);
1242   }
1243 }
1244
1245 TEST(Gen, GroupBy) {
1246   vector<string> strs{"zero", "one", "two",   "three", "four",
1247                       "five", "six", "seven", "eight", "nine"};
1248
1249   auto gb = from(strs) | groupBy([](const string& str) { return str.size(); });
1250
1251   EXPECT_EQ(10, gb | mapOp(count) | sum);
1252   EXPECT_EQ(3, gb | count);
1253
1254   vector<string> mode{"zero", "four", "five", "nine"};
1255   EXPECT_EQ(mode,
1256             gb | maxBy([](const Group<size_t, string>& g) { return g.size(); })
1257                | unwrap
1258                | as<vector>());
1259
1260   vector<string> largest{"three", "seven", "eight"};
1261   EXPECT_EQ(largest,
1262             gb | maxBy([](const Group<size_t, string>& g) { return g.key(); })
1263                | unwrap
1264                | as<vector>());
1265 }
1266
1267 TEST(Gen, Unwrap) {
1268   Optional<int> o(4);
1269   Optional<int> e;
1270   EXPECT_EQ(4, o | unwrap);
1271   EXPECT_THROW(e | unwrap, OptionalEmptyException);
1272
1273   auto oup = folly::make_optional(folly::make_unique<int>(5));
1274   // optional has a value, and that value is non-null
1275   EXPECT_TRUE(bool(oup | unwrap));
1276   EXPECT_EQ(5, *(oup | unwrap));
1277   EXPECT_TRUE(oup.hasValue()); // still has a pointer (null or not)
1278   EXPECT_TRUE(bool(oup.value())); // that value isn't null
1279
1280   auto moved1 = std::move(oup) | unwrapOr(folly::make_unique<int>(6));
1281   // oup still has a value, but now it's now nullptr since the pointer was moved
1282   // into moved1
1283   EXPECT_TRUE(oup.hasValue());
1284   EXPECT_FALSE(oup.value());
1285   EXPECT_TRUE(bool(moved1));
1286   EXPECT_EQ(5, *moved1);
1287
1288   auto moved2 = std::move(oup) | unwrapOr(folly::make_unique<int>(7));
1289   // oup's still-valid nullptr value wins here, the pointer to 7 doesn't apply
1290   EXPECT_FALSE(moved2);
1291
1292   oup.clear();
1293   auto moved3 = std::move(oup) | unwrapOr(folly::make_unique<int>(8));
1294   // oup is empty now, so the unwrapOr comes into play.
1295   EXPECT_TRUE(bool(moved3));
1296   EXPECT_EQ(8, *moved3);
1297
1298   {
1299   // mixed types, with common type matching optional
1300     Optional<double> full(3.3);
1301     decltype(full) empty;
1302     auto fallback = unwrapOr(4);
1303     EXPECT_EQ(3.3, full | fallback);
1304     EXPECT_EQ(3.3, std::move(full) | fallback);
1305     EXPECT_EQ(3.3, full | std::move(fallback));
1306     EXPECT_EQ(3.3, std::move(full) | std::move(fallback));
1307     EXPECT_EQ(4.0, empty | fallback);
1308     EXPECT_EQ(4.0, std::move(empty) | fallback);
1309     EXPECT_EQ(4.0, empty | std::move(fallback));
1310     EXPECT_EQ(4.0, std::move(empty) | std::move(fallback));
1311   }
1312
1313   {
1314   // mixed types, with common type matching fallback
1315     Optional<int> full(3);
1316     decltype(full) empty;
1317     auto fallback = unwrapOr(5.0); // type: double
1318     // if we chose 'int' as the common type, we'd see truncation here
1319     EXPECT_EQ(1.5, (full | fallback) / 2);
1320     EXPECT_EQ(1.5, (std::move(full) | fallback) / 2);
1321     EXPECT_EQ(1.5, (full | std::move(fallback)) / 2);
1322     EXPECT_EQ(1.5, (std::move(full) | std::move(fallback)) / 2);
1323     EXPECT_EQ(2.5, (empty | fallback) / 2);
1324     EXPECT_EQ(2.5, (std::move(empty) | fallback) / 2);
1325     EXPECT_EQ(2.5, (empty | std::move(fallback)) / 2);
1326     EXPECT_EQ(2.5, (std::move(empty) | std::move(fallback)) / 2);
1327   }
1328
1329   {
1330     auto opt = folly::make_optional(std::make_shared<int>(8));
1331     auto fallback = unwrapOr(folly::make_unique<int>(9));
1332     // fallback must be std::move'd to be used
1333     EXPECT_EQ(8, *(opt | std::move(fallback)));
1334     EXPECT_TRUE(bool(opt.value())); // shared_ptr copied out, not moved
1335     EXPECT_TRUE(bool(opt)); // value still present
1336     EXPECT_TRUE(bool(fallback.value())); // fallback value not needed
1337
1338     EXPECT_EQ(8, *(std::move(opt) | std::move(fallback)));
1339     EXPECT_FALSE(opt.value()); // shared_ptr moved out
1340     EXPECT_TRUE(bool(opt)); // gutted value still present
1341     EXPECT_TRUE(bool(fallback.value())); // fallback value not needed
1342
1343     opt.clear();
1344
1345     EXPECT_FALSE(opt); // opt is empty now
1346     EXPECT_EQ(9, *(std::move(opt) | std::move(fallback)));
1347     EXPECT_FALSE(fallback.value()); // fallback moved out!
1348   }
1349
1350   {
1351     // test with nullptr
1352     vector<int> v{1, 2};
1353     EXPECT_EQ(&v[1], from(v) | indirect | max | unwrap);
1354     v.clear();
1355     EXPECT_FALSE(from(v) | indirect | max | unwrapOr(nullptr));
1356   }
1357
1358   {
1359     // mixed type determined by fallback
1360     Optional<std::nullptr_t> empty;
1361     int x = 3;
1362     EXPECT_EQ(&x, empty | unwrapOr(&x));
1363   }
1364 }
1365
1366 int main(int argc, char *argv[]) {
1367   testing::InitGoogleTest(&argc, argv);
1368   gflags::ParseCommandLineFlags(&argc, &argv, true);
1369   return RUN_ALL_TESTS();
1370 }