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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#include "app.hpp"
#include "compat/intrusive-ptr.hpp"
#include "compat/defs.hpp"
#ifdef __CLION_IDE__
#pragma clang diagnostic ignored "-Wunknown-pragmas"
#pragma ide diagnostic ignored "performance-unnecessary-copy-initialization"
#endif
namespace floormat {
namespace { struct Test2 { int val = 0; uint32_t _counter = 0; }; }
template<>
constexpr auto
iptr::refcount_access<iptr::non_atomic_u32_tag, Test2>::access(Test2* ptr) noexcept -> counter_type&
{
return ptr->_counter;
}
namespace {
struct non_copyable
{
int value = 0;
explicit constexpr non_copyable(DirectInitT) {}
explicit constexpr non_copyable(DirectInitT, int value) : value{value} {}
fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT(non_copyable);
fm_DECLARE_DELETED_COPY_ASSIGNMENT(non_copyable);
};
struct S { // todo fm_assert_equal
int instances;
unsigned allocs, frees;
bool operator==(const S&) const = default;
} s = {};
struct Test1
{
friend struct iptr::refcount_access<iptr::non_atomic_u32_tag, Test1>;
Test1(non_copyable nc) noexcept : value{nc.value} { s.instances++; s.allocs++; }
~Test1() noexcept { s.instances--; fm_assert(s.instances >= 0); s.frees++; }
int value = 0;
private:
iptr::refcount_traits<iptr::non_atomic_u32_tag, Test1>::counter_type counter{0};
};
} // namespace
template<>
constexpr auto iptr::refcount_access<iptr::non_atomic_u32_tag, Test1>::access(Test1* ptr) noexcept -> counter_type&
{
return ptr->counter;
}
template class basic_iptr<iptr::non_atomic_u32_tag, Test1>;
namespace {
void test_copy()
{
using myptr = local_iptr<Test1>;
s = {};
{ (void)myptr{nullptr};
fm_assert(s == S{});
(void)myptr{};
fm_assert(s == S{});
{ Test1 t1{non_copyable{DirectInit}}; }
fm_assert(s != S{});
fm_assert(s == S{0, 1, 1});
{ Test1 t1{non_copyable{DirectInit}};
fm_assert(s == S{1, 2, 1});
} fm_assert(s == S{0, 2, 2});
}
{ auto a = myptr{InPlaceInit, non_copyable{DirectInit}};
fm_assert(s == S{1, 3, 2});
auto b = a;
fm_assert(s == S{1, 3, 2});
fm_assert(b.get() == a.get());
} fm_assert(s == S{0, 3, 3});
{ auto a = myptr{InPlaceInit, non_copyable{DirectInit}};
fm_assert(s == S{1, 4, 3});
a = {};
fm_assert(s == S{0, 4, 4});
} fm_assert(s == S{0, 4, 4});
{ auto a = myptr{InPlaceInit, non_copyable{DirectInit, 1}};
auto b = myptr{InPlaceInit, non_copyable{DirectInit, 2}};
{ auto c = myptr{InPlaceInit, non_copyable{DirectInit, 3}};
auto d = myptr{InPlaceInit, non_copyable{DirectInit, 4}};
}
fm_assert(a.use_count() == 1);
fm_assert(a->value == 1);
fm_assert(b->value == 2);
fm_assert(s == S{2, 8, 6});
b = a;
fm_assert(b.use_count() == 2);
fm_assert(a.use_count() == 2);
b = nullptr;
fm_assert(b.use_count() == 0);
fm_assert(a.use_count() == 1);
fm_assert(s == S{1, 8, 7});
fm_assert(a->value == 1);
} fm_assert(s == S{0, 8, 8});
}
#define fm_assert_free() do { fm_assert(s.allocs == s.frees); fm_assert(s.instances == 0); } while (false)
void test_move()
{
using myptr = local_iptr<Test1>;
fm_assert_free();
s = {};
{ auto a = myptr{InPlaceInit, non_copyable{DirectInit, 1}};
auto b = myptr{InPlaceInit, non_copyable{DirectInit, 2}};
fm_assert(s == S{2, 2, 0});
{ auto c = myptr{InPlaceInit, non_copyable{DirectInit, 3}};
auto d = myptr{InPlaceInit, non_copyable{DirectInit, 4}};
fm_assert(s == S{4, 4, 0});
} fm_assert(s == S{2, 4, 2});
{ a = move(b);
fm_assert(s == S{1, 4, 3});
fm_assert(!b);
fm_assert(a);
fm_assert(b.use_count() == 0);
fm_assert(a.use_count() == 1);
}
} fm_assert(s == S{0, 4, 4});
}
constexpr bool test_cexpr() // todo
{
using myptr = local_iptr<Test2>;
// construct
auto foo1 = myptr{};
auto foo2 = myptr{nullptr};
fm_assert(foo1.use_count() == 0);
fm_assert(foo2.use_count() == 0);
foo1 = move(foo2);
fm_assert(foo1.use_count() == 0);
fm_assert(foo2.use_count() == 0);
return true;
}
} // namespace
void test_app::test_iptr()
{
static_assert(test_cexpr());
test_copy();
test_move();
}
} // namespace floormat
|