proxygen
TryTest.cpp File Reference
#include <folly/Try.h>
#include <glog/logging.h>
#include <folly/Memory.h>
#include <folly/Traits.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

 TEST (Try, basic)
 
 TEST (Try, in_place)
 
 TEST (Try, in_place_nested)
 
 TEST (Try, assignmentWithThrowingCopyConstructor)
 
 TEST (Try, assignmentWithThrowingMoveConstructor)
 
 TEST (Try, emplace)
 
 TEST (Try, emplaceWithThrowingConstructor)
 
 TEST (Try, tryEmplace)
 
 TEST (Try, tryEmplaceWithThrowingConstructor)
 
 TEST (Try, emplaceVoidTry)
 
 TEST (Try, tryEmplaceVoidTry)
 
 TEST (Try, tryEmplaceWith)
 
 TEST (Try, tryEmplaceWithFunctionThrows)
 
 TEST (Try, tryEmplaceWithConstructorThrows)
 
 TEST (Try, tryEmplaceWithVoidTry)
 
 TEST (Try, nothrow)
 
 TEST (Try, MoveDereference)
 
 TEST (Try, MoveConstRvalue)
 
 TEST (Try, ValueOverloads)
 
 TEST (Try, copy)
 
 TEST (Try, moveOnly)
 
 TEST (Try, makeTryWith)
 
 TEST (Try, makeTryWithThrow)
 
 TEST (Try, makeTryWithVoid)
 
 TEST (Try, makeTryWithVoidThrow)
 
 TEST (Try, exception)
 
template<typename E >
static Eget_exception (std::exception_ptr eptr)
 
 TEST (Try, tryGetExceptionObject)
 
 TEST (Try, withException)
 
 TEST (Try, TestUnwrapTuple)
 
 TEST (Try, TestUnwrapPair)
 
 TEST (Try, TestUnwrapForward)
 

Function Documentation

template<typename E >
static E* get_exception ( std::exception_ptr  eptr)
static

Definition at line 541 of file TryTest.cpp.

Referenced by folly::exception_wrapper::exception_wrapper().

541  {
542  try {
544  } catch (E& e) {
545  return &e;
546  } catch (...) {
547  return nullptr;
548  }
549 }
void rethrow_exception(std::exception_ptr ep)
TEST ( Try  ,
basic   
)

Definition at line 64 of file TryTest.cpp.

References a, EXPECT_EQ, folly::gen::move, and folly::Try< T >::value().

64  {
65  A a(5);
66  Try<A> t_a(std::move(a));
67 
68  Try<Unit> t_void;
69 
70  EXPECT_EQ(5, t_a.value().x());
71 }
std::unique_ptr< int > A
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
char a
Definition: Try.h:51
TEST ( Try  ,
in_place   
)

Definition at line 73 of file TryTest.cpp.

References EXPECT_EQ, folly::in_place(), and folly::Try< T >::value().

73  {
74  Try<A> t_a(in_place, 5);
75 
76  EXPECT_EQ(5, t_a.value().x());
77 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
in_place_tag in_place(in_place_tag={})
Definition: Utility.h:235
Definition: Try.h:51
TEST ( Try  ,
in_place_nested   
)

Definition at line 79 of file TryTest.cpp.

References EXPECT_EQ, folly::in_place(), and folly::Try< T >::value().

79  {
80  Try<Try<A>> t_t_a(in_place, in_place, 5);
81 
82  EXPECT_EQ(5, t_t_a.value().value().x());
83 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
in_place_tag in_place(in_place_tag={})
Definition: Utility.h:235
Definition: Try.h:51
TEST ( Try  ,
assignmentWithThrowingCopyConstructor   
)

Definition at line 85 of file TryTest.cpp.

References counter, counter_, EXPECT_EQ, EXPECT_FALSE, EXPECT_THROW, EXPECT_TRUE, folly::Try< T >::hasValue(), folly::in_place(), and folly::pushmi::__adl::noexcept().

85  {
86  struct MyException : std::exception {};
87  struct ThrowingCopyConstructor {
88  int& counter_;
89  explicit ThrowingCopyConstructor(int& counter) : counter_(counter) {
90  ++counter_;
91  }
92 
93  [[noreturn]] ThrowingCopyConstructor(
94  const ThrowingCopyConstructor& other) noexcept(false)
95  : counter_(other.counter_) {
96  throw MyException{};
97  }
98 
99  ThrowingCopyConstructor& operator=(const ThrowingCopyConstructor&) = delete;
100 
101  ~ThrowingCopyConstructor() {
102  --counter_;
103  }
104  };
105 
106  int counter = 0;
107 
108  {
111  EXPECT_EQ(2, counter);
112  EXPECT_THROW(t2 = t1, MyException);
113  EXPECT_EQ(1, counter);
114  EXPECT_FALSE(t2.hasValue());
115  EXPECT_TRUE(t1.hasValue());
116  }
117  EXPECT_EQ(0, counter);
118  {
121  EXPECT_EQ(1, counter);
122  EXPECT_THROW(t2 = t1, MyException);
123  EXPECT_EQ(1, counter);
124  EXPECT_FALSE(t2.hasValue());
125  EXPECT_TRUE(t1.hasValue());
126  }
127  EXPECT_EQ(0, counter);
128 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
in_place_tag in_place(in_place_tag={})
Definition: Utility.h:235
requires E e noexcept(noexcept(s.error(std::move(e))))
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool hasValue() const
Definition: Try.h:242
std::atomic< int > counter
folly::detail::CompressionCounter * counter_
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Try  ,
assignmentWithThrowingMoveConstructor   
)

Definition at line 130 of file TryTest.cpp.

References counter, counter_, EXPECT_EQ, EXPECT_FALSE, EXPECT_THROW, EXPECT_TRUE, folly::Try< T >::hasValue(), folly::in_place(), folly::gen::move, and folly::pushmi::__adl::noexcept().

130  {
131  struct MyException : std::exception {};
132  struct ThrowingMoveConstructor {
133  int& counter_;
134  explicit ThrowingMoveConstructor(int& counter) : counter_(counter) {
135  ++counter_;
136  }
137 
138  [[noreturn]] ThrowingMoveConstructor(
139  ThrowingMoveConstructor&& other) noexcept(false)
140  : counter_(other.counter_) {
141  throw MyException{};
142  }
143 
144  ThrowingMoveConstructor& operator=(ThrowingMoveConstructor&&) = delete;
145 
146  ~ThrowingMoveConstructor() {
147  --counter_;
148  }
149  };
150 
151  int counter = 0;
152 
153  {
156  EXPECT_EQ(2, counter);
158  EXPECT_EQ(1, counter);
159  EXPECT_FALSE(t2.hasValue());
160  EXPECT_TRUE(t1.hasValue());
161  }
162  EXPECT_EQ(0, counter);
163  {
166  EXPECT_EQ(1, counter);
168  EXPECT_EQ(1, counter);
169  EXPECT_FALSE(t2.hasValue());
170  EXPECT_TRUE(t1.hasValue());
171  }
172  EXPECT_EQ(0, counter);
173 }
#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
in_place_tag in_place(in_place_tag={})
Definition: Utility.h:235
requires E e noexcept(noexcept(s.error(std::move(e))))
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool hasValue() const
Definition: Try.h:242
std::atomic< int > counter
folly::detail::CompressionCounter * counter_
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Try  ,
emplace   
)

Definition at line 175 of file TryTest.cpp.

References folly::Try< T >::emplace(), EXPECT_EQ, EXPECT_TRUE, folly::Try< T >::hasValue(), and folly::pushmi::detail::t.

175  {
176  Try<A> t;
177  A& t_a = t.emplace(10);
178  EXPECT_TRUE(t.hasValue());
179  EXPECT_EQ(t_a.x(), 10);
180 }
std::unique_ptr< int > A
T & emplace(Args &&...args) noexcept(std::is_nothrow_constructible< T, Args &&... >::value)
Definition: Try-inl.h:121
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool hasValue() const
Definition: Try.h:242
TEST ( Try  ,
emplaceWithThrowingConstructor   
)

Definition at line 182 of file TryTest.cpp.

References folly::Try< T >::emplace(), EXPECT_FALSE, EXPECT_THROW, EXPECT_TRUE, folly::Try< T >::hasException(), folly::Try< T >::hasValue(), folly::in_place(), and folly::pushmi::detail::t.

182  {
183  struct MyException : std::exception {};
184  struct ThrowingConstructor {
185  explicit ThrowingConstructor(bool shouldThrow) {
186  if (shouldThrow) {
187  throw MyException{};
188  }
189  }
190  };
191 
192  {
193  // Try constructing from empty state to new value and constructor throws.
195  EXPECT_FALSE(t.hasValue());
197  EXPECT_THROW(t.emplace(true), MyException);
198 
199  EXPECT_FALSE(t.hasValue());
201  }
202 
203  {
204  // Initialise to value, then re-emplace with throwing constructor.
205  // This should reset the object back to empty.
207  EXPECT_TRUE(t.hasValue());
208  EXPECT_THROW(t.emplace(true), MyException);
209  EXPECT_FALSE(t.hasValue());
211  }
212 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
T & emplace(Args &&...args) noexcept(std::is_nothrow_constructible< T, Args &&... >::value)
Definition: Try-inl.h:121
in_place_tag in_place(in_place_tag={})
Definition: Utility.h:235
bool hasException() const
Definition: Try.h:248
MyException(char const *const what)
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool hasValue() const
Definition: Try.h:242
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Try  ,
tryEmplace   
)

Definition at line 214 of file TryTest.cpp.

References a, EXPECT_EQ, EXPECT_TRUE, folly::Try< T >::hasValue(), folly::pushmi::detail::t, folly::tryEmplace(), and folly::Try< T >::value().

214  {
215  Try<A> t;
216  A* a = tryEmplace(t, 10);
217  EXPECT_EQ(&t.value(), a);
218  EXPECT_TRUE(t.hasValue());
219  EXPECT_EQ(10, t.value().x());
220 }
T * tryEmplace(Try< T > &t, Args &&...args) noexcept
Definition: Try-inl.h:249
std::unique_ptr< int > A
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool hasValue() const
Definition: Try.h:242
T & value()&
Definition: Try-inl.h:140
TEST ( Try  ,
tryEmplaceWithThrowingConstructor   
)

Definition at line 222 of file TryTest.cpp.

References EXPECT_EQ, EXPECT_NE, EXPECT_TRUE, folly::Try< T >::hasException(), folly::Try< T >::hasValue(), folly::pushmi::__adl::noexcept(), folly::pushmi::detail::t, folly::tryEmplace(), and folly::Try< T >::tryGetExceptionObject().

222  {
223  struct MyException : std::exception {};
224  struct NonInheritingException {};
225  struct ThrowingConstructor {
226  [[noreturn]] ThrowingConstructor() noexcept(false) {
227  throw NonInheritingException{}; // @nolint
228  }
229 
230  explicit ThrowingConstructor(bool shouldThrow) {
231  if (shouldThrow) {
232  throw MyException{};
233  }
234  }
235  };
236 
237  {
239  EXPECT_EQ(nullptr, tryEmplace(t, true));
242  }
243 
244  {
246  EXPECT_EQ(nullptr, tryEmplace(t));
248  EXPECT_NE(t.tryGetExceptionObject<NonInheritingException>(), nullptr);
249  }
250 
251  {
253  EXPECT_NE(nullptr, tryEmplace(t, false));
254  EXPECT_TRUE(t.hasValue());
255  EXPECT_EQ(nullptr, tryEmplace(t, true));
258  }
259 }
T * tryEmplace(Try< T > &t, Args &&...args) noexcept
Definition: Try-inl.h:249
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
requires E e noexcept(noexcept(s.error(std::move(e))))
bool hasException() const
Definition: Try.h:248
std::exception * tryGetExceptionObject()
Definition: Try.h:292
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool hasValue() const
Definition: Try.h:242
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( Try  ,
emplaceVoidTry   
)

Definition at line 261 of file TryTest.cpp.

References folly::Try< void >::emplace(), folly::Try< void >::emplaceException(), EXPECT_FALSE, EXPECT_TRUE, folly::Try< void >::hasException(), folly::Try< void >::hasValue(), and folly::pushmi::detail::t.

261  {
262  struct MyException : std::exception {};
263  Try<void> t;
264  t.emplace();
265  EXPECT_TRUE(t.hasValue());
266  t.emplaceException(folly::in_place_type<MyException>);
267  EXPECT_FALSE(t.hasValue());
270  t.emplace();
271  EXPECT_TRUE(t.hasValue());
273 }
bool hasException() const
Definition: Try.h:463
void emplace() noexcept
Definition: Try.h:423
exception_wrapper & emplaceException(Args &&...args) noexcept(std::is_nothrow_constructible< exception_wrapper, Args &&... >::value)
Definition: Try-inl.h:203
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
bool hasValue() const
Definition: Try.h:459
TEST ( Try  ,
tryEmplaceVoidTry   
)

Definition at line 275 of file TryTest.cpp.

References folly::Try< void >::emplace(), folly::Try< void >::emplaceException(), EXPECT_FALSE, EXPECT_TRUE, folly::Try< void >::hasException(), folly::Try< void >::hasValue(), folly::pushmi::detail::t, and folly::tryEmplace().

275  {
276  struct MyException : std::exception {};
277  Try<void> t;
278  tryEmplace(t);
279  EXPECT_TRUE(t.hasValue());
280  t.emplaceException(folly::in_place_type<MyException>);
281  EXPECT_FALSE(t.hasValue());
284  t.emplace();
285  EXPECT_TRUE(t.hasValue());
287 }
T * tryEmplace(Try< T > &t, Args &&...args) noexcept
Definition: Try-inl.h:249
bool hasException() const
Definition: Try.h:463
void emplace() noexcept
Definition: Try.h:423
exception_wrapper & emplaceException(Args &&...args) noexcept(std::is_nothrow_constructible< exception_wrapper, Args &&... >::value)
Definition: Try-inl.h:203
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
bool hasValue() const
Definition: Try.h:459
TEST ( Try  ,
tryEmplaceWith   
)

Definition at line 289 of file TryTest.cpp.

References EXPECT_EQ, folly::pushmi::detail::t, folly::tryEmplaceWith(), and folly::Try< T >::value().

289  {
291  tryEmplaceWith(t, [] { return "hello"; });
292  EXPECT_EQ("hello", t.value());
293 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
T * tryEmplaceWith(Try< T > &t, Func &&func) noexcept
Definition: Try-inl.h:266
Definition: Try.h:51
T & value()&
Definition: Try-inl.h:140
TEST ( Try  ,
tryEmplaceWithFunctionThrows   
)

Definition at line 295 of file TryTest.cpp.

References EXPECT_TRUE, folly::Try< T >::hasException(), folly::pushmi::detail::t, and folly::tryEmplaceWith().

295  {
296  struct MyException : std::exception {};
297  Try<int> t;
298  tryEmplaceWith(t, []() -> int { throw MyException{}; });
301 }
T * tryEmplaceWith(Try< T > &t, Func &&func) noexcept
Definition: Try-inl.h:266
bool hasException() const
Definition: Try.h:248
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( Try  ,
tryEmplaceWithConstructorThrows   
)

Definition at line 303 of file TryTest.cpp.

References EXPECT_TRUE, folly::Try< T >::hasException(), folly::Try< T >::hasValue(), folly::pushmi::__adl::noexcept(), folly::pushmi::detail::t, and folly::tryEmplaceWith().

303  {
304  struct MyException : std::exception {};
305  struct ThrowingConstructor {
306  int value_;
307  explicit ThrowingConstructor(bool shouldThrow) noexcept(false) : value_(0) {
308  if (shouldThrow) {
309  throw MyException{};
310  }
311  }
312  };
313 
315  tryEmplaceWith(t, [] { return false; });
316  EXPECT_TRUE(t.hasValue());
317  tryEmplaceWith(t, [] { return true; });
320 }
requires E e noexcept(noexcept(s.error(std::move(e))))
T * tryEmplaceWith(Try< T > &t, Func &&func) noexcept
Definition: Try-inl.h:266
bool hasException() const
Definition: Try.h:248
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool hasValue() const
Definition: Try.h:242
TEST ( Try  ,
tryEmplaceWithVoidTry   
)

Definition at line 322 of file TryTest.cpp.

References EXPECT_TRUE, folly::Try< void >::hasException(), folly::Try< void >::hasValue(), folly::pushmi::detail::t, and folly::tryEmplaceWith().

322  {
323  Try<void> t;
324  bool hasRun = false;
325  tryEmplaceWith(t, [&] { hasRun = true; });
326  EXPECT_TRUE(t.hasValue());
327  EXPECT_TRUE(hasRun);
328 
329  struct MyException : std::exception {};
330  tryEmplaceWith(t, [&] { throw MyException{}; });
333 }
bool hasException() const
Definition: Try.h:463
T * tryEmplaceWith(Try< T > &t, Func &&func) noexcept
Definition: Try-inl.h:266
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool hasValue() const
Definition: Try.h:459
TEST ( Try  ,
nothrow   
)

Definition at line 335 of file TryTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, folly::T, and folly::value().

335  {
336  using F = HasCtors<false>;
337  using T = HasCtors<true>;
338 
339  // default ctor
340  EXPECT_TRUE(std::is_nothrow_default_constructible<Try<F>>::value);
341  EXPECT_TRUE(std::is_nothrow_default_constructible<Try<T>>::value);
342  EXPECT_TRUE(std::is_nothrow_default_constructible<Try<void>>::value);
343 
344  // inner ctor - no void
345  EXPECT_FALSE((std::is_nothrow_constructible<Try<F>, F&&>::value));
346  EXPECT_TRUE((std::is_nothrow_constructible<Try<T>, T&&>::value));
347  EXPECT_FALSE((std::is_nothrow_constructible<Try<F>, F const&>::value));
348  EXPECT_TRUE((std::is_nothrow_constructible<Try<T>, T const&>::value));
349 
350  // emplacing ctor - no void
351  EXPECT_FALSE((std::is_nothrow_constructible<Try<F>, in_place_t, int>::value));
352  EXPECT_TRUE((std::is_nothrow_constructible<Try<T>, in_place_t, int>::value));
353 
354  // copy/move ctor/assign
355  EXPECT_TRUE(std::is_nothrow_constructible<Try<void>>::value);
356  EXPECT_FALSE(std::is_nothrow_move_constructible<Try<F>>::value);
357  EXPECT_TRUE(std::is_nothrow_move_constructible<Try<T>>::value);
358  EXPECT_TRUE(std::is_nothrow_move_constructible<Try<void>>::value);
359  EXPECT_FALSE(std::is_nothrow_move_assignable<Try<F>>::value);
360  EXPECT_TRUE(std::is_nothrow_move_assignable<Try<T>>::value);
361  EXPECT_TRUE(std::is_nothrow_move_assignable<Try<void>>::value);
362  EXPECT_FALSE(std::is_nothrow_copy_constructible<Try<F>>::value);
363  EXPECT_TRUE(std::is_nothrow_copy_constructible<Try<T>>::value);
364  EXPECT_TRUE(std::is_nothrow_copy_constructible<Try<void>>::value);
365  EXPECT_FALSE(std::is_nothrow_copy_assignable<Try<F>>::value);
366  EXPECT_TRUE(std::is_nothrow_copy_assignable<Try<T>>::value);
367  EXPECT_TRUE(std::is_nothrow_copy_assignable<Try<void>>::value);
368 
369  // conversion ctor - void to unit
370  EXPECT_TRUE((std::is_nothrow_constructible<Try<Unit>, Try<void>&&>::value));
371  EXPECT_TRUE(
372  (std::is_nothrow_constructible<Try<Unit>, Try<void> const&>::value));
373 }
#define T(v)
Definition: http_parser.c:233
in_place_tag(&)(in_place_tag) in_place_t
Definition: Utility.h:229
static const char *const value
Definition: Conv.cpp:50
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Try  ,
MoveDereference   
)

Definition at line 375 of file TryTest.cpp.

References EXPECT_EQ, folly::gen::move, ptr, and folly::pushmi::detail::t.

375  {
376  auto ptr = std::make_unique<int>(1);
378  auto result = *std::move(t);
379  EXPECT_EQ(*result, 1);
380 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Definition: Try.h:51
TEST ( Try  ,
MoveConstRvalue   
)

Definition at line 382 of file TryTest.cpp.

References folly::in_place(), folly::gen::move, folly::pushmi::detail::t, val, and folly::value().

382  {
383  // tests to see if Try returns a const Rvalue, this is required in the case
384  // where for example MutableContainer has a mutable memebr that is move only
385  // and you want to fetch the value from the Try and move it into a member
386  {
388  auto val = MoveConstructOnly(std::move(t).value().val);
389  static_cast<void>(val);
390  }
391  {
393  auto val = (*(std::move(t))).val;
394  static_cast<void>(val);
395  }
396 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
double val
Definition: String.cpp:273
in_place_tag in_place(in_place_tag={})
Definition: Utility.h:235
static const char *const value
Definition: Conv.cpp:50
Definition: Try.h:51
TEST ( Try  ,
ValueOverloads   
)

Definition at line 398 of file TryTest.cpp.

References folly::as_const(), CR, EXPECT_EQ, EXPECT_THROW, EXPECT_TRUE, folly::gen::move, value, and folly::value().

398  {
399  using ML = int&;
400  using MR = int&&;
401  using CL = const int&;
402  using CR = const int&&;
403 
404  {
405  auto obj = Try<int>{};
406  using ActualML = decltype(obj.value());
407  using ActualMR = decltype(std::move(obj).value());
408  using ActualCL = decltype(as_const(obj).value());
409  using ActualCR = decltype(std::move(as_const(obj)).value());
414  }
415 
416  {
417  auto obj = Try<int>{3};
418  EXPECT_EQ(obj.value(), 3);
419  EXPECT_EQ(std::move(obj).value(), 3);
420  EXPECT_EQ(as_const(obj).value(), 3);
421  EXPECT_EQ(std::move(as_const(obj)).value(), 3);
422  }
423 
424  {
425  auto obj = Try<int>{make_exception_wrapper<std::range_error>("oops")};
426  EXPECT_THROW(obj.value(), std::range_error);
427  EXPECT_THROW(std::move(obj.value()), std::range_error);
428  EXPECT_THROW(as_const(obj.value()), std::range_error);
429  EXPECT_THROW(std::move(as_const(obj.value())), std::range_error);
430  }
431 }
#define CR
Definition: http_parser.c:395
#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
static const char *const value
Definition: Conv.cpp:50
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::detail::as_const_fn as_const
TEST ( Try  ,
copy   
)

Definition at line 434 of file TryTest.cpp.

References folly::pushmi::detail::t.

434  {
435  Try<int> t;
436  auto t2 = t;
437 }
Definition: Try.h:51
TEST ( Try  ,
moveOnly   
)

Definition at line 440 of file TryTest.cpp.

References folly::pushmi::detail::t, and v.

440  {
442  std::vector<Try<std::unique_ptr<int>>> v;
443  v.reserve(10);
444 }
auto v
Definition: Try.h:51
TEST ( Try  ,
makeTryWith   
)

Definition at line 446 of file TryTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, and folly::makeTryWith().

446  {
447  auto func = []() { return std::make_unique<int>(1); };
448 
449  auto result = makeTryWith(func);
450  EXPECT_TRUE(result.hasValue());
451  EXPECT_EQ(*result.value(), 1);
452 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< !std::is_same< invoke_result_t< F >, void >::value, Try< invoke_result_t< F > > >::type makeTryWith(F &&f)
Definition: Try-inl.h:223
TEST ( Try  ,
makeTryWithThrow   
)

Definition at line 454 of file TryTest.cpp.

References EXPECT_TRUE, and folly::makeTryWith().

454  {
455  auto func = []() -> std::unique_ptr<int> {
456  throw std::runtime_error("Runtime");
457  };
458 
459  auto result = makeTryWith(func);
460  EXPECT_TRUE(result.hasException<std::runtime_error>());
461 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< !std::is_same< invoke_result_t< F >, void >::value, Try< invoke_result_t< F > > >::type makeTryWith(F &&f)
Definition: Try-inl.h:223
TEST ( Try  ,
makeTryWithVoid   
)

Definition at line 463 of file TryTest.cpp.

References EXPECT_TRUE, and folly::makeTryWith().

463  {
464  auto func = []() { return; };
465 
466  auto result = makeTryWith(func);
467  EXPECT_TRUE(result.hasValue());
468 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< !std::is_same< invoke_result_t< F >, void >::value, Try< invoke_result_t< F > > >::type makeTryWith(F &&f)
Definition: Try-inl.h:223
TEST ( Try  ,
makeTryWithVoidThrow   
)

Definition at line 470 of file TryTest.cpp.

References EXPECT_TRUE, and folly::makeTryWith().

470  {
471  auto func = []() { throw std::runtime_error("Runtime"); };
472 
473  auto result = makeTryWith(func);
474  EXPECT_TRUE(result.hasException<std::runtime_error>());
475 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< !std::is_same< invoke_result_t< F >, void >::value, Try< invoke_result_t< F > > >::type makeTryWith(F &&f)
Definition: Try-inl.h:223
TEST ( Try  ,
exception   
)

Definition at line 477 of file TryTest.cpp.

References folly::as_const(), CR, EXPECT_EQ, EXPECT_THROW, EXPECT_TRUE, folly::gen::move, and value.

477  {
478  using ML = exception_wrapper&;
479  using MR = exception_wrapper&&;
480  using CL = exception_wrapper const&;
481  using CR = exception_wrapper const&&;
482 
483  {
484  auto obj = Try<int>();
485  using ActualML = decltype(obj.exception());
486  using ActualMR = decltype(std::move(obj).exception());
487  using ActualCL = decltype(as_const(obj).exception());
488  using ActualCR = decltype(std::move(as_const(obj)).exception());
493  }
494 
495  {
496  auto obj = Try<int>(3);
497  EXPECT_THROW(obj.exception(), TryException);
498  EXPECT_THROW(std::move(obj).exception(), TryException);
499  EXPECT_THROW(as_const(obj).exception(), TryException);
500  EXPECT_THROW(std::move(as_const(obj)).exception(), TryException);
501  }
502 
503  {
504  auto obj = Try<int>(make_exception_wrapper<int>(-3));
505  EXPECT_EQ(-3, *obj.exception().get_exception<int>());
506  EXPECT_EQ(-3, *std::move(obj).exception().get_exception<int>());
507  EXPECT_EQ(-3, *as_const(obj).exception().get_exception<int>());
508  EXPECT_EQ(-3, *std::move(as_const(obj)).exception().get_exception<int>());
509  }
510 
511  {
512  auto obj = Try<void>();
513  using ActualML = decltype(obj.exception());
514  using ActualMR = decltype(std::move(obj).exception());
515  using ActualCL = decltype(as_const(obj).exception());
516  using ActualCR = decltype(std::move(as_const(obj)).exception());
521  }
522 
523  {
524  auto obj = Try<void>();
525  EXPECT_THROW(obj.exception(), TryException);
526  EXPECT_THROW(std::move(obj).exception(), TryException);
527  EXPECT_THROW(as_const(obj).exception(), TryException);
528  EXPECT_THROW(std::move(as_const(obj)).exception(), TryException);
529  }
530 
531  {
532  auto obj = Try<void>(make_exception_wrapper<int>(-3));
533  EXPECT_EQ(-3, *obj.exception().get_exception<int>());
534  EXPECT_EQ(-3, *std::move(obj).exception().get_exception<int>());
535  EXPECT_EQ(-3, *as_const(obj).exception().get_exception<int>());
536  EXPECT_EQ(-3, *std::move(as_const(obj)).exception().get_exception<int>());
537  }
538 }
#define CR
Definition: http_parser.c:395
#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
static const char *const value
Definition: Conv.cpp:50
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::detail::as_const_fn as_const
TEST ( Try  ,
tryGetExceptionObject   
)

Definition at line 551 of file TryTest.cpp.

References EXPECT_EQ, and folly::pushmi::detail::t.

551  {
552  auto epexn = std::make_exception_ptr(std::range_error("oops"));
553  auto epnum = std::make_exception_ptr(17);
554 
555  auto exn = CHECK_NOTNULL(get_exception<std::range_error>(epexn));
556  auto num = CHECK_NOTNULL(get_exception<int>(epnum));
557 
558  {
559  auto t = Try<bool>(true);
560  EXPECT_EQ(nullptr, t.tryGetExceptionObject());
561  EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
562  EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
563  }
564 
565  {
566  auto t = Try<bool>(exception_wrapper(epexn, *exn));
567  EXPECT_EQ(exn, t.tryGetExceptionObject());
568  EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
569  EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
570  }
571 
572  {
573  auto t = Try<bool>(exception_wrapper(epnum, *num));
574  EXPECT_EQ(nullptr, t.tryGetExceptionObject());
575  EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
576  EXPECT_EQ(num, t.tryGetExceptionObject<int>());
577  }
578 
579  {
580  auto t = Try<void>();
581  EXPECT_EQ(nullptr, t.tryGetExceptionObject());
582  EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
583  EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
584  }
585 
586  {
587  auto t = Try<void>(exception_wrapper(epexn, *exn));
588  EXPECT_EQ(exn, t.tryGetExceptionObject());
589  EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
590  EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
591  }
592 
593  {
594  auto t = Try<void>(exception_wrapper(epnum, *num));
595  EXPECT_EQ(nullptr, t.tryGetExceptionObject());
596  EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
597  EXPECT_EQ(num, t.tryGetExceptionObject<int>());
598  }
599 
600  {
601  auto const t = Try<bool>(true);
602  EXPECT_EQ(nullptr, t.tryGetExceptionObject());
603  EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
604  EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
605  }
606 
607  {
608  auto const t = Try<bool>(exception_wrapper(epexn, *exn));
609  EXPECT_EQ(exn, t.tryGetExceptionObject());
610  EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
611  EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
612  }
613 
614  {
615  auto const t = Try<bool>(exception_wrapper(epnum, *num));
616  EXPECT_EQ(nullptr, t.tryGetExceptionObject());
617  EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
618  EXPECT_EQ(num, t.tryGetExceptionObject<int>());
619  }
620 
621  {
622  auto const t = Try<void>();
623  EXPECT_EQ(nullptr, t.tryGetExceptionObject());
624  EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
625  EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
626  }
627 
628  {
629  auto const t = Try<void>(exception_wrapper(epexn, *exn));
630  EXPECT_EQ(exn, t.tryGetExceptionObject());
631  EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
632  EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
633  }
634 
635  {
636  auto const t = Try<void>(exception_wrapper(epnum, *num));
637  EXPECT_EQ(nullptr, t.tryGetExceptionObject());
638  EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
639  EXPECT_EQ(num, t.tryGetExceptionObject<int>());
640  }
641 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Definition: Try.h:51
TEST ( Try  ,
withException   
)

Definition at line 643 of file TryTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, and folly::pushmi::detail::t.

643  {
644  auto ew = make_exception_wrapper<std::range_error>("oops");
645 
646  {
647  auto t = Try<bool>(true);
648  EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
649  EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
650  EXPECT_FALSE(t.withException([](std::runtime_error&) {}));
651  EXPECT_FALSE(t.withException([](std::logic_error&) {}));
652  }
653 
654  {
655  auto t = Try<bool>(ew);
656  EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
657  EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
658  EXPECT_TRUE(t.withException([](std::runtime_error&) {}));
659  EXPECT_FALSE(t.withException([](std::logic_error&) {}));
660  }
661 
662  {
663  auto t = Try<void>();
664  EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
665  EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
666  EXPECT_FALSE(t.withException([](std::runtime_error&) {}));
667  EXPECT_FALSE(t.withException([](std::logic_error&) {}));
668  }
669 
670  {
671  auto t = Try<void>(ew);
672  EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
673  EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
674  EXPECT_TRUE(t.withException([](std::runtime_error&) {}));
675  EXPECT_FALSE(t.withException([](std::logic_error&) {}));
676  }
677 
678  {
679  auto const t = Try<bool>(true);
680  EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
681  EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
682  EXPECT_FALSE(t.withException([](std::runtime_error const&) {}));
683  EXPECT_FALSE(t.withException([](std::logic_error const&) {}));
684  }
685 
686  {
687  auto const t = Try<bool>(ew);
688  EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
689  EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
690  EXPECT_TRUE(t.withException([](std::runtime_error const&) {}));
691  EXPECT_FALSE(t.withException([](std::logic_error const&) {}));
692  }
693 
694  {
695  auto const t = Try<void>();
696  EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
697  EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
698  EXPECT_FALSE(t.withException([](std::runtime_error const&) {}));
699  EXPECT_FALSE(t.withException([](std::logic_error const&) {}));
700  }
701 
702  {
703  auto const t = Try<void>(ew);
704  EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
705  EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
706  EXPECT_TRUE(t.withException([](std::runtime_error const&) {}));
707  EXPECT_FALSE(t.withException([](std::logic_error const&) {}));
708  }
709 }
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Try  ,
TestUnwrapTuple   
)

Definition at line 711 of file TryTest.cpp.

References folly::as_const(), folly::copy(), EXPECT_EQ, std::tr1::make_tuple(), and folly::unwrapTryTuple().

711  {
712  auto original = std::make_tuple(Try<int>{1}, Try<int>{2});
713  EXPECT_EQ(std::make_tuple(1, 2), unwrapTryTuple(original));
716 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
tuple make_tuple()
Definition: gtest-tuple.h:675
constexpr T const & as_const(T &t) noexcept
Definition: Utility.h:96
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
Definition: Try.h:51
auto unwrapTryTuple(Tuple &&instance)
Definition: Try-inl.h:320
TEST ( Try  ,
TestUnwrapPair   
)

Definition at line 718 of file TryTest.cpp.

References folly::as_const(), folly::copy(), EXPECT_EQ, and folly::unwrapTryTuple().

718  {
719  auto original = std::make_pair(Try<int>{1}, Try<int>{2});
720  EXPECT_EQ(std::make_pair(1, 2), unwrapTryTuple(original));
721  EXPECT_EQ(std::make_pair(1, 2), unwrapTryTuple(folly::copy(original)));
722  EXPECT_EQ(std::make_pair(1, 2), unwrapTryTuple(folly::as_const(original)));
723 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr T const & as_const(T &t) noexcept
Definition: Utility.h:96
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
Definition: Try.h:51
auto unwrapTryTuple(Tuple &&instance)
Definition: Try-inl.h:320
TEST ( Try  ,
TestUnwrapForward   
)

Definition at line 725 of file TryTest.cpp.

References EXPECT_EQ, std::tr1::make_tuple(), folly::gen::move, and folly::unwrapTryTuple().

725  {
726  using UPtr_t = std::unique_ptr<int>;
727  auto original = std::make_tuple(Try<UPtr_t>{std::make_unique<int>(1)});
728  auto unwrapped = unwrapTryTuple(std::move(original));
729  EXPECT_EQ(*std::get<0>(unwrapped), 1);
730 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
tuple make_tuple()
Definition: gtest-tuple.h:675
Definition: Try.h:51
auto unwrapTryTuple(Tuple &&instance)
Definition: Try-inl.h:320