proxygen
UtilityTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-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 
17 #include <type_traits>
18 
19 #include <folly/Utility.h>
21 
22 namespace {
23 
24 class UtilityTest : public testing::Test {};
25 } // namespace
26 
27 TEST_F(UtilityTest, copy) {
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 }
47 
48 TEST_F(UtilityTest, copy_noexcept_spec) {
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 }
63 
64 TEST_F(UtilityTest, as_const) {
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 }
79 
80 template <typename T>
81 static T& as_mutable(T const& t) {
82  return const_cast<T&>(t);
83 }
84 
85 TEST_F(UtilityTest, forward_like) {
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 }
92 
93 TEST_F(UtilityTest, exchange) {
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 }
99 
100 TEST(FollyIntegerSequence, core) {
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 }
121 
122 TEST_F(UtilityTest, MoveOnly) {
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 }
142 
143 TEST_F(UtilityTest, to_signed) {
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 }
155 
156 TEST_F(UtilityTest, to_unsigned) {
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 }
Definition: InvokeTest.cpp:58
#define T(v)
Definition: http_parser.c:233
static constexpr std::size_t size() noexcept
Definition: Utility.h:185
TEST_F(TestInfoTest, Names)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Gen seq(Value first, Value last)
Definition: Base.h:484
requires E e noexcept(noexcept(s.error(std::move(e))))
constexpr T const & as_const(T &t) noexcept
Definition: Utility.h:96
constexpr auto to_unsigned(T const &t) -> typename std::make_unsigned< T >::type
Definition: Utility.h:399
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
static T & as_mutable(T const &t)
Definition: UtilityTest.cpp:81
TEST_F(UtilityTest, copy)
Definition: UtilityTest.cpp:27
constexpr like_t< Src, Dst > && forward_like(Dst &&dst) noexcept
Definition: Utility.h:107
make_integer_sequence< std::size_t, Size > make_index_sequence
Definition: Utility.h:209
char a
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
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
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
moveonly_::MoveOnly MoveOnly
Definition: Utility.h:386
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::detail::as_const_fn as_const
static set< string > s
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
decltype(auto) constexpr apply(F &&func, Tuple &&tuple)
Definition: ApplyTuple.h:87
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
TEST(FollyIntegerSequence, core)