Skip to content

Commit

Permalink
add test.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdai committed Feb 14, 2019
1 parent 8698654 commit 1eb6929
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions source/extensions/common/wasm/shared_from_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <memory>
#include "gtest/gtest.h"
class ShareDefault : public std::enable_shared_from_this<ShareDefault> {
};

class ShareNoExplicitEnableSharedFromThis : public std::enable_shared_from_this<ShareNoExplicitEnableSharedFromThis> {
public:
ShareNoExplicitEnableSharedFromThis() = default;
ShareNoExplicitEnableSharedFromThis(const ShareNoExplicitEnableSharedFromThis&) /*: enable_shared_from_this<>() */ {}
};

class ShareNoExplicitCtorNeedArg : public std::enable_shared_from_this<ShareNoExplicitCtorNeedArg> {
public:
ShareNoExplicitCtorNeedArg(int i) : i_(i);
ShareNoExplicitCtorNeedArg(const ShareNoExplicitCtorNeedArg&) /*: enable_shared_from_this<>() */ {}
private:
int i_;
};

class ShareFoo : public std::enable_shared_from_this<ShareFoo> {
public:
ShareFoo() = default;
ShareFoo(const ShareFoo&) : enable_shared_from_this<ShareFoo>() {}
};

class ShareBar : public std::enable_shared_from_this<ShareBar> {
public:
ShareBar() = default;
ShareBar(const ShareBar& that) : enable_shared_from_this<ShareBar>(that) {}
};

TEST(ShareDefaultTest, copyShareNoExplicitEnableSharedFromThis) {
using ShareStruct = ShareNoExplicitEnableSharedFromThis;
auto pShare = std::make_shared<ShareStruct>();
EXPECT_EQ(1, pShare.use_count());
auto copyShare = std::make_shared<ShareStruct>(*pShare);
ASSERT_EQ(1, pShare.use_count());
ASSERT_EQ(1, copyShare.use_count());
auto copyPtr = pShare;
ASSERT_EQ(2, copyPtr.use_count());
ASSERT_EQ(2, pShare.use_count());
ASSERT_EQ(1, copyShare.use_count());
}

TEST(ShareDefaultTest, copyShareNoExplicitCtorNeedArg) {
using ShareStruct = ShareNoExplicitCtorNeedArg;
auto pShare = std::make_shared<ShareStruct>();
EXPECT_EQ(1, pShare.use_count());
auto copyShare = std::make_shared<ShareStruct>(*pShare);
ASSERT_EQ(1, pShare.use_count());
ASSERT_EQ(1, copyShare.use_count());
auto copyPtr = pShare;
ASSERT_EQ(2, copyPtr.use_count());
ASSERT_EQ(2, pShare.use_count());
ASSERT_EQ(1, copyShare.use_count());
}

TEST(ShareDefaultTest, copyShareDefault) {
using ShareStruct = ShareDefault;
auto pShare = std::make_shared<ShareStruct>();
EXPECT_EQ(1, pShare.use_count());
auto copyShare = std::make_shared<ShareStruct>(*pShare);
ASSERT_EQ(1, pShare.use_count());
ASSERT_EQ(1, copyShare.use_count());
auto copyPtr = pShare;
ASSERT_EQ(2, copyPtr.use_count());
ASSERT_EQ(2, pShare.use_count());
ASSERT_EQ(1, copyShare.use_count());
}

TEST(ShareFooTest, copyShareFoo) {
using ShareStruct = ShareFoo;
auto pShare = std::make_shared<ShareStruct>();
EXPECT_EQ(1, pShare.use_count());
auto copyShare = std::make_shared<ShareStruct>(*pShare);
ASSERT_EQ(1, pShare.use_count());
ASSERT_EQ(1, copyShare.use_count());
auto copyPtr = pShare;
ASSERT_EQ(2, copyPtr.use_count());
ASSERT_EQ(2, pShare.use_count());
ASSERT_EQ(1, copyShare.use_count());
}

TEST(ShareBarTest, copyShareBar) {
using ShareStruct = ShareBar;
auto pShare = std::make_shared<ShareStruct>();
EXPECT_EQ(1, pShare.use_count());
auto copyShare = std::make_shared<ShareStruct>(*pShare);
EXPECT_EQ(1, pShare.use_count());
ASSERT_EQ(1, copyShare.use_count());
auto copyPtr = pShare;
ASSERT_EQ(2, copyPtr.use_count());
ASSERT_EQ(2, pShare.use_count());
ASSERT_EQ(1, copyShare.use_count());
}

0 comments on commit 1eb6929

Please sign in to comment.