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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
// { dg-do run { target c++26 } }
#include <memory>
#include <scoped_allocator>
#include <utility>
#include <vector>
#include <optional>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
struct Base {
friend constexpr
bool operator==(const Base& lhs, const Base& rhs)
{ return lhs.eq(rhs); }
private:
constexpr virtual bool
eq(const Base& other) const = 0;
};
struct Derived : Base
{
constexpr Derived()
: x(0), y(0), z(0)
{ }
constexpr Derived(int a, int b, int c)
: x(a), y(b), z(c)
{ }
private:
constexpr bool
eq(const Base& other) const override
{
if (auto op = dynamic_cast<const Derived*>(&other))
return this->x == op->x && this->y == op->y && this->z == op->z;
return false;
}
int x;
int y;
int z;
};
using __gnu_test::tracker_allocator;
using Counter = __gnu_test::tracker_allocator_counter;
using Polymorphic = std::polymorphic<Base, tracker_allocator<Base>>;
const Polymorphic val(std::in_place_type<Derived>, 1, 2, 3);
void
verifyNoAllocations()
{
VERIFY( Counter::get_allocation_count() == 0 );
VERIFY( Counter::get_deallocation_count() == 0 );
VERIFY( Counter::get_construct_count() == 0 );
VERIFY( Counter::get_destruct_count() == 0 );
}
void
test_ctor()
{
std::optional<Polymorphic> src;
auto make = [&src] -> Polymorphic&& {
src.emplace(val);
Counter::reset();
return std::move(*src);
};
Polymorphic i1(make());
VERIFY( src->valueless_after_move() );
VERIFY( *i1 == *val );
verifyNoAllocations();
Polymorphic i2(std::allocator_arg, {}, make());
VERIFY( src->valueless_after_move() );
VERIFY( *i2 == *val );
verifyNoAllocations();
}
void
test_assign()
{
std::optional<Polymorphic> src;
auto make = [&src] -> Polymorphic&& {
src.emplace(val);
Counter::reset();
return std::move(*src);
};
Polymorphic i1(std::in_place_type<Derived>);
i1 = make();
VERIFY( src->valueless_after_move() );
VERIFY( *i1 == *val );
VERIFY( Counter::get_allocation_count() == 0 );
VERIFY( Counter::get_deallocation_count() >= sizeof(Derived) );
VERIFY( Counter::get_construct_count() == 0 );
VERIFY( Counter::get_destruct_count() == 1 );
auto(std::move(i1));
i1 = make();
VERIFY( *i1 == *val );
VERIFY( src->valueless_after_move() );
verifyNoAllocations();
}
void
test_swap()
{
const Polymorphic val1(std::in_place_type<Derived>, 1, 2, 3);
const Polymorphic val2(std::in_place_type<Derived>, 2, 4, 6);
Polymorphic i1(val1);
Polymorphic i2(val2);
Counter::reset();
i1.swap(i2);
VERIFY( *i2 == *val1 );
VERIFY( *i1 == *val2 );
verifyNoAllocations();
auto(std::move(i1));
Counter::reset();
i1.swap(i2);
VERIFY( *i1 == *val1 );
VERIFY( i2.valueless_after_move() );
verifyNoAllocations();
}
void
test_valueless()
{
auto e = [] {
Polymorphic res(std::in_place_type<Derived>);
auto(std::move(res));
Counter::reset();
return res;
};
Polymorphic i1(e());
VERIFY( i1.valueless_after_move() );
verifyNoAllocations();
Polymorphic i2(std::allocator_arg, {}, e());
VERIFY( i2.valueless_after_move() );
verifyNoAllocations();
Polymorphic i3(val);
i3 = e();
VERIFY( Counter::get_allocation_count() == 0 );
VERIFY( Counter::get_deallocation_count() >= sizeof(Derived) );
VERIFY( Counter::get_construct_count() == 0 );
VERIFY( Counter::get_destruct_count() == 1 );
i3 = e();
verifyNoAllocations();
}
constexpr void
test_constexpr()
{
using Polymorphic = std::polymorphic<Base, __gnu_test::uneq_allocator<Base>>;
const Polymorphic val(std::in_place_type<Derived>, 1, 2, 3);
std::optional<Polymorphic> src;
auto make = [&src, &val] -> Polymorphic&& {
src.emplace(val);
return std::move(*src);
};
Polymorphic i1(make());
VERIFY( src->valueless_after_move() );
VERIFY( *i1 == *val );
Polymorphic i2(std::allocator_arg, {}, make());
VERIFY( src->valueless_after_move() );
VERIFY( *i2 == *val );
i1 = make();
VERIFY( src->valueless_after_move() );
VERIFY( *i1 == *val );
auto(std::move(i1));
i1 = make();
VERIFY( *i1 == *val );
VERIFY( src->valueless_after_move() );
const Polymorphic val1(std::in_place_type<Derived>, 1, 2, 3);
const Polymorphic val2(std::in_place_type<Derived>, 2, 4, 6);
Polymorphic s1(val1);
Polymorphic s2(val2);
s1.swap(s2);
VERIFY( *s2 == *val1 );
VERIFY( *s1 == *val2 );
auto(std::move(s1));
s1.swap(s2);
VERIFY( *s1 == *val1 );
VERIFY( s2.valueless_after_move() );
auto e = [] {
Polymorphic res(std::in_place_type<Derived>);
auto(std::move(res));
return res;
};
Polymorphic e1(e());
VERIFY( e1.valueless_after_move() );
Polymorphic e2(std::allocator_arg, {}, e());
VERIFY( e2.valueless_after_move() );
Polymorphic e3(val);
e3 = e();
e3 = e();
}
int main()
{
test_ctor();
test_assign();
test_swap();
test_valueless();
test_constexpr();
static_assert([] {
test_constexpr();
return true;
}());
}
|