2020-02-07 22:24:41 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
|
2021-02-11 06:18:33 +00:00
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
|
2021-09-07 20:04:07 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2020-02-07 22:24:41 +00:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "port/stack_trace.h"
|
2021-02-11 06:18:33 +00:00
|
|
|
#include "rocksdb/data_structure.h"
|
|
|
|
#include "rocksdb/types.h"
|
2020-02-07 22:24:41 +00:00
|
|
|
#include "test_util/testharness.h"
|
|
|
|
#include "test_util/testutil.h"
|
2024-03-15 18:43:28 +00:00
|
|
|
#include "util/cast_util.h"
|
2020-02-07 22:24:41 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2020-02-07 22:24:41 +00:00
|
|
|
|
2022-02-05 01:12:03 +00:00
|
|
|
TEST(SliceTest, StringView) {
|
|
|
|
std::string s = "foo";
|
|
|
|
std::string_view sv = s;
|
|
|
|
ASSERT_EQ(Slice(s), Slice(sv));
|
|
|
|
ASSERT_EQ(Slice(s), Slice(std::move(sv)));
|
|
|
|
}
|
|
|
|
|
2020-02-07 22:24:41 +00:00
|
|
|
// Use this to keep track of the cleanups that were actually performed
|
|
|
|
void Multiplier(void* arg1, void* arg2) {
|
Prefer static_cast in place of most reinterpret_cast (#12308)
Summary:
The following are risks associated with pointer-to-pointer reinterpret_cast:
* Can produce the "wrong result" (crash or memory corruption). IIRC, in theory this can happen for any up-cast or down-cast for a non-standard-layout type, though in practice would only happen for multiple inheritance cases (where the base class pointer might be "inside" the derived object). We don't use multiple inheritance a lot, but we do.
* Can mask useful compiler errors upon code change, including converting between unrelated pointer types that you are expecting to be related, and converting between pointer and scalar types unintentionally.
I can only think of some obscure cases where static_cast could be troublesome when it compiles as a replacement:
* Going through `void*` could plausibly cause unnecessary or broken pointer arithmetic. Suppose we have
`struct Derived: public Base1, public Base2`. If we have `Derived*` -> `void*` -> `Base2*` -> `Derived*` through reinterpret casts, this could plausibly work (though technical UB) assuming the `Base2*` is not dereferenced. Changing to static cast could introduce breaking pointer arithmetic.
* Unnecessary (but safe) pointer arithmetic could arise in a case like `Derived*` -> `Base2*` -> `Derived*` where before the Base2 pointer might not have been dereferenced. This could potentially affect performance.
With some light scripting, I tried replacing pointer-to-pointer reinterpret_casts with static_cast and kept the cases that still compile. Most occurrences of reinterpret_cast have successfully been changed (except for java/ and third-party/). 294 changed, 257 remain.
A couple of related interventions included here:
* Previously Cache::Handle was not actually derived from in the implementations and just used as a `void*` stand-in with reinterpret_cast. Now there is a relationship to allow static_cast. In theory, this could introduce pointer arithmetic (as described above) but is unlikely without multiple inheritance AND non-empty Cache::Handle.
* Remove some unnecessary casts to void* as this is allowed to be implicit (for better or worse).
Most of the remaining reinterpret_casts are for converting to/from raw bytes of objects. We could consider better idioms for these patterns in follow-up work.
I wish there were a way to implement a template variant of static_cast that would only compile if no pointer arithmetic is generated, but best I can tell, this is not possible. AFAIK the best you could do is a dynamic check that the void* conversion after the static cast is unchanged.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/12308
Test Plan: existing tests, CI
Reviewed By: ltamasi
Differential Revision: D53204947
Pulled By: pdillinger
fbshipit-source-id: 9de23e618263b0d5b9820f4e15966876888a16e2
2024-02-07 18:44:11 +00:00
|
|
|
int* res = static_cast<int*>(arg1);
|
|
|
|
int* num = static_cast<int*>(arg2);
|
2020-02-07 22:24:41 +00:00
|
|
|
*res *= *num;
|
|
|
|
}
|
|
|
|
|
|
|
|
class PinnableSliceTest : public testing::Test {
|
|
|
|
public:
|
2022-10-26 19:08:20 +00:00
|
|
|
void AssertSameData(const std::string& expected, const PinnableSlice& slice) {
|
2020-02-07 22:24:41 +00:00
|
|
|
std::string got;
|
|
|
|
got.assign(slice.data(), slice.size());
|
|
|
|
ASSERT_EQ(expected, got);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-11 02:12:04 +00:00
|
|
|
// Test that the external buffer is moved instead of being copied.
|
|
|
|
TEST_F(PinnableSliceTest, MoveExternalBuffer) {
|
|
|
|
Slice s("123");
|
|
|
|
std::string buf;
|
|
|
|
PinnableSlice v1(&buf);
|
|
|
|
v1.PinSelf(s);
|
|
|
|
|
|
|
|
PinnableSlice v2(std::move(v1));
|
|
|
|
ASSERT_EQ(buf.data(), v2.data());
|
|
|
|
ASSERT_EQ(&buf, v2.GetSelf());
|
|
|
|
|
|
|
|
PinnableSlice v3;
|
|
|
|
v3 = std::move(v2);
|
|
|
|
ASSERT_EQ(buf.data(), v3.data());
|
|
|
|
ASSERT_EQ(&buf, v3.GetSelf());
|
|
|
|
}
|
|
|
|
|
2020-02-07 22:24:41 +00:00
|
|
|
TEST_F(PinnableSliceTest, Move) {
|
|
|
|
int n2 = 2;
|
|
|
|
int res = 1;
|
|
|
|
const std::string const_str1 = "123";
|
|
|
|
const std::string const_str2 = "ABC";
|
|
|
|
Slice slice1(const_str1);
|
|
|
|
Slice slice2(const_str2);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test move constructor on a pinned slice.
|
|
|
|
res = 1;
|
|
|
|
PinnableSlice v1;
|
|
|
|
v1.PinSlice(slice1, Multiplier, &res, &n2);
|
|
|
|
PinnableSlice v2(std::move(v1));
|
|
|
|
|
|
|
|
// Since v1's Cleanable has been moved to v2,
|
|
|
|
// no cleanup should happen in Reset.
|
|
|
|
v1.Reset();
|
|
|
|
ASSERT_EQ(1, res);
|
|
|
|
|
|
|
|
AssertSameData(const_str1, v2);
|
|
|
|
}
|
|
|
|
// v2 is cleaned up.
|
|
|
|
ASSERT_EQ(2, res);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test move constructor on an unpinned slice.
|
|
|
|
PinnableSlice v1;
|
|
|
|
v1.PinSelf(slice1);
|
|
|
|
PinnableSlice v2(std::move(v1));
|
|
|
|
|
|
|
|
AssertSameData(const_str1, v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test move assignment from a pinned slice to
|
|
|
|
// another pinned slice.
|
|
|
|
res = 1;
|
|
|
|
PinnableSlice v1;
|
|
|
|
v1.PinSlice(slice1, Multiplier, &res, &n2);
|
|
|
|
PinnableSlice v2;
|
|
|
|
v2.PinSlice(slice2, Multiplier, &res, &n2);
|
|
|
|
v2 = std::move(v1);
|
|
|
|
|
|
|
|
// v2's Cleanable will be Reset before moving
|
|
|
|
// anything from v1.
|
|
|
|
ASSERT_EQ(2, res);
|
|
|
|
// Since v1's Cleanable has been moved to v2,
|
|
|
|
// no cleanup should happen in Reset.
|
|
|
|
v1.Reset();
|
|
|
|
ASSERT_EQ(2, res);
|
|
|
|
|
|
|
|
AssertSameData(const_str1, v2);
|
|
|
|
}
|
|
|
|
// The Cleanable moved from v1 to v2 will be Reset.
|
|
|
|
ASSERT_EQ(4, res);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test move assignment from a pinned slice to
|
|
|
|
// an unpinned slice.
|
|
|
|
res = 1;
|
|
|
|
PinnableSlice v1;
|
|
|
|
v1.PinSlice(slice1, Multiplier, &res, &n2);
|
|
|
|
PinnableSlice v2;
|
|
|
|
v2.PinSelf(slice2);
|
|
|
|
v2 = std::move(v1);
|
|
|
|
|
|
|
|
// Since v1's Cleanable has been moved to v2,
|
|
|
|
// no cleanup should happen in Reset.
|
|
|
|
v1.Reset();
|
|
|
|
ASSERT_EQ(1, res);
|
|
|
|
|
|
|
|
AssertSameData(const_str1, v2);
|
|
|
|
}
|
|
|
|
// The Cleanable moved from v1 to v2 will be Reset.
|
|
|
|
ASSERT_EQ(2, res);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test move assignment from an upinned slice to
|
|
|
|
// another unpinned slice.
|
|
|
|
PinnableSlice v1;
|
|
|
|
v1.PinSelf(slice1);
|
|
|
|
PinnableSlice v2;
|
|
|
|
v2.PinSelf(slice2);
|
|
|
|
v2 = std::move(v1);
|
|
|
|
|
|
|
|
AssertSameData(const_str1, v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test move assignment from an upinned slice to
|
|
|
|
// a pinned slice.
|
|
|
|
res = 1;
|
|
|
|
PinnableSlice v1;
|
|
|
|
v1.PinSelf(slice1);
|
|
|
|
PinnableSlice v2;
|
|
|
|
v2.PinSlice(slice2, Multiplier, &res, &n2);
|
|
|
|
v2 = std::move(v1);
|
|
|
|
|
|
|
|
// v2's Cleanable will be Reset before moving
|
|
|
|
// anything from v1.
|
|
|
|
ASSERT_EQ(2, res);
|
|
|
|
|
|
|
|
AssertSameData(const_str1, v2);
|
|
|
|
}
|
|
|
|
// No Cleanable is moved from v1 to v2, so no more cleanup.
|
|
|
|
ASSERT_EQ(2, res);
|
|
|
|
}
|
|
|
|
|
2021-09-07 20:04:07 +00:00
|
|
|
// ***************************************************************** //
|
2021-02-11 06:18:33 +00:00
|
|
|
// Unit test for SmallEnumSet
|
|
|
|
class SmallEnumSetTest : public testing::Test {
|
|
|
|
public:
|
2024-03-04 18:08:32 +00:00
|
|
|
SmallEnumSetTest() = default;
|
|
|
|
~SmallEnumSetTest() = default;
|
2021-02-11 06:18:33 +00:00
|
|
|
};
|
|
|
|
|
2023-02-09 04:14:57 +00:00
|
|
|
TEST_F(SmallEnumSetTest, SmallEnumSetTest1) {
|
|
|
|
FileTypeSet fs; // based on a legacy enum type
|
|
|
|
ASSERT_TRUE(fs.empty());
|
2021-02-11 06:18:33 +00:00
|
|
|
ASSERT_TRUE(fs.Add(FileType::kIdentityFile));
|
2023-02-09 04:14:57 +00:00
|
|
|
ASSERT_FALSE(fs.empty());
|
2021-02-11 06:18:33 +00:00
|
|
|
ASSERT_FALSE(fs.Add(FileType::kIdentityFile));
|
|
|
|
ASSERT_TRUE(fs.Add(FileType::kInfoLogFile));
|
|
|
|
ASSERT_TRUE(fs.Contains(FileType::kIdentityFile));
|
|
|
|
ASSERT_FALSE(fs.Contains(FileType::kDBLockFile));
|
2023-02-09 04:14:57 +00:00
|
|
|
ASSERT_FALSE(fs.empty());
|
|
|
|
ASSERT_FALSE(fs.Remove(FileType::kDBLockFile));
|
|
|
|
ASSERT_TRUE(fs.Remove(FileType::kIdentityFile));
|
|
|
|
ASSERT_FALSE(fs.empty());
|
|
|
|
ASSERT_TRUE(fs.Remove(FileType::kInfoLogFile));
|
|
|
|
ASSERT_TRUE(fs.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
enum class MyEnumClass { A, B, C };
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
using MyEnumClassSet = SmallEnumSet<MyEnumClass, MyEnumClass::C>;
|
|
|
|
|
|
|
|
TEST_F(SmallEnumSetTest, SmallEnumSetTest2) {
|
|
|
|
MyEnumClassSet s; // based on an enum class type
|
|
|
|
ASSERT_TRUE(s.Add(MyEnumClass::A));
|
|
|
|
ASSERT_TRUE(s.Contains(MyEnumClass::A));
|
|
|
|
ASSERT_FALSE(s.Contains(MyEnumClass::B));
|
|
|
|
ASSERT_TRUE(s.With(MyEnumClass::B).Contains(MyEnumClass::B));
|
|
|
|
ASSERT_TRUE(s.With(MyEnumClass::A).Contains(MyEnumClass::A));
|
|
|
|
ASSERT_FALSE(s.Contains(MyEnumClass::B));
|
|
|
|
ASSERT_FALSE(s.Without(MyEnumClass::A).Contains(MyEnumClass::A));
|
|
|
|
ASSERT_FALSE(
|
|
|
|
s.With(MyEnumClass::B).Without(MyEnumClass::B).Contains(MyEnumClass::B));
|
|
|
|
ASSERT_TRUE(
|
|
|
|
s.Without(MyEnumClass::B).With(MyEnumClass::B).Contains(MyEnumClass::B));
|
|
|
|
ASSERT_TRUE(s.Contains(MyEnumClass::A));
|
|
|
|
|
|
|
|
const MyEnumClassSet cs = s;
|
|
|
|
ASSERT_TRUE(cs.Contains(MyEnumClass::A));
|
|
|
|
ASSERT_EQ(cs, MyEnumClassSet{MyEnumClass::A});
|
|
|
|
ASSERT_EQ(cs.Without(MyEnumClass::A), MyEnumClassSet{});
|
|
|
|
ASSERT_EQ(cs, MyEnumClassSet::All().Without(MyEnumClass::B, MyEnumClass::C));
|
|
|
|
ASSERT_EQ(cs.With(MyEnumClass::B, MyEnumClass::C), MyEnumClassSet::All());
|
|
|
|
ASSERT_EQ(
|
|
|
|
MyEnumClassSet::All(),
|
|
|
|
MyEnumClassSet{}.With(MyEnumClass::A, MyEnumClass::B, MyEnumClass::C));
|
|
|
|
ASSERT_NE(cs, MyEnumClassSet{MyEnumClass::B});
|
|
|
|
ASSERT_NE(cs, MyEnumClassSet::All());
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
for (MyEnumClass e : cs) {
|
|
|
|
ASSERT_EQ(e, MyEnumClass::A);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(count, 1);
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
for (MyEnumClass e : MyEnumClassSet::All().Without(MyEnumClass::B)) {
|
|
|
|
ASSERT_NE(e, MyEnumClass::B);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(count, 2);
|
|
|
|
|
|
|
|
for (MyEnumClass e : MyEnumClassSet{}) {
|
|
|
|
(void)e;
|
|
|
|
assert(false);
|
|
|
|
}
|
2021-02-11 06:18:33 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 21:14:22 +00:00
|
|
|
// ***************************************************************** //
|
|
|
|
// Unit test for Status
|
|
|
|
TEST(StatusTest, Update) {
|
|
|
|
const Status ok = Status::OK();
|
|
|
|
const Status inc = Status::Incomplete("blah");
|
|
|
|
const Status notf = Status::NotFound("meow");
|
|
|
|
|
|
|
|
Status s = ok;
|
|
|
|
ASSERT_TRUE(s.UpdateIfOk(Status::Corruption("bad")).IsCorruption());
|
|
|
|
ASSERT_TRUE(s.IsCorruption());
|
|
|
|
|
|
|
|
s = ok;
|
|
|
|
ASSERT_TRUE(s.UpdateIfOk(Status::OK()).ok());
|
|
|
|
ASSERT_TRUE(s.UpdateIfOk(ok).ok());
|
|
|
|
ASSERT_TRUE(s.ok());
|
|
|
|
|
|
|
|
ASSERT_TRUE(s.UpdateIfOk(inc).IsIncomplete());
|
|
|
|
ASSERT_TRUE(s.IsIncomplete());
|
|
|
|
|
|
|
|
ASSERT_TRUE(s.UpdateIfOk(notf).IsIncomplete());
|
|
|
|
ASSERT_TRUE(s.UpdateIfOk(ok).IsIncomplete());
|
|
|
|
ASSERT_TRUE(s.IsIncomplete());
|
|
|
|
|
|
|
|
// Keeps left-most non-OK status
|
|
|
|
s = ok;
|
|
|
|
ASSERT_TRUE(
|
|
|
|
s.UpdateIfOk(Status()).UpdateIfOk(notf).UpdateIfOk(inc).IsNotFound());
|
|
|
|
ASSERT_TRUE(s.IsNotFound());
|
|
|
|
}
|
|
|
|
|
2024-03-15 18:43:28 +00:00
|
|
|
// ***************************************************************** //
|
|
|
|
// Unit test for UnownedPtr
|
|
|
|
TEST(UnownedPtrTest, Tests) {
|
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
UnownedPtr<int> p(&x);
|
|
|
|
ASSERT_EQ(p.get(), &x);
|
|
|
|
ASSERT_EQ(*p, 0);
|
|
|
|
x = 1;
|
|
|
|
ASSERT_EQ(*p, 1);
|
|
|
|
ASSERT_EQ(p.get(), &x);
|
|
|
|
ASSERT_EQ(*p, 1);
|
|
|
|
*p = 2;
|
|
|
|
ASSERT_EQ(x, 2);
|
|
|
|
ASSERT_EQ(*p, 2);
|
|
|
|
ASSERT_EQ(p.get(), &x);
|
|
|
|
ASSERT_EQ(*p, 2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::unique_ptr<std::pair<int, int>> u =
|
|
|
|
std::make_unique<std::pair<int, int>>();
|
|
|
|
*u = {1, 2};
|
|
|
|
UnownedPtr<std::pair<int, int>> p;
|
|
|
|
ASSERT_FALSE(p);
|
|
|
|
p = u.get();
|
|
|
|
ASSERT_TRUE(p);
|
|
|
|
ASSERT_EQ(p->first, 1);
|
|
|
|
// These must not compile:
|
|
|
|
/*
|
|
|
|
u = p;
|
|
|
|
u = std::move(p);
|
|
|
|
std::unique_ptr<std::pair<int, int>> v{p};
|
|
|
|
std::unique_ptr<std::pair<int, int>> v{std::move(p)};
|
|
|
|
*/
|
|
|
|
// END must not compile
|
|
|
|
|
|
|
|
UnownedPtr<std::pair<int, int>> q;
|
|
|
|
q = std::move(p);
|
|
|
|
ASSERT_EQ(q->first, 1);
|
|
|
|
// Not committing to any moved-from semantics (on p here)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::shared_ptr<std::pair<int, int>> s =
|
|
|
|
std::make_shared<std::pair<int, int>>();
|
|
|
|
*s = {1, 2};
|
|
|
|
UnownedPtr<std::pair<int, int>> p;
|
|
|
|
ASSERT_FALSE(p);
|
|
|
|
p = s.get();
|
|
|
|
ASSERT_TRUE(p);
|
|
|
|
ASSERT_EQ(p->first, 1);
|
|
|
|
// These must not compile:
|
|
|
|
/*
|
|
|
|
s = p;
|
|
|
|
s = std::move(p);
|
|
|
|
std::unique_ptr<std::pair<int, int>> t{p};
|
|
|
|
std::unique_ptr<std::pair<int, int>> t{std::move(p)};
|
|
|
|
*/
|
|
|
|
// END must not compile
|
|
|
|
UnownedPtr<std::pair<int, int>> q;
|
|
|
|
q = std::move(p);
|
|
|
|
ASSERT_EQ(q->first, 1);
|
|
|
|
// Not committing to any moved-from semantics (on p here)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2020-02-07 22:24:41 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2020-02-07 22:24:41 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|