proxygen
CachelinePaddedTest.cpp File Reference
#include <folly/CachelinePadded.h>
#include <type_traits>
#include <folly/lang/Align.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Classes

struct  SizedData< dataSize, alignment >
 
struct  CachelinePaddedTests< T >
 

Typedefs

template<typename T , size_t N = 1>
using SizedDataMimic = SizedData< N *sizeof(T), alignof(T)>
 
using CachelinePaddedTypes = ::testing::Types< SizedData< kCachelineSize >, SizedData< 2 *kCachelineSize >, SizedData< kCachelineSize/2 >, SizedData< kCachelineSize+kCachelineSize/2 >, SizedDataMimic< folly::max_align_t >, SizedDataMimic< void * >, SizedDataMimic< long double >, SizedDataMimic< double >, SizedDataMimic< float >, SizedDataMimic< long long >, SizedDataMimic< long >, SizedDataMimic< int >, SizedDataMimic< short >, SizedDataMimic< char >, SizedDataMimic< folly::max_align_t, 3 >, SizedDataMimic< void *, 3 >, SizedDataMimic< long double, 3 >, SizedDataMimic< double, 3 >, SizedDataMimic< float, 3 >, SizedDataMimic< long long, 3 >, SizedDataMimic< long, 3 >, SizedDataMimic< int, 3 >, SizedDataMimic< short, 3 >, SizedDataMimic< char, 3 >, SizedDataMimic< folly::max_align_t, kCachelineSize+3 >, SizedDataMimic< void *, kCachelineSize+3 >, SizedDataMimic< long double, kCachelineSize+3 >, SizedDataMimic< double, kCachelineSize+3 >, SizedDataMimic< float, kCachelineSize+3 >, SizedDataMimic< long long, kCachelineSize+3 >, SizedDataMimic< long, kCachelineSize+3 >, SizedDataMimic< int, kCachelineSize+3 >, SizedDataMimic< short, kCachelineSize+3 >, SizedDataMimic< char, kCachelineSize+3 >>
 

Functions

 TYPED_TEST_CASE (CachelinePaddedTests, CachelinePaddedTypes)
 
 TYPED_TEST (CachelinePaddedTests, alignment)
 
 TYPED_TEST (CachelinePaddedTests, integrity)
 
 TYPED_TEST (CachelinePaddedTests, size)
 
 TEST (CachelinePadded, PtrOperator)
 
 TEST (CachelinePadded, PropagatesConstness)
 
 TEST (CachelinePadded, ConstructsAndDestructs)
 
 TEST (CachelinePadded, ConstructsAndDestructsArrays)
 
 TEST (CachelinePadded, ForwardsCorrectly)
 

Variables

static constexpr int kCachelineSize
 

Typedef Documentation

template<typename T , size_t N = 1>
using SizedDataMimic = SizedData<N * sizeof(T), alignof(T)>

Definition at line 63 of file CachelinePaddedTest.cpp.

Function Documentation

TEST ( CachelinePadded  ,
PtrOperator   
)

Definition at line 128 of file CachelinePaddedTest.cpp.

References EXPECT_TRUE, and folly::CachelinePadded< T >::get().

128  {
129  CachelinePadded<int> padded;
130  EXPECT_TRUE(padded.get() == padded.operator->());
131  EXPECT_TRUE(&*padded == padded.get());
132  const auto constPadded = CachelinePadded<int>{};
133  EXPECT_TRUE(constPadded.get() == constPadded.operator->());
134  EXPECT_TRUE(constPadded.get() == &*constPadded.get());
135 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( CachelinePadded  ,
PropagatesConstness   
)

Definition at line 137 of file CachelinePaddedTest.cpp.

References EXPECT_EQ, and i.

137  {
138  struct OverloadedOnConst {
139  void assign(int* dst) {
140  *dst = 31415;
141  }
142  void assign(int* dst) const {
143  *dst = 271828;
144  }
145  };
146 
148 
149  int i = 0;
150  padded->assign(&i);
151  EXPECT_EQ(31415, i);
152 
153  const auto constPadded = CachelinePadded<OverloadedOnConst>{};
154  constPadded->assign(&i);
155  EXPECT_EQ(271828, i);
156 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( CachelinePadded  ,
ConstructsAndDestructs   
)

Definition at line 158 of file CachelinePaddedTest.cpp.

References EXPECT_EQ, and ptr.

158  {
159  enum LifetimeStatus {
160  kNone,
161  kConstructed,
162  kDestroyed,
163  };
164  struct WriteOnLifetimeOp {
165  explicit WriteOnLifetimeOp(LifetimeStatus* dst) : dst_(dst) {
166  *dst = kConstructed;
167  }
168  ~WriteOnLifetimeOp() {
169  *dst_ = kDestroyed;
170  }
171  LifetimeStatus* dst_;
172  };
173  LifetimeStatus status = kNone;
176  EXPECT_EQ(kConstructed, status);
177  delete ptr;
178  EXPECT_EQ(kDestroyed, status);
179 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( CachelinePadded  ,
ConstructsAndDestructsArrays   
)

Definition at line 181 of file CachelinePaddedTest.cpp.

References EXPECT_EQ, and ptr.

181  {
182  static thread_local int numConstructions;
183  static thread_local int numDestructions;
184  numConstructions = 0;
185  numDestructions = 0;
186  struct LifetimeCountingClass {
187  LifetimeCountingClass() {
188  ++numConstructions;
189  }
190  ~LifetimeCountingClass() {
191  ++numDestructions;
192  }
193  };
194  const static int kNumItems = 123;
197  EXPECT_EQ(kNumItems, numConstructions);
198  delete[] ptr;
199  EXPECT_EQ(kNumItems, numDestructions);
200 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( CachelinePadded  ,
ForwardsCorrectly   
)

Definition at line 202 of file CachelinePaddedTest.cpp.

References EXPECT_EQ.

202  {
203  struct RvalueOverloadedConstructor {
204  RvalueOverloadedConstructor(int* dst, int& /* ignored */) {
205  *dst = 0;
206  }
207  RvalueOverloadedConstructor(int* dst, int&& /* ignored */) {
208  *dst = 1;
209  }
210  };
211  int shouldBeZero = 12345;
212  int shouldBeOne = 67890;
213  {
214  int ignored = 42;
216  &shouldBeZero, ignored);
218  &shouldBeOne, static_cast<int&&>(ignored));
219  }
220  EXPECT_EQ(0, shouldBeZero);
221  EXPECT_EQ(1, shouldBeOne);
222 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TYPED_TEST ( CachelinePaddedTests  ,
alignment   
)

Definition at line 108 of file CachelinePaddedTest.cpp.

References EXPECT_EQ.

108  {
109  EXPECT_EQ(alignof(TypeParam), alignof(CachelinePadded<TypeParam>));
110 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TYPED_TEST ( CachelinePaddedTests  ,
integrity   
)

Definition at line 112 of file CachelinePaddedTest.cpp.

References folly::CachelinePadded< T >::get().

112  {
114  item.get()->doModifications();
115 }
TYPED_TEST ( CachelinePaddedTests  ,
size   
)

Definition at line 117 of file CachelinePaddedTest.cpp.

References EXPECT_EQ, EXPECT_GT, and kCachelineSize.

117  {
118  EXPECT_GT(
119  sizeof(TypeParam) + 2 * kCachelineSize,
121  size_t const rawSize = sizeof(TypeParam);
122  size_t const rawAlign = alignof(TypeParam);
123  size_t const expectedPadding = kCachelineSize - (rawAlign % kCachelineSize);
124  size_t const expectedPaddedSize = rawSize + 2 * expectedPadding;
125  EXPECT_EQ(expectedPaddedSize, sizeof(CachelinePadded<TypeParam>));
126 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static constexpr int kCachelineSize
#define EXPECT_GT(val1, val2)
Definition: gtest.h:1934
TYPED_TEST_CASE ( CachelinePaddedTests  ,
CachelinePaddedTypes   
)

Variable Documentation

constexpr int kCachelineSize
static
Initial value:
=
constexpr std::size_t hardware_destructive_interference_size
Definition: Align.h:107

Definition at line 30 of file CachelinePaddedTest.cpp.

Referenced by TYPED_TEST().