2 * Copyright 2017-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/experimental/StampedPtr.h>
19 #include <folly/portability/GTest.h>
21 TEST(StampedPtr, Basic) {
22 folly::StampedPtr<char> sp;
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);
32 uint64_t raw = sp.raw;
34 EXPECT_NE(sp.stamp(), 11);
36 EXPECT_EQ(sp.stamp(), 11);
37 EXPECT_EQ(sp.ptr(), target + 1);
40 TEST(StampedPtr, Zero) {
41 folly::StampedPtr<char> sp{0};
42 EXPECT_TRUE(sp.ptr() == nullptr);
43 EXPECT_EQ(sp.stamp(), 0);
46 TEST(StampedPtr, Void) {
47 folly::StampedPtr<void> sp;
50 EXPECT_EQ(sp.ptr(), target);
53 TEST(StampedPtr, Make) {
54 auto sp = folly::makeStampedPtr("abc", 0xffff);
55 EXPECT_EQ(*sp.ptr(), 'a');
56 EXPECT_EQ(sp.stamp(), 0xffff);
59 auto sp2 = folly::makeStampedPtr(&x, 0);
60 EXPECT_EQ(sp2.ptr(), &x);
61 EXPECT_EQ(sp2.stamp(), 0);
64 TEST(StampedPtr, Const) {
65 folly::StampedPtr<const char> sp{};
70 TEST(StampedPtr, BitExtension) {
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);
77 folly::StampedPtr<char> sp{0};
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));