mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05:00
6db879ee66
We would leak a ref when assigning a T& to a NonnullRefPtr that already contains that same T.
36 lines
722 B
C++
36 lines
722 B
C++
#include <AK/TestSuite.h>
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
#include <AK/AKString.h>
|
|
|
|
struct Object : public RefCounted<Object> {
|
|
int x;
|
|
};
|
|
|
|
TEST_CASE(basics)
|
|
{
|
|
auto object = adopt(*new Object);
|
|
EXPECT(object.ptr() != nullptr);
|
|
EXPECT_EQ(object->ref_count(), 1);
|
|
object->ref();
|
|
EXPECT_EQ(object->ref_count(), 2);
|
|
object->deref();
|
|
EXPECT_EQ(object->ref_count(), 1);
|
|
|
|
{
|
|
NonnullRefPtr another = object;
|
|
EXPECT_EQ(object->ref_count(), 2);
|
|
}
|
|
|
|
EXPECT_EQ(object->ref_count(), 1);
|
|
}
|
|
|
|
TEST_CASE(assign_reference)
|
|
{
|
|
auto object = adopt(*new Object);
|
|
EXPECT_EQ(object->ref_count(), 1);
|
|
object = *object;
|
|
EXPECT_EQ(object->ref_count(), 1);
|
|
}
|
|
|
|
TEST_MAIN(String)
|