From a4ce49aef4367032a0cf0722946a0d8632650ff2 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Fri, 22 Jul 2016 14:07:06 +0200 Subject: [PATCH] Fix unique_ref test cases --- .../onblocks/datatreestore/impl/LeafTraverser.cpp | 3 ++- src/cpp-utils/pointer/unique_ref.h | 11 ++++++++--- test/cpp-utils/pointer/cast_test.cpp | 4 ++-- test/cpp-utils/pointer/unique_ref_test.cpp | 12 ++++++------ 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/blobstore/implementations/onblocks/datatreestore/impl/LeafTraverser.cpp b/src/blobstore/implementations/onblocks/datatreestore/impl/LeafTraverser.cpp index ba08bfba..cc46de10 100644 --- a/src/blobstore/implementations/onblocks/datatreestore/impl/LeafTraverser.cpp +++ b/src/blobstore/implementations/onblocks/datatreestore/impl/LeafTraverser.cpp @@ -163,7 +163,8 @@ namespace blobstore { ASSERT(beginIndex <= endIndex, "Invalid parameters"); 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)."); - auto leafCreator = (beginIndex == 0) ? onCreateLeaf : _createMaxSizeLeaf(); + auto leafCreator = (beginIndex + == 0) ? onCreateLeaf : _createMaxSizeLeaf(); return _nodeStore->createNewLeafNode(leafCreator(leafOffset)); } diff --git a/src/cpp-utils/pointer/unique_ref.h b/src/cpp-utils/pointer/unique_ref.h index 88beb2da..61440282 100644 --- a/src/cpp-utils/pointer/unique_ref.h +++ b/src/cpp-utils/pointer/unique_ref.h @@ -82,6 +82,7 @@ private: template friend class unique_ref; template friend boost::optional> dynamic_pointer_move(unique_ref &source); template friend std::unique_ptr to_unique_ptr(unique_ref ref); + template friend U* _extract_ptr(const unique_ref &obj); std::unique_ptr _target; @@ -105,6 +106,10 @@ template inline void destruct(unique_ref ptr) { to_unique_ptr(std::move(ptr)).reset(); } +template T* _extract_ptr(const unique_ref &obj) { + return obj._target.get(); +} + //TODO Also allow passing a rvalue reference, otherwise dynamic_pointer_move(func()) won't work template inline boost::optional> dynamic_pointer_move(unique_ref &source) { @@ -119,7 +124,7 @@ inline std::unique_ptr to_unique_ptr(unique_ref ref) { template inline bool operator==(const unique_ref &lhs, const unique_ref &rhs) { - return lhs.get() == rhs.get(); + return _extract_ptr(lhs) == _extract_ptr(rhs); } template @@ -148,14 +153,14 @@ namespace std { // Allow using it in std::unordered_set / std::unordered_map template struct hash> { size_t operator()(const cpputils::unique_ref &ref) const { - return (size_t)ref.get(); + return (size_t)_extract_ptr(ref); } }; // Allow using it in std::map / std::set template struct less> { bool operator()(const cpputils::unique_ref &lhs, const cpputils::unique_ref &rhs) const { - return lhs.get() < rhs.get(); + return _extract_ptr(lhs) < _extract_ptr(rhs); } }; } diff --git a/test/cpp-utils/pointer/cast_test.cpp b/test/cpp-utils/pointer/cast_test.cpp index ec2eeb9a..8855f3ee 100644 --- a/test/cpp-utils/pointer/cast_test.cpp +++ b/test/cpp-utils/pointer/cast_test.cpp @@ -67,7 +67,7 @@ TEST(UniqueRef_DynamicPointerMoveTest, ValidParentToChildCast) { Child *obj = new Child(); unique_ref source(nullcheck(unique_ptr(obj)).value()); unique_ref casted = dynamic_pointer_move(source).value(); - EXPECT_EQ(nullptr, source.get()); // source lost ownership + EXPECT_FALSE(source.isValid()); // source lost ownership EXPECT_EQ(obj, casted.get()); } @@ -115,7 +115,7 @@ TEST(UniqueRef_DynamicPointerMoveTest, ChildToParentCast) { Child *obj = new Child(); unique_ref source(nullcheck(unique_ptr(obj)).value()); unique_ref casted = dynamic_pointer_move(source).value(); - EXPECT_EQ(nullptr, source.get()); // source lost ownership + EXPECT_FALSE(source.isValid()); // source lost ownership EXPECT_EQ(obj, casted.get()); } diff --git a/test/cpp-utils/pointer/unique_ref_test.cpp b/test/cpp-utils/pointer/unique_ref_test.cpp index 53eb066e..4b12d656 100644 --- a/test/cpp-utils/pointer/unique_ref_test.cpp +++ b/test/cpp-utils/pointer/unique_ref_test.cpp @@ -149,7 +149,7 @@ TEST_F(UniqueRefTest, Assignment) { SomeClass *obj1ptr = obj1.get(); obj2 = std::move(obj1); EXPECT_EQ(obj1ptr, obj2.get()); - EXPECT_EQ(nullptr, obj1.get()); + EXPECT_FALSE(obj1.isValid()); } TEST_F(UniqueRefTest, MoveConstructor) { @@ -157,7 +157,7 @@ TEST_F(UniqueRefTest, MoveConstructor) { SomeClass *obj1ptr = obj1.get(); unique_ref obj2 = std::move(obj1); EXPECT_EQ(obj1ptr, obj2.get()); - EXPECT_EQ(nullptr, obj1.get()); + EXPECT_FALSE(obj1.isValid()); } TEST_F(UniqueRefTest, Swap) { @@ -177,7 +177,7 @@ TEST_F(UniqueRefTest, SwapFromInvalid) { SomeClass *obj2ptr = obj2.get(); std::swap(obj1, obj2); EXPECT_EQ(obj2ptr, obj1.get()); - EXPECT_EQ(nullptr, obj2.get()); + EXPECT_FALSE(obj2.isValid()); } TEST_F(UniqueRefTest, SwapWithInvalid) { @@ -186,7 +186,7 @@ TEST_F(UniqueRefTest, SwapWithInvalid) { makeInvalid(std::move(obj2)); SomeClass *obj1ptr = obj1.get(); std::swap(obj1, obj2); - EXPECT_EQ(nullptr, obj1.get()); + EXPECT_FALSE(obj1.isValid()); EXPECT_EQ(obj1ptr, obj2.get()); } @@ -196,8 +196,8 @@ TEST_F(UniqueRefTest, SwapInvalidWithInvalid) { makeInvalid(std::move(obj1)); makeInvalid(std::move(obj2)); std::swap(obj1, obj2); - EXPECT_EQ(nullptr, obj1.get()); - EXPECT_EQ(nullptr, obj2.get()); + EXPECT_FALSE(obj1.isValid()); + EXPECT_FALSE(obj2.isValid()); } TEST_F(UniqueRefTest, SwapFromRValue) {