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