* Copyright (c) 2024 Huawei Technologies Co., Ltd.
* openUBMC is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <array>
#include <gtest/gtest.h>
#include <mc/exception.h>
#include <mc/memory.h>
#include <memory>
class ctor_test_object : public mc::enable_shared_from_this<ctor_test_object> {
public:
ctor_test_object()
: m_id(s_next_id++), m_data(std::make_unique<int>(42)) {
++s_construct_count;
}
explicit ctor_test_object(int value)
: m_id(s_next_id++), m_data(std::make_unique<int>(value)) {
++s_construct_count;
}
~ctor_test_object() {
++s_destruct_count;
m_data.reset();
}
int get_id() const {
return m_id;
}
int get_data() const {
return m_data ? *m_data : -1;
}
void set_data(int value) {
if (m_data) {
*m_data = value;
}
}
static void reset_counters() {
s_construct_count = 0;
s_destruct_count = 0;
s_next_id = 1;
}
static int get_construct_count() {
return s_construct_count;
}
static int get_destruct_count() {
return s_destruct_count;
}
private:
int m_id;
std::unique_ptr<int> m_data;
static int s_construct_count;
static int s_destruct_count;
static int s_next_id;
};
int ctor_test_object::s_construct_count = 0;
int ctor_test_object::s_destruct_count = 0;
int ctor_test_object::s_next_id = 1;
class exception_test_object : public mc::enable_shared_from_this<exception_test_object> {
public:
explicit exception_test_object(bool throw_in_constructor = false) {
if (throw_in_constructor) {
throw std::runtime_error("构造函数中的测试异常");
}
++s_construct_count;
}
~exception_test_object() {
++s_destruct_count;
}
void may_throw() {
if (m_should_throw) {
throw std::runtime_error("测试异常");
}
}
void set_throw_flag(bool should_throw) {
m_should_throw = should_throw;
}
static void reset_counters() {
s_construct_count = 0;
s_destruct_count = 0;
}
static int get_construct_count() {
return s_construct_count;
}
static int get_destruct_count() {
return s_destruct_count;
}
private:
bool m_should_throw = false;
static int s_construct_count;
static int s_destruct_count;
};
int exception_test_object::s_construct_count = 0;
int exception_test_object::s_destruct_count = 0;
class SharedPtrConstructTest : public ::testing::Test {
protected:
void SetUp() override {
ctor_test_object::reset_counters();
exception_test_object::reset_counters();
}
void TearDown() override {
EXPECT_EQ(ctor_test_object::get_construct_count(), ctor_test_object::get_destruct_count());
EXPECT_EQ(exception_test_object::get_construct_count(), exception_test_object::get_destruct_count());
}
};
TEST_F(SharedPtrConstructTest, UninitializedStateOperations) {
auto* raw_obj = new ctor_test_object(100);
EXPECT_FALSE(raw_obj->is_managed());
EXPECT_FALSE(raw_obj->is_destroyed());
EXPECT_EQ(raw_obj->ref_count(), std::numeric_limits<uint32_t>::max());
mc::shared_ptr<ctor_test_object> ptr(raw_obj);
EXPECT_TRUE(raw_obj->is_managed());
EXPECT_FALSE(raw_obj->is_destroyed());
EXPECT_EQ(raw_obj->ref_count(), 1);
EXPECT_EQ(ptr.use_count(), 1);
}
TEST_F(SharedPtrConstructTest, FromRawWithoutAddRefThrowsOnReset) {
auto* raw_obj = new ctor_test_object(150);
EXPECT_FALSE(raw_obj->is_managed());
EXPECT_THROW(ctor_test_object::from_raw(raw_obj), mc::invalid_op_exception);
delete raw_obj;
}
TEST_F(SharedPtrConstructTest, ResetThrowsWhenRefCountAlreadyDestroyed) {
auto* raw_obj = new ctor_test_object(160);
mc::shared_ptr<ctor_test_object> ptr(raw_obj);
EXPECT_TRUE(raw_obj->is_managed());
EXPECT_EQ(ptr.use_count(), 1);
bool should_delete = raw_obj->release_ref();
EXPECT_TRUE(should_delete);
EXPECT_TRUE(raw_obj->is_destroyed());
try {
ptr.reset();
FAIL() << "reset 应该抛出 mc::invalid_op_exception";
} catch (const mc::invalid_op_exception&) {
}
EXPECT_FALSE(ptr);
delete raw_obj;
}
TEST_F(SharedPtrConstructTest, SharedFromThisStates) {
auto obj = mc::make_shared<ctor_test_object>(200);
auto shared1 = obj->shared_from_this();
EXPECT_EQ(shared1.get(), obj.get());
EXPECT_EQ(obj.use_count(), 2);
auto weak = obj->weak_from_this();
EXPECT_FALSE(weak.expired());
EXPECT_EQ(weak.get(), obj.get());
obj.reset();
shared1.reset();
EXPECT_TRUE(weak.expired());
EXPECT_FALSE(weak.lock());
}
TEST_F(SharedPtrConstructTest, ExceptionSafety) {
EXPECT_THROW({
mc::make_shared<exception_test_object>(true);
},
std::runtime_error);
EXPECT_EQ(exception_test_object::get_construct_count(), 0);
EXPECT_EQ(exception_test_object::get_destruct_count(), 0);
auto obj = mc::make_shared<exception_test_object>(false);
EXPECT_EQ(exception_test_object::get_construct_count(), 1);
obj->set_throw_flag(true);
EXPECT_THROW(obj->may_throw(), std::runtime_error);
EXPECT_TRUE(obj);
EXPECT_EQ(obj.use_count(), 1);
obj.reset();
EXPECT_EQ(exception_test_object::get_destruct_count(), 1);
}
TEST_F(SharedPtrConstructTest, SelfSwap) {
auto obj = mc::make_shared<ctor_test_object>(300);
auto* original_ptr = obj.get();
auto original_count = obj.use_count();
obj.swap(obj);
EXPECT_EQ(obj.get(), original_ptr);
EXPECT_EQ(obj.use_count(), original_count);
EXPECT_EQ(obj->get_data(), 300);
}
TEST_F(SharedPtrConstructTest, NullptrOperations) {
mc::shared_ptr<ctor_test_object> null_ptr;
mc::shared_ptr<ctor_test_object> nullptr_ptr(nullptr);
EXPECT_TRUE(null_ptr == nullptr_ptr);
EXPECT_TRUE(null_ptr == nullptr);
EXPECT_TRUE(nullptr == null_ptr);
EXPECT_FALSE(null_ptr != nullptr_ptr);
auto obj = mc::make_shared<ctor_test_object>(400);
null_ptr = obj;
EXPECT_TRUE(null_ptr);
EXPECT_EQ(null_ptr.get(), obj.get());
null_ptr = nullptr;
EXPECT_FALSE(null_ptr);
EXPECT_EQ(null_ptr.get(), nullptr);
null_ptr.swap(nullptr_ptr);
EXPECT_FALSE(null_ptr);
EXPECT_FALSE(nullptr_ptr);
null_ptr.reset();
EXPECT_FALSE(null_ptr);
}
TEST_F(SharedPtrConstructTest, ReferenceCountBoundaries) {
auto obj = mc::make_shared<ctor_test_object>(500);
std::vector<mc::shared_ptr<ctor_test_object>> ptrs;
ptrs.reserve(10000);
for (int i = 0; i < 10000; ++i) {
ptrs.push_back(obj);
}
EXPECT_EQ(obj.use_count(), 10001);
for (int i = 0; i < 5000; ++i) {
ptrs.pop_back();
}
EXPECT_EQ(obj.use_count(), 5001);
ptrs.clear();
EXPECT_EQ(obj.use_count(), 1);
EXPECT_TRUE(obj.unique());
}
TEST_F(SharedPtrConstructTest, WeakPtrEdgeCases) {
mc::weak_ptr<ctor_test_object> weak;
EXPECT_TRUE(weak.expired());
EXPECT_FALSE(weak.lock());
EXPECT_EQ(weak.use_count(), 0);
{
auto obj = mc::make_shared<ctor_test_object>(600);
weak = obj;
EXPECT_FALSE(weak.expired());
EXPECT_EQ(weak.use_count(), 1);
auto lock1 = weak.lock();
auto lock2 = weak.lock();
auto lock3 = weak.lock();
EXPECT_TRUE(lock1);
EXPECT_TRUE(lock2);
EXPECT_TRUE(lock3);
EXPECT_EQ(weak.use_count(), 4);
EXPECT_EQ(lock1.get(), lock2.get());
EXPECT_EQ(lock2.get(), lock3.get());
}
EXPECT_TRUE(weak.expired());
EXPECT_FALSE(weak.lock());
EXPECT_EQ(weak.use_count(), 0);
}
class edge_base : public mc::enable_shared_from_this<edge_base> {
public:
virtual ~edge_base() = default;
virtual int get_type() const {
return 1;
}
};
class edge_derived : public edge_base {
public:
int get_type() const override {
return 2;
}
int get_derived_value() const {
return 42;
}
};
class edge_other_derived : public edge_base {
public:
int get_type() const override {
return 3;
}
int get_other_value() const {
return 84;
}
};
TEST_F(SharedPtrConstructTest, TypeConversionEdgeCases) {
auto derived = mc::make_shared<edge_derived>();
mc::shared_ptr<edge_base> base = derived;
EXPECT_EQ(base.get(), derived.get());
EXPECT_EQ(base->get_type(), 2);
EXPECT_EQ(derived.use_count(), 2);
auto back_to_derived = base.template dynamic_pointer_cast<edge_derived>();
EXPECT_TRUE(back_to_derived);
EXPECT_EQ(back_to_derived.get(), derived.get());
EXPECT_EQ(back_to_derived->get_derived_value(), 42);
auto failed_cast = base.template dynamic_pointer_cast<edge_other_derived>();
EXPECT_FALSE(failed_cast);
EXPECT_EQ(failed_cast.get(), nullptr);
EXPECT_EQ(failed_cast.use_count(), 0);
auto static_cast_result = base.template static_pointer_cast<edge_derived>();
EXPECT_TRUE(static_cast_result);
EXPECT_EQ(static_cast_result.get(), derived.get());
mc::shared_ptr<edge_base> null_base;
auto null_derived = null_base.template dynamic_pointer_cast<edge_derived>();
EXPECT_FALSE(null_derived);
EXPECT_EQ(null_derived.get(), nullptr);
}
TEST_F(SharedPtrConstructTest, FromRawEdgeCases) {
auto null_ptr = ctor_test_object::from_raw(nullptr);
EXPECT_FALSE(null_ptr);
EXPECT_EQ(null_ptr.get(), nullptr);
auto* raw_obj = new ctor_test_object(700);
auto ptr1 = ctor_test_object::from_raw(raw_obj, true);
EXPECT_TRUE(ptr1);
EXPECT_EQ(ptr1.get(), raw_obj);
EXPECT_EQ(ptr1.use_count(), 1);
EXPECT_TRUE(raw_obj->is_managed());
auto ptr2 = ctor_test_object::from_raw(raw_obj, true);
EXPECT_TRUE(ptr2);
EXPECT_EQ(ptr2.get(), raw_obj);
EXPECT_EQ(ptr1.use_count(), 2);
EXPECT_EQ(ptr2.use_count(), 2);
EXPECT_EQ(ptr1->get_data(), 700);
EXPECT_EQ(ptr2->get_data(), 700);
}
TEST_F(SharedPtrConstructTest, DetachOperation) {
auto obj = mc::make_shared<ctor_test_object>(800);
auto* raw_ptr = obj.get();
EXPECT_EQ(obj.use_count(), 1);
auto* detached_ptr = obj.detach();
EXPECT_FALSE(obj);
EXPECT_EQ(obj.get(), nullptr);
EXPECT_EQ(obj.use_count(), 0);
EXPECT_EQ(detached_ptr, raw_ptr);
EXPECT_EQ(detached_ptr->get_data(), 800);
EXPECT_EQ(detached_ptr->ref_count(), 1);
bool should_delete = detached_ptr->release_ref();
EXPECT_TRUE(should_delete);
delete detached_ptr;
EXPECT_EQ(ctor_test_object::get_construct_count(), 1);
EXPECT_EQ(ctor_test_object::get_destruct_count(), 1);
}
TEST_F(SharedPtrConstructTest, DestructorExceptionSafety) {
{
std::vector<mc::shared_ptr<ctor_test_object>> ptrs;
for (int i = 0; i < 10; ++i) {
ptrs.push_back(mc::make_shared<ctor_test_object>(i));
}
for (size_t i = 0; i < ptrs.size() && i < 5; ++i) {
auto copy = ptrs[i];
ptrs.push_back(copy);
}
for (size_t i = 0; i < ptrs.size() && i < 5; i += 2) {
ptrs[i].reset();
}
}
EXPECT_GT(ctor_test_object::get_construct_count(), 0);
EXPECT_EQ(ctor_test_object::get_construct_count(), ctor_test_object::get_destruct_count());
}
TEST_F(SharedPtrConstructTest, HashFunctionality) {
auto obj1 = mc::make_shared<ctor_test_object>(900);
auto obj2 = mc::make_shared<ctor_test_object>(901);
auto obj1_copy = obj1;
std::hash<mc::shared_ptr<ctor_test_object>> hasher;
EXPECT_EQ(hasher(obj1), hasher(obj1_copy));
auto hash1 = hasher(obj1);
auto hash2 = hasher(obj2);
mc::shared_ptr<ctor_test_object> null_ptr;
auto null_hash = hasher(null_ptr);
EXPECT_EQ(hasher(obj1), hash1);
EXPECT_EQ(hasher(obj2), hash2);
EXPECT_EQ(hasher(null_ptr), null_hash);
}