5507b96f8855c193f08667b3a89b0c62bb2491cb
[folly.git] / folly / test / UtilityTest.cpp
1 /*
2  * Copyright 2017 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 }
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
93 TEST_F(UtilityTest, MoveOnly) {
94   class FooBar : folly::MoveOnly {
95     int a;
96   };
97
98   static_assert(
99       !std::is_copy_constructible<FooBar>::value,
100       "Should not be copy constructible");
101
102   // Test that move actually works.
103   FooBar foobar;
104   FooBar foobar2(std::move(foobar));
105   (void)foobar2;
106
107   // Test that inheriting from MoveOnly doesn't prevent the move
108   // constructor from being noexcept.
109   static_assert(
110       std::is_nothrow_move_constructible<FooBar>::value,
111       "Should have noexcept move constructor");
112 }