• 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() == 4) {
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() == 4) {
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() == 2) {
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_enum(Enum e)497 void c_take_enum(Enum e) {
498   if (e == Enum::AVal) {
499     cxx_test_suite_set_correct();
500   }
501 }
502 
c_take_ns_enum(::A::AEnum e)503 void c_take_ns_enum(::A::AEnum e) {
504   if (e == ::A::AEnum::AAVal) {
505     cxx_test_suite_set_correct();
506   }
507 }
508 
c_take_nested_ns_enum(::A::B::ABEnum e)509 void c_take_nested_ns_enum(::A::B::ABEnum e) {
510   if (e == ::A::B::ABEnum::ABAVal) {
511     cxx_test_suite_set_correct();
512   }
513 }
514 
c_take_const_ptr(const C * c)515 size_t c_take_const_ptr(const C *c) { return c->get(); }
516 
c_take_mut_ptr(C * c)517 size_t c_take_mut_ptr(C *c) {
518   size_t result = c->get();
519   delete c;
520   return result;
521 }
522 
c_try_return_void()523 void c_try_return_void() {}
524 
c_try_return_primitive()525 size_t c_try_return_primitive() { return 2020; }
526 
c_fail_return_primitive()527 size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
528 
c_try_return_box()529 rust::Box<R> c_try_return_box() { return c_return_box(); }
530 
c_try_return_ref(const rust::String & s)531 const rust::String &c_try_return_ref(const rust::String &s) { return s; }
532 
c_try_return_str(rust::Str s)533 rust::Str c_try_return_str(rust::Str s) { return s; }
534 
c_try_return_sliceu8(rust::Slice<const uint8_t> s)535 rust::Slice<const uint8_t> c_try_return_sliceu8(rust::Slice<const uint8_t> s) {
536   return s;
537 }
538 
c_try_return_mutsliceu8(rust::Slice<uint8_t> s)539 rust::Slice<uint8_t> c_try_return_mutsliceu8(rust::Slice<uint8_t> s) {
540   return s;
541 }
542 
c_try_return_rust_string()543 rust::String c_try_return_rust_string() { return c_return_rust_string(); }
544 
c_try_return_unique_ptr_string()545 std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
546   return c_return_unique_ptr_string();
547 }
548 
c_try_return_rust_vec()549 rust::Vec<uint8_t> c_try_return_rust_vec() {
550   throw std::runtime_error("unimplemented");
551 }
552 
c_try_return_rust_vec_string()553 rust::Vec<rust::String> c_try_return_rust_vec_string() {
554   throw std::runtime_error("unimplemented");
555 }
556 
c_try_return_ref_rust_vec(const C & c)557 const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
558   (void)c;
559   throw std::runtime_error("unimplemented");
560 }
561 
c_get_use_count(const std::weak_ptr<C> & weak)562 size_t c_get_use_count(const std::weak_ptr<C> &weak) noexcept {
563   return weak.use_count();
564 }
565 
cxx_test_suite_get_unique_ptr()566 extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
567   return std::unique_ptr<C>(new C{2020}).release();
568 }
569 
570 extern "C" void
cxx_test_suite_get_shared_ptr(std::shared_ptr<C> * repr)571 cxx_test_suite_get_shared_ptr(std::shared_ptr<C> *repr) noexcept {
572   new (repr) std::shared_ptr<C>(new C{2020});
573 }
574 
cxx_test_suite_get_unique_ptr_string()575 extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
576   return std::unique_ptr<std::string>(new std::string("2020")).release();
577 }
578 
cOverloadedMethod(int32_t x) const579 rust::String C::cOverloadedMethod(int32_t x) const {
580   return rust::String(std::to_string(x));
581 }
582 
cOverloadedMethod(rust::Str x) const583 rust::String C::cOverloadedMethod(rust::Str x) const {
584   return rust::String(std::string(x));
585 }
586 
cOverloadedFunction(int x)587 rust::String cOverloadedFunction(int x) {
588   return rust::String(std::to_string(x));
589 }
590 
cOverloadedFunction(rust::Str x)591 rust::String cOverloadedFunction(rust::Str x) {
592   return rust::String(std::string(x));
593 }
594 
c_take_trivial_ptr(std::unique_ptr<D> d)595 void c_take_trivial_ptr(std::unique_ptr<D> d) {
596   if (d->d == 30) {
597     cxx_test_suite_set_correct();
598   }
599 }
600 
c_take_trivial_ref(const D & d)601 void c_take_trivial_ref(const D &d) {
602   if (d.d == 30) {
603     cxx_test_suite_set_correct();
604   }
605 }
606 
c_take_trivial_mut_ref(D & d)607 void c_take_trivial_mut_ref(D &d) { (void)d; }
608 
c_take_trivial_pin_ref(const D & d)609 void c_take_trivial_pin_ref(const D &d) { (void)d; }
610 
c_take_trivial_pin_mut_ref(D & d)611 void c_take_trivial_pin_mut_ref(D &d) { (void)d; }
612 
c_take_trivial_ref_method() const613 void D::c_take_trivial_ref_method() const {
614   if (d == 30) {
615     cxx_test_suite_set_correct();
616   }
617 }
618 
c_take_trivial_mut_ref_method()619 void D::c_take_trivial_mut_ref_method() {
620   if (d == 30) {
621     cxx_test_suite_set_correct();
622   }
623 }
624 
c_take_trivial(D d)625 void c_take_trivial(D d) {
626   if (d.d == 30) {
627     cxx_test_suite_set_correct();
628   }
629 }
630 
c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g)631 void c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g) {
632   if (g->g == 30) {
633     cxx_test_suite_set_correct();
634   }
635 }
636 
c_take_trivial_ns_ref(const::G::G & g)637 void c_take_trivial_ns_ref(const ::G::G &g) {
638   if (g.g == 30) {
639     cxx_test_suite_set_correct();
640   }
641 }
642 
c_take_trivial_ns(::G::G g)643 void c_take_trivial_ns(::G::G g) {
644   if (g.g == 30) {
645     cxx_test_suite_set_correct();
646   }
647 }
648 
c_take_opaque_ptr(std::unique_ptr<E> e)649 void c_take_opaque_ptr(std::unique_ptr<E> e) {
650   if (e->e == 40) {
651     cxx_test_suite_set_correct();
652   }
653 }
654 
c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f)655 void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f) {
656   if (f->f == 40) {
657     cxx_test_suite_set_correct();
658   }
659 }
660 
c_take_opaque_ref(const E & e)661 void c_take_opaque_ref(const E &e) {
662   if (e.e == 40 && e.e_str == "hello") {
663     cxx_test_suite_set_correct();
664   }
665 }
666 
c_take_opaque_ref_method() const667 void E::c_take_opaque_ref_method() const {
668   if (e == 40 && e_str == "hello") {
669     cxx_test_suite_set_correct();
670   }
671 }
672 
c_take_opaque_mut_ref_method()673 void E::c_take_opaque_mut_ref_method() {
674   if (e == 40 && e_str == "hello") {
675     cxx_test_suite_set_correct();
676   }
677 }
678 
c_take_opaque_ns_ref(const::F::F & f)679 void c_take_opaque_ns_ref(const ::F::F &f) {
680   if (f.f == 40 && f.f_str == "hello") {
681     cxx_test_suite_set_correct();
682   }
683 }
684 
c_return_trivial_ptr()685 std::unique_ptr<D> c_return_trivial_ptr() {
686   auto d = std::unique_ptr<D>(new D());
687   d->d = 30;
688   return d;
689 }
690 
c_return_trivial()691 D c_return_trivial() {
692   D d;
693   d.d = 30;
694   return d;
695 }
696 
c_return_trivial_ns_ptr()697 std::unique_ptr<::G::G> c_return_trivial_ns_ptr() {
698   auto g = std::unique_ptr<::G::G>(new ::G::G());
699   g->g = 30;
700   return g;
701 }
702 
c_return_trivial_ns()703 ::G::G c_return_trivial_ns() {
704   ::G::G g;
705   g.g = 30;
706   return g;
707 }
708 
c_return_opaque_ptr()709 std::unique_ptr<E> c_return_opaque_ptr() {
710   auto e = std::unique_ptr<E>(new E());
711   e->e = 40;
712   e->e_str = std::string("hello");
713   return e;
714 }
715 
c_return_opaque_mut_pin(E & e)716 E &c_return_opaque_mut_pin(E &e) { return e; }
717 
c_return_ns_opaque_ptr()718 std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
719   auto f = std::unique_ptr<::F::F>(new ::F::F());
720   f->f = 40;
721   f->f_str = std::string("hello");
722   return f;
723 }
724 
cxx_run_test()725 extern "C" const char *cxx_run_test() noexcept {
726 #define STRINGIFY(x) #x
727 #define TOSTRING(x) STRINGIFY(x)
728 #define ASSERT(x)                                                              \
729   do {                                                                         \
730     if (!(x)) {                                                                \
731       return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__);   \
732     }                                                                          \
733   } while (false)
734 
735   ASSERT(rust::size_of<R>() == sizeof(size_t));
736   ASSERT(rust::align_of<R>() == alignof(size_t));
737   ASSERT(rust::size_of<size_t>() == sizeof(size_t));
738   ASSERT(rust::align_of<size_t>() == alignof(size_t));
739 
740   ASSERT(r_return_primitive() == 2020);
741   ASSERT(r_return_shared().z == 2020);
742   ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
743   ASSERT(r_return_unique_ptr()->get() == 2020);
744   ASSERT(r_return_shared_ptr()->get() == 2020);
745   ASSERT(r_return_ref(Shared{2020}) == 2020);
746   ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
747   ASSERT(std::string(r_return_rust_string()) == "2020");
748   ASSERT(*r_return_unique_ptr_string() == "2020");
749   ASSERT(r_return_identity(2020) == 2020);
750   ASSERT(r_return_sum(2020, 1) == 2021);
751   ASSERT(r_return_enum(0) == Enum::AVal);
752   ASSERT(r_return_enum(1) == Enum::BVal);
753   ASSERT(r_return_enum(2021) == Enum::CVal);
754 
755   r_take_primitive(2020);
756   r_take_shared(Shared{2020});
757   r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
758   r_take_shared_ptr(std::shared_ptr<C>(new C{2020}));
759   r_take_ref_c(C{2020});
760   r_take_str(rust::Str("2020"));
761   r_take_slice_char(rust::Slice<const char>(SLICE_DATA, sizeof(SLICE_DATA)));
762   r_take_rust_string(rust::String("2020"));
763   r_take_unique_ptr_string(
764       std::unique_ptr<std::string>(new std::string("2020")));
765   r_take_ref_vector(std::vector<uint8_t>{20, 2, 0});
766   std::vector<uint64_t> empty_vector;
767   r_take_ref_empty_vector(empty_vector);
768   empty_vector.reserve(10);
769   r_take_ref_empty_vector(empty_vector);
770   r_take_enum(Enum::AVal);
771 
772   ASSERT(r_try_return_primitive() == 2020);
773   try {
774     r_fail_return_primitive();
775     ASSERT(false);
776   } catch (const rust::Error &e) {
777     ASSERT(std::strcmp(e.what(), "rust error") == 0);
778   }
779 
780   auto r = r_return_box();
781   ASSERT(r->get() == 2020);
782   ASSERT(r->set(2021) == 2021);
783   ASSERT(r->get() == 2021);
784 
785   using std::swap;
786   auto r2 = r_return_box();
787   swap(r, r2);
788   ASSERT(r->get() == 2020);
789   ASSERT(r2->get() == 2021);
790 
791   ASSERT(std::string(Shared{0}.r_method_on_shared()) == "2020");
792 
793   ASSERT(std::string(rAliasedFunction(2020)) == "2020");
794 
795   ASSERT(Shared{1} == Shared{1});
796   ASSERT(Shared{1} != Shared{2});
797 
798   rust::String first = "first", second = "second", sec = "sec";
799   bool (rust::String::*cmp)(const rust::String &) const;
800   bool first_first, first_second, sec_second, second_sec;
801   for (auto test : {
802            std::tuple<decltype(cmp), bool, bool, bool, bool>{
803                &rust::String::operator==, true, false, false, false},
804            {&rust::String::operator!=, false, true, true, true},
805            {&rust::String::operator<, false, true, true, false},
806            {&rust::String::operator<=, true, true, true, false},
807            {&rust::String::operator>, false, false, false, true},
808            {&rust::String::operator>=, true, false, false, true},
809        }) {
810     std::tie(cmp, first_first, first_second, sec_second, second_sec) = test;
811     ASSERT((first.*cmp)(first) == first_first);
812     ASSERT((first.*cmp)(second) == first_second);
813     ASSERT((sec.*cmp)(second) == sec_second);
814     ASSERT((second.*cmp)(sec) == second_sec);
815   }
816 
817   rust::String cstring = "test";
818   ASSERT(cstring.length() == 4);
819   ASSERT(strncmp(cstring.data(), "test", 4) == 0);
820   ASSERT(strncmp(cstring.c_str(), "test", 5) == 0);
821   ASSERT(cstring.length() == 4);
822 
823   rust::String other_cstring = "foo";
824   swap(cstring, other_cstring);
825   ASSERT(cstring == "foo");
826   ASSERT(other_cstring == "test");
827 
828   rust::Str cstr = "test";
829   rust::Str other_cstr = "foo";
830   swap(cstr, other_cstr);
831   ASSERT(cstr == "foo");
832   ASSERT(other_cstr == "test");
833 
834   rust::Vec<int> vec1{1, 2};
835   rust::Vec<int> vec2{3, 4};
836   swap(vec1, vec2);
837   ASSERT(vec1[0] == 3 && vec1[1] == 4);
838   ASSERT(vec2[0] == 1 && vec2[1] == 2);
839 
840   // Test Vec<usize> and Vec<isize>. These are weird because on Linux and
841   // Windows size_t is exactly the same C++ type as one of the sized integer
842   // types (typically uint64_t, both of which are defined as unsigned long),
843   // while on macOS it is a distinct type.
844   // https://github.com/dtolnay/cxx/issues/705
845   (void)rust::Vec<size_t>();
846   (void)rust::Vec<rust::isize>();
847 
848   cxx_test_suite_set_correct();
849   return nullptr;
850 }
851 
852 } // namespace tests
853 
854 namespace other {
ns_c_take_trivial(::tests::D d)855 void ns_c_take_trivial(::tests::D d) {
856   if (d.d == 30) {
857     cxx_test_suite_set_correct();
858   }
859 }
860 
ns_c_return_trivial()861 ::tests::D ns_c_return_trivial() {
862   ::tests::D d;
863   d.d = 30;
864   return d;
865 }
866 
ns_c_take_ns_shared(::A::AShared shared)867 void ns_c_take_ns_shared(::A::AShared shared) {
868   if (shared.z == 2020) {
869     cxx_test_suite_set_correct();
870   }
871 }
872 } // namespace other
873 
874 namespace I {
get() const875 uint32_t I::get() const { return a; }
876 
ns_c_return_unique_ptr_ns()877 std::unique_ptr<I> ns_c_return_unique_ptr_ns() {
878   return std::unique_ptr<I>(new I());
879 }
880 } // namespace I
881 
882 // Instantiate any remaining class member functions not already covered above.
883 // This is an easy way to at least typecheck anything missed by unit tests.
884 // https://en.cppreference.com/w/cpp/language/class_template#Explicit_instantiation
885 // > When an explicit instantiation names a class template specialization, it
886 // > serves as an explicit instantiation of the same kind (declaration or
887 // > definition) of each of its non-inherited non-template members that has not
888 // > been previously explicitly specialized in the translation unit.
889 #if defined(CXX_TEST_INSTANTIATIONS)
890 template class rust::Box<tests::Shared>;
891 template class rust::Slice<const char>;
892 template class rust::Slice<const uint8_t>;
893 template class rust::Slice<uint8_t>;
894 template class rust::Slice<const tests::Shared>;
895 template class rust::Slice<tests::Shared>;
896 template class rust::Slice<const tests::R>;
897 template class rust::Slice<tests::R>;
898 template class rust::Vec<uint8_t>;
899 template class rust::Vec<rust::String>;
900 template class rust::Vec<tests::Shared>;
901 template class rust::Fn<size_t(rust::String)>;
902 #endif
903