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