proxygen
DiscriminatedPtrTest.cpp File Reference

Go to the source code of this file.

Functions

 TEST (DiscriminatedPtr, Basic)
 
 TEST (DiscriminatedPtr, Apply)
 
 TEST (DiscriminatedPtr, ApplyVoid)
 

Function Documentation

TEST ( DiscriminatedPtr  ,
Basic   
)

Definition at line 23 of file DiscriminatedPtrTest.cpp.

References a, EXPECT_EQ, EXPECT_FALSE, EXPECT_THROW, EXPECT_TRUE, and folly::foo.

23  {
24  struct Foo {};
25  struct Bar {};
27 
28  int a = 10;
29  Ptr p;
30  EXPECT_TRUE(p.empty());
31  EXPECT_FALSE(p.hasType<void>());
32  EXPECT_FALSE(p.hasType<int>());
33  EXPECT_FALSE(p.hasType<Foo>());
34  EXPECT_FALSE(p.hasType<Bar>());
35 
36  p.set(&a);
37  EXPECT_FALSE(p.empty());
38  EXPECT_FALSE(p.hasType<void>());
39  EXPECT_TRUE(p.hasType<int>());
40  EXPECT_FALSE(p.hasType<Foo>());
41  EXPECT_FALSE(p.hasType<Bar>());
42 
43  EXPECT_EQ(&a, p.get_nothrow<int>());
44  EXPECT_EQ(&a, static_cast<const Ptr&>(p).get_nothrow<int>());
45  EXPECT_EQ(&a, p.get<int>());
46  EXPECT_EQ(&a, static_cast<const Ptr&>(p).get<int>());
47  EXPECT_EQ(static_cast<void*>(nullptr), p.get_nothrow<void>());
48  EXPECT_THROW({ p.get<void>(); }, std::invalid_argument);
49 
50  Foo foo;
51  p.set(&foo);
52  EXPECT_FALSE(p.empty());
53  EXPECT_FALSE(p.hasType<void>());
54  EXPECT_FALSE(p.hasType<int>());
55  EXPECT_TRUE(p.hasType<Foo>());
56  EXPECT_FALSE(p.hasType<Bar>());
57 
58  EXPECT_EQ(static_cast<int*>(nullptr), p.get_nothrow<int>());
59 
60  p.clear();
61  EXPECT_TRUE(p.empty());
62  EXPECT_FALSE(p.hasType<void>());
63  EXPECT_FALSE(p.hasType<int>());
64  EXPECT_FALSE(p.hasType<Foo>());
65  EXPECT_FALSE(p.hasType<Bar>());
66 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( DiscriminatedPtr  ,
Apply   
)

Definition at line 68 of file DiscriminatedPtrTest.cpp.

References a, folly::apply(), folly::apply_visitor(), EXPECT_EQ, EXPECT_THROW, folly::foo, folly::gen::move, and string.

68  {
69  struct Foo {};
70  struct Visitor {
71  std::string operator()(int* /* ptr */) {
72  return "int";
73  }
74  std::string operator()(const int* /* ptr */) {
75  return "const int";
76  }
77  std::string operator()(Foo* /* ptr */) {
78  return "Foo";
79  }
80  std::string operator()(const Foo* /* ptr */) {
81  return "const Foo";
82  }
83  };
84 
85  typedef DiscriminatedPtr<int, Foo> Ptr;
86  Ptr p;
87 
88  int a = 0;
89  p.set(&a);
90  EXPECT_EQ("int", p.apply(Visitor()));
91  EXPECT_EQ("const int", static_cast<const Ptr&>(p).apply(Visitor()));
92 
93  Foo foo;
94  p.set(&foo);
95  EXPECT_EQ("Foo", p.apply(Visitor()));
96  EXPECT_EQ("const Foo", static_cast<const Ptr&>(p).apply(Visitor()));
97  EXPECT_EQ("Foo", apply_visitor(Visitor(), p));
98  EXPECT_EQ("const Foo", apply_visitor(Visitor(), static_cast<const Ptr&>(p)));
99  EXPECT_EQ("Foo", apply_visitor(Visitor(), std::move(p)));
100 
101  p.clear();
102  EXPECT_THROW({ p.apply(Visitor()); }, std::invalid_argument);
103 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
char a
const char * string
Definition: Conv.cpp:212
decltype(auto) apply_visitor(Visitor &&visitor, const DiscriminatedPtr< Args... > &variant)
decltype(auto) constexpr apply(F &&func, Tuple &&tuple)
Definition: ApplyTuple.h:87
TEST ( DiscriminatedPtr  ,
ApplyVoid   
)

Definition at line 105 of file DiscriminatedPtrTest.cpp.

References a, folly::apply(), EXPECT_EQ, EXPECT_THROW, folly::foo, string, and v.

105  {
106  struct Foo {};
107  struct Visitor {
108  void operator()(int* /* ptr */) {
109  result = "int";
110  }
111  void operator()(const int* /* ptr */) {
112  result = "const int";
113  }
114  void operator()(Foo* /* ptr */) {
115  result = "Foo";
116  }
117  void operator()(const Foo* /* ptr */) {
118  result = "const Foo";
119  }
120 
121  std::string result;
122  };
123 
124  typedef DiscriminatedPtr<int, Foo> Ptr;
125  Ptr p;
126  Visitor v;
127 
128  int a = 0;
129  p.set(&a);
130  p.apply(v);
131  EXPECT_EQ("int", v.result);
132  static_cast<const Ptr&>(p).apply(v);
133  EXPECT_EQ("const int", v.result);
134 
135  Foo foo;
136  p.set(&foo);
137  p.apply(v);
138  EXPECT_EQ("Foo", v.result);
139  static_cast<const Ptr&>(p).apply(v);
140  EXPECT_EQ("const Foo", v.result);
141 
142  p.clear();
143  EXPECT_THROW({ p.apply(v); }, std::invalid_argument);
144 }
auto v
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
const char * string
Definition: Conv.cpp:212
decltype(auto) constexpr apply(F &&func, Tuple &&tuple)
Definition: ApplyTuple.h:87