-
Notifications
You must be signed in to change notification settings - Fork 6
/
test-in.cpp
328 lines (274 loc) · 8.64 KB
/
test-in.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <https://raw.githubusercontent.com/hsutter/misc/master/hst.h>
#include <iostream>
//------------------------------------------------------------------------------
// "In" tests
// Helper, just to try a different kind of copy than initializing/assigning a
// local variable (in case the difference matters).
template<typename T>
void copy_from(T) { }
//- Built-in type --------------------------------------------------------------
// Passing a small trivial type should be a copy
void int_in(in int t, int* p) {
hst::history += &t==p ? "pass-by-pointer " : "pass-by-copy ";
}
//- Nontrivial concrete type ---------------------------------------------------
using String = hst::noisy<std::string>;
// Just plain "in" with no attempt to copy, function just reads its param
void string_in(in String t) {
(void)t;
}
// "in+copy" where the only thing in the body is an attempt to copy
// (which should invoke move if arg is an rvalue)
void string_in_copy(in String t) {
String local;
local = t; // should be a move if arg is an rvalue
}
// "in+copy" with a more complex path, where the last use is a copy attempt
// (which should invoke move if arg is an rvalue)
void string_in_copy_last(in String t) {
if (rand()%2) {
String local;
local = t; // should always be a copy
} else {
String local2;
local2 = t; // should always be a copy
}
String last_use;
last_use = t; // should be a move assignment if arg is an rvalue
}
//- Template -------------------------------------------------------------------
// Just plain "in" with no attempt to copy, function just reads its param
template<typename T>
void t_in(in T t) {
(void)t;
}
// "in+copy" where the only thing in the body is an attempt to copy
// (which should invoke move if arg is an rvalue)
template<typename T>
void t_in_copy(in T t) {
copy_from(t); // should be a move if arg is an rvalue
}
// "in+copy" with a more complex path, where the last use is a copy attempt
// (which should invoke move if arg is an rvalue)
template<typename T>
void t_in_copy_last(in T t) {
if (rand()%2) {
copy_from(t); // should always be a copy
} else {
copy_from(t); // should always be a copy
}
copy_from(t); // should be a move if arg is an rvalue
}
//- Comparison with traditional ------------------------------------------------
template<typename T> constexpr bool should_pass_by_value_v
= std::is_trivially_copyable_v<T> && sizeof(T) < 8;
template<typename T>
requires should_pass_by_value_v<T>
void traditional_in(T t) {
hst::history += "pass-by-copy ";
copy_from(t);
}
template<typename T>
requires (!should_pass_by_value_v<T>)
void traditional_in(const T& t) {
hst::history += "pass-by-pointer ";
copy_from(t);
}
template<typename T>
requires ( !should_pass_by_value_v<T>
&& !std::is_reference_v<T>) // don’t grab non-const lvalues
void traditional_in(T&& t) {
copy_from(forward<T>(t));
}
template<typename T>
void new_in(in T t, T* p = nullptr) { // p is &arg or null
if (p) hst::history += (&t == p) ? "pass-by-pointer " : "pass-by-copy ";
copy_from(t);
}
//------------------------------------------------------------------------------
// Test cases: in
void in_tests() {
hst::tester test("in parameter cases");
//------------------------------------------------------------------------------
// Pass trivial lvalue: Should pass by copy
test.run(
"in with trivial lvalue",
[]{
int i = 0;
int_in(i, &i);
},
"pass-by-copy ");
//------------------------------------------------------------------------------
// Pass nontrivial lvalue: Should pass by ptr/ref, then copy inside string_in_copy*
test.run(
"in with nontrivial lvalue",
[]{
String s;
string_in(s);
},
"default-ctor dtor ");
test.run(
"in_copy with nontrivial lvalue",
[]{
String s;
string_in_copy(s);
},
"default-ctor default-ctor copy-assign dtor dtor ");
test.run(
"in_copy_last with nontrivial lvalue",
[]{
String s;
string_in_copy_last(s);
},
"default-ctor default-ctor copy-assign dtor default-ctor copy-assign dtor dtor ");
test.run(
"templated in with nontrivial lvalue",
[]{
String s;
t_in(s);
},
"default-ctor dtor ");
test.run(
"templated in_copy with nontrivial lvalue",
[]{
String s;
t_in_copy(s);
},
"default-ctor copy-ctor dtor dtor ");
test.run(
"templated in_copy_last with nontrivial lvalue",
[]{
String s;
t_in_copy_last(s);
},
"default-ctor copy-ctor dtor copy-ctor dtor dtor ");
//------------------------------------------------------------------------------
// Pass nontrivial xvalue: Should pass by ptr/ref, then move inside string_in_copy*
test.run(
"in with nontrivial xvalue",
[]{
String s;
string_in(move(s));
},
"default-ctor dtor ");
test.run(
"in_copy with nontrivial xvalue",
[]{
String s;
string_in_copy(move(s));
},
"default-ctor default-ctor move-assign dtor dtor ");
test.run(
"in_copy_last with nontrivial xvalue",
[]{
String s;
string_in_copy_last(move(s));
},
"default-ctor default-ctor copy-assign dtor default-ctor move-assign dtor dtor ");
test.run(
"templated in with nontrivial xvalue",
[]{
String s;
t_in(move(s));
},
"default-ctor dtor ");
test.run(
"templated in_copy with nontrivial xvalue",
[]{
String s;
t_in_copy(move(s));
},
"default-ctor move-ctor dtor dtor ");
test.run(
"templated in_copy_last with nontrivial xvalue",
[]{
String s;
t_in_copy_last(move(s));
},
"default-ctor copy-ctor dtor move-ctor dtor dtor ");
//------------------------------------------------------------------------------
// Pass nontrivial prvalue: Should pass by ptr/ref, then move inside string_in_copy*
test.run(
"in_copy with nontrivial prvalue",
[]{
string_in(String());
},
"default-ctor dtor ");
test.run(
"in_copy with nontrivial prvalue",
[]{
string_in_copy(String());
},
"default-ctor default-ctor move-assign dtor dtor ");
test.run(
"in_copy_last with nontrivial prvalue",
[]{
string_in_copy_last(String());
},
"default-ctor default-ctor copy-assign dtor default-ctor move-assign dtor dtor ");
test.run(
"templated in_copy with nontrivial prvalue",
[]{
t_in(String());
},
"default-ctor dtor ");
test.run(
"templated in_copy with nontrivial prvalue",
[]{
t_in_copy(String());
},
"default-ctor move-ctor dtor dtor ");
test.run(
"templated in_copy_last with nontrivial prvalue",
[]{
t_in_copy_last(String());
},
"default-ctor copy-ctor dtor move-ctor dtor dtor ");
//------------------------------------------------------------------------------
// Compare traditional_in and new_in
test.run(
"in equivalence with traditional, trivial lvalue",
[]{
int i = 0;
new_in(i, &i);
},
[]{
int i = 0;
traditional_in(i);
});
test.run(
"in equivalence with traditional, nontrivial lvalue",
[]{
String s;
new_in(s, &s);
},
[]{
String s;
traditional_in(s);
});
test.run(
"in equivalence with traditional, nontrivial xvalue",
[]{
String s;
new_in(move(s));
},
[]{
String s;
traditional_in(move(s));
});
test.run(
"in equivalence with traditional, nontrivial prvalue",
[]{
int i = 0;
new_in(String());
},
[]{
traditional_in(String());
});
std::cout << test.summary();
}
//------------------------------------------------------------------------------
// One main to run them all
int main() {
in_tests();
}