proxygen
OverloadTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present 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 #include <folly/Overload.h>
17 #include <boost/variant.hpp>
18 #include <folly/DiscriminatedPtr.h>
20 
21 namespace folly {
22 namespace test {
23 
24 struct One {
26  return "One";
27  }
28 };
29 struct Two {
31  return "Two";
32  }
33 };
34 using OneOrTwo = boost::variant<One, Two>;
35 
36 TEST(Overload, BoostVariant) {
37  OneOrTwo one(One{});
38  OneOrTwo two(Two{});
39 
41  one, [](const One&) { return true; }, [](const Two&) { return false; }));
43  two, [](const One&) { return false; }, [](const Two&) { return true; }));
44 
45  auto toString = [](const auto& variant) {
46  return variant_match(
47  variant, [](const auto& value) { return value.toString(); });
48  };
49  EXPECT_EQ(toString(one), "One");
50  EXPECT_EQ(toString(two), "Two");
51 }
52 
55  One one_obj;
56  Two two_obj;
57  V one_ptr(&one_obj);
58  V two_ptr(&two_obj);
59 
61  one_ptr,
62  [](const One*) { return true; },
63  [](const Two*) { return false; }));
65  two_ptr,
66  [](const One*) { return false; },
67  [](const Two*) { return true; }));
68 
69  auto toString = [](const auto& variant) {
70  return variant_match(
71  variant, [](const auto* value) { return value->toString(); });
72  };
73  EXPECT_EQ(toString(one_ptr), "One");
74  EXPECT_EQ(toString(two_ptr), "Two");
75 }
76 
77 TEST(Overload, Pattern) {
78  OneOrTwo one(One{});
79  OneOrTwo two(Two{});
80 
81  auto is_one_overload = overload(
82  [](const One&) { return true; }, [](const Two&) { return false; });
83  EXPECT_TRUE(boost::apply_visitor(is_one_overload, one));
84  EXPECT_TRUE(variant_match(one, is_one_overload));
85  EXPECT_FALSE(variant_match(two, is_one_overload));
86 
87  auto is_two_overload = overload(
88  [](const One&) { return false; }, [](const Two&) { return true; });
89  EXPECT_TRUE(boost::apply_visitor(is_two_overload, two));
90  EXPECT_FALSE(variant_match(one, is_two_overload));
91  EXPECT_TRUE(variant_match(two, is_two_overload));
92 
93  auto is_one_copy = overload(is_one_overload);
94  auto is_one_const_copy =
95  overload(static_cast<const decltype(is_one_overload)&>(is_one_overload));
96  EXPECT_TRUE(variant_match(one, is_one_copy));
97  EXPECT_TRUE(variant_match(one, is_one_const_copy));
98  EXPECT_FALSE(variant_match(two, is_one_copy));
99  EXPECT_FALSE(variant_match(two, is_one_const_copy));
100 }
101 } // namespace test
102 } // namespace folly
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::string toString() const
TEST(ProgramOptionsTest, Errors)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const char * string
Definition: Conv.cpp:212
std::string toString() const
decltype(auto) apply_visitor(Visitor &&visitor, const DiscriminatedPtr< Args... > &variant)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
decltype(auto) variant_match(Variant &&variant, Cases &&...cases)
Definition: Overload.h:74
decltype(auto) overload(Cases &&...cases)
Definition: Overload.h:62
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
boost::variant< One, Two > OneOrTwo