summaryrefslogtreecommitdiffhomepage
path: root/test/bptr.cpp
blob: 6f82334444e32f56b0e403de8a884554974640e9 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "app.hpp"
#include "compat/assert.hpp"
#include "compat/borrowed-ptr.inl"

namespace floormat {

namespace {
struct Foo {};
struct Bar : Foo {};
struct Baz {};
} // namespace

// NOLINTBEGIN(*-use-anonymous-namespace)

template struct detail_borrowed_ptr::control_block_impl<Foo>;
template struct detail_borrowed_ptr::control_block_impl<Bar>;
template struct detail_borrowed_ptr::control_block_impl<Baz>;

template bool operator==(const bptr<Foo>&, const bptr<Foo>&) noexcept;
template bool operator==(const bptr<Bar>&, const bptr<Bar>&) noexcept;
template bool operator==(const bptr<Baz>&, const bptr<Baz>&) noexcept;

template class bptr<Foo>;
template class bptr<Bar>;
template class bptr<Baz>;

template bptr<Foo> static_pointer_cast(const bptr<Foo>&) noexcept;
template bptr<Bar> static_pointer_cast(const bptr<Bar>&) noexcept;
template bptr<Baz> static_pointer_cast(const bptr<Baz>&) noexcept;

template bptr<Bar> static_pointer_cast(const bptr<Foo>&) noexcept;
template bptr<Foo> static_pointer_cast(const bptr<Bar>&) noexcept;

//template bptr<Baz> static_pointer_cast(const bptr<Bar>&) noexcept; // must fail
//template bptr<Foo> static_pointer_cast(const bptr<Baz>&) noexcept; // must fail
//template bptr<Bar> static_pointer_cast(const bptr<Baz>&) noexcept; // must fail

// NOLINTEND(*-use-anonymous-namespace)

namespace {

int A_total = 0, A_alive = 0; // NOLINT

struct A
{
    int val, serial;
    explicit A(int val) : val{val}, serial{++A_total} { ++A_alive; }
    ~A() noexcept { --A_alive; fm_assert(A_alive >= 0); }
};

void test1()
{
    A_total = 0; A_alive = 0;

    auto p1 = bptr<A>{InPlace, 1};
    fm_assert(p1.use_count() == 1);
    fm_assert(p1.get());
    fm_assert(A_total == 1);
    fm_assert(A_alive == 1);

    p1 = nullptr;
    fm_assert(p1.use_count() == 0);
    fm_assert(!p1.get());
    fm_assert(A_total == 1);
    fm_assert(A_alive == 0);
}

void test2()
{
    A_total = 0; A_alive = 0;

    auto p1 = bptr<A>{InPlace, 2};
    auto p2 = p1;

    fm_assert(p1.get());
    fm_assert(p1.get() == p2.get());
    fm_assert(p1->val == 2);
    fm_assert(p1->serial == 1);
    fm_assert(A_total == 1);
    fm_assert(A_alive == 1);

    p1 = nullptr;
    fm_assert(!p1.get());
    fm_assert(p2.get());
    fm_assert(p2->val == 2);
    fm_assert(p2->serial == 1);
    fm_assert(A_total == 1);
    fm_assert(A_alive == 1);

    p2 = nullptr;
    fm_assert(!p1.get());
    fm_assert(!p2.get());
    fm_assert(A_total == 1);
    fm_assert(A_alive == 0);
}

} // namespace

void test_app::test_bptr()
{
    test1();
    test2();
}

} // namespace floormat