2017
[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
26 using namespace folly;
27
28 TEST(make_unique, compatible_with_std_make_unique) {
29   //  HACK: To enforce that `folly::` is imported here.
30   to_shared_ptr(std::unique_ptr<std::string>());
31
32   using namespace std;
33   make_unique<string>("hello, world");
34 }
35
36 TEST(to_weak_ptr, example) {
37   auto s = std::make_shared<int>(17);
38   EXPECT_EQ(1, s.use_count());
39   EXPECT_EQ(2, (to_weak_ptr(s).lock(), s.use_count())) << "lvalue";
40   EXPECT_EQ(3, (to_weak_ptr(decltype(s)(s)).lock(), s.use_count())) << "rvalue";
41 }
42
43 TEST(allocate_sys_buffer, compiles) {
44   auto buf = allocate_sys_buffer(256);
45   //  Freed at the end of the scope.
46 }
47
48 template <std::size_t> struct T {};
49 template <std::size_t> struct S {};
50 template <std::size_t> struct P {};
51
52 TEST(as_stl_allocator, sanity_check) {
53   typedef StlAllocator<SysArena, int> stl_arena_alloc;
54
55   EXPECT_TRUE((std::is_same<
56     as_stl_allocator<int, SysArena>::type,
57     stl_arena_alloc
58   >::value));
59
60   EXPECT_TRUE((std::is_same<
61     as_stl_allocator<int, stl_arena_alloc>::type,
62     stl_arena_alloc
63   >::value));
64 }
65
66 TEST(StlAllocator, void_allocator) {
67   typedef StlAllocator<SysArena, void> void_allocator;
68   SysArena arena;
69   void_allocator valloc(&arena);
70
71   typedef void_allocator::rebind<int>::other int_allocator;
72   int_allocator ialloc(valloc);
73
74   auto i = std::allocate_shared<int>(ialloc, 10);
75   ASSERT_NE(nullptr, i.get());
76   EXPECT_EQ(10, *i);
77   i.reset();
78   ASSERT_EQ(nullptr, i.get());
79 }
80
81 TEST(rebind_allocator, sanity_check) {
82   std::allocator<long> alloc;
83
84   auto i = std::allocate_shared<int>(
85     rebind_allocator<int, decltype(alloc)>(alloc), 10
86   );
87   ASSERT_NE(nullptr, i.get());
88   EXPECT_EQ(10, *i);
89   i.reset();
90   ASSERT_EQ(nullptr, i.get());
91
92   auto d = std::allocate_shared<double>(
93     rebind_allocator<double>(alloc), 5.6
94   );
95   ASSERT_NE(nullptr, d.get());
96   EXPECT_EQ(5.6, *d);
97   d.reset();
98   ASSERT_EQ(nullptr, d.get());
99
100   auto s = std::allocate_shared<std::string>(
101     rebind_allocator<std::string>(alloc), "HELLO, WORLD"
102   );
103   ASSERT_NE(nullptr, s.get());
104   EXPECT_EQ("HELLO, WORLD", *s);
105   s.reset();
106   ASSERT_EQ(nullptr, s.get());
107 }