* 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 <gtest/gtest.h>
#include <limits>
#include <mc/exception.h>
#include <mc/memory.h>
#include <test_utilities/test_base.h>
class base_test_object : public mc::enable_shared_from_this<base_test_object> {
public:
base_test_object() : m_value(0) {
++s_construct_count;
}
explicit base_test_object(int value) : m_value(value) {
++s_construct_count;
}
base_test_object(const base_test_object& other)
: mc::enable_shared_from_this<base_test_object>(other),
m_value(other.m_value) {
++s_construct_count;
}
base_test_object(base_test_object&& other) noexcept
: mc::enable_shared_from_this<base_test_object>(std::move(other)),
m_value(other.m_value) {
++s_construct_count;
}
base_test_object& operator=(const base_test_object& other) {
if (this != &other) {
mc::enable_shared_from_this<base_test_object>::operator=(other);
m_value = other.m_value;
}
return *this;
}
base_test_object& operator=(base_test_object&& other) noexcept {
if (this != &other) {
mc::enable_shared_from_this<base_test_object>::operator=(std::move(other));
m_value = other.m_value;
}
return *this;
}
~base_test_object() {
++s_destruct_count;
m_value = -9999;
}
int get_value() const {
return m_value;
}
void set_value(int value) {
m_value = value;
}
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:
int m_value;
static int s_construct_count;
static int s_destruct_count;
};
int base_test_object::s_construct_count = 0;
int base_test_object::s_destruct_count = 0;
class shared_base_test : public mc::test::TestBase {
protected:
void SetUp() override {
base_test_object::reset_counters();
}
void TearDown() override {
EXPECT_EQ(base_test_object::get_construct_count(), base_test_object::get_destruct_count());
}
};
TEST_F(shared_base_test, InitialState) {
auto obj = new base_test_object(42);
EXPECT_FALSE(obj->is_managed());
EXPECT_FALSE(obj->is_destroyed());
EXPECT_EQ(obj->ref_count(), std::numeric_limits<uint32_t>::max());
EXPECT_EQ(obj->weak_count(), 1);
EXPECT_EQ(obj->get_value(), 42);
delete obj;
}
TEST_F(shared_base_test, ManualReferenceCountManagement) {
auto obj = new base_test_object(100);
obj->add_ref();
EXPECT_TRUE(obj->is_managed());
EXPECT_FALSE(obj->is_destroyed());
EXPECT_EQ(obj->ref_count(), 1);
obj->add_ref();
EXPECT_EQ(obj->ref_count(), 2);
bool should_delete = obj->release_ref();
EXPECT_FALSE(should_delete);
EXPECT_EQ(obj->ref_count(), 1);
EXPECT_FALSE(obj->is_destroyed());
should_delete = obj->release_ref();
EXPECT_TRUE(should_delete);
EXPECT_EQ(obj->ref_count(), 0);
EXPECT_TRUE(obj->is_destroyed());
delete obj;
}
TEST_F(shared_base_test, WeakReferenceCountManagement) {
auto obj = new base_test_object(200);
obj->add_weak_ref();
EXPECT_EQ(obj->weak_count(), 2);
bool should_delete = obj->release_weak_ref();
EXPECT_FALSE(should_delete);
EXPECT_EQ(obj->weak_count(), 1);
obj->add_ref();
EXPECT_EQ(obj->ref_count(), 1);
bool strong_released = obj->release_ref();
EXPECT_TRUE(strong_released);
EXPECT_TRUE(obj->is_destroyed());
should_delete = obj->release_weak_ref();
EXPECT_TRUE(should_delete);
EXPECT_EQ(obj->weak_count(), 0);
delete obj;
}
TEST_F(shared_base_test, TryAddRefFunctionality) {
auto obj = new base_test_object(300);
bool success = obj->try_add_ref();
EXPECT_FALSE(success);
EXPECT_FALSE(obj->is_managed());
obj->add_ref();
EXPECT_TRUE(obj->is_managed());
EXPECT_EQ(obj->ref_count(), 1);
success = obj->try_add_ref();
EXPECT_TRUE(success);
EXPECT_EQ(obj->ref_count(), 2);
obj->release_ref();
obj->release_ref();
EXPECT_TRUE(obj->is_destroyed());
success = obj->try_add_ref();
EXPECT_FALSE(success);
delete obj;
}
TEST_F(shared_base_test, StateTransitions) {
auto obj = new base_test_object(400);
EXPECT_EQ(obj->ref_count(), std::numeric_limits<uint32_t>::max());
EXPECT_FALSE(obj->is_managed());
EXPECT_FALSE(obj->is_destroyed());
obj->add_ref();
EXPECT_EQ(obj->ref_count(), 1);
EXPECT_TRUE(obj->is_managed());
EXPECT_FALSE(obj->is_destroyed());
obj->add_ref();
EXPECT_EQ(obj->ref_count(), 2);
EXPECT_TRUE(obj->is_managed());
EXPECT_FALSE(obj->is_destroyed());
obj->release_ref();
EXPECT_EQ(obj->ref_count(), 1);
EXPECT_TRUE(obj->is_managed());
EXPECT_FALSE(obj->is_destroyed());
bool should_delete = obj->release_ref();
EXPECT_TRUE(should_delete);
EXPECT_EQ(obj->ref_count(), 0);
EXPECT_TRUE(obj->is_managed());
EXPECT_TRUE(obj->is_destroyed());
delete obj;
}
TEST_F(shared_base_test, ExceptionCases) {
auto obj = new base_test_object(500);
obj->add_ref();
obj->release_ref();
EXPECT_TRUE(obj->is_destroyed());
EXPECT_THROW(obj->add_ref(), mc::invalid_op_exception);
EXPECT_THROW(obj->release_ref(), mc::invalid_op_exception);
delete obj;
auto obj2 = new base_test_object(600);
EXPECT_THROW(obj2->release_ref(), mc::invalid_op_exception);
delete obj2;
}
TEST_F(shared_base_test, CopyAndMoveConstructors) {
int initial_construct_count = base_test_object::get_construct_count();
int initial_destruct_count = base_test_object::get_destruct_count();
auto obj1 = new base_test_object(700);
obj1->add_ref();
auto obj2 = new base_test_object(*obj1);
EXPECT_EQ(obj2->ref_count(), base_test_object::INVALID);
EXPECT_FALSE(obj2->is_managed());
EXPECT_FALSE(obj2->is_destroyed());
EXPECT_EQ(obj2->get_value(), 700);
auto obj3 = new base_test_object(std::move(*obj1));
EXPECT_EQ(obj3->ref_count(), base_test_object::INVALID);
EXPECT_FALSE(obj3->is_managed());
EXPECT_FALSE(obj3->is_destroyed());
EXPECT_EQ(obj3->get_value(), 700);
EXPECT_EQ(obj1->ref_count(), 1);
EXPECT_TRUE(obj1->is_managed());
EXPECT_TRUE(obj1->release_ref());
delete obj1;
delete obj2;
delete obj3;
int final_construct_count = base_test_object::get_construct_count();
int final_destruct_count = base_test_object::get_destruct_count();
EXPECT_EQ(final_construct_count - initial_construct_count,
final_destruct_count - initial_destruct_count);
}
TEST_F(shared_base_test, AssignmentOperators) {
auto obj1 = new base_test_object(800);
auto obj2 = new base_test_object(900);
obj1->add_ref();
obj2->add_ref();
auto obj1_initial_count = obj1->ref_count();
auto obj2_initial_count = obj2->ref_count();
*obj2 = *obj1;
EXPECT_EQ(obj1->ref_count(), obj1_initial_count);
EXPECT_EQ(obj2->ref_count(), obj2_initial_count);
EXPECT_EQ(obj2->get_value(), 800);
*obj2 = std::move(*obj1);
EXPECT_EQ(obj1->ref_count(), obj1_initial_count);
EXPECT_EQ(obj2->ref_count(), obj2_initial_count);
EXPECT_EQ(obj2->get_value(), 800);
obj1->release_ref();
obj2->release_ref();
delete obj1;
delete obj2;
}
TEST_F(shared_base_test, FromThisMethods) {
auto obj = new base_test_object(1000);
auto shared = obj->shared_from_this();
EXPECT_TRUE(shared);
EXPECT_EQ(shared.get(), obj);
EXPECT_TRUE(obj->is_managed());
EXPECT_EQ(obj->ref_count(), 1);
auto weak = obj->weak_from_this();
EXPECT_TRUE(weak);
EXPECT_EQ(weak.get(), obj);
EXPECT_FALSE(weak.expired());
auto shared2 = obj->shared_from_this();
EXPECT_EQ(shared2.get(), obj);
EXPECT_EQ(obj->ref_count(), 2);
shared.reset();
shared2.reset();
EXPECT_TRUE(weak.expired());
EXPECT_FALSE(weak.lock());
}
TEST_F(shared_base_test, FromRawStaticMethod) {
auto null_ptr = base_test_object::from_raw(nullptr);
EXPECT_FALSE(null_ptr);
auto obj = new base_test_object(1100);
auto shared1 = base_test_object::from_raw(obj, true);
EXPECT_TRUE(shared1);
EXPECT_EQ(shared1.get(), obj);
EXPECT_TRUE(obj->is_managed());
EXPECT_EQ(obj->ref_count(), 1);
auto shared2 = base_test_object::from_raw(obj, true);
EXPECT_TRUE(shared2);
EXPECT_EQ(shared2.get(), obj);
EXPECT_EQ(obj->ref_count(), 2);
shared1.reset();
shared2.reset();
}
TEST_F(shared_base_test, ReferenceCountBoundaries) {
auto obj = new base_test_object(1200);
EXPECT_EQ(obj->ref_count(), std::numeric_limits<uint32_t>::max());
obj->add_ref();
EXPECT_EQ(obj->ref_count(), 1);
for (int i = 0; i < 1000; ++i) {
obj->add_ref();
}
EXPECT_EQ(obj->ref_count(), 1001);
for (int i = 0; i < 1000; ++i) {
bool should_delete = obj->release_ref();
EXPECT_FALSE(should_delete);
}
EXPECT_EQ(obj->ref_count(), 1);
bool should_delete = obj->release_ref();
EXPECT_TRUE(should_delete);
EXPECT_EQ(obj->ref_count(), 0);
EXPECT_TRUE(obj->is_destroyed());
delete obj;
}
TEST_F(shared_base_test, MixedStrongWeakReferences) {
auto obj = new base_test_object(1300);
obj->add_weak_ref();
EXPECT_EQ(obj->weak_count(), 2);
obj->add_ref();
EXPECT_EQ(obj->ref_count(), 1);
EXPECT_TRUE(obj->is_managed());
bool success = obj->try_add_ref();
EXPECT_TRUE(success);
EXPECT_EQ(obj->ref_count(), 2);
obj->release_ref();
obj->release_ref();
EXPECT_TRUE(obj->is_destroyed());
EXPECT_EQ(obj->ref_count(), 0);
success = obj->try_add_ref();
EXPECT_FALSE(success);
bool should_delete = obj->release_weak_ref();
EXPECT_FALSE(should_delete);
EXPECT_EQ(obj->weak_count(), 1);
should_delete = obj->release_weak_ref();
EXPECT_TRUE(should_delete);
EXPECT_EQ(obj->weak_count(), 0);
delete obj;
}
TEST_F(shared_base_test, ConcurrencyBasics) {
for (int iteration = 0; iteration < 10; ++iteration) {
auto obj = new base_test_object(1400 + iteration);
EXPECT_FALSE(obj->is_managed());
EXPECT_EQ(obj->ref_count(), std::numeric_limits<uint32_t>::max());
obj->add_ref();
EXPECT_TRUE(obj->is_managed());
EXPECT_EQ(obj->ref_count(), 1);
for (int i = 0; i < 10; ++i) {
bool success = obj->try_add_ref();
EXPECT_TRUE(success);
}
EXPECT_EQ(obj->ref_count(), 11);
for (int i = 0; i < 11; ++i) {
bool should_delete = obj->release_ref();
if (i < 10) {
EXPECT_FALSE(should_delete);
} else {
EXPECT_TRUE(should_delete);
}
}
EXPECT_TRUE(obj->is_destroyed());
EXPECT_EQ(obj->ref_count(), 0);
delete obj;
}
}