Add benchmarks for SpookyHashV2 and constify SpookyHashV2::Final
[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(to_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 TEST(to_shared_ptr, example_with_dtor) {
65   bool disposed = false;
66   using disposable_deleter =
67     static_function_deleter<disposable, &disposable::dispose>;
68   auto uptr =
69     make_unique<disposable, disposable_deleter>([&] { disposed = true; });
70   EXPECT_FALSE(disposed);
71   auto sptr = to_shared_ptr(std::move(uptr));
72   EXPECT_FALSE(disposed);
73   sptr = nullptr;
74   EXPECT_TRUE(disposed);
75 }
76
77 template <std::size_t> struct T {};
78 template <std::size_t> struct S {};
79 template <std::size_t> struct P {};
80
81 TEST(as_stl_allocator, sanity_check) {
82   typedef StlAllocator<SysArena, int> stl_arena_alloc;
83
84   EXPECT_TRUE((std::is_same<
85     as_stl_allocator<int, SysArena>::type,
86     stl_arena_alloc
87   >::value));
88
89   EXPECT_TRUE((std::is_same<
90     as_stl_allocator<int, stl_arena_alloc>::type,
91     stl_arena_alloc
92   >::value));
93 }
94
95 TEST(StlAllocator, void_allocator) {
96   typedef StlAllocator<SysArena, void> void_allocator;
97   SysArena arena;
98   void_allocator valloc(&arena);
99
100   typedef void_allocator::rebind<int>::other int_allocator;
101   int_allocator ialloc(valloc);
102
103   auto i = std::allocate_shared<int>(ialloc, 10);
104   ASSERT_NE(nullptr, i.get());
105   EXPECT_EQ(10, *i);
106   i.reset();
107   ASSERT_EQ(nullptr, i.get());
108 }
109
110 TEST(rebind_allocator, sanity_check) {
111   std::allocator<long> alloc;
112
113   auto i = std::allocate_shared<int>(
114     rebind_allocator<int, decltype(alloc)>(alloc), 10
115   );
116   ASSERT_NE(nullptr, i.get());
117   EXPECT_EQ(10, *i);
118   i.reset();
119   ASSERT_EQ(nullptr, i.get());
120
121   auto d = std::allocate_shared<double>(
122     rebind_allocator<double>(alloc), 5.6
123   );
124   ASSERT_NE(nullptr, d.get());
125   EXPECT_EQ(5.6, *d);
126   d.reset();
127   ASSERT_EQ(nullptr, d.get());
128
129   auto s = std::allocate_shared<std::string>(
130     rebind_allocator<std::string>(alloc), "HELLO, WORLD"
131   );
132   ASSERT_NE(nullptr, s.get());
133   EXPECT_EQ("HELLO, WORLD", *s);
134   s.reset();
135   ASSERT_EQ(nullptr, s.get());
136 }
137
138 int main(int argc, char **argv) {
139   FLAGS_logtostderr = true;
140   google::InitGoogleLogging(argv[0]);
141   testing::InitGoogleTest(&argc, argv);
142
143   return RUN_ALL_TESTS();
144 }