fix a multiline comment warning
[folly.git] / folly / test / UtilityTest.cpp
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>
20 #include <folly/portability/GTest.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;
51   EXPECT_TRUE(noexcept(folly::copy(noe)));
52   EXPECT_TRUE(noexcept(folly::copy(std::move(noe))));
53
54   struct MyThrowingCopyable {
55     MyThrowingCopyable() {}
56     MyThrowingCopyable(const MyThrowingCopyable&) noexcept(false) {}
57     MyThrowingCopyable(MyThrowingCopyable&&) = default;
58   };
59   MyThrowingCopyable thr;
60   EXPECT_FALSE(noexcept(folly::copy(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());
75   EXPECT_TRUE(folly::as_const(s).member());
76   EXPECT_EQ(&s, &folly::as_const(s));
77   EXPECT_TRUE(noexcept(folly::as_const(s)));
78 }
79
80 TEST(FollyIntegerSequence, core) {
81   constexpr auto seq = folly::integer_sequence<int, 0, 3, 2>();
82   static_assert(seq.size() == 3, "");
83   EXPECT_EQ(3, seq.size());
84
85   auto seq2 = folly::index_sequence<0, 4, 3>();
86   EXPECT_EQ(3, seq2.size());
87
88   constexpr auto seq3 = folly::make_index_sequence<3>();
89   static_assert(seq3.size() == 3, "");
90   EXPECT_EQ(3, seq3.size());
91
92   // check our own implementation even when the builtin is available
93   using seq4 = typename folly::utility_detail::make_seq<5>::template apply<
94       folly::integer_sequence<int>,
95       folly::integer_sequence<int, 0>>;
96   EXPECT_EQ(5, seq4{}.size());
97   EXPECT_TRUE((std::is_same<seq4::value_type, int>::value));
98   using seq4_expected = folly::integer_sequence<int, 0, 1, 2, 3, 4>;
99   EXPECT_TRUE((std::is_same<seq4, seq4_expected>::value));
100 }
101
102 TEST_F(UtilityTest, MoveOnly) {
103   class FooBar : folly::MoveOnly {
104     int a;
105   };
106
107   static_assert(
108       !std::is_copy_constructible<FooBar>::value,
109       "Should not be copy constructible");
110
111   // Test that move actually works.
112   FooBar foobar;
113   FooBar foobar2(std::move(foobar));
114   (void)foobar2;
115
116   // Test that inheriting from MoveOnly doesn't prevent the move
117   // constructor from being noexcept.
118   static_assert(
119       std::is_nothrow_move_constructible<FooBar>::value,
120       "Should have noexcept move constructor");
121 }