-
Notifications
You must be signed in to change notification settings - Fork 350
/
Copy pathtests.cc
423 lines (339 loc) · 11 KB
/
tests.cc
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "tests/ffi/tests.h"
#include "tests/ffi/lib.rs.h"
#include <cstring>
#include <iterator>
#include <numeric>
#include <stdexcept>
extern "C" void cxx_test_suite_set_correct() noexcept;
extern "C" tests::R *cxx_test_suite_get_box() noexcept;
extern "C" bool cxx_test_suite_r_is_correct(const tests::R *) noexcept;
namespace tests {
static constexpr char SLICE_DATA[] = "2020";
C::C(size_t n) : n(n) {}
size_t C::get() const { return this->n; }
size_t C::get2() const { return this->n; }
size_t C::set(size_t n) {
this->n = n;
return this->n;
}
size_t C::set2(size_t n) {
this->n = n;
return this->n;
}
size_t C::set_succeed(size_t n) { return this->set2(n); }
size_t C::get_fail() { throw std::runtime_error("unimplemented"); }
const std::vector<uint8_t> &C::get_v() const { return this->v; }
std::vector<uint8_t> &C::get_v() { return this->v; }
size_t c_return_primitive() { return 2020; }
Shared c_return_shared() { return Shared{2020}; }
rust::Box<R> c_return_box() {
return rust::Box<R>::from_raw(cxx_test_suite_get_box());
}
std::unique_ptr<C> c_return_unique_ptr() {
return std::unique_ptr<C>(new C{2020});
}
const size_t &c_return_ref(const Shared &shared) { return shared.z; }
size_t &c_return_mut(Shared &shared) { return shared.z; }
rust::Str c_return_str(const Shared &shared) {
(void)shared;
return "2020";
}
rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
(void)shared;
return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA),
sizeof(SLICE_DATA));
}
rust::String c_return_rust_string() { return "2020"; }
std::unique_ptr<std::string> c_return_unique_ptr_string() {
return std::unique_ptr<std::string>(new std::string("2020"));
}
std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
vec->push_back(86);
vec->push_back(75);
vec->push_back(30);
vec->push_back(9);
return vec;
}
std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
vec->push_back(86.0);
vec->push_back(75.0);
vec->push_back(30.0);
vec->push_back(9.5);
return vec;
}
std::unique_ptr<std::vector<std::string>> c_return_unique_ptr_vector_string() {
return std::unique_ptr<std::vector<std::string>>(
new std::vector<std::string>());
}
std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
vec->push_back(Shared{1010});
vec->push_back(Shared{1011});
return vec;
}
std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
return std::unique_ptr<std::vector<C>>(new std::vector<C>());
}
const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
return c.get_v();
}
std::vector<uint8_t> &c_return_mut_vector(C &c) { return c.get_v(); }
rust::Vec<uint8_t> c_return_rust_vec() {
throw std::runtime_error("unimplemented");
}
const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
(void)c;
throw std::runtime_error("unimplemented");
}
rust::Vec<uint8_t> &c_return_mut_rust_vec(C &c) {
(void)c;
throw std::runtime_error("unimplemented");
}
rust::Vec<rust::String> c_return_rust_vec_string() {
throw std::runtime_error("unimplemented");
}
size_t c_return_identity(size_t n) { return n; }
size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
Enum c_return_enum(uint16_t n) {
if (n <= static_cast<uint16_t>(Enum::AVal)) {
return Enum::AVal;
} else if (n <= static_cast<uint16_t>(Enum::BVal)) {
return Enum::BVal;
} else {
return Enum::CVal;
}
}
void c_take_primitive(size_t n) {
if (n == 2020) {
cxx_test_suite_set_correct();
}
}
void c_take_shared(Shared shared) {
if (shared.z == 2020) {
cxx_test_suite_set_correct();
}
}
void c_take_box(rust::Box<R> r) {
if (cxx_test_suite_r_is_correct(&*r)) {
cxx_test_suite_set_correct();
}
}
void c_take_unique_ptr(std::unique_ptr<C> c) {
if (c->get() == 2020) {
cxx_test_suite_set_correct();
}
}
void c_take_ref_r(const R &r) {
if (cxx_test_suite_r_is_correct(&r)) {
cxx_test_suite_set_correct();
}
}
void c_take_ref_c(const C &c) {
if (c.get() == 2020) {
cxx_test_suite_set_correct();
}
}
void c_take_str(rust::Str s) {
if (std::string(s) == "2020") {
cxx_test_suite_set_correct();
}
}
void c_take_sliceu8(rust::Slice<uint8_t> s) {
if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
"2020") {
cxx_test_suite_set_correct();
}
}
void c_take_rust_string(rust::String s) {
if (std::string(s) == "2020") {
cxx_test_suite_set_correct();
}
}
void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
if (*s == "2020") {
cxx_test_suite_set_correct();
}
}
void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
if (v->size() == 4) {
cxx_test_suite_set_correct();
}
}
void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
if (v->size() == 4) {
cxx_test_suite_set_correct();
}
}
void c_take_unique_ptr_vector_string(
std::unique_ptr<std::vector<std::string>> v) {
(void)v;
cxx_test_suite_set_correct();
}
void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
if (v->size() == 2) {
cxx_test_suite_set_correct();
}
}
void c_take_ref_vector(const std::vector<uint8_t> &v) {
if (v.size() == 4) {
cxx_test_suite_set_correct();
}
}
void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
void c_take_rust_vec_index(rust::Vec<uint8_t> v) {
try {
v.at(100);
} catch (const std::out_of_range &ex) {
std::string expected = "rust::Vec index out of range";
if (ex.what() == expected) {
cxx_test_suite_set_correct();
}
}
}
void c_take_rust_vec_shared(rust::Vec<Shared> v) {
uint32_t sum = 0;
for (auto i : v) {
sum += i.z;
}
if (sum == 2021) {
cxx_test_suite_set_correct();
}
}
void c_take_rust_vec_string(rust::Vec<rust::String> v) {
(void)v;
cxx_test_suite_set_correct();
}
void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
// Exercise requirements of ForwardIterator
// https://en.cppreference.com/w/cpp/named_req/ForwardIterator
uint32_t sum = 0;
for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
sum += it->z;
}
if (sum == 2021) {
cxx_test_suite_set_correct();
}
}
void c_take_rust_vec_shared_index(rust::Vec<Shared> v) {
if (v[0].z == 1010 && v.at(0).z == 1010 && v.front().z == 1010 &&
v[1].z == 1011 && v.at(1).z == 1011 && v.back().z == 1011) {
cxx_test_suite_set_correct();
}
}
void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
if (sum == 200) {
cxx_test_suite_set_correct();
}
}
void c_take_ref_rust_vec_string(const rust::Vec<rust::String> &v) {
(void)v;
cxx_test_suite_set_correct();
}
void c_take_ref_rust_vec_index(const rust::Vec<uint8_t> &v) {
if (v[0] == 86 && v.at(0) == 86 && v.front() == 86 && v[1] == 75 &&
v.at(1) == 75 && v[3] == 9 && v.at(3) == 9 && v.back() == 9) {
cxx_test_suite_set_correct();
}
}
void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
// The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
// requirements for std::iterator_traits.
// https://en.cppreference.com/w/cpp/iterator/iterator_traits
std::vector<uint8_t> stdv;
std::copy(v.begin(), v.end(), std::back_inserter(stdv));
uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
if (sum == 200) {
cxx_test_suite_set_correct();
}
}
/*
// https://github.com/dtolnay/cxx/issues/232
void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
callback("2020");
}
*/
void c_take_enum(Enum e) {
if (e == Enum::AVal) {
cxx_test_suite_set_correct();
}
}
void c_try_return_void() {}
size_t c_try_return_primitive() { return 2020; }
size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
rust::Box<R> c_try_return_box() { return c_return_box(); }
const rust::String &c_try_return_ref(const rust::String &s) { return s; }
rust::Str c_try_return_str(rust::Str s) { return s; }
rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
rust::String c_try_return_rust_string() { return c_return_rust_string(); }
std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
return c_return_unique_ptr_string();
}
rust::Vec<uint8_t> c_try_return_rust_vec() {
throw std::runtime_error("unimplemented");
}
rust::Vec<rust::String> c_try_return_rust_vec_string() {
throw std::runtime_error("unimplemented");
}
const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
(void)c;
throw std::runtime_error("unimplemented");
}
extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
return std::unique_ptr<C>(new C{2020}).release();
}
extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
return std::unique_ptr<std::string>(new std::string("2020")).release();
}
extern "C" const char *cxx_run_test() noexcept {
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define ASSERT(x) \
do { \
if (!(x)) { \
return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
} \
} while (false)
ASSERT(r_return_primitive() == 2020);
ASSERT(r_return_shared().z == 2020);
ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
ASSERT(r_return_unique_ptr()->get() == 2020);
ASSERT(r_return_ref(Shared{2020}) == 2020);
ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
ASSERT(std::string(r_return_rust_string()) == "2020");
ASSERT(*r_return_unique_ptr_string() == "2020");
ASSERT(r_return_identity(2020) == 2020);
ASSERT(r_return_sum(2020, 1) == 2021);
ASSERT(r_return_enum(0) == Enum::AVal);
ASSERT(r_return_enum(1) == Enum::BVal);
ASSERT(r_return_enum(2021) == Enum::CVal);
r_take_primitive(2020);
r_take_shared(Shared{2020});
r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
r_take_ref_c(C{2020});
r_take_str(rust::Str("2020"));
r_take_sliceu8(rust::Slice<uint8_t>(
reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
r_take_rust_string(rust::String("2020"));
r_take_unique_ptr_string(
std::unique_ptr<std::string>(new std::string("2020")));
r_take_enum(Enum::AVal);
ASSERT(r_try_return_primitive() == 2020);
try {
r_fail_return_primitive();
ASSERT(false);
} catch (const rust::Error &e) {
ASSERT(std::strcmp(e.what(), "rust error") == 0);
}
auto r2 = r_return_r2(2020);
ASSERT(r2->get() == 2020);
ASSERT(r2->set(2021) == 2021);
ASSERT(r2->get() == 2021);
ASSERT(r2->set(2020) == 2020);
ASSERT(r2->get() == 2020);
cxx_test_suite_set_correct();
return nullptr;
}
} // namespace tests