AK: Use unsigned int for refcount

And while fixing all the tests that look at ref_count(),
sneak in a fix for the test suite name.
This commit is contained in:
Sergey Bugaev 2020-06-12 16:33:38 +03:00 committed by Andreas Kling
parent 0466810638
commit 583108004c
Notes: sideshowbarker 2024-07-19 05:42:18 +09:00
3 changed files with 29 additions and 27 deletions

View file

@ -58,22 +58,24 @@ constexpr auto call_one_ref_left_if_present(...) -> FalseType
class RefCountedBase {
public:
typedef unsigned int RefCountType;
ALWAYS_INLINE void ref() const
{
ASSERT(m_ref_count);
++m_ref_count;
}
ALWAYS_INLINE int ref_count() const
ALWAYS_INLINE RefCountType ref_count() const
{
return m_ref_count;
}
protected:
RefCountedBase() {}
RefCountedBase() { }
ALWAYS_INLINE ~RefCountedBase()
{
ASSERT(!m_ref_count);
ASSERT(m_ref_count == 0);
}
ALWAYS_INLINE void deref_base() const
@ -82,7 +84,7 @@ protected:
--m_ref_count;
}
mutable int m_ref_count { 1 };
mutable RefCountType m_ref_count { 1 };
};
template<typename T>

View file

@ -37,26 +37,26 @@ TEST_CASE(basics)
{
auto object = adopt(*new Object);
EXPECT(object.ptr() != nullptr);
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
object->ref();
EXPECT_EQ(object->ref_count(), 2);
EXPECT_EQ(object->ref_count(), 2u);
object->unref();
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
{
NonnullRefPtr another = object;
EXPECT_EQ(object->ref_count(), 2);
EXPECT_EQ(object->ref_count(), 2u);
}
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
}
TEST_CASE(assign_reference)
{
auto object = adopt(*new Object);
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
object = *object;
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
}
TEST_CASE(assign_owner_of_self)
@ -70,14 +70,14 @@ TEST_CASE(assign_owner_of_self)
child->parent = move(parent);
child = *child->parent;
EXPECT_EQ(child->ref_count(), 1);
EXPECT_EQ(child->ref_count(), 1u);
}
TEST_CASE(swap_with_self)
{
auto object = adopt(*new Object);
swap(object, object);
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
}
TEST_MAIN(String)
TEST_MAIN(NonnullRefPtr)

View file

@ -37,48 +37,48 @@ TEST_CASE(basics)
{
RefPtr<Object> object = adopt(*new Object);
EXPECT(object.ptr() != nullptr);
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
object->ref();
EXPECT_EQ(object->ref_count(), 2);
EXPECT_EQ(object->ref_count(), 2u);
object->unref();
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
{
NonnullRefPtr another = *object;
EXPECT_EQ(object->ref_count(), 2);
EXPECT_EQ(object->ref_count(), 2u);
}
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
}
TEST_CASE(assign_reference)
{
RefPtr<Object> object = adopt(*new Object);
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
object = *object;
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
}
TEST_CASE(assign_ptr)
{
RefPtr<Object> object = adopt(*new Object);
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
object = object.ptr();
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
}
TEST_CASE(assign_moved_self)
{
RefPtr<Object> object = adopt(*new Object);
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
object = move(object);
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
}
TEST_CASE(assign_copy_self)
{
RefPtr<Object> object = adopt(*new Object);
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
#ifdef __clang__
#pragma clang diagnostic push
@ -89,7 +89,7 @@ TEST_CASE(assign_copy_self)
#pragma clang diagnostic pop
#endif
EXPECT_EQ(object->ref_count(), 1);
EXPECT_EQ(object->ref_count(), 1u);
}
TEST_MAIN(RefPtr)