From 3952ff5c343844d76f75ede0afc8ddad55748a0d Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Tue, 24 Jul 2018 21:25:21 +0300 Subject: [core] Replace unique_any with peer from mapbox-bindgen --- test/util/peer.test.cpp | 194 +++++++++++++++++++++++++++++++++++++ test/util/unique_any.test.cpp | 218 ------------------------------------------ 2 files changed, 194 insertions(+), 218 deletions(-) create mode 100644 test/util/peer.test.cpp delete mode 100644 test/util/unique_any.test.cpp (limited to 'test') diff --git a/test/util/peer.test.cpp b/test/util/peer.test.cpp new file mode 100644 index 0000000000..aa4dae5a88 --- /dev/null +++ b/test/util/peer.test.cpp @@ -0,0 +1,194 @@ +#include + +#include + +using namespace mbgl::util; + +class TestType { +public: + TestType() : i1(0), i2(1) { + str[0] = 'a'; + } + + //Detect moves + TestType(TestType&& t): i1(t.i1+1), i2(t.i2+2) { + str[0] = t.str[0]+1; + } + + TestType(const TestType&) = delete; + TestType& operator=(const TestType&) = delete; + + int i1; + int i2; + char str[256]; +}; + +bool IsStackAllocated (const peer& a, const void* obj1) { + uintptr_t a_ptr = (uintptr_t)(&a); + uintptr_t obj = (uintptr_t)(obj1); + return (obj >= a_ptr && obj < a_ptr + sizeof(peer)); +}; + +TEST(Peer, Empty) { + EXPECT_FALSE(peer().has_value()); +} + +TEST(Peer, BasicTypes) { + peer i = 3; + EXPECT_TRUE(i.has_value()); + EXPECT_TRUE(i.get() == 3); + + auto iValue = i.get(); + EXPECT_TRUE(iValue == 3); + + EXPECT_TRUE(peer(4).has_value()); + EXPECT_TRUE(peer(4).get() == 4); + + peer f = 6.2f; + EXPECT_TRUE(f.has_value()); + EXPECT_TRUE(f.get() == 6.2f); + + const float fValue = f.get(); + EXPECT_TRUE(fValue == 6.2f); + + EXPECT_TRUE(peer(1.0f).has_value()); + EXPECT_TRUE(peer(1.0f).get() == 1.0f); + + peer c = 'z'; + EXPECT_TRUE(c.has_value()); + EXPECT_TRUE(c.get() == 'z'); + + EXPECT_TRUE(peer('z').has_value()); + EXPECT_TRUE(peer('z').get() == 'z'); +} + +TEST(Peer, BasicTypes_Move) { + peer i = 3; + EXPECT_TRUE(i.has_value()); + + peer f = 6.2f; + EXPECT_TRUE(f.has_value()); + + f = std::move(i); + EXPECT_FALSE(i.has_value()); + + EXPECT_TRUE(f.has_value()); + EXPECT_TRUE(f.get() == 3); +} + +TEST(Peer, SmallType) { + struct T { + T(int32_t* p_) : p(p_) { + (*p)++; + } + + T(T&& t) noexcept : p(t.p) { + (*p)++; + } + + ~T() { + (*p)--; + } + + T(const T&) = delete; + T& operator=(const T&) = delete; + + int32_t* p; + }; + + int32_t p = 0; + + { + peer u1 = peer(T(&p)); + EXPECT_EQ(p, 1); + + auto u2(std::move(u1)); + EXPECT_EQ(p, 1); + } + + EXPECT_EQ(p, 0); +} + +// peer is not able to receive large types, unless we increase storage_t +// capacity. +//TEST(Peer, LargeType) { +// TestType t1; +// peer u1 = peer(std::move(t1)); +// EXPECT_TRUE(u1.has_value()); +// +// //TestType should be moved into owning peer +// EXPECT_EQ(u1.get().i1, 1); +// +// auto u2(std::move(u1)); +// EXPECT_FALSE(u1.has_value()); +// +// //TestType should not be moved when owning peer is moved; +// EXPECT_EQ(u2.get().i1, 1); +// +// //TestType should be moved out of owning peer +// // Note: two moves are involved in returning the moved value +// // First out of the peer, and then in the return statement +// auto t2 = std::move(u2.get()); +// EXPECT_FALSE(u2.has_value()); +// EXPECT_EQ(t2.i1, 3); +//} + +TEST(Peer, Pointer) { + auto t1 = new TestType(); + + auto u1 = peer(std::move(t1)); + EXPECT_TRUE(u1.has_value()); + + //Only the pointer should be moved + TestType * t2 = u1.get(); + EXPECT_EQ(t2->i1, 0); + + peer u2(4); + std::swap(u2, u1); + + EXPECT_TRUE(u1.has_value()); + + EXPECT_TRUE(u2.has_value()); + + t2 = u2.get(); + EXPECT_EQ(t2->i1, 0); + delete t2; +} + + +TEST(Peer, UniquePtr) { + auto t1 = std::make_unique(); + auto u1 = peer(std::move(t1)); + + EXPECT_EQ(t1.get(), nullptr); + EXPECT_TRUE(u1.has_value()); + + auto t2 = std::move(u1.take()); + EXPECT_FALSE(u1.has_value()); + (void)t2; + + peer u2; + TestType * t3 = new TestType(); + u2 = std::unique_ptr(t3); + EXPECT_TRUE(u2.has_value()); +} + +TEST(Peer, SharedPtr) { + + std::shared_ptr shared(new int(3)); + std::weak_ptr weak = shared; + peer u1 = 0; + + EXPECT_EQ(weak.use_count(), 1); + peer u2 = shared; + EXPECT_EQ(weak.use_count(), 2); + + u1 = std::move(u2); + EXPECT_EQ(weak.use_count(), 2); + u2.swap(u1); + EXPECT_EQ(weak.use_count(), 2); + u2 = 0; + EXPECT_EQ(weak.use_count(), 1); + shared = nullptr; + EXPECT_EQ(weak.use_count(), 0); +} diff --git a/test/util/unique_any.test.cpp b/test/util/unique_any.test.cpp deleted file mode 100644 index 9b622cd284..0000000000 --- a/test/util/unique_any.test.cpp +++ /dev/null @@ -1,218 +0,0 @@ -#include - -#include - -using namespace mbgl::util; - -class TestType { -public: - TestType() : i1(0), i2(1) { - str[0] = 'a'; - } - - //Detect moves - TestType(TestType&& t): i1(t.i1+1), i2(t.i2+2) { - str[0] = t.str[0]+1; - } - - TestType(const TestType&) = delete; - TestType& operator=(const TestType&) = delete; - - int i1; - int i2; - char str[256]; -}; - -bool IsStackAllocated (const unique_any& a, const void* obj1) { - uintptr_t a_ptr = (uintptr_t)(&a); - uintptr_t obj = (uintptr_t)(obj1); - return (obj >= a_ptr && obj < a_ptr + sizeof(unique_any)); -}; - -TEST(UniqueAny, Empty) { - EXPECT_FALSE(unique_any().has_value()); - EXPECT_TRUE(unique_any().type() == typeid(void)); - EXPECT_THROW(any_cast(unique_any()), bad_any_cast); -} - -TEST(UniqueAny, BasicTypes) { - unique_any i = 3; - EXPECT_TRUE(i.has_value()); - EXPECT_TRUE(i.type() == typeid(int)); - EXPECT_TRUE(IsStackAllocated(i, any_cast(&i))); - - auto iValue = any_cast(i); - EXPECT_TRUE(iValue == 3); - - EXPECT_TRUE(unique_any(4).has_value()); - EXPECT_TRUE(unique_any(4).type() == typeid(int)); - - unique_any f = 6.2f; - EXPECT_TRUE(f.has_value()); - EXPECT_TRUE(f.type() == typeid(float)); - EXPECT_TRUE(IsStackAllocated(f, any_cast(&f))); - - const float fValue = any_cast(f); - EXPECT_TRUE(fValue == 6.2f); - - EXPECT_TRUE(unique_any(1.0f).has_value()); - EXPECT_TRUE(unique_any(1.0f).type() == typeid(float)); - - unique_any c = 'z'; - EXPECT_TRUE(c.has_value()); - EXPECT_TRUE(c.type() == typeid(char)); - EXPECT_TRUE(IsStackAllocated(c, any_cast(&c))); - - EXPECT_THROW(any_cast(c), bad_any_cast); - - EXPECT_TRUE(unique_any('4').has_value()); - EXPECT_TRUE(unique_any('4').type() == typeid(char)); -} - -TEST(UniqueAny, BasicTypes_Move) { - unique_any i = 3; - EXPECT_TRUE(i.has_value()); - EXPECT_TRUE(i.type() == typeid(int)); - - unique_any f = 6.2f; - EXPECT_TRUE(f.has_value()); - EXPECT_TRUE(f.type() == typeid(float)); - - f = std::move(i); - EXPECT_FALSE(i.has_value()); - EXPECT_TRUE(i.type() == typeid(void)); - - EXPECT_TRUE(f.has_value()); - EXPECT_TRUE(f.type() == typeid(int)); - -} - -TEST(UniqueAny, SmallType) { - struct T { - T(int32_t* p_) : p(p_) { - (*p)++; - } - - T(T&& t) noexcept : p(t.p) { - (*p)++; - } - - ~T() { - (*p)--; - } - - T(const T&) = delete; - T& operator=(const T&) = delete; - - int32_t* p; - }; - - int32_t p = 0; - - { - unique_any u1 = unique_any(T(&p)); - EXPECT_EQ(p, 1); - - auto u2(std::move(u1)); - EXPECT_EQ(p, 1); - } - - EXPECT_EQ(p, 0); -} - -TEST(UniqueAny, LargeType) { - TestType t1; - unique_any u1 = unique_any(std::move(t1)); - EXPECT_TRUE(u1.has_value()); - EXPECT_TRUE(u1.type() == typeid(TestType)); - EXPECT_FALSE(IsStackAllocated(u1, any_cast(&u1))); - - //TestType should be moved into owning unique_any - EXPECT_EQ(any_cast(&u1)->i1, 1); - - auto u2(std::move(u1)); - EXPECT_TRUE(u2.type() == typeid(TestType)); - EXPECT_TRUE(u1.type() == typeid(void)); - - //TestType should not be moved when owning unique_any is moved; - EXPECT_EQ(any_cast(&u2)->i1, 1); - - //TestType should be moved out of owning unique_any - // Note: two moves are involved in returning the moved value - // First out of the unique_any, and then in the return statement - auto t2 = any_cast(std::move(u2)); - EXPECT_EQ(t2.i1, 3); - EXPECT_TRUE(u2.type() == typeid(void)); -} - -TEST(UniqueAny, Pointer) { - auto t1 = new TestType(); - - auto u1 = unique_any(std::move(t1)); - EXPECT_TRUE(u1.has_value()); - EXPECT_TRUE(u1.type() == typeid(TestType *)); - EXPECT_TRUE(IsStackAllocated(u1, any_cast(&u1))); - - //Only the pointer should be moved - TestType * t2 = *any_cast(&u1); - EXPECT_EQ(t2->i1, 0); - - unique_any u2(4); - std::swap(u2, u1); - - EXPECT_TRUE(u1.has_value()); - EXPECT_TRUE(u1.type() == typeid(int)); - - EXPECT_TRUE(u2.has_value()); - EXPECT_TRUE(u2.type() == typeid(TestType *)); - - t2 = *any_cast(&u2); - EXPECT_EQ(t2->i1, 0); - delete t2; -} - - -TEST(UniqueAny, UniquePtr) { - auto t1 = std::make_unique(); - auto u1 = unique_any(std::move(t1)); - - EXPECT_EQ(t1.get(), nullptr); - EXPECT_TRUE(u1.has_value()); - EXPECT_TRUE(u1.type() == typeid(std::unique_ptr)); - - EXPECT_TRUE(IsStackAllocated(u1, any_cast>(&u1))); - - auto t2 = any_cast >(std::move(u1)); - EXPECT_FALSE(u1.has_value()); - - unique_any u2; - TestType * t3 = new TestType(); - u2 = std::unique_ptr(t3); - EXPECT_TRUE(u2.has_value()); - EXPECT_TRUE(any_cast>(&u2)->get() == t3); -} - -TEST(UniqueAny, SharedPtr) { - - std::shared_ptr shared(new int(3)); - std::weak_ptr weak = shared; - unique_any u1 = 0; - - EXPECT_THROW(any_cast(u1), bad_any_cast); - - EXPECT_EQ(weak.use_count(), 1); - unique_any u2 = shared; - EXPECT_EQ(weak.use_count(), 2); - - EXPECT_EQ(any_cast>(&u1), nullptr); - EXPECT_FALSE(IsStackAllocated(u1, any_cast>(&u1))); - - u1 = std::move(u2); - EXPECT_EQ(weak.use_count(), 2); - u2.swap(u1); - EXPECT_EQ(weak.use_count(), 2); - u2 = 0; - EXPECT_EQ(weak.use_count(), 1); - shared = nullptr; - EXPECT_EQ(weak.use_count(), 0); -} -- cgit v1.2.1