summaryrefslogtreecommitdiff
path: root/AK/Tests/TestNonnullRefPtr.cpp
blob: a42e5cdcdd4a1403908b713ca94688a70dc1bb44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <AK/TestSuite.h>

#include <AK/NonnullRefPtr.h>
#include <AK/String.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)