1 #include "tests/ffi/tests.h"
2 #include "tests/ffi/lib.rs.h"
3 #include <cstdlib>
4 #include <cstring>
5 #include <iterator>
6 #include <memory>
7 #include <numeric>
8 #include <stdexcept>
9 #include <string>
10 #include <tuple>
11
12 extern "C" void cxx_test_suite_set_correct() noexcept;
13 extern "C" tests::R *cxx_test_suite_get_box() noexcept;
14 extern "C" bool cxx_test_suite_r_is_correct(const tests::R *) noexcept;
15
16 namespace tests {
17
18 static constexpr char SLICE_DATA[] = "2020";
19
C(size_t n)20 C::C(size_t n) : n(n) {}
21
get() const22 size_t C::get() const { return this->n; }
23
get2() const24 size_t C::get2() const { return this->n; }
25
getRef() const26 const size_t &C::getRef() const { return this->n; }
27
getMut()28 size_t &C::getMut() { return this->n; }
29
set(size_t n)30 size_t C::set(size_t n) {
31 this->n = n;
32 return this->n;
33 }
34
set_succeed(size_t n)35 size_t C::set_succeed(size_t n) { return this->set(n); }
36
get_fail()37 size_t C::get_fail() { throw std::runtime_error("unimplemented"); }
38
c_method_on_shared() const39 size_t Shared::c_method_on_shared() const noexcept { return 2021; }
40
c_method_ref_on_shared() const41 const size_t &Shared::c_method_ref_on_shared() const noexcept {
42 return this->z;
43 }
44
c_method_mut_on_shared()45 size_t &Shared::c_method_mut_on_shared() noexcept { return this->z; }
46
c_set_array(int32_t val)47 void Array::c_set_array(int32_t val) noexcept {
48 this->a = {val, val, val, val};
49 }
50
get_v() const51 const std::vector<uint8_t> &C::get_v() const { return this->v; }
52
get_v()53 std::vector<uint8_t> &C::get_v() { return this->v; }
54
c_return_primitive()55 size_t c_return_primitive() { return 2020; }
56
c_return_shared()57 Shared c_return_shared() { return Shared{2020}; }
58
c_return_ns_shared()59 ::A::AShared c_return_ns_shared() { return ::A::AShared{2020}; }
60
c_return_nested_ns_shared()61 ::A::B::ABShared c_return_nested_ns_shared() { return ::A::B::ABShared{2020}; }
62
c_return_box()63 rust::Box<R> c_return_box() {
64 Shared shared{0};
65 rust::Box<Shared> box{shared}; // explicit constructor from const T&
66 rust::Box<Shared> other{std::move(shared)}; // explicit constructor from T&&
67 box = std::move(other); // move assignment
68 rust::Box<Shared> box2(*box); // copy from another Box
69 rust::Box<Shared> other2(std::move(other)); // move constructor
70 rust::Box<Shared>::in_place(shared.z); // placement static factory
71 rust::Box<Shared>::in_place<size_t>(0);
72 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
73 }
74
c_return_unique_ptr()75 std::unique_ptr<C> c_return_unique_ptr() {
76 return std::unique_ptr<C>(new C{2020});
77 }
78
c_return_shared_ptr()79 std::shared_ptr<C> c_return_shared_ptr() {
80 return std::shared_ptr<C>(new C{2020});
81 }
82
c_return_ns_unique_ptr()83 std::unique_ptr<::H::H> c_return_ns_unique_ptr() {
84 return std::unique_ptr<::H::H>(new ::H::H{"hello"});
85 }
86
c_return_ref(const Shared & shared)87 const size_t &c_return_ref(const Shared &shared) { return shared.z; }
88
c_return_ns_ref(const::A::AShared & shared)89 const size_t &c_return_ns_ref(const ::A::AShared &shared) { return shared.z; }
90
c_return_nested_ns_ref(const::A::B::ABShared & shared)91 const size_t &c_return_nested_ns_ref(const ::A::B::ABShared &shared) {
92 return shared.z;
93 }
94
c_return_mut(Shared & shared)95 size_t &c_return_mut(Shared &shared) { return shared.z; }
96
c_return_str(const Shared & shared)97 rust::Str c_return_str(const Shared &shared) {
98 (void)shared;
99 return "2020";
100 }
101
c_return_slice_char(const Shared & shared)102 rust::Slice<const char> c_return_slice_char(const Shared &shared) {
103 (void)shared;
104 return rust::Slice<const char>(SLICE_DATA, sizeof(SLICE_DATA));
105 }
106
c_return_mutsliceu8(rust::Slice<uint8_t> slice)107 rust::Slice<uint8_t> c_return_mutsliceu8(rust::Slice<uint8_t> slice) {
108 return slice;
109 }
110
c_return_rust_string()111 rust::String c_return_rust_string() { return "2020"; }
112
c_return_rust_string_lossy()113 rust::String c_return_rust_string_lossy() {
114 return rust::String::lossy("Hello \xf0\x90\x80World");
115 }
116
c_return_unique_ptr_string()117 std::unique_ptr<std::string> c_return_unique_ptr_string() {
118 return std::unique_ptr<std::string>(new std::string("2020"));
119 }
120
c_return_unique_ptr_vector_u8()121 std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
122 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
123 vec->push_back(86);
124 vec->push_back(75);
125 vec->push_back(30);
126 vec->push_back(9);
127 return vec;
128 }
129
c_return_unique_ptr_vector_f64()130 std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
131 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
132 vec->push_back(86.0);
133 vec->push_back(75.0);
134 vec->push_back(30.0);
135 vec->push_back(9.5);
136 return vec;
137 }
138
c_return_unique_ptr_vector_string()139 std::unique_ptr<std::vector<std::string>> c_return_unique_ptr_vector_string() {
140 return std::unique_ptr<std::vector<std::string>>(
141 new std::vector<std::string>());
142 }
143
c_return_unique_ptr_vector_shared()144 std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
145 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
146 vec->push_back(Shared{1010});
147 vec->push_back(Shared{1011});
148 return vec;
149 }
150
c_return_unique_ptr_vector_opaque()151 std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
152 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
153 }
154
c_return_ref_vector(const C & c)155 const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
156 return c.get_v();
157 }
158
c_return_mut_vector(C & c)159 std::vector<uint8_t> &c_return_mut_vector(C &c) { return c.get_v(); }
160
c_return_rust_vec_u8()161 rust::Vec<uint8_t> c_return_rust_vec_u8() {
162 rust::Vec<uint8_t> vec{2, 0, 2, 0};
163 return vec;
164 }
165
c_return_ref_rust_vec(const C & c)166 const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
167 (void)c;
168 throw std::runtime_error("unimplemented");
169 }
170
c_return_mut_rust_vec(C & c)171 rust::Vec<uint8_t> &c_return_mut_rust_vec(C &c) {
172 (void)c;
173 throw std::runtime_error("unimplemented");
174 }
175
c_return_rust_vec_string()176 rust::Vec<rust::String> c_return_rust_vec_string() {
177 return {"2", "0", "2", "0"};
178 }
179
c_return_rust_vec_bool()180 rust::Vec<bool> c_return_rust_vec_bool() { return {true, true, false}; }
181
c_return_identity(size_t n)182 size_t c_return_identity(size_t n) { return n; }
183
c_return_sum(size_t n1,size_t n2)184 size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
185
c_return_enum(uint16_t n)186 Enum c_return_enum(uint16_t n) {
187 if (n <= static_cast<uint16_t>(Enum::AVal)) {
188 return Enum::AVal;
189 } else if (n <= static_cast<uint16_t>(Enum::BVal)) {
190 return Enum::BVal;
191 } else {
192 return Enum::CVal;
193 }
194 }
195
c_return_ns_enum(uint16_t n)196 ::A::AEnum c_return_ns_enum(uint16_t n) {
197 if (n <= static_cast<uint16_t>(::A::AEnum::AAVal)) {
198 return ::A::AEnum::AAVal;
199 } else if (n <= static_cast<uint16_t>(::A::AEnum::ABVal)) {
200 return ::A::AEnum::ABVal;
201 } else {
202 return ::A::AEnum::ACVal;
203 }
204 }
205
c_return_nested_ns_enum(uint16_t n)206 ::A::B::ABEnum c_return_nested_ns_enum(uint16_t n) {
207 if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABAVal)) {
208 return ::A::B::ABEnum::ABAVal;
209 } else if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABBVal)) {
210 return ::A::B::ABEnum::ABBVal;
211 } else {
212 return ::A::B::ABEnum::ABCVal;
213 }
214 }
215
c_return_const_ptr(size_t c)216 const C *c_return_const_ptr(size_t c) { return new C(c); }
217
c_return_mut_ptr(size_t c)218 C *c_return_mut_ptr(size_t c) { return new C(c); }
219
Borrow(const std::string & s)220 Borrow::Borrow(const std::string &s) : s(s) {}
221
const_member() const222 void Borrow::const_member() const {}
223
nonconst_member()224 void Borrow::nonconst_member() {}
225
c_return_borrow(const std::string & s)226 std::unique_ptr<Borrow> c_return_borrow(const std::string &s) {
227 return std::unique_ptr<Borrow>(new Borrow(s));
228 }
229
c_take_primitive(size_t n)230 void c_take_primitive(size_t n) {
231 if (n == 2020) {
232 cxx_test_suite_set_correct();
233 }
234 }
235
c_take_shared(Shared shared)236 void c_take_shared(Shared shared) {
237 if (shared.z == 2020) {
238 cxx_test_suite_set_correct();
239 }
240 }
241
c_take_ns_shared(::A::AShared shared)242 void c_take_ns_shared(::A::AShared shared) {
243 if (shared.z == 2020) {
244 cxx_test_suite_set_correct();
245 }
246 }
247
c_take_nested_ns_shared(::A::B::ABShared shared)248 void c_take_nested_ns_shared(::A::B::ABShared shared) {
249 if (shared.z == 2020) {
250 cxx_test_suite_set_correct();
251 }
252 }
253
c_take_box(rust::Box<R> r)254 void c_take_box(rust::Box<R> r) {
255 if (cxx_test_suite_r_is_correct(&*r)) {
256 cxx_test_suite_set_correct();
257 }
258 }
259
c_take_unique_ptr(std::unique_ptr<C> c)260 void c_take_unique_ptr(std::unique_ptr<C> c) {
261 if (c->get() == 2020) {
262 cxx_test_suite_set_correct();
263 }
264 }
265
c_take_ref_r(const R & r)266 void c_take_ref_r(const R &r) {
267 if (cxx_test_suite_r_is_correct(&r)) {
268 cxx_test_suite_set_correct();
269 }
270 }
271
c_take_ref_c(const C & c)272 void c_take_ref_c(const C &c) {
273 if (c.get() == 2020) {
274 cxx_test_suite_set_correct();
275 }
276 }
277
c_take_ref_ns_c(const::H::H & h)278 void c_take_ref_ns_c(const ::H::H &h) {
279 if (h.h == "hello") {
280 cxx_test_suite_set_correct();
281 }
282 }
283
c_take_str(rust::Str s)284 void c_take_str(rust::Str s) {
285 if (std::string(s) == "2020") {
286 cxx_test_suite_set_correct();
287 }
288 }
289
c_take_slice_char(rust::Slice<const char> s)290 void c_take_slice_char(rust::Slice<const char> s) {
291 if (std::string(s.data(), s.size()) == "2020") {
292 cxx_test_suite_set_correct();
293 }
294 }
295
c_take_slice_shared(rust::Slice<const Shared> s)296 void c_take_slice_shared(rust::Slice<const Shared> s) {
297 if (s.size() == 2 && s.data()->z == 2020 && s[1].z == 2021 &&
298 s.at(1).z == 2021 && s.front().z == 2020 && s.back().z == 2021) {
299 cxx_test_suite_set_correct();
300 }
301 }
302
c_take_slice_shared_sort(rust::Slice<Shared> s)303 void c_take_slice_shared_sort(rust::Slice<Shared> s) {
304 // Exercise requirements of RandomAccessIterator.
305 // https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
306 std::sort(s.begin(), s.end());
307 if (s[0].z == 0 && s[1].z == 2 && s[2].z == 4 && s[3].z == 7) {
308 cxx_test_suite_set_correct();
309 }
310 }
311
c_take_slice_r(rust::Slice<const R> s)312 void c_take_slice_r(rust::Slice<const R> s) {
313 if (s.size() == 3 && s[0].get() == 2020 && s[1].get() == 2050) {
314 cxx_test_suite_set_correct();
315 }
316 }
317
operator <(const R & a,const R & b)318 bool operator<(const R &a, const R &b) noexcept { return a.get() < b.get(); }
319
c_take_slice_r_sort(rust::Slice<R> s)320 void c_take_slice_r_sort(rust::Slice<R> s) {
321 std::qsort(s.data(), s.size(), rust::size_of<decltype(s)::value_type>(),
322 [](const void *fst, const void *snd) {
323 auto &a = *static_cast<const R *>(fst);
324 auto &b = *static_cast<const R *>(snd);
325 return a < b ? -1 : b < a ? 1 : 0;
326 });
327 if (s[0].get() == 2020 && s[1].get() == 2021 && s[2].get() == 2050) {
328 cxx_test_suite_set_correct();
329 }
330 }
331
c_take_rust_string(rust::String s)332 void c_take_rust_string(rust::String s) {
333 if (std::string(s) == "2020") {
334 cxx_test_suite_set_correct();
335 }
336 }
337
c_take_unique_ptr_string(std::unique_ptr<std::string> s)338 void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
339 if (*s == "2020") {
340 cxx_test_suite_set_correct();
341 }
342 }
343
c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v)344 void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
345 if (v->size() == 3) {
346 cxx_test_suite_set_correct();
347 }
348 }
349
c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v)350 void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
351 if (v->size() == 5) {
352 cxx_test_suite_set_correct();
353 }
354 }
355
c_take_unique_ptr_vector_string(std::unique_ptr<std::vector<std::string>> v)356 void c_take_unique_ptr_vector_string(
357 std::unique_ptr<std::vector<std::string>> v) {
358 (void)v;
359 cxx_test_suite_set_correct();
360 }
361
c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v)362 void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
363 if (v->size() == 3) {
364 cxx_test_suite_set_correct();
365 }
366 }
367
c_take_ref_vector(const std::vector<uint8_t> & v)368 void c_take_ref_vector(const std::vector<uint8_t> &v) {
369 if (v.size() == 4) {
370 cxx_test_suite_set_correct();
371 }
372 }
373
c_take_rust_vec(rust::Vec<uint8_t> v)374 void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
375
c_take_rust_vec_index(rust::Vec<uint8_t> v)376 void c_take_rust_vec_index(rust::Vec<uint8_t> v) {
377 try {
378 v.at(100);
379 } catch (const std::out_of_range &ex) {
380 std::string expected = "rust::Vec index out of range";
381 if (ex.what() == expected) {
382 cxx_test_suite_set_correct();
383 }
384 }
385 }
386
c_take_rust_vec_shared(rust::Vec<Shared> v)387 void c_take_rust_vec_shared(rust::Vec<Shared> v) {
388 uint32_t sum = 0;
389 for (auto i : v) {
390 sum += i.z;
391 }
392 if (sum == 2021) {
393 cxx_test_suite_set_correct();
394 }
395 }
396
c_take_rust_vec_ns_shared(rust::Vec<::A::AShared> v)397 void c_take_rust_vec_ns_shared(rust::Vec<::A::AShared> v) {
398 uint32_t sum = 0;
399 for (auto i : v) {
400 sum += i.z;
401 }
402 if (sum == 2021) {
403 cxx_test_suite_set_correct();
404 }
405 }
406
c_take_rust_vec_nested_ns_shared(rust::Vec<::A::B::ABShared> v)407 void c_take_rust_vec_nested_ns_shared(rust::Vec<::A::B::ABShared> v) {
408 uint32_t sum = 0;
409 for (auto i : v) {
410 sum += i.z;
411 }
412 if (sum == 2021) {
413 cxx_test_suite_set_correct();
414 }
415 }
416
c_take_rust_vec_string(rust::Vec<rust::String> v)417 void c_take_rust_vec_string(rust::Vec<rust::String> v) {
418 (void)v;
419 cxx_test_suite_set_correct();
420 }
421
c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v)422 void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
423 // Exercise requirements of ForwardIterator
424 // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
425 uint32_t sum = 0, csum = 0;
426 for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
427 sum += it->z;
428 }
429 for (auto it = v.cbegin(), it_end = v.cend(); it != it_end; it++) {
430 csum += it->z;
431 }
432 if (sum == 2021 && csum == 2021) {
433 cxx_test_suite_set_correct();
434 }
435 }
436
c_take_rust_vec_shared_sort(rust::Vec<Shared> v)437 void c_take_rust_vec_shared_sort(rust::Vec<Shared> v) {
438 // Exercise requirements of RandomAccessIterator.
439 // https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
440 std::sort(v.begin(), v.end());
441 if (v[0].z == 0 && v[1].z == 2 && v[2].z == 4 && v[3].z == 7) {
442 cxx_test_suite_set_correct();
443 }
444 }
445
c_take_rust_vec_shared_index(rust::Vec<Shared> v)446 void c_take_rust_vec_shared_index(rust::Vec<Shared> v) {
447 if (v[0].z == 1010 && v.at(0).z == 1010 && v.front().z == 1010 &&
448 v[1].z == 1011 && v.at(1).z == 1011 && v.back().z == 1011) {
449 cxx_test_suite_set_correct();
450 }
451 }
452
c_take_rust_vec_shared_push(rust::Vec<Shared> v)453 void c_take_rust_vec_shared_push(rust::Vec<Shared> v) {
454 v.push_back(Shared{3});
455 v.emplace_back(Shared{2});
456 if (v[v.size() - 2].z == 3 && v.back().z == 2) {
457 cxx_test_suite_set_correct();
458 }
459 }
460
c_take_rust_vec_shared_truncate(rust::Vec<Shared> v)461 void c_take_rust_vec_shared_truncate(rust::Vec<Shared> v) {
462 v.truncate(1);
463 if (v.size() == 1) {
464 v.truncate(0);
465 if (v.size() == 0) {
466 cxx_test_suite_set_correct();
467 }
468 }
469 }
470
c_take_rust_vec_shared_clear(rust::Vec<Shared> v)471 void c_take_rust_vec_shared_clear(rust::Vec<Shared> v) {
472 v.clear();
473 if (v.size() == 0) {
474 cxx_test_suite_set_correct();
475 }
476 }
477
c_take_ref_rust_vec(const rust::Vec<uint8_t> & v)478 void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
479 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
480 if (sum == 200) {
481 cxx_test_suite_set_correct();
482 }
483 }
484
c_take_ref_rust_vec_string(const rust::Vec<rust::String> & v)485 void c_take_ref_rust_vec_string(const rust::Vec<rust::String> &v) {
486 (void)v;
487 cxx_test_suite_set_correct();
488 }
489
c_take_ref_rust_vec_index(const rust::Vec<uint8_t> & v)490 void c_take_ref_rust_vec_index(const rust::Vec<uint8_t> &v) {
491 if (v[0] == 86 && v.at(0) == 86 && v.front() == 86 && v[1] == 75 &&
492 v.at(1) == 75 && v[3] == 9 && v.at(3) == 9 && v.back() == 9) {
493 cxx_test_suite_set_correct();
494 }
495 }
496
c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> & v)497 void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
498 // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
499 // requirements for std::iterator_traits.
500 // https://en.cppreference.com/w/cpp/iterator/iterator_traits
501 std::vector<uint8_t> stdv;
502 std::copy(v.begin(), v.end(), std::back_inserter(stdv));
503 uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
504 if (sum == 200) {
505 cxx_test_suite_set_correct();
506 }
507 }
508
c_take_ref_shared_string(const SharedString & s)509 const SharedString &c_take_ref_shared_string(const SharedString &s) {
510 if (std::string(s.msg) == "2020") {
511 cxx_test_suite_set_correct();
512 }
513 return s;
514 }
515
c_take_callback(rust::Fn<size_t (rust::String)> callback)516 void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
517 callback("2020");
518 }
519
c_take_callback_ref(rust::Fn<void (const rust::String &)> callback)520 void c_take_callback_ref(rust::Fn<void(const rust::String &)> callback) {
521 const rust::String string = "2020";
522 callback(string);
523 }
524
c_take_callback_mut(rust::Fn<void (rust::String &)> callback)525 void c_take_callback_mut(rust::Fn<void(rust::String &)> callback) {
526 rust::String string = "2020";
527 callback(string);
528 }
529
c_take_enum(Enum e)530 void c_take_enum(Enum e) {
531 if (e == Enum::AVal) {
532 cxx_test_suite_set_correct();
533 }
534 }
535
c_take_ns_enum(::A::AEnum e)536 void c_take_ns_enum(::A::AEnum e) {
537 if (e == ::A::AEnum::AAVal) {
538 cxx_test_suite_set_correct();
539 }
540 }
541
c_take_nested_ns_enum(::A::B::ABEnum e)542 void c_take_nested_ns_enum(::A::B::ABEnum e) {
543 if (e == ::A::B::ABEnum::ABAVal) {
544 cxx_test_suite_set_correct();
545 }
546 }
547
c_take_const_ptr(const C * c)548 size_t c_take_const_ptr(const C *c) { return c->get(); }
549
c_take_mut_ptr(C * c)550 size_t c_take_mut_ptr(C *c) {
551 size_t result = c->get();
552 delete c;
553 return result;
554 }
555
c_try_return_void()556 void c_try_return_void() {}
557
c_try_return_primitive()558 size_t c_try_return_primitive() { return 2020; }
559
c_fail_return_primitive()560 size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
561
c_try_return_box()562 rust::Box<R> c_try_return_box() { return c_return_box(); }
563
c_try_return_ref(const rust::String & s)564 const rust::String &c_try_return_ref(const rust::String &s) { return s; }
565
c_try_return_str(rust::Str s)566 rust::Str c_try_return_str(rust::Str s) { return s; }
567
c_try_return_sliceu8(rust::Slice<const uint8_t> s)568 rust::Slice<const uint8_t> c_try_return_sliceu8(rust::Slice<const uint8_t> s) {
569 return s;
570 }
571
c_try_return_mutsliceu8(rust::Slice<uint8_t> s)572 rust::Slice<uint8_t> c_try_return_mutsliceu8(rust::Slice<uint8_t> s) {
573 return s;
574 }
575
c_try_return_rust_string()576 rust::String c_try_return_rust_string() { return c_return_rust_string(); }
577
c_try_return_unique_ptr_string()578 std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
579 return c_return_unique_ptr_string();
580 }
581
c_try_return_rust_vec()582 rust::Vec<uint8_t> c_try_return_rust_vec() {
583 throw std::runtime_error("unimplemented");
584 }
585
c_try_return_rust_vec_string()586 rust::Vec<rust::String> c_try_return_rust_vec_string() {
587 throw std::runtime_error("unimplemented");
588 }
589
c_try_return_ref_rust_vec(const C & c)590 const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
591 (void)c;
592 throw std::runtime_error("unimplemented");
593 }
594
c_get_use_count(const std::weak_ptr<C> & weak)595 size_t c_get_use_count(const std::weak_ptr<C> &weak) noexcept {
596 return weak.use_count();
597 }
598
cxx_test_suite_get_unique_ptr()599 extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
600 return std::unique_ptr<C>(new C{2020}).release();
601 }
602
603 extern "C" void
cxx_test_suite_get_shared_ptr(std::shared_ptr<C> * repr)604 cxx_test_suite_get_shared_ptr(std::shared_ptr<C> *repr) noexcept {
605 new (repr) std::shared_ptr<C>(new C{2020});
606 }
607
cxx_test_suite_get_unique_ptr_string()608 extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
609 return std::unique_ptr<std::string>(new std::string("2020")).release();
610 }
611
cOverloadedMethod(int32_t x) const612 rust::String C::cOverloadedMethod(int32_t x) const {
613 return rust::String(std::to_string(x));
614 }
615
cOverloadedMethod(rust::Str x) const616 rust::String C::cOverloadedMethod(rust::Str x) const {
617 return rust::String(std::string(x));
618 }
619
cOverloadedFunction(int x)620 rust::String cOverloadedFunction(int x) {
621 return rust::String(std::to_string(x));
622 }
623
cOverloadedFunction(rust::Str x)624 rust::String cOverloadedFunction(rust::Str x) {
625 return rust::String(std::string(x));
626 }
627
c_take_trivial_ptr(std::unique_ptr<D> d)628 void c_take_trivial_ptr(std::unique_ptr<D> d) {
629 if (d->d == 30) {
630 cxx_test_suite_set_correct();
631 }
632 }
633
c_take_trivial_ref(const D & d)634 void c_take_trivial_ref(const D &d) {
635 if (d.d == 30) {
636 cxx_test_suite_set_correct();
637 }
638 }
639
c_take_trivial_mut_ref(D & d)640 void c_take_trivial_mut_ref(D &d) { (void)d; }
641
c_take_trivial_pin_ref(const D & d)642 void c_take_trivial_pin_ref(const D &d) { (void)d; }
643
c_take_trivial_pin_mut_ref(D & d)644 void c_take_trivial_pin_mut_ref(D &d) { (void)d; }
645
c_take_trivial_ref_method() const646 void D::c_take_trivial_ref_method() const {
647 if (d == 30) {
648 cxx_test_suite_set_correct();
649 }
650 }
651
c_take_trivial_mut_ref_method()652 void D::c_take_trivial_mut_ref_method() {
653 if (d == 30) {
654 cxx_test_suite_set_correct();
655 }
656 }
657
c_take_trivial(D d)658 void c_take_trivial(D d) {
659 if (d.d == 30) {
660 cxx_test_suite_set_correct();
661 }
662 }
663
c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g)664 void c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g) {
665 if (g->g == 30) {
666 cxx_test_suite_set_correct();
667 }
668 }
669
c_take_trivial_ns_ref(const::G::G & g)670 void c_take_trivial_ns_ref(const ::G::G &g) {
671 if (g.g == 30) {
672 cxx_test_suite_set_correct();
673 }
674 }
675
c_take_trivial_ns(::G::G g)676 void c_take_trivial_ns(::G::G g) {
677 if (g.g == 30) {
678 cxx_test_suite_set_correct();
679 }
680 }
681
c_take_opaque_ptr(std::unique_ptr<E> e)682 void c_take_opaque_ptr(std::unique_ptr<E> e) {
683 if (e->e == 40) {
684 cxx_test_suite_set_correct();
685 }
686 }
687
c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f)688 void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f) {
689 if (f->f == 40) {
690 cxx_test_suite_set_correct();
691 }
692 }
693
c_take_opaque_ref(const E & e)694 void c_take_opaque_ref(const E &e) {
695 if (e.e == 40 && e.e_str == "hello") {
696 cxx_test_suite_set_correct();
697 }
698 }
699
c_take_opaque_ref_method() const700 void E::c_take_opaque_ref_method() const {
701 if (e == 40 && e_str == "hello") {
702 cxx_test_suite_set_correct();
703 }
704 }
705
c_take_opaque_mut_ref_method()706 void E::c_take_opaque_mut_ref_method() {
707 if (e == 40 && e_str == "hello") {
708 cxx_test_suite_set_correct();
709 }
710 }
711
c_take_opaque_ns_ref(const::F::F & f)712 void c_take_opaque_ns_ref(const ::F::F &f) {
713 if (f.f == 40 && f.f_str == "hello") {
714 cxx_test_suite_set_correct();
715 }
716 }
717
c_return_trivial_ptr()718 std::unique_ptr<D> c_return_trivial_ptr() {
719 auto d = std::unique_ptr<D>(new D());
720 d->d = 30;
721 return d;
722 }
723
c_return_trivial()724 D c_return_trivial() {
725 D d;
726 d.d = 30;
727 return d;
728 }
729
c_return_trivial_ns_ptr()730 std::unique_ptr<::G::G> c_return_trivial_ns_ptr() {
731 auto g = std::unique_ptr<::G::G>(new ::G::G());
732 g->g = 30;
733 return g;
734 }
735
c_return_trivial_ns()736 ::G::G c_return_trivial_ns() {
737 ::G::G g;
738 g.g = 30;
739 return g;
740 }
741
c_return_opaque_ptr()742 std::unique_ptr<E> c_return_opaque_ptr() {
743 auto e = std::unique_ptr<E>(new E());
744 e->e = 40;
745 e->e_str = std::string("hello");
746 return e;
747 }
748
c_return_opaque_mut_pin(E & e)749 E &c_return_opaque_mut_pin(E &e) { return e; }
750
c_return_ns_opaque_ptr()751 std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
752 auto f = std::unique_ptr<::F::F>(new ::F::F());
753 f->f = 40;
754 f->f_str = std::string("hello");
755 return f;
756 }
757
cxx_run_test()758 extern "C" const char *cxx_run_test() noexcept {
759 #define STRINGIFY(x) #x
760 #define TOSTRING(x) STRINGIFY(x)
761 #define ASSERT(x) \
762 do { \
763 if (!(x)) { \
764 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
765 } \
766 } while (false)
767
768 ASSERT(rust::size_of<R>() == sizeof(size_t));
769 ASSERT(rust::align_of<R>() == alignof(size_t));
770 ASSERT(rust::size_of<size_t>() == sizeof(size_t));
771 ASSERT(rust::align_of<size_t>() == alignof(size_t));
772
773 ASSERT(r_return_primitive() == 2020);
774 ASSERT(r_return_shared().z == 2020);
775 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
776 ASSERT(r_return_unique_ptr()->get() == 2020);
777 ASSERT(r_return_shared_ptr()->get() == 2020);
778 ASSERT(r_return_ref(Shared{2020}) == 2020);
779 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
780 ASSERT(std::string(r_return_rust_string()) == "2020");
781 ASSERT(*r_return_unique_ptr_string() == "2020");
782 ASSERT(r_return_identity(2020) == 2020);
783 ASSERT(r_return_sum(2020, 1) == 2021);
784 ASSERT(r_return_enum(0) == Enum::AVal);
785 ASSERT(r_return_enum(1) == Enum::BVal);
786 ASSERT(r_return_enum(2021) == Enum::CVal);
787
788 r_take_primitive(2020);
789 r_take_shared(Shared{2020});
790 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
791 r_take_shared_ptr(std::shared_ptr<C>(new C{2020}));
792 r_take_ref_c(C{2020});
793 r_take_str(rust::Str("2020"));
794 r_take_slice_char(rust::Slice<const char>(SLICE_DATA, sizeof(SLICE_DATA)));
795 r_take_rust_string(rust::String("2020"));
796 r_take_unique_ptr_string(
797 std::unique_ptr<std::string>(new std::string("2020")));
798 r_take_ref_vector(std::vector<uint8_t>{20, 2, 0});
799 std::vector<uint64_t> empty_vector;
800 r_take_ref_empty_vector(empty_vector);
801 empty_vector.reserve(10);
802 r_take_ref_empty_vector(empty_vector);
803 r_take_enum(Enum::AVal);
804
805 ASSERT(r_try_return_primitive() == 2020);
806 try {
807 r_fail_return_primitive();
808 ASSERT(false);
809 } catch (const rust::Error &e) {
810 ASSERT(std::strcmp(e.what(), "rust error") == 0);
811 }
812
813 auto r = r_return_box();
814 ASSERT(r->get() == 2020);
815 ASSERT(r->set(2021) == 2021);
816 ASSERT(r->get() == 2021);
817
818 using std::swap;
819 auto r2 = r_return_box();
820 swap(r, r2);
821 ASSERT(r->get() == 2020);
822 ASSERT(r2->get() == 2021);
823
824 ASSERT(std::string(Shared{0}.r_method_on_shared()) == "2020");
825
826 ASSERT(std::string(rAliasedFunction(2020)) == "2020");
827
828 ASSERT(Shared{1} == Shared{1});
829 ASSERT(Shared{1} != Shared{2});
830
831 rust::String first = "first", second = "second", sec = "sec";
832 bool (rust::String::*cmp)(const rust::String &) const;
833 bool first_first, first_second, sec_second, second_sec;
834 for (auto test : {
835 std::tuple<decltype(cmp), bool, bool, bool, bool>{
836 &rust::String::operator==, true, false, false, false},
837 {&rust::String::operator!=, false, true, true, true},
838 {&rust::String::operator<, false, true, true, false},
839 {&rust::String::operator<=, true, true, true, false},
840 {&rust::String::operator>, false, false, false, true},
841 {&rust::String::operator>=, true, false, false, true},
842 }) {
843 std::tie(cmp, first_first, first_second, sec_second, second_sec) = test;
844 ASSERT((first.*cmp)(first) == first_first);
845 ASSERT((first.*cmp)(second) == first_second);
846 ASSERT((sec.*cmp)(second) == sec_second);
847 ASSERT((second.*cmp)(sec) == second_sec);
848 }
849
850 rust::String cstring = "test";
851 ASSERT(cstring.length() == 4);
852 ASSERT(strncmp(cstring.data(), "test", 4) == 0);
853 ASSERT(strncmp(cstring.c_str(), "test", 5) == 0);
854 ASSERT(cstring.length() == 4);
855
856 rust::String other_cstring = "foo";
857 swap(cstring, other_cstring);
858 ASSERT(cstring == "foo");
859 ASSERT(other_cstring == "test");
860
861 ASSERT(cstring.capacity() == 3);
862 cstring.reserve(2);
863 ASSERT(cstring.capacity() == 3);
864 cstring.reserve(5);
865 ASSERT(cstring.capacity() >= 5);
866
867 rust::Str cstr = "test";
868 rust::Str other_cstr = "foo";
869 swap(cstr, other_cstr);
870 ASSERT(cstr == "foo");
871 ASSERT(other_cstr == "test");
872
873 const char *utf8_literal = u8"Test string";
874 const char16_t *utf16_literal = u"Test string";
875 rust::String utf8_rstring = utf8_literal;
876 rust::String utf16_rstring = utf16_literal;
877 ASSERT(utf8_rstring == utf16_rstring);
878
879 const char *bad_utf8_literal = "test\x80";
880 const char16_t *bad_utf16_literal = u"test\xDD1E";
881 rust::String bad_utf8_rstring = rust::String::lossy(bad_utf8_literal);
882 rust::String bad_utf16_rstring = rust::String::lossy(bad_utf16_literal);
883 ASSERT(bad_utf8_rstring == bad_utf16_rstring);
884
885 rust::Vec<int> vec1{1, 2};
886 rust::Vec<int> vec2{3, 4};
887 swap(vec1, vec2);
888 ASSERT(vec1[0] == 3 && vec1[1] == 4);
889 ASSERT(vec2[0] == 1 && vec2[1] == 2);
890
891 // Test Vec<usize> and Vec<isize>. These are weird because on Linux and
892 // Windows size_t is exactly the same C++ type as one of the sized integer
893 // types (typically uint64_t, both of which are defined as unsigned long),
894 // while on macOS it is a distinct type.
895 // https://github.com/dtolnay/cxx/issues/705
896 (void)rust::Vec<size_t>();
897 (void)rust::Vec<rust::isize>();
898
899 cxx_test_suite_set_correct();
900 return nullptr;
901 }
902
903 } // namespace tests
904
905 namespace other {
ns_c_take_trivial(::tests::D d)906 void ns_c_take_trivial(::tests::D d) {
907 if (d.d == 30) {
908 cxx_test_suite_set_correct();
909 }
910 }
911
ns_c_return_trivial()912 ::tests::D ns_c_return_trivial() {
913 ::tests::D d;
914 d.d = 30;
915 return d;
916 }
917
ns_c_take_ns_shared(::A::AShared shared)918 void ns_c_take_ns_shared(::A::AShared shared) {
919 if (shared.z == 2020) {
920 cxx_test_suite_set_correct();
921 }
922 }
923 } // namespace other
924
925 namespace I {
get() const926 uint32_t I::get() const { return a; }
927
ns_c_return_unique_ptr_ns()928 std::unique_ptr<I> ns_c_return_unique_ptr_ns() {
929 return std::unique_ptr<I>(new I());
930 }
931 } // namespace I
932
933 // Instantiate any remaining class member functions not already covered above.
934 // This is an easy way to at least typecheck anything missed by unit tests.
935 // https://en.cppreference.com/w/cpp/language/class_template#Explicit_instantiation
936 // > When an explicit instantiation names a class template specialization, it
937 // > serves as an explicit instantiation of the same kind (declaration or
938 // > definition) of each of its non-inherited non-template members that has not
939 // > been previously explicitly specialized in the translation unit.
940 #if defined(CXX_TEST_INSTANTIATIONS)
941 template class rust::Box<tests::Shared>;
942 template class rust::Slice<const char>;
943 template class rust::Slice<const uint8_t>;
944 template class rust::Slice<uint8_t>;
945 template class rust::Slice<const tests::Shared>;
946 template class rust::Slice<tests::Shared>;
947 template class rust::Slice<const tests::R>;
948 template class rust::Slice<tests::R>;
949 template class rust::Vec<uint8_t>;
950 template class rust::Vec<rust::String>;
951 template class rust::Vec<tests::Shared>;
952 template class rust::Fn<size_t(rust::String)>;
953 #endif
954