proxygen
UtilityTest.cpp File Reference
#include <type_traits>
#include <folly/Utility.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

 TEST_F (UtilityTest, copy)
 
 TEST_F (UtilityTest, copy_noexcept_spec)
 
 TEST_F (UtilityTest, as_const)
 
template<typename T >
static Tas_mutable (T const &t)
 
 TEST_F (UtilityTest, forward_like)
 
 TEST_F (UtilityTest, exchange)
 
 TEST (FollyIntegerSequence, core)
 
 TEST_F (UtilityTest, MoveOnly)
 
 TEST_F (UtilityTest, to_signed)
 
 TEST_F (UtilityTest, to_unsigned)
 

Function Documentation

template<typename T >
static T& as_mutable ( T const &  t)
static

Definition at line 81 of file UtilityTest.cpp.

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

Referenced by TEST_F().

81  {
82  return const_cast<T&>(t);
83 }
#define T(v)
Definition: http_parser.c:233
TEST ( FollyIntegerSequence  ,
core   
)

Definition at line 100 of file UtilityTest.cpp.

References folly::apply(), EXPECT_EQ, EXPECT_TRUE, folly::gen::seq(), folly::integer_sequence< T, Ints >::size(), and value.

100  {
101  constexpr auto seq = folly::integer_sequence<int, 0, 3, 2>();
102  static_assert(seq.size() == 3, "");
103  EXPECT_EQ(3, seq.size());
104 
105  auto seq2 = folly::index_sequence<0, 4, 3>();
106  EXPECT_EQ(3, seq2.size());
107 
108  constexpr auto seq3 = folly::make_index_sequence<3>();
109  static_assert(seq3.size() == 3, "");
110  EXPECT_EQ(3, seq3.size());
111 
112  // check our own implementation even when the builtin is available
116  EXPECT_EQ(5, seq4{}.size());
118  using seq4_expected = folly::integer_sequence<int, 0, 1, 2, 3, 4>;
120 }
static constexpr std::size_t size() noexcept
Definition: Utility.h:185
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen seq(Value first, Value last)
Definition: Base.h:484
make_integer_sequence< std::size_t, Size > make_index_sequence
Definition: Utility.h:209
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
decltype(auto) constexpr apply(F &&func, Tuple &&tuple)
Definition: ApplyTuple.h:87
TEST_F ( UtilityTest  ,
copy   
)

Definition at line 27 of file UtilityTest.cpp.

References folly::copy(), data, EXPECT_EQ, and folly::gen::move.

27  {
28  struct MyData {};
29  struct Worker {
30  size_t rrefs = 0, crefs = 0;
31  void something(MyData&&) {
32  ++rrefs;
33  }
34  void something(const MyData&) {
35  ++crefs;
36  }
37  };
38 
39  MyData data;
40  Worker worker;
41  worker.something(folly::copy(data));
42  worker.something(std::move(data));
43  worker.something(data);
44  EXPECT_EQ(2, worker.rrefs);
45  EXPECT_EQ(1, worker.crefs);
46 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
TEST_F ( UtilityTest  ,
copy_noexcept_spec   
)

Definition at line 48 of file UtilityTest.cpp.

References folly::copy(), EXPECT_FALSE, EXPECT_TRUE, folly::gen::move, and folly::pushmi::__adl::noexcept().

48  {
49  struct MyNoexceptCopyable {};
50  MyNoexceptCopyable noe;
53 
54  struct MyThrowingCopyable {
55  MyThrowingCopyable() {}
56  MyThrowingCopyable(const MyThrowingCopyable&) noexcept(false) {}
57  MyThrowingCopyable(MyThrowingCopyable&&) = default;
58  };
59  MyThrowingCopyable thr;
61  EXPECT_TRUE(noexcept(folly::copy(std::move(thr)))); // note: does not copy
62 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
requires E e noexcept(noexcept(s.error(std::move(e))))
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST_F ( UtilityTest  ,
as_const   
)

Definition at line 64 of file UtilityTest.cpp.

References folly::as_const(), EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::gen::member(), folly::pushmi::__adl::noexcept(), and s.

64  {
65  struct S {
66  bool member() {
67  return false;
68  }
69  bool member() const {
70  return true;
71  }
72  };
73  S s;
74  EXPECT_FALSE(s.member());
76  EXPECT_EQ(&s, &folly::as_const(s));
78 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
requires E e noexcept(noexcept(s.error(std::move(e))))
constexpr T const & as_const(T &t) noexcept
Definition: Utility.h:96
std::enable_if< ExprIsConst< Constness >::value, Map >::type member(Return(Class::*member)() const)
Definition: Base.h:605
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static set< string > s
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST_F ( UtilityTest  ,
forward_like   
)

Definition at line 85 of file UtilityTest.cpp.

References as_mutable(), and EXPECT_EQ.

85  {
86  int x = 0;
87  // just show that it may be invoked, and that it is purely a cast
88  // the real work is done by like_t, in terms of which forward_like is defined
89  EXPECT_EQ(&x, std::addressof(folly::forward_like<char&>(x)));
90  EXPECT_EQ(&x, std::addressof(as_mutable(folly::forward_like<char const>(x))));
91 }
Definition: InvokeTest.cpp:58
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static T & as_mutable(T const &t)
Definition: UtilityTest.cpp:81
TEST_F ( UtilityTest  ,
exchange   
)

Definition at line 93 of file UtilityTest.cpp.

References folly::exchange(), and EXPECT_EQ.

93  {
94  auto obj = std::map<std::string, int>{{"hello", 3}};
95  auto old = exchange(obj, {{"world", 4}});
96  EXPECT_EQ((std::map<std::string, int>{{"world", 4}}), obj);
97  EXPECT_EQ((std::map<std::string, int>{{"hello", 3}}), old);
98 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
TEST_F ( UtilityTest  ,
MoveOnly   
)

Definition at line 122 of file UtilityTest.cpp.

References a, folly::gen::move, and value.

122  {
123  class FooBar : folly::MoveOnly {
124  int a;
125  };
126 
127  static_assert(
129  "Should not be copy constructible");
130 
131  // Test that move actually works.
132  FooBar foobar;
133  FooBar foobar2(std::move(foobar));
134  (void)foobar2;
135 
136  // Test that inheriting from MoveOnly doesn't prevent the move
137  // constructor from being noexcept.
138  static_assert(
140  "Should have noexcept move constructor");
141 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
char a
static const char *const value
Definition: Conv.cpp:50
TEST_F ( UtilityTest  ,
to_signed   
)

Definition at line 143 of file UtilityTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, int32_t, folly::to_signed(), uint32_t, and value.

143  {
144  {
145  constexpr auto actual = folly::to_signed(int32_t(-12));
146  EXPECT_TRUE(std::is_signed<decltype(actual)>::value);
147  EXPECT_EQ(-12, actual);
148  }
149  {
150  constexpr auto actual = folly::to_signed(uint32_t(-12));
151  EXPECT_TRUE(std::is_signed<decltype(actual)>::value);
152  EXPECT_EQ(-12, actual);
153  }
154 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static const char *const value
Definition: Conv.cpp:50
constexpr auto to_signed(T const &t) -> typename std::make_signed< T >::type
Definition: Utility.h:389
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST_F ( UtilityTest  ,
to_unsigned   
)

Definition at line 156 of file UtilityTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, int32_t, folly::to_unsigned(), uint32_t, and value.

156  {
157  {
158  constexpr auto actual = folly::to_unsigned(int32_t(-12));
159  EXPECT_TRUE(!std::is_signed<decltype(actual)>::value);
160  EXPECT_EQ(-12, actual);
161  }
162  {
163  constexpr auto actual = folly::to_unsigned(uint32_t(-12));
164  EXPECT_TRUE(!std::is_signed<decltype(actual)>::value);
165  EXPECT_EQ(-12, actual);
166  }
167 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr auto to_unsigned(T const &t) -> typename std::make_unsigned< T >::type
Definition: Utility.h:399
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859