proxygen
SynchronizedPtrTest.cpp File Reference

Go to the source code of this file.

Functions

template<typename SPtr >
void basics (SPtr &sptr)
 
 TEST (SynchronizedPtrTest, Shared)
 
 TEST (SynchronizedPtrTest, UniqueBasic)
 
 TEST (SynchronizedPtrTest, UniqueDeleter)
 
 TEST (SynchronizedPtrTest, Replaceable)
 
 TEST (SynchronizedPtrTest, Optional)
 
 TEST (SynchronizedPtrTest, Virtual)
 

Function Documentation

template<typename SPtr >
void basics ( SPtr &  sptr)

Definition at line 24 of file SynchronizedPtrTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, and value.

Referenced by TEST().

24  {
25  EXPECT_TRUE((std::is_same<int const&, decltype(*sptr.rlock())>::value));
26  auto initialValue = *sptr.rlock();
27  bool rlockedTypeOK{false};
28  sptr.withRLock([&](auto&& value) {
30  });
31  EXPECT_TRUE(rlockedTypeOK);
32  EXPECT_TRUE((std::is_same<int&, decltype(*sptr.wlock())>::value));
33  bool wlockedTypeOK{false};
34  sptr.withWLock([&](auto&& value) {
36  ++value;
37  });
38  EXPECT_TRUE(wlockedTypeOK);
39  EXPECT_EQ(initialValue + 1, *sptr.rlock());
40 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( SynchronizedPtrTest  ,
Shared   
)

Definition at line 42 of file SynchronizedPtrTest.cpp.

References basics().

42  {
43  folly::SynchronizedPtr<std::shared_ptr<int>> pInt{std::make_shared<int>(0)};
44  basics(pInt);
45 }
void basics(SPtr &sptr)
TEST ( SynchronizedPtrTest  ,
UniqueBasic   
)

Definition at line 47 of file SynchronizedPtrTest.cpp.

References basics().

47  {
48  folly::SynchronizedPtr<std::unique_ptr<int>> pInt{std::make_unique<int>(0)};
49  basics(pInt);
50 }
void basics(SPtr &sptr)
TEST ( SynchronizedPtrTest  ,
UniqueDeleter   
)

Definition at line 52 of file SynchronizedPtrTest.cpp.

References basics(), EXPECT_TRUE, ptr, value, and x.

52  {
53  bool calledDeleter = false;
54  auto x = [&](int* ptr) {
55  delete ptr;
56  calledDeleter = true;
57  };
58  {
60  std::unique_ptr<int, decltype(x)>(new int(0), x)};
61  basics(pInt);
62  EXPECT_TRUE((std::is_same<
63  std::unique_ptr<int, decltype(x)>&,
64  decltype(*pInt.wlockPointer())>::value));
65  pInt.wlockPointer()->reset(new int(5));
66  EXPECT_TRUE(calledDeleter);
67  calledDeleter = false;
68  }
69  EXPECT_TRUE(calledDeleter);
70 }
Definition: InvokeTest.cpp:58
void * ptr
const int x
void basics(SPtr &sptr)
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( SynchronizedPtrTest  ,
Replaceable   
)

Definition at line 72 of file SynchronizedPtrTest.cpp.

References basics(), EXPECT_EQ, EXPECT_TRUE, ptr, and value.

72  {
75  basics(pInt);
77  (std::is_same<folly::Replaceable<int>&, decltype(*pInt.wlockPointer())>::
78  value));
79  EXPECT_TRUE((std::is_same<
81  decltype(*pcInt.wlockPointer())>::value));
82  pcInt.withWLockPointer([](auto&& ptr) {
84  (std::is_same<folly::Replaceable<int const>&, decltype(ptr)>::value));
85  ptr.emplace(4);
86  });
87  EXPECT_EQ(4, *pcInt.rlock());
88 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void basics(SPtr &sptr)
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( SynchronizedPtrTest  ,
Optional   
)

Definition at line 90 of file SynchronizedPtrTest.cpp.

References basics(), EXPECT_FALSE, EXPECT_TRUE, ptr, and value.

90  {
92  basics(pInt);
94  (std::is_same<folly::Optional<int>&, decltype(*pInt.wlockPointer())>::
95  value));
96  EXPECT_TRUE(static_cast<bool>(pInt.rlock()));
97  pInt.withWLockPointer([](auto&& ptr) {
98  EXPECT_TRUE((std::is_same<folly::Optional<int>&, decltype(ptr)>::value));
99  ptr.clear();
100  });
101  EXPECT_FALSE(static_cast<bool>(pInt.rlock()));
102 }
void * ptr
void basics(SPtr &sptr)
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( SynchronizedPtrTest  ,
Virtual   
)

Definition at line 104 of file SynchronizedPtrTest.cpp.

References b, B, EXPECT_TRUE, ptr, and value.

104  {
105  struct A {
106  virtual void poke(bool&) const {}
107  virtual ~A() = default;
108  };
109  struct B : A {
110  void poke(bool& b) const override {
111  b = true;
112  }
113  };
114  folly::SynchronizedPtr<A*> pA{new B()};
115  bool itWorks = false;
116  pA.rlock()->poke(itWorks);
117  EXPECT_TRUE(itWorks);
118  itWorks = false;
119  pA.wlock()->poke(itWorks);
120  EXPECT_TRUE(itWorks);
121  pA.withWLockPointer([](auto&& ptr) {
122  EXPECT_TRUE((std::is_same<A*&, decltype(ptr)>::value));
123  delete ptr;
124  ptr = new B();
125  });
126  {
127  auto lockedPtr = pA.wlockPointer();
128  EXPECT_TRUE((std::is_same<A*&, decltype(*lockedPtr)>::value));
129  delete *lockedPtr;
130  *lockedPtr = new B();
131  }
132  itWorks = false;
133  pA.wlock()->poke(itWorks);
134  EXPECT_TRUE(itWorks);
135  delete *pA.wlockPointer();
136 }
void * ptr
std::unique_ptr< int > A
char b
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define B(name, bit)
Definition: CpuId.h:178