f5df99fb80563b3179eac46ef785abd3b3950d32
[folly.git] / folly / test / MemoryTest.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 <folly/Memory.h>
18 #include <folly/Arena.h>
19 #include <folly/String.h>
20 #include <folly/portability/GTest.h>
21
22 #include <glog/logging.h>
23
24 #include <type_traits>
25 #include <utility>
26
27 using namespace folly;
28
29 TEST(make_unique, compatible_with_std_make_unique) {
30   //  HACK: To enforce that `folly::` is imported here.
31   to_shared_ptr(std::unique_ptr<std::string>());
32
33   using namespace std;
34   make_unique<string>("hello, world");
35 }
36
37 TEST(to_weak_ptr, example) {
38   auto s = std::make_shared<int>(17);
39   EXPECT_EQ(1, s.use_count());
40   EXPECT_EQ(2, (to_weak_ptr(s).lock(), s.use_count())) << "lvalue";
41   EXPECT_EQ(3, (to_weak_ptr(decltype(s)(s)).lock(), s.use_count())) << "rvalue";
42 }
43
44 TEST(allocate_sys_buffer, compiles) {
45   auto buf = allocate_sys_buffer(256);
46   //  Freed at the end of the scope.
47 }
48
49 template <std::size_t> struct T {};
50 template <std::size_t> struct S {};
51 template <std::size_t> struct P {};
52
53 TEST(as_stl_allocator, sanity_check) {
54   typedef StlAllocator<SysArena, int> stl_arena_alloc;
55
56   EXPECT_TRUE((std::is_same<
57     as_stl_allocator<int, SysArena>::type,
58     stl_arena_alloc
59   >::value));
60
61   EXPECT_TRUE((std::is_same<
62     as_stl_allocator<int, stl_arena_alloc>::type,
63     stl_arena_alloc
64   >::value));
65 }
66
67 TEST(StlAllocator, void_allocator) {
68   typedef StlAllocator<SysArena, void> void_allocator;
69   SysArena arena;
70   void_allocator valloc(&arena);
71
72   typedef void_allocator::rebind<int>::other int_allocator;
73   int_allocator ialloc(valloc);
74
75   auto i = std::allocate_shared<int>(ialloc, 10);
76   ASSERT_NE(nullptr, i.get());
77   EXPECT_EQ(10, *i);
78   i.reset();
79   ASSERT_EQ(nullptr, i.get());
80 }
81
82 TEST(rebind_allocator, sanity_check) {
83   std::allocator<long> alloc;
84
85   auto i = std::allocate_shared<int>(
86     rebind_allocator<int, decltype(alloc)>(alloc), 10
87   );
88   ASSERT_NE(nullptr, i.get());
89   EXPECT_EQ(10, *i);
90   i.reset();
91   ASSERT_EQ(nullptr, i.get());
92
93   auto d = std::allocate_shared<double>(
94     rebind_allocator<double>(alloc), 5.6
95   );
96   ASSERT_NE(nullptr, d.get());
97   EXPECT_EQ(5.6, *d);
98   d.reset();
99   ASSERT_EQ(nullptr, d.get());
100
101   auto s = std::allocate_shared<std::string>(
102     rebind_allocator<std::string>(alloc), "HELLO, WORLD"
103   );
104   ASSERT_NE(nullptr, s.get());
105   EXPECT_EQ("HELLO, WORLD", *s);
106   s.reset();
107   ASSERT_EQ(nullptr, s.get());
108 }
109
110 template <typename C>
111 static void test_enable_shared_from_this(std::shared_ptr<C> sp) {
112   ASSERT_EQ(1l, sp.use_count());
113
114   // Test shared_from_this().
115   std::shared_ptr<C> sp2 = sp->shared_from_this();
116   ASSERT_EQ(sp, sp2);
117
118   // Test weak_from_this().
119   std::weak_ptr<C> wp = sp->weak_from_this();
120   ASSERT_EQ(sp, wp.lock());
121   sp.reset();
122   sp2.reset();
123   ASSERT_EQ(nullptr, wp.lock());
124
125   // Test shared_from_this() and weak_from_this() on object not owned by a
126   // shared_ptr. Undefined in C++14 but well-defined in C++17. Also known to
127   // work with libstdc++ >= 20150123. Feel free to add other standard library
128   // versions where the behavior is known.
129 #if __cplusplus >= 201700L || \
130     __GLIBCXX__ >= 20150123L
131   C stack_resident;
132   ASSERT_THROW(stack_resident.shared_from_this(), std::bad_weak_ptr);
133   ASSERT_TRUE(stack_resident.weak_from_this().expired());
134 #endif
135 }
136
137 TEST(enable_shared_from_this, compatible_with_std_enable_shared_from_this) {
138   // Compile-time compatibility.
139   class C_std : public std::enable_shared_from_this<C_std> {};
140   class C_folly : public folly::enable_shared_from_this<C_folly> {};
141   static_assert(
142     noexcept(std::declval<C_std>().shared_from_this()) ==
143     noexcept(std::declval<C_folly>().shared_from_this()), "");
144   static_assert(
145     noexcept(std::declval<C_std const>().shared_from_this()) ==
146     noexcept(std::declval<C_folly const>().shared_from_this()), "");
147   static_assert(noexcept(std::declval<C_folly>().weak_from_this()), "");
148   static_assert(noexcept(std::declval<C_folly const>().weak_from_this()), "");
149
150   // Runtime compatibility.
151   test_enable_shared_from_this(std::make_shared<C_folly>());
152   test_enable_shared_from_this(std::make_shared<C_folly const>());
153 }