2 * Copyright 2016 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;
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>());
33 make_unique<string>("hello, world");
36 TEST(allocate_sys_buffer, compiles) {
37 auto buf = allocate_sys_buffer(256);
38 // Freed at the end of the scope.
41 template <std::size_t> struct T {};
42 template <std::size_t> struct S {};
43 template <std::size_t> struct P {};
45 TEST(as_stl_allocator, sanity_check) {
46 typedef StlAllocator<SysArena, int> stl_arena_alloc;
48 EXPECT_TRUE((std::is_same<
49 as_stl_allocator<int, SysArena>::type,
53 EXPECT_TRUE((std::is_same<
54 as_stl_allocator<int, stl_arena_alloc>::type,
59 TEST(StlAllocator, void_allocator) {
60 typedef StlAllocator<SysArena, void> void_allocator;
62 void_allocator valloc(&arena);
64 typedef void_allocator::rebind<int>::other int_allocator;
65 int_allocator ialloc(valloc);
67 auto i = std::allocate_shared<int>(ialloc, 10);
68 ASSERT_NE(nullptr, i.get());
71 ASSERT_EQ(nullptr, i.get());
74 TEST(rebind_allocator, sanity_check) {
75 std::allocator<long> alloc;
77 auto i = std::allocate_shared<int>(
78 rebind_allocator<int, decltype(alloc)>(alloc), 10
80 ASSERT_NE(nullptr, i.get());
83 ASSERT_EQ(nullptr, i.get());
85 auto d = std::allocate_shared<double>(
86 rebind_allocator<double>(alloc), 5.6
88 ASSERT_NE(nullptr, d.get());
91 ASSERT_EQ(nullptr, d.get());
93 auto s = std::allocate_shared<std::string>(
94 rebind_allocator<std::string>(alloc), "HELLO, WORLD"
96 ASSERT_NE(nullptr, s.get());
97 EXPECT_EQ("HELLO, WORLD", *s);
99 ASSERT_EQ(nullptr, s.get());