proxygen
ArenaSmartPtrTest.cpp File Reference

Go to the source code of this file.

Classes

struct  global_counter
 
class  Foo
 

Functions

template<typename Allocator >
void unique_ptr_test (Allocator &allocator)
 
 TEST (ArenaSmartPtr, unique_ptr_SysArena)
 
template<typename Allocator >
void shared_ptr_test (Allocator &allocator)
 
 TEST (ArenaSmartPtr, shared_ptr_SysArena)
 
int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 166 of file ArenaSmartPtrTest.cpp.

References testing::InitGoogleTest(), and RUN_ALL_TESTS().

166  {
168  return RUN_ALL_TESTS();
169 }
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
char ** argv
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
template<typename Allocator >
void shared_ptr_test ( Allocator &  allocator)

Definition at line 106 of file ArenaSmartPtrTest.cpp.

References global_counter::count(), counter, EXPECT_EQ, folly::foo, and g().

Referenced by TEST().

106  {
107  typedef std::shared_ptr<Foo> ptr_type;
108 
110  EXPECT_EQ(counter.count(), 0);
111 
112  ptr_type foo;
113  EXPECT_EQ(counter.count(), 0);
114  EXPECT_EQ(foo.use_count(), 0);
115 
116  {
117  auto p = std::allocate_shared<Foo>(allocator, counter);
118  EXPECT_EQ(counter.count(), 1);
119  EXPECT_EQ(p.use_count(), 1);
120 
121  p.reset();
122  EXPECT_EQ(counter.count(), 0);
123  EXPECT_EQ(p.use_count(), 0);
124 
125  p = std::allocate_shared<Foo>(allocator, counter);
126  EXPECT_EQ(counter.count(), 1);
127  EXPECT_EQ(p.use_count(), 1);
128 
129  foo = p;
130  EXPECT_EQ(p.use_count(), 2);
131  }
132  EXPECT_EQ(counter.count(), 1);
133  EXPECT_EQ(foo.use_count(), 1);
134 
135  {
136  auto p = foo;
137  EXPECT_EQ(counter.count(), 1);
138  EXPECT_EQ(p.use_count(), 2);
139 
140  [&](ptr_type g) {
141  EXPECT_EQ(counter.count(), 1);
142  EXPECT_EQ(p.use_count(), 3);
143  EXPECT_EQ(g.use_count(), 3);
144  g.reset();
145  EXPECT_EQ(counter.count(), 1);
146  EXPECT_EQ(p.use_count(), 2);
147  EXPECT_EQ(g.use_count(), 0);
148  }(p);
149  EXPECT_EQ(counter.count(), 1);
150  EXPECT_EQ(p.use_count(), 2);
151  }
152  EXPECT_EQ(counter.count(), 1);
153  EXPECT_EQ(foo.use_count(), 1);
154 
155  foo.reset();
156  EXPECT_EQ(counter.count(), 0);
157  EXPECT_EQ(foo.use_count(), 0);
158 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
unsigned count() const
std::atomic< int > counter
g_t g(f_t)
TEST ( ArenaSmartPtr  ,
unique_ptr_SysArena   
)

Definition at line 99 of file ArenaSmartPtrTest.cpp.

References unique_ptr_test().

99  {
100  SysArena arena;
101  SysArenaAllocator<Foo> alloc(arena);
102  unique_ptr_test(alloc);
103 }
void unique_ptr_test(Allocator &allocator)
TEST ( ArenaSmartPtr  ,
shared_ptr_SysArena   
)

Definition at line 160 of file ArenaSmartPtrTest.cpp.

References shared_ptr_test().

160  {
161  SysArena arena;
162  SysArenaAllocator<Foo> alloc(arena);
163  shared_ptr_test(alloc);
164 }
void shared_ptr_test(Allocator &allocator)
template<typename Allocator >
void unique_ptr_test ( Allocator &  allocator)

Definition at line 60 of file ArenaSmartPtrTest.cpp.

References global_counter::count(), counter, destroy(), EXPECT_EQ, g(), and folly::gen::move.

Referenced by TEST().

60  {
61  using ptr_type = std::unique_ptr<Foo, allocator_delete<Allocator>>;
62 
64  EXPECT_EQ(counter.count(), 0);
65 
66  Foo* foo = nullptr;
67 
68  {
69  auto p = folly::allocate_unique<Foo>(allocator, counter);
70  EXPECT_EQ(counter.count(), 1);
71 
72  p.reset();
73  EXPECT_EQ(counter.count(), 0);
74 
75  p = folly::allocate_unique<Foo>(allocator, counter);
76  EXPECT_EQ(counter.count(), 1);
77 
78  foo = p.release();
79  EXPECT_EQ(counter.count(), 1);
80  }
81  EXPECT_EQ(counter.count(), 1);
82 
83  {
84  auto p = folly::allocate_unique<Foo>(allocator, counter);
85  EXPECT_EQ(counter.count(), 2);
86 
87  [&](ptr_type g) {
88  EXPECT_EQ(counter.count(), 2);
89  g.reset();
90  EXPECT_EQ(counter.count(), 1);
91  }(std::move(p));
92  }
93  EXPECT_EQ(counter.count(), 1);
94 
96  EXPECT_EQ(counter.count(), 0);
97 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static void destroy()
unsigned count() const
std::atomic< int > counter
g_t g(f_t)