proxygen
StampedPtrTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
18 
20 
21 TEST(StampedPtr, Basic) {
23  char target[10];
24  sp.set(target, 10);
25  EXPECT_EQ(sp.ptr(), target);
26  EXPECT_EQ(sp.stamp(), 10);
27  sp.setStamp(sp.stamp() + 1);
28  EXPECT_EQ(sp.stamp(), 11);
29  sp.setPtr(target + 1);
30  EXPECT_EQ(sp.ptr(), target + 1);
31 
32  uint64_t raw = sp.raw;
33  sp.raw = 0;
34  EXPECT_NE(sp.stamp(), 11);
35  sp.raw = raw;
36  EXPECT_EQ(sp.stamp(), 11);
37  EXPECT_EQ(sp.ptr(), target + 1);
38 }
39 
40 TEST(StampedPtr, Zero) {
42  EXPECT_TRUE(sp.ptr() == nullptr);
43  EXPECT_EQ(sp.stamp(), 0);
44 }
45 
46 TEST(StampedPtr, Void) {
48  char target[10];
49  sp.set(target, 10);
50  EXPECT_EQ(sp.ptr(), target);
51 }
52 
53 TEST(StampedPtr, Make) {
54  auto sp = folly::makeStampedPtr("abc", 0xffff);
55  EXPECT_EQ(*sp.ptr(), 'a');
56  EXPECT_EQ(sp.stamp(), 0xffff);
57 
58  double x;
59  auto sp2 = folly::makeStampedPtr(&x, 0);
60  EXPECT_EQ(sp2.ptr(), &x);
61  EXPECT_EQ(sp2.stamp(), 0);
62 }
63 
64 TEST(StampedPtr, Const) {
66  char target;
67  sp.setPtr(&target);
68 }
69 
70 TEST(StampedPtr, BitExtension) {
71  // fedcba9876543210
72  auto lo = static_cast<uintptr_t>(0x00007fff672333ecLL);
73  auto hi = static_cast<uintptr_t>(0xfffffffff72333ecLL);
74  ASSERT_TRUE(static_cast<intptr_t>(lo) > 0);
75  ASSERT_TRUE(static_cast<intptr_t>(hi) < 0);
76 
78  sp.setPtr(reinterpret_cast<char*>(lo));
79  EXPECT_EQ(sp.ptr(), reinterpret_cast<char*>(lo));
80  sp.setPtr(reinterpret_cast<char*>(hi));
81  EXPECT_EQ(sp.ptr(), reinterpret_cast<char*>(hi));
82 }
void setPtr(T *ptr)
Definition: StampedPtr.h:84
StampedPtr< T > makeStampedPtr(T *ptr, uint16_t stamp)
Definition: StampedPtr.h:124
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
uint16_t stamp() const
Definition: StampedPtr.h:76
T * ptr() const
Definition: StampedPtr.h:72
TEST(StampedPtr, Basic)
void set(T *ptr, uint16_t stamp)
Definition: StampedPtr.h:80
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
void setStamp(uint16_t stamp)
Definition: StampedPtr.h:88