2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <type_traits>
19 #include <folly/Utility.h>
20 #include <folly/portability/GTest.h>
24 class UtilityTest : public testing::Test {};
27 TEST_F(UtilityTest, copy) {
30 size_t rrefs = 0, crefs = 0;
31 void something(MyData&&) {
34 void something(const MyData&) {
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);
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))));
54 struct MyThrowingCopyable {
55 MyThrowingCopyable() {}
56 MyThrowingCopyable(const MyThrowingCopyable&) noexcept(false) {}
57 MyThrowingCopyable(MyThrowingCopyable&&) = default;
59 MyThrowingCopyable thr;
60 EXPECT_FALSE(noexcept(folly::copy(thr)));
61 EXPECT_TRUE(noexcept(folly::copy(std::move(thr)))); // note: does not copy
64 TEST_F(UtilityTest, as_const) {
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)));
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());
85 auto seq2 = folly::index_sequence<0, 4, 3>();
86 EXPECT_EQ(3, seq2.size());
88 constexpr auto seq3 = folly::make_index_sequence<3>();
89 static_assert(seq3.size() == 3, "");
90 EXPECT_EQ(3, seq3.size());
93 TEST_F(UtilityTest, MoveOnly) {
94 class FooBar : folly::MoveOnly {
99 !std::is_copy_constructible<FooBar>::value,
100 "Should not be copy constructible");
102 // Test that move actually works.
104 FooBar foobar2(std::move(foobar));
107 // Test that inheriting from MoveOnly doesn't prevent the move
108 // constructor from being noexcept.
110 std::is_nothrow_move_constructible<FooBar>::value,
111 "Should have noexcept move constructor");