proxygen
PackedSyncPtrTest.cpp File Reference
#include <folly/PackedSyncPtr.h>
#include <cinttypes>
#include <thread>
#include <unordered_map>
#include <utility>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Classes

struct  SyncVec< T >
 

Typedefs

typedef SyncVec< intptr_t > VecT
 
typedef std::unordered_map< int64_t, VecTMap
 

Functions

 TEST (PackedSyncPtr, Basic)
 
static Map map (mapCap)
 
void appThread (intptr_t id)
 
 TEST (PackedSyncPtr, Application)
 
 TEST (PackedSyncPtr, extraData)
 

Variables

const int mapCap = 1317
 
const int nthrs = 297
 

Typedef Documentation

typedef std::unordered_map<int64_t, VecT> Map

Definition at line 101 of file PackedSyncPtrTest.cpp.

typedef SyncVec<intptr_t> VecT

Definition at line 100 of file PackedSyncPtrTest.cpp.

Function Documentation

void appThread ( intptr_t  id)

Definition at line 108 of file PackedSyncPtrTest.cpp.

Referenced by TEST().

108  {
109  for (auto& kv : map) {
110  kv.second.lock();
111  kv.second.push_back(id);
112  kv.second.unlock();
113  }
114 }
Definition: Traits.h:594
TEST ( PackedSyncPtr  ,
Basic   
)

Definition at line 42 of file PackedSyncPtrTest.cpp.

References EXPECT_EQ, folly::PackedSyncPtr< T >::extra(), folly::gen::first, folly::PackedSyncPtr< T >::get(), folly::PackedSyncPtr< T >::init(), folly::PackedSyncPtr< T >::lock(), folly::PackedSyncPtr< T >::set(), folly::PackedSyncPtr< T >::setExtra(), and folly::PackedSyncPtr< T >::unlock().

42  {
44  sp.init(new std::pair<int, int>[2]);
45  EXPECT_EQ(sizeof(sp), 8);
46  sp->first = 5;
47  EXPECT_EQ(sp[0].first, 5);
48  sp[1].second = 7;
49  EXPECT_EQ(sp[1].second, 7);
50  sp.lock();
51  EXPECT_EQ(sp[1].second, 7);
52  sp[0].first = 9;
53  EXPECT_EQ(sp->first, 9);
54  sp.unlock();
55  EXPECT_EQ((sp.get() + 1)->second, 7);
56 
57  sp.lock();
58  EXPECT_EQ(sp.extra(), 0);
59  sp.setExtra(0x13);
60  EXPECT_EQ(sp.extra(), 0x13);
61  EXPECT_EQ((sp.get() + 1)->second, 7);
62  delete[] sp.get();
63  auto newP = new std::pair<int, int>();
64  sp.set(newP);
65  EXPECT_EQ(sp.extra(), 0x13);
66  EXPECT_EQ(sp.get(), newP);
67  sp.unlock();
68  delete sp.get();
69 }
uint16_t extra() const
void setExtra(uint16_t extra)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void unlock() const
void init(T *initialPtr=nullptr, uint16_t initialExtra=0)
Definition: PackedSyncPtr.h:77
void lock() const
constexpr detail::First first
Definition: Base-inl.h:2553
TEST ( PackedSyncPtr  ,
Application   
)

Definition at line 116 of file PackedSyncPtrTest.cpp.

References appThread(), EXPECT_EQ, EXPECT_TRUE, i, int64_t, nthrs, and folly::pushmi::detail::t.

116  {
117  for (int64_t i = 0; i < mapCap / 2; ++i) {
118  map.insert(std::make_pair(i, VecT()));
119  }
120  std::vector<std::thread> thrs;
121  for (intptr_t i = 0; i < nthrs; i++) {
122  thrs.push_back(std::thread(appThread, i));
123  }
124  for (auto& t : thrs) {
125  t.join();
126  }
127 
128  for (auto& kv : map) {
129  // Make sure every thread successfully inserted it's ID into every vec
130  std::set<intptr_t> idsFound;
131  for (auto& elem : kv.second) {
132  EXPECT_TRUE(idsFound.insert(elem).second); // check for dups
133  }
134  EXPECT_EQ(idsFound.size(), nthrs); // check they are all there
135  }
136 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
SyncVec< intptr_t > VecT
const int nthrs
Definition: Traits.h:594
void appThread(intptr_t id)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const int mapCap
TEST ( PackedSyncPtr  ,
extraData   
)

Definition at line 138 of file PackedSyncPtrTest.cpp.

References EXPECT_EQ, folly::PackedSyncPtr< T >::get(), folly::PackedSyncPtr< T >::init(), folly::PackedSyncPtr< T >::lock(), folly::PackedSyncPtr< T >::set(), and folly::PackedSyncPtr< T >::unlock().

138  {
140  p.init();
141  int* unaligned = reinterpret_cast<int*>(0xf003);
142  p.lock();
143  p.set(unaligned);
144  uintptr_t* bytes = reinterpret_cast<uintptr_t*>(&p);
145  LOG(INFO) << "Bytes integer is: 0x" << std::hex << *bytes;
146  EXPECT_EQ(p.get(), unaligned);
147  p.unlock();
148 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void unlock() const
void init(T *initialPtr=nullptr, uint16_t initialExtra=0)
Definition: PackedSyncPtr.h:77
void lock() const

Variable Documentation

const int mapCap = 1317

Definition at line 102 of file PackedSyncPtrTest.cpp.

const int nthrs = 297

Definition at line 103 of file PackedSyncPtrTest.cpp.

Referenced by TEST().