-
Notifications
You must be signed in to change notification settings - Fork 6
/
test-inout.cpp
330 lines (277 loc) · 9.91 KB
/
test-inout.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
329
330
#include <https://raw.githubusercontent.com/hsutter/misc/master/hst.h>
#include <iostream>
//------------------------------------------------------------------------------
// "Inout" 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) { }
// Helper, to test side effects
template<typename T>
requires std::is_arithmetic_v<T>
void modify(T& t) { ++t; }
//- Built-in type --------------------------------------------------------------
// Passing a small trivial type should be by pointer
void int_inout(inout int t, int* p) {
hst::history += &t==p ? "pass-by-pointer " : "pass-by-copy ";
modify(t);
}
//- Nontrivial concrete type ---------------------------------------------------
using String = hst::noisy<std::string>;
void modify(String& t) { t.t.append("xyzzy "); }
// Just plain "inout" with no attempt to copy, function just reads its param
void string_inout(inout String t) {
(void)t;
modify(t);
}
// "inout+copy" where the only thing in the body is an attempt to copy
void string_inout_copy(inout String t) {
auto local = t; // should always be a copy
modify(t);
}
// "inout+copy" with a more complex path, where the last use is a copy attempt
void string_inout_copy_last(inout String t) {
if (rand()%2) {
auto local = t; // should always be a copy
} else {
auto local2 = t; // should always be a copy
}
String last_use;
last_use = t; // should always be a copy
modify(t);
}
//- Template -------------------------------------------------------------------
// Just plain "inout" with no attempt to copy, function just reads its param
template<typename T>
void t_inout(inout T t) {
(void)t;
modify(t);
}
// "inout+copy" where the only thing in the body is an attempt to copy
template<typename T>
void t_inout_copy(inout T t) {
copy_from(t); // should always be a copy
modify(t);
}
// "inout+copy" with a more complex path, where the last use is a copy attempt
template<typename T>
void t_inout_copy_last(inout 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 always be a copy
modify(t);
}
//- Comparison with traditional ------------------------------------------------
template<typename T>
void traditional_inout(T& t) {
hst::history += "pass-by-pointer ";
copy_from(t);
modify(t);
}
template<typename T>
void new_inout(inout 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);
modify(t);
}
//------------------------------------------------------------------------------
// Test cases: inout
void inout_tests() {
hst::tester test("inout parameter cases");
//------------------------------------------------------------------------------
// Pass trivial lvalue: Should pass by pointer
test.run(
"inout with trivial lvalue",
[]{
int i = 0;
int_inout(i, &i);
hst::history += std::to_string(i);
},
"pass-by-pointer 1");
//------------------------------------------------------------------------------
// Pass nontrivial lvalue: Should pass by ptr/ref, then copy inside string_inout_copy*
test.run(
"inout with nontrivial lvalue",
[]{
String s;
string_inout(s);
hst::history += s.t;
},
"default-ctor xyzzy dtor ");
test.run(
"inout_copy with nontrivial lvalue",
[]{
String s;
string_inout_copy(s);
hst::history += s.t;
},
"default-ctor copy-ctor dtor xyzzy dtor ");
test.run(
"inout_copy_last with nontrivial lvalue",
[]{
String s;
string_inout_copy_last(s);
hst::history += s.t;
},
"default-ctor copy-ctor dtor default-ctor copy-assign dtor xyzzy dtor ");
test.run(
"templated inout with nontrivial lvalue",
[]{
String s;
t_inout(s);
hst::history += s.t;
},
"default-ctor xyzzy dtor ");
test.run(
"templated inout_copy with nontrivial lvalue",
[]{
String s;
t_inout_copy(s);
hst::history += s.t;
},
"default-ctor copy-ctor dtor xyzzy dtor ");
test.run(
"templated inout_copy_last with nontrivial lvalue",
[]{
String s;
t_inout_copy_last(s);
hst::history += s.t;
},
"default-ctor copy-ctor dtor copy-ctor dtor xyzzy dtor ");
//------------------------------------------------------------------------------
// Pass nontrivial xvalue: Should be rejected (which we detect by using the 'in' overload)
test.run(
"inout with nontrivial xvalue",
[]{
String s;
HST_CAN_INVOKE(string_inout)(move(s));
},
"default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
test.run(
"inout_copy with nontrivial xvalue",
[]{
String s;
HST_CAN_INVOKE(string_inout_copy)(move(s));
},
"default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
test.run(
"inout_copy_last with nontrivial xvalue",
[]{
String s;
HST_CAN_INVOKE(string_inout_copy_last)(move(s));
},
"default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
// test.run(
// "templated inout with nontrivial xvalue",
// []{
// String s;
// HST_CAN_INVOKE(t_inout<String>)(move(s));
// },
// "default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
// test.run(
// "templated inout_copy with nontrivial xvalue",
// []{
// String s;
// HST_CAN_INVOKE(t_inout_copy<String>)(move(s));
// },
// "default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
// test.run(
// "templated inout_copy_last with nontrivial xvalue",
// []{
// String s;
// HST_CAN_INVOKE(t_inout_copy_last<String>)(move(s));
// },
// "default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
//------------------------------------------------------------------------------
// Pass nontrivial prvalue: Should be rejected (which we detect by using the 'in' overload)
test.run(
"inout_copy with nontrivial prvalue",
[]{
HST_CAN_INVOKE(string_inout)(String());
},
"default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
test.run(
"inout_copy with nontrivial prvalue",
[]{
HST_CAN_INVOKE(string_inout_copy)(String());
},
"default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
test.run(
"inout_copy_last with nontrivial prvalue",
[]{
HST_CAN_INVOKE(string_inout_copy_last)(String());
},
"default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
// test.run(
// "templated inout_copy with nontrivial prvalue",
// []{
// HST_CAN_INVOKE(t_inout<String>)(String());
// },
// "default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
// test.run(
// "templated inout_copy with nontrivial prvalue",
// []{
// HST_CAN_INVOKE(t_inout_copy<String>)(String());
// },
// "default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
// test.run(
// "templated inout_copy_last with nontrivial prvalue",
// []{
// HST_CAN_INVOKE(t_inout_copy_last<String>)(String());
// },
// "default-ctor cannot-invoke dtor "); // i.e., without 'in' overload would fail to compile
//------------------------------------------------------------------------------
// Compare traditional_inout and new_inout
// test.run(
// "inout equivalence with traditional, trivial lvalue",
// []{
// int i = 0;
// new_inout(i, &i); // TODO: this is currently ambiguous, but should not be
// hst::history += std::to_string(i);
// },
// []{
// int i = 0;
// traditional_inout(i);
// hst::history += std::to_string(i);
// });
// }
test.run(
"inout equivalence with traditional, nontrivial lvalue",
[]{
String s;
new_inout(s, &s);
hst::history += s.t;
},
[]{
String s;
traditional_inout(s);
hst::history += s.t;
});
// test.run(
// "inout equivalence with traditional, nontrivial xvalue",
// []{
// String s;
// HST_CAN_INVOKE(new_inout)(move(s));
// },
// []{
// String s;
// HST_CAN_INVOKE(traditional_inout)(move(s));
// });
// test.run(
// "inout equivalence with traditional, nontrivial prvalue",
// []{
// HST_CAN_INVOKE(new_inout)(String());
// },
// []{
// HST_CAN_INVOKE(traditional_inout)(String());
// });
std::cout << test.summary();
}
//------------------------------------------------------------------------------
// One main to run them all
int main() {
inout_tests();
}