Port Folly to PPC64
[folly.git] / folly / test / MemoryTest.cpp
1 /*
2  * Copyright 2015 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
21 #include <glog/logging.h>
22 #include <gtest/gtest.h>
23
24 #include <type_traits>
25
26 using namespace folly;
27
28 namespace {
29 class disposable {
30  public:
31   explicit disposable(std::function<void()> onDispose) :
32     onDispose_(std::move(onDispose)) {}
33   static void dispose(disposable* f) {
34     ASSERT_NE(nullptr, f);
35     f->onDispose_();
36     delete f;
37   }
38  private:
39   std::function<void()> onDispose_;
40 };
41 }
42
43 TEST(static_function_deleter, example) {
44   size_t count = 0;
45   using disposable_deleter =
46     static_function_deleter<disposable, &disposable::dispose>;
47   make_unique<disposable, disposable_deleter>([&] { ++count; });
48   EXPECT_EQ(1, count);
49 }
50
51 TEST(static_function_deleter, nullptr) {
52   using disposable_deleter =
53     static_function_deleter<disposable, &disposable::dispose>;
54   std::unique_ptr<disposable, disposable_deleter>(nullptr);
55 }
56
57 TEST(shared_ptr, example) {
58   auto uptr = make_unique<std::string>("hello");
59   auto sptr = to_shared_ptr(std::move(uptr));
60   EXPECT_EQ(nullptr, uptr);
61   EXPECT_EQ("hello", *sptr);
62 }
63
64 template <std::size_t> struct T {};
65 template <std::size_t> struct S {};
66 template <std::size_t> struct P {};
67
68 TEST(as_stl_allocator, sanity_check) {
69   typedef StlAllocator<SysArena, int> stl_arena_alloc;
70
71   EXPECT_TRUE((std::is_same<
72     as_stl_allocator<int, SysArena>::type,
73     stl_arena_alloc
74   >::value));
75
76   EXPECT_TRUE((std::is_same<
77     as_stl_allocator<int, stl_arena_alloc>::type,
78     stl_arena_alloc
79   >::value));
80 }
81
82 TEST(StlAllocator, void_allocator) {
83   typedef StlAllocator<SysArena, void> void_allocator;
84   SysArena arena;
85   void_allocator valloc(&arena);
86
87   typedef void_allocator::rebind<int>::other int_allocator;
88   int_allocator ialloc(valloc);
89
90   auto i = std::allocate_shared<int>(ialloc, 10);
91   ASSERT_NE(nullptr, i.get());
92   EXPECT_EQ(10, *i);
93   i.reset();
94   ASSERT_EQ(nullptr, i.get());
95 }
96
97 TEST(rebind_allocator, sanity_check) {
98   std::allocator<long> alloc;
99
100   auto i = std::allocate_shared<int>(
101     rebind_allocator<int, decltype(alloc)>(alloc), 10
102   );
103   ASSERT_NE(nullptr, i.get());
104   EXPECT_EQ(10, *i);
105   i.reset();
106   ASSERT_EQ(nullptr, i.get());
107
108   auto d = std::allocate_shared<double>(
109     rebind_allocator<double>(alloc), 5.6
110   );
111   ASSERT_NE(nullptr, d.get());
112   EXPECT_EQ(5.6, *d);
113   d.reset();
114   ASSERT_EQ(nullptr, d.get());
115
116   auto s = std::allocate_shared<std::string>(
117     rebind_allocator<std::string>(alloc), "HELLO, WORLD"
118   );
119   ASSERT_NE(nullptr, s.get());
120   EXPECT_EQ("HELLO, WORLD", *s);
121   s.reset();
122   ASSERT_EQ(nullptr, s.get());
123 }
124
125 int main(int argc, char **argv) {
126   FLAGS_logtostderr = true;
127   google::InitGoogleLogging(argv[0]);
128   testing::InitGoogleTest(&argc, argv);
129
130   return RUN_ALL_TESTS();
131 }