Fix unique_ref test cases
This commit is contained in:
parent
271cb0c42d
commit
a4ce49aef4
@ -163,7 +163,8 @@ namespace blobstore {
|
|||||||
ASSERT(beginIndex <= endIndex, "Invalid parameters");
|
ASSERT(beginIndex <= endIndex, "Invalid parameters");
|
||||||
if (0 == depth) {
|
if (0 == depth) {
|
||||||
ASSERT(beginIndex <= 1 && endIndex == 1, "With depth 0, we can only traverse one or zero leaves (i.e. traverse one leaf or traverse a gap leaf).");
|
ASSERT(beginIndex <= 1 && endIndex == 1, "With depth 0, we can only traverse one or zero leaves (i.e. traverse one leaf or traverse a gap leaf).");
|
||||||
auto leafCreator = (beginIndex == 0) ? onCreateLeaf : _createMaxSizeLeaf();
|
auto leafCreator = (beginIndex
|
||||||
|
== 0) ? onCreateLeaf : _createMaxSizeLeaf();
|
||||||
return _nodeStore->createNewLeafNode(leafCreator(leafOffset));
|
return _nodeStore->createNewLeafNode(leafCreator(leafOffset));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,6 +82,7 @@ private:
|
|||||||
template<typename U> friend class unique_ref;
|
template<typename U> friend class unique_ref;
|
||||||
template<typename DST, typename SRC> friend boost::optional<unique_ref<DST>> dynamic_pointer_move(unique_ref<SRC> &source);
|
template<typename DST, typename SRC> friend boost::optional<unique_ref<DST>> dynamic_pointer_move(unique_ref<SRC> &source);
|
||||||
template<typename U> friend std::unique_ptr<U> to_unique_ptr(unique_ref<U> ref);
|
template<typename U> friend std::unique_ptr<U> to_unique_ptr(unique_ref<U> ref);
|
||||||
|
template<class U> friend U* _extract_ptr(const unique_ref<U> &obj);
|
||||||
|
|
||||||
std::unique_ptr<T> _target;
|
std::unique_ptr<T> _target;
|
||||||
|
|
||||||
@ -105,6 +106,10 @@ template<typename T> inline void destruct(unique_ref<T> ptr) {
|
|||||||
to_unique_ptr(std::move(ptr)).reset();
|
to_unique_ptr(std::move(ptr)).reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T> T* _extract_ptr(const unique_ref<T> &obj) {
|
||||||
|
return obj._target.get();
|
||||||
|
}
|
||||||
|
|
||||||
//TODO Also allow passing a rvalue reference, otherwise dynamic_pointer_move(func()) won't work
|
//TODO Also allow passing a rvalue reference, otherwise dynamic_pointer_move(func()) won't work
|
||||||
template<typename DST, typename SRC>
|
template<typename DST, typename SRC>
|
||||||
inline boost::optional<unique_ref<DST>> dynamic_pointer_move(unique_ref<SRC> &source) {
|
inline boost::optional<unique_ref<DST>> dynamic_pointer_move(unique_ref<SRC> &source) {
|
||||||
@ -119,7 +124,7 @@ inline std::unique_ptr<T> to_unique_ptr(unique_ref<T> ref) {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline bool operator==(const unique_ref<T> &lhs, const unique_ref<T> &rhs) {
|
inline bool operator==(const unique_ref<T> &lhs, const unique_ref<T> &rhs) {
|
||||||
return lhs.get() == rhs.get();
|
return _extract_ptr(lhs) == _extract_ptr(rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -148,14 +153,14 @@ namespace std {
|
|||||||
// Allow using it in std::unordered_set / std::unordered_map
|
// Allow using it in std::unordered_set / std::unordered_map
|
||||||
template<typename T> struct hash<cpputils::unique_ref<T>> {
|
template<typename T> struct hash<cpputils::unique_ref<T>> {
|
||||||
size_t operator()(const cpputils::unique_ref<T> &ref) const {
|
size_t operator()(const cpputils::unique_ref<T> &ref) const {
|
||||||
return (size_t)ref.get();
|
return (size_t)_extract_ptr(ref);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Allow using it in std::map / std::set
|
// Allow using it in std::map / std::set
|
||||||
template <typename T> struct less<cpputils::unique_ref<T>> {
|
template <typename T> struct less<cpputils::unique_ref<T>> {
|
||||||
bool operator()(const cpputils::unique_ref<T> &lhs, const cpputils::unique_ref<T> &rhs) const {
|
bool operator()(const cpputils::unique_ref<T> &lhs, const cpputils::unique_ref<T> &rhs) const {
|
||||||
return lhs.get() < rhs.get();
|
return _extract_ptr(lhs) < _extract_ptr(rhs);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ TEST(UniqueRef_DynamicPointerMoveTest, ValidParentToChildCast) {
|
|||||||
Child *obj = new Child();
|
Child *obj = new Child();
|
||||||
unique_ref<Parent> source(nullcheck(unique_ptr<Parent>(obj)).value());
|
unique_ref<Parent> source(nullcheck(unique_ptr<Parent>(obj)).value());
|
||||||
unique_ref<Child> casted = dynamic_pointer_move<Child>(source).value();
|
unique_ref<Child> casted = dynamic_pointer_move<Child>(source).value();
|
||||||
EXPECT_EQ(nullptr, source.get()); // source lost ownership
|
EXPECT_FALSE(source.isValid()); // source lost ownership
|
||||||
EXPECT_EQ(obj, casted.get());
|
EXPECT_EQ(obj, casted.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +115,7 @@ TEST(UniqueRef_DynamicPointerMoveTest, ChildToParentCast) {
|
|||||||
Child *obj = new Child();
|
Child *obj = new Child();
|
||||||
unique_ref<Child> source(nullcheck(unique_ptr<Child>(obj)).value());
|
unique_ref<Child> source(nullcheck(unique_ptr<Child>(obj)).value());
|
||||||
unique_ref<Parent> casted = dynamic_pointer_move<Parent>(source).value();
|
unique_ref<Parent> casted = dynamic_pointer_move<Parent>(source).value();
|
||||||
EXPECT_EQ(nullptr, source.get()); // source lost ownership
|
EXPECT_FALSE(source.isValid()); // source lost ownership
|
||||||
EXPECT_EQ(obj, casted.get());
|
EXPECT_EQ(obj, casted.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,7 +149,7 @@ TEST_F(UniqueRefTest, Assignment) {
|
|||||||
SomeClass *obj1ptr = obj1.get();
|
SomeClass *obj1ptr = obj1.get();
|
||||||
obj2 = std::move(obj1);
|
obj2 = std::move(obj1);
|
||||||
EXPECT_EQ(obj1ptr, obj2.get());
|
EXPECT_EQ(obj1ptr, obj2.get());
|
||||||
EXPECT_EQ(nullptr, obj1.get());
|
EXPECT_FALSE(obj1.isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(UniqueRefTest, MoveConstructor) {
|
TEST_F(UniqueRefTest, MoveConstructor) {
|
||||||
@ -157,7 +157,7 @@ TEST_F(UniqueRefTest, MoveConstructor) {
|
|||||||
SomeClass *obj1ptr = obj1.get();
|
SomeClass *obj1ptr = obj1.get();
|
||||||
unique_ref<SomeClass> obj2 = std::move(obj1);
|
unique_ref<SomeClass> obj2 = std::move(obj1);
|
||||||
EXPECT_EQ(obj1ptr, obj2.get());
|
EXPECT_EQ(obj1ptr, obj2.get());
|
||||||
EXPECT_EQ(nullptr, obj1.get());
|
EXPECT_FALSE(obj1.isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(UniqueRefTest, Swap) {
|
TEST_F(UniqueRefTest, Swap) {
|
||||||
@ -177,7 +177,7 @@ TEST_F(UniqueRefTest, SwapFromInvalid) {
|
|||||||
SomeClass *obj2ptr = obj2.get();
|
SomeClass *obj2ptr = obj2.get();
|
||||||
std::swap(obj1, obj2);
|
std::swap(obj1, obj2);
|
||||||
EXPECT_EQ(obj2ptr, obj1.get());
|
EXPECT_EQ(obj2ptr, obj1.get());
|
||||||
EXPECT_EQ(nullptr, obj2.get());
|
EXPECT_FALSE(obj2.isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(UniqueRefTest, SwapWithInvalid) {
|
TEST_F(UniqueRefTest, SwapWithInvalid) {
|
||||||
@ -186,7 +186,7 @@ TEST_F(UniqueRefTest, SwapWithInvalid) {
|
|||||||
makeInvalid(std::move(obj2));
|
makeInvalid(std::move(obj2));
|
||||||
SomeClass *obj1ptr = obj1.get();
|
SomeClass *obj1ptr = obj1.get();
|
||||||
std::swap(obj1, obj2);
|
std::swap(obj1, obj2);
|
||||||
EXPECT_EQ(nullptr, obj1.get());
|
EXPECT_FALSE(obj1.isValid());
|
||||||
EXPECT_EQ(obj1ptr, obj2.get());
|
EXPECT_EQ(obj1ptr, obj2.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,8 +196,8 @@ TEST_F(UniqueRefTest, SwapInvalidWithInvalid) {
|
|||||||
makeInvalid(std::move(obj1));
|
makeInvalid(std::move(obj1));
|
||||||
makeInvalid(std::move(obj2));
|
makeInvalid(std::move(obj2));
|
||||||
std::swap(obj1, obj2);
|
std::swap(obj1, obj2);
|
||||||
EXPECT_EQ(nullptr, obj1.get());
|
EXPECT_FALSE(obj1.isValid());
|
||||||
EXPECT_EQ(nullptr, obj2.get());
|
EXPECT_FALSE(obj2.isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(UniqueRefTest, SwapFromRValue) {
|
TEST_F(UniqueRefTest, SwapFromRValue) {
|
||||||
|
Loading…
Reference in New Issue
Block a user