2 * Copyright 2013 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.
18 * @author: Marcelo Juchem <marcelo@fb.com>
21 #include "folly/StlAllocator.h"
22 #include "folly/Arena.h"
24 #include <gtest/gtest.h>
26 using namespace folly;
28 struct global_counter {
29 global_counter(): count_(0) {}
31 void increase() { ++count_; }
37 unsigned count() const { return count_; }
44 explicit Foo(global_counter& counter):
55 global_counter& counter_;
58 template <typename Allocator>
59 void unique_ptr_test(Allocator& allocator) {
60 typedef typename AllocatorUniquePtr<Foo, Allocator>::type ptr_type;
62 global_counter counter;
63 EXPECT_EQ(counter.count(), 0);
68 auto p = folly::allocate_unique<Foo>(allocator, counter);
69 EXPECT_EQ(counter.count(), 1);
72 EXPECT_EQ(counter.count(), 0);
74 p = folly::allocate_unique<Foo>(allocator, counter);
75 EXPECT_EQ(counter.count(), 1);
78 EXPECT_EQ(counter.count(), 1);
80 EXPECT_EQ(counter.count(), 1);
83 auto p = folly::allocate_unique<Foo>(allocator, counter);
84 EXPECT_EQ(counter.count(), 2);
87 EXPECT_EQ(counter.count(), 2);
89 EXPECT_EQ(counter.count(), 1);
92 EXPECT_EQ(counter.count(), 1);
94 StlAllocator<Allocator, Foo>().destroy(foo);
95 EXPECT_EQ(counter.count(), 0);
98 TEST(ArenaSmartPtr, unique_ptr_SysArena) {
100 unique_ptr_test(arena);
103 TEST(ArenaSmartPtr, unique_ptr_StlAlloc_SysArena) {
105 StlAllocator<SysArena, Foo> alloc(&arena);
106 unique_ptr_test(alloc);
109 template <typename Allocator>
110 void shared_ptr_test(Allocator& allocator) {
111 typedef std::shared_ptr<Foo> ptr_type;
113 global_counter counter;
114 EXPECT_EQ(counter.count(), 0);
117 EXPECT_EQ(counter.count(), 0);
118 EXPECT_EQ(foo.use_count(), 0);
121 auto p = folly::allocate_shared<Foo>(allocator, counter);
122 EXPECT_EQ(counter.count(), 1);
123 EXPECT_EQ(p.use_count(), 1);
126 EXPECT_EQ(counter.count(), 0);
127 EXPECT_EQ(p.use_count(), 0);
129 p = folly::allocate_shared<Foo>(allocator, counter);
130 EXPECT_EQ(counter.count(), 1);
131 EXPECT_EQ(p.use_count(), 1);
134 EXPECT_EQ(p.use_count(), 2);
136 EXPECT_EQ(counter.count(), 1);
137 EXPECT_EQ(foo.use_count(), 1);
141 EXPECT_EQ(counter.count(), 1);
142 EXPECT_EQ(p.use_count(), 2);
145 EXPECT_EQ(counter.count(), 1);
146 EXPECT_EQ(p.use_count(), 3);
147 EXPECT_EQ(g.use_count(), 3);
149 EXPECT_EQ(counter.count(), 1);
150 EXPECT_EQ(p.use_count(), 2);
151 EXPECT_EQ(g.use_count(), 0);
153 EXPECT_EQ(counter.count(), 1);
154 EXPECT_EQ(p.use_count(), 2);
156 EXPECT_EQ(counter.count(), 1);
157 EXPECT_EQ(foo.use_count(), 1);
160 EXPECT_EQ(counter.count(), 0);
161 EXPECT_EQ(foo.use_count(), 0);
164 TEST(ArenaSmartPtr, shared_ptr_SysArena) {
166 shared_ptr_test(arena);
169 TEST(ArenaSmartPtr, shared_ptr_StlAlloc_SysArena) {
171 StlAllocator<SysArena, Foo> alloc(&arena);
172 shared_ptr_test(alloc);
175 int main(int argc, char *argv[]) {
176 testing::InitGoogleTest(&argc, argv);
177 return RUN_ALL_TESTS();