2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/Memory.h>
18 #include <folly/Arena.h>
19 #include <folly/String.h>
21 #include <glog/logging.h>
22 #include <gtest/gtest.h>
24 #include <type_traits>
26 using namespace folly;
31 explicit disposable(std::function<void()> onDispose) :
32 onDispose_(std::move(onDispose)) {}
33 static void dispose(disposable* f) {
34 ASSERT_NE(nullptr, f);
39 std::function<void()> onDispose_;
43 TEST(static_function_deleter, example) {
45 using disposable_deleter =
46 static_function_deleter<disposable, &disposable::dispose>;
47 make_unique<disposable, disposable_deleter>([&] { ++count; });
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);
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);
64 template <std::size_t> struct T {};
65 template <std::size_t> struct S {};
66 template <std::size_t> struct P {};
68 TEST(as_stl_allocator, sanity_check) {
69 typedef StlAllocator<SysArena, int> stl_arena_alloc;
71 EXPECT_TRUE((std::is_same<
72 as_stl_allocator<int, SysArena>::type,
76 EXPECT_TRUE((std::is_same<
77 as_stl_allocator<int, stl_arena_alloc>::type,
82 TEST(StlAllocator, void_allocator) {
83 typedef StlAllocator<SysArena, void> void_allocator;
85 void_allocator valloc(&arena);
87 typedef void_allocator::rebind<int>::other int_allocator;
88 int_allocator ialloc(valloc);
90 auto i = std::allocate_shared<int>(ialloc, 10);
91 ASSERT_NE(nullptr, i.get());
94 ASSERT_EQ(nullptr, i.get());
97 TEST(rebind_allocator, sanity_check) {
98 std::allocator<long> alloc;
100 auto i = std::allocate_shared<int>(
101 rebind_allocator<int, decltype(alloc)>(alloc), 10
103 ASSERT_NE(nullptr, i.get());
106 ASSERT_EQ(nullptr, i.get());
108 auto d = std::allocate_shared<double>(
109 rebind_allocator<double>(alloc), 5.6
111 ASSERT_NE(nullptr, d.get());
114 ASSERT_EQ(nullptr, d.get());
116 auto s = std::allocate_shared<std::string>(
117 rebind_allocator<std::string>(alloc), "HELLO, WORLD"
119 ASSERT_NE(nullptr, s.get());
120 EXPECT_EQ("HELLO, WORLD", *s);
122 ASSERT_EQ(nullptr, s.get());
125 int main(int argc, char **argv) {
126 FLAGS_logtostderr = true;
127 google::InitGoogleLogging(argv[0]);
128 testing::InitGoogleTest(&argc, argv);
130 return RUN_ALL_TESTS();