blob: 747788f2ead4c68f5a28d814ab270bfe16dc4359 [file] [log] [blame]
David Tolnayd41eef52020-10-07 16:33:55 -07001#include "tests/ffi/tests.h"
2#include "tests/ffi/lib.rs.h"
David Tolnayb6c5ea72020-03-16 13:36:28 -07003#include <cstring>
David Tolnaya08b19f2020-08-25 19:21:14 -07004#include <iterator>
David Tolnaybf23e3e2020-10-30 21:09:16 -07005#include <memory>
David Tolnay37dd7e12020-04-25 12:51:59 -07006#include <numeric>
David Tolnayebef4a22020-03-17 15:33:47 -07007#include <stdexcept>
Adrian Taylor121cca42020-10-10 15:32:00 -07008#include <string>
David Tolnay97c72102020-01-25 16:49:00 -08009
David Tolnay2fe58c62020-03-06 16:23:09 -080010extern "C" void cxx_test_suite_set_correct() noexcept;
11extern "C" tests::R *cxx_test_suite_get_box() noexcept;
12extern "C" bool cxx_test_suite_r_is_correct(const tests::R *) noexcept;
David Tolnay3fd7f562020-01-26 17:47:11 -080013
David Tolnay97c72102020-01-25 16:49:00 -080014namespace tests {
15
David Tolnay4037de22020-04-30 19:51:04 -070016static constexpr char SLICE_DATA[] = "2020";
Adrian Taylorf5dd5522020-04-13 16:50:14 -070017
David Tolnayad5b8af2020-01-26 16:59:13 -080018C::C(size_t n) : n(n) {}
19
David Tolnay3fd7f562020-01-26 17:47:11 -080020size_t C::get() const { return this->n; }
21
Joel Galensone1e969d2020-04-21 12:50:20 -070022size_t C::get2() const { return this->n; }
23
Joel Galenson3d4f6122020-04-07 15:54:05 -070024size_t C::set(size_t n) {
25 this->n = n;
26 return this->n;
27}
28
Joel Galensone1e969d2020-04-21 12:50:20 -070029size_t C::set2(size_t n) {
30 this->n = n;
31 return this->n;
32}
33
myronahne3b78ea2020-05-23 01:08:13 +070034size_t C::set_succeed(size_t n) { return this->set2(n); }
35
36size_t C::get_fail() { throw std::runtime_error("unimplemented"); }
37
David Tolnayde5340e2020-04-25 14:03:21 -070038const std::vector<uint8_t> &C::get_v() const { return this->v; }
39
David Tolnay18d93b62020-08-27 00:55:48 -070040std::vector<uint8_t> &C::get_v() { return this->v; }
41
David Tolnayb8715772020-01-28 00:54:05 -080042size_t c_return_primitive() { return 2020; }
David Tolnayad5b8af2020-01-26 16:59:13 -080043
David Tolnayb8715772020-01-28 00:54:05 -080044Shared c_return_shared() { return Shared{2020}; }
David Tolnayad5b8af2020-01-26 16:59:13 -080045
Adrian Taylor9a158e42020-10-24 20:51:25 -070046::A::AShared c_return_ns_shared() { return ::A::AShared{2020}; }
47
48::A::B::ABShared c_return_nested_ns_shared() { return ::A::B::ABShared{2020}; }
49
David Tolnaybe13d8a2020-03-06 15:45:39 -080050rust::Box<R> c_return_box() {
51 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
52}
53
David Tolnayad5b8af2020-01-26 16:59:13 -080054std::unique_ptr<C> c_return_unique_ptr() {
55 return std::unique_ptr<C>(new C{2020});
56}
57
Adrian Taylord47af7a2020-10-26 13:18:48 -070058std::unique_ptr<::H::H> c_return_ns_unique_ptr() {
59 return std::unique_ptr<::H::H>(new ::H::H{"hello"});
60}
61
David Tolnayb8715772020-01-28 00:54:05 -080062const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080063
Adrian Taylor9a158e42020-10-24 20:51:25 -070064const size_t &c_return_ns_ref(const ::A::AShared &shared) { return shared.z; }
65
David Tolnaybf23e3e2020-10-30 21:09:16 -070066const size_t &c_return_nested_ns_ref(const ::A::B::ABShared &shared) {
67 return shared.z;
68}
Adrian Taylor9a158e42020-10-24 20:51:25 -070069
David Tolnay18d93b62020-08-27 00:55:48 -070070size_t &c_return_mut(Shared &shared) { return shared.z; }
71
David Tolnay750755e2020-03-01 13:04:08 -080072rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080073 (void)shared;
74 return "2020";
75}
76
David Tolnayeb952ba2020-04-14 15:02:24 -070077rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070078 (void)shared;
David Tolnay4037de22020-04-30 19:51:04 -070079 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA),
80 sizeof(SLICE_DATA));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070081}
82
David Tolnay750755e2020-03-01 13:04:08 -080083rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080084
85std::unique_ptr<std::string> c_return_unique_ptr_string() {
86 return std::unique_ptr<std::string>(new std::string("2020"));
87}
88
Myron Ahneba35cf2020-02-05 19:41:51 +070089std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
David Tolnay85db5a02020-04-25 13:17:27 -070090 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
91 vec->push_back(86);
92 vec->push_back(75);
93 vec->push_back(30);
94 vec->push_back(9);
95 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070096}
97
98std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
David Tolnay85db5a02020-04-25 13:17:27 -070099 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
100 vec->push_back(86.0);
101 vec->push_back(75.0);
102 vec->push_back(30.0);
103 vec->push_back(9.5);
104 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +0700105}
106
David Tolnay47e239d2020-08-28 00:32:04 -0700107std::unique_ptr<std::vector<std::string>> c_return_unique_ptr_vector_string() {
108 return std::unique_ptr<std::vector<std::string>>(
109 new std::vector<std::string>());
110}
111
Myron Ahneba35cf2020-02-05 19:41:51 +0700112std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
David Tolnay85db5a02020-04-25 13:17:27 -0700113 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
114 vec->push_back(Shared{1010});
115 vec->push_back(Shared{1011});
116 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +0700117}
118
David Tolnay1bcc9fe2020-04-25 13:51:07 -0700119std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
120 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
121}
122
David Tolnayde5340e2020-04-25 14:03:21 -0700123const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
124 return c.get_v();
125}
126
David Tolnay18d93b62020-08-27 00:55:48 -0700127std::vector<uint8_t> &c_return_mut_vector(C &c) { return c.get_v(); }
128
David Tolnayb41e74c2020-04-25 15:06:18 -0700129rust::Vec<uint8_t> c_return_rust_vec() {
130 throw std::runtime_error("unimplemented");
131}
132
David Tolnay77989692020-04-25 15:57:47 -0700133const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
134 (void)c;
135 throw std::runtime_error("unimplemented");
136}
137
David Tolnay18d93b62020-08-27 00:55:48 -0700138rust::Vec<uint8_t> &c_return_mut_rust_vec(C &c) {
139 (void)c;
140 throw std::runtime_error("unimplemented");
141}
142
David Tolnay33f56ad2020-08-27 17:06:35 -0700143rust::Vec<rust::String> c_return_rust_vec_string() {
144 throw std::runtime_error("unimplemented");
145}
146
David Tolnay378ca502020-04-27 16:47:55 -0700147size_t c_return_identity(size_t n) { return n; }
Joel Galensonba676072020-04-27 15:55:45 -0700148
David Tolnay378ca502020-04-27 16:47:55 -0700149size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
Joel Galensonba676072020-04-27 15:55:45 -0700150
David Tolnayf6a89f22020-05-10 23:39:27 -0700151Enum c_return_enum(uint16_t n) {
152 if (n <= static_cast<uint16_t>(Enum::AVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700153 return Enum::AVal;
David Tolnayf6a89f22020-05-10 23:39:27 -0700154 } else if (n <= static_cast<uint16_t>(Enum::BVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700155 return Enum::BVal;
156 } else {
157 return Enum::CVal;
158 }
159}
160
Adrian Taylor9a158e42020-10-24 20:51:25 -0700161::A::AEnum c_return_ns_enum(uint16_t n) {
162 if (n <= static_cast<uint16_t>(::A::AEnum::AAVal)) {
163 return ::A::AEnum::AAVal;
164 } else if (n <= static_cast<uint16_t>(::A::AEnum::ABVal)) {
165 return ::A::AEnum::ABVal;
166 } else {
167 return ::A::AEnum::ACVal;
168 }
169}
170
171::A::B::ABEnum c_return_nested_ns_enum(uint16_t n) {
172 if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABAVal)) {
173 return ::A::B::ABEnum::ABAVal;
174 } else if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABBVal)) {
175 return ::A::B::ABEnum::ABBVal;
176 } else {
177 return ::A::B::ABEnum::ABCVal;
178 }
179}
180
David Tolnay3fd7f562020-01-26 17:47:11 -0800181void c_take_primitive(size_t n) {
182 if (n == 2020) {
183 cxx_test_suite_set_correct();
184 }
185}
David Tolnayad5b8af2020-01-26 16:59:13 -0800186
David Tolnay3fd7f562020-01-26 17:47:11 -0800187void c_take_shared(Shared shared) {
188 if (shared.z == 2020) {
189 cxx_test_suite_set_correct();
190 }
191}
David Tolnayad5b8af2020-01-26 16:59:13 -0800192
Adrian Taylor9a158e42020-10-24 20:51:25 -0700193void c_take_ns_shared(::A::AShared shared) {
194 if (shared.z == 2020) {
195 cxx_test_suite_set_correct();
196 }
197}
198
199void c_take_nested_ns_shared(::A::B::ABShared shared) {
200 if (shared.z == 2020) {
201 cxx_test_suite_set_correct();
202 }
203}
204
David Tolnay750755e2020-03-01 13:04:08 -0800205void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -0800206 if (cxx_test_suite_r_is_correct(&*r)) {
207 cxx_test_suite_set_correct();
208 }
David Tolnay3fd7f562020-01-26 17:47:11 -0800209}
David Tolnayad5b8af2020-01-26 16:59:13 -0800210
David Tolnay3fd7f562020-01-26 17:47:11 -0800211void c_take_unique_ptr(std::unique_ptr<C> c) {
212 if (c->get() == 2020) {
213 cxx_test_suite_set_correct();
214 }
215}
David Tolnayad5b8af2020-01-26 16:59:13 -0800216
David Tolnaya7d00e82020-03-06 15:50:14 -0800217void c_take_ref_r(const R &r) {
218 if (cxx_test_suite_r_is_correct(&r)) {
219 cxx_test_suite_set_correct();
220 }
221}
David Tolnayad5b8af2020-01-26 16:59:13 -0800222
David Tolnay3fd7f562020-01-26 17:47:11 -0800223void c_take_ref_c(const C &c) {
224 if (c.get() == 2020) {
225 cxx_test_suite_set_correct();
226 }
227}
David Tolnayad5b8af2020-01-26 16:59:13 -0800228
Adrian Taylord47af7a2020-10-26 13:18:48 -0700229void c_take_ref_ns_c(const ::H::H &h) {
230 if (h.h == "hello") {
231 cxx_test_suite_set_correct();
232 }
233}
234
David Tolnay750755e2020-03-01 13:04:08 -0800235void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800236 if (std::string(s) == "2020") {
237 cxx_test_suite_set_correct();
238 }
239}
David Tolnayad5b8af2020-01-26 16:59:13 -0800240
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700241void c_take_sliceu8(rust::Slice<uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700242 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
243 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700244 cxx_test_suite_set_correct();
245 }
246}
247
David Tolnay750755e2020-03-01 13:04:08 -0800248void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800249 if (std::string(s) == "2020") {
250 cxx_test_suite_set_correct();
251 }
252}
David Tolnayad5b8af2020-01-26 16:59:13 -0800253
David Tolnay3fd7f562020-01-26 17:47:11 -0800254void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
255 if (*s == "2020") {
256 cxx_test_suite_set_correct();
257 }
258}
David Tolnayad5b8af2020-01-26 16:59:13 -0800259
Myron Ahneba35cf2020-02-05 19:41:51 +0700260void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
261 if (v->size() == 4) {
262 cxx_test_suite_set_correct();
263 }
264}
265
266void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
267 if (v->size() == 4) {
268 cxx_test_suite_set_correct();
269 }
270}
271
David Tolnay47e239d2020-08-28 00:32:04 -0700272void c_take_unique_ptr_vector_string(
273 std::unique_ptr<std::vector<std::string>> v) {
274 (void)v;
275 cxx_test_suite_set_correct();
276}
277
Myron Ahneba35cf2020-02-05 19:41:51 +0700278void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
279 if (v->size() == 2) {
280 cxx_test_suite_set_correct();
281 }
282}
283
David Tolnay2244d1f2020-04-25 13:58:18 -0700284void c_take_ref_vector(const std::vector<uint8_t> &v) {
285 if (v.size() == 4) {
286 cxx_test_suite_set_correct();
287 }
288}
289
David Tolnayd2ce8a92020-04-25 16:16:45 -0700290void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
Myron Ahneba35cf2020-02-05 19:41:51 +0700291
David Tolnay61adf422020-08-26 20:51:27 -0700292void c_take_rust_vec_index(rust::Vec<uint8_t> v) {
293 try {
294 v.at(100);
295 } catch (const std::out_of_range &ex) {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700296 std::string expected = "rust::Vec index out of range";
David Tolnay61adf422020-08-26 20:51:27 -0700297 if (ex.what() == expected) {
298 cxx_test_suite_set_correct();
299 }
300 }
301}
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700302
David Tolnayd2ce8a92020-04-25 16:16:45 -0700303void c_take_rust_vec_shared(rust::Vec<Shared> v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700304 uint32_t sum = 0;
David Tolnayc87c2152020-04-24 17:07:41 -0700305 for (auto i : v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700306 sum += i.z;
307 }
308 if (sum == 2021) {
309 cxx_test_suite_set_correct();
310 }
311}
312
Adrian Taylor9a158e42020-10-24 20:51:25 -0700313void c_take_rust_vec_ns_shared(rust::Vec<::A::AShared> v) {
314 uint32_t sum = 0;
315 for (auto i : v) {
316 sum += i.z;
317 }
318 if (sum == 2021) {
319 cxx_test_suite_set_correct();
320 }
321}
322
323void c_take_rust_vec_nested_ns_shared(rust::Vec<::A::B::ABShared> v) {
324 uint32_t sum = 0;
325 for (auto i : v) {
326 sum += i.z;
327 }
328 if (sum == 2021) {
329 cxx_test_suite_set_correct();
330 }
331}
332
David Tolnay33f56ad2020-08-27 17:06:35 -0700333void c_take_rust_vec_string(rust::Vec<rust::String> v) {
334 (void)v;
335 cxx_test_suite_set_correct();
336}
337
myronahnda9be502020-04-29 05:47:23 +0700338void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
339 // Exercise requirements of ForwardIterator
340 // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
341 uint32_t sum = 0;
342 for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
343 sum += it->z;
344 }
345 if (sum == 2021) {
346 cxx_test_suite_set_correct();
347 }
348}
349
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700350void c_take_rust_vec_shared_index(rust::Vec<Shared> v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700351 if (v[0].z == 1010 && v.at(0).z == 1010 && v.front().z == 1010 &&
352 v[1].z == 1011 && v.at(1).z == 1011 && v.back().z == 1011) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700353 cxx_test_suite_set_correct();
354 }
355}
356
David Tolnayd2ce8a92020-04-25 16:16:45 -0700357void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
358 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
359 if (sum == 200) {
360 cxx_test_suite_set_correct();
361 }
362}
363
David Tolnay33f56ad2020-08-27 17:06:35 -0700364void c_take_ref_rust_vec_string(const rust::Vec<rust::String> &v) {
365 (void)v;
366 cxx_test_suite_set_correct();
367}
368
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700369void c_take_ref_rust_vec_index(const rust::Vec<uint8_t> &v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700370 if (v[0] == 86 && v.at(0) == 86 && v.front() == 86 && v[1] == 75 &&
371 v.at(1) == 75 && v[3] == 9 && v.at(3) == 9 && v.back() == 9) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700372 cxx_test_suite_set_correct();
373 }
374}
375
myronahnda9be502020-04-29 05:47:23 +0700376void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
377 // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
378 // requirements for std::iterator_traits.
379 // https://en.cppreference.com/w/cpp/iterator/iterator_traits
David Tolnayf5aeea22020-04-30 19:34:51 -0700380 std::vector<uint8_t> stdv;
381 std::copy(v.begin(), v.end(), std::back_inserter(stdv));
382 uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
myronahnda9be502020-04-29 05:47:23 +0700383 if (sum == 200) {
384 cxx_test_suite_set_correct();
385 }
386}
387
David Tolnay701c6882020-11-02 10:44:39 -0800388const SharedString &c_take_ref_shared_string(const SharedString &s) {
389 if (std::string(s.msg) == "2020") {
390 cxx_test_suite_set_correct();
391 }
392 return s;
393}
394
David Tolnay75dca2e2020-03-25 20:17:52 -0700395void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
396 callback("2020");
397}
398
Joel Galensonc03402a2020-04-23 17:31:09 -0700399void c_take_enum(Enum e) {
400 if (e == Enum::AVal) {
401 cxx_test_suite_set_correct();
402 }
403}
404
Adrian Taylor9a158e42020-10-24 20:51:25 -0700405void c_take_ns_enum(::A::AEnum e) {
406 if (e == ::A::AEnum::AAVal) {
407 cxx_test_suite_set_correct();
408 }
409}
410
411void c_take_nested_ns_enum(::A::B::ABEnum e) {
412 if (e == ::A::B::ABEnum::ABAVal) {
413 cxx_test_suite_set_correct();
414 }
415}
416
David Tolnayebef4a22020-03-17 15:33:47 -0700417void c_try_return_void() {}
418
419size_t c_try_return_primitive() { return 2020; }
420
421size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
422
David Tolnay99642622020-03-25 13:07:35 -0700423rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700424
David Tolnay99642622020-03-25 13:07:35 -0700425const rust::String &c_try_return_ref(const rust::String &s) { return s; }
426
427rust::Str c_try_return_str(rust::Str s) { return s; }
428
Adrian Taylorec9430e2020-04-14 16:09:58 -0700429rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700430
David Tolnay99642622020-03-25 13:07:35 -0700431rust::String c_try_return_rust_string() { return c_return_rust_string(); }
432
433std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
434 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700435}
436
David Tolnay8b9d1762020-04-25 16:05:46 -0700437rust::Vec<uint8_t> c_try_return_rust_vec() {
438 throw std::runtime_error("unimplemented");
439}
440
David Tolnay33f56ad2020-08-27 17:06:35 -0700441rust::Vec<rust::String> c_try_return_rust_vec_string() {
442 throw std::runtime_error("unimplemented");
443}
444
David Tolnay77989692020-04-25 15:57:47 -0700445const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
446 (void)c;
447 throw std::runtime_error("unimplemented");
448}
449
David Tolnay2fe58c62020-03-06 16:23:09 -0800450extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800451 return std::unique_ptr<C>(new C{2020}).release();
452}
453
David Tolnay85db24862020-03-06 16:24:41 -0800454extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
455 return std::unique_ptr<std::string>(new std::string("2020")).release();
456}
457
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700458rust::String C::cOverloadedMethod(int32_t x) const {
459 return rust::String(std::to_string(x));
460}
461
462rust::String C::cOverloadedMethod(rust::Str x) const {
463 return rust::String(std::string(x));
464}
465
466rust::String cOverloadedFunction(int x) {
467 return rust::String(std::to_string(x));
468}
469
470rust::String cOverloadedFunction(rust::Str x) {
471 return rust::String(std::string(x));
472}
473
Adrian Taylor121cca42020-10-10 15:32:00 -0700474void c_take_trivial_ptr(std::unique_ptr<D> d) {
475 if (d->d == 30) {
476 cxx_test_suite_set_correct();
477 }
478}
479
David Tolnaybf23e3e2020-10-30 21:09:16 -0700480void c_take_trivial_ref(const D &d) {
Adrian Taylor121cca42020-10-10 15:32:00 -0700481 if (d.d == 30) {
482 cxx_test_suite_set_correct();
483 }
484}
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700485
Adrian Taylor121cca42020-10-10 15:32:00 -0700486void c_take_trivial(D d) {
487 if (d.d == 30) {
488 cxx_test_suite_set_correct();
489 }
490}
491
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700492void c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g) {
493 if (g->g == 30) {
494 cxx_test_suite_set_correct();
495 }
496}
497
David Tolnaybf23e3e2020-10-30 21:09:16 -0700498void c_take_trivial_ns_ref(const ::G::G &g) {
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700499 if (g.g == 30) {
500 cxx_test_suite_set_correct();
501 }
502}
503
504void c_take_trivial_ns(::G::G g) {
505 if (g.g == 30) {
506 cxx_test_suite_set_correct();
507 }
508}
509
Adrian Taylor121cca42020-10-10 15:32:00 -0700510void c_take_opaque_ptr(std::unique_ptr<E> e) {
511 if (e->e == 40) {
512 cxx_test_suite_set_correct();
513 }
514}
515
Adrian Taylor5e79c642020-10-24 21:09:42 -0700516void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f) {
517 if (f->f == 40) {
518 cxx_test_suite_set_correct();
519 }
520}
521
David Tolnaybf23e3e2020-10-30 21:09:16 -0700522void c_take_opaque_ref(const E &e) {
Adrian Taylor121cca42020-10-10 15:32:00 -0700523 if (e.e == 40 && e.e_str == "hello") {
524 cxx_test_suite_set_correct();
525 }
526}
527
David Tolnaybf23e3e2020-10-30 21:09:16 -0700528void c_take_opaque_ns_ref(const ::F::F &f) {
Adrian Taylor5e79c642020-10-24 21:09:42 -0700529 if (f.f == 40 && f.f_str == "hello") {
530 cxx_test_suite_set_correct();
531 }
532}
533
Adrian Taylor121cca42020-10-10 15:32:00 -0700534std::unique_ptr<D> c_return_trivial_ptr() {
535 auto d = std::unique_ptr<D>(new D());
536 d->d = 30;
537 return d;
538}
539
540D c_return_trivial() {
541 D d;
542 d.d = 30;
543 return d;
544}
545
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700546std::unique_ptr<::G::G> c_return_trivial_ns_ptr() {
547 auto g = std::unique_ptr<::G::G>(new ::G::G());
548 g->g = 30;
549 return g;
550}
551
552::G::G c_return_trivial_ns() {
553 ::G::G g;
554 g.g = 30;
555 return g;
556}
557
Adrian Taylor121cca42020-10-10 15:32:00 -0700558std::unique_ptr<E> c_return_opaque_ptr() {
559 auto e = std::unique_ptr<E>(new E());
560 e->e = 40;
561 e->e_str = std::string("hello");
562 return e;
563}
564
Adrian Taylor5e79c642020-10-24 21:09:42 -0700565std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
566 auto f = std::unique_ptr<::F::F>(new ::F::F());
567 f->f = 40;
568 f->f_str = std::string("hello");
569 return f;
570}
571
David Tolnayf306da42020-02-22 19:55:43 -0800572extern "C" const char *cxx_run_test() noexcept {
573#define STRINGIFY(x) #x
574#define TOSTRING(x) STRINGIFY(x)
575#define ASSERT(x) \
576 do { \
577 if (!(x)) { \
578 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
579 } \
580 } while (false)
581
582 ASSERT(r_return_primitive() == 2020);
583 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800584 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800585 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800586 ASSERT(r_return_ref(Shared{2020}) == 2020);
587 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
588 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800589 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700590 ASSERT(r_return_identity(2020) == 2020);
591 ASSERT(r_return_sum(2020, 1) == 2021);
Joel Galensonc03402a2020-04-23 17:31:09 -0700592 ASSERT(r_return_enum(0) == Enum::AVal);
593 ASSERT(r_return_enum(1) == Enum::BVal);
594 ASSERT(r_return_enum(2021) == Enum::CVal);
David Tolnayf306da42020-02-22 19:55:43 -0800595
596 r_take_primitive(2020);
597 r_take_shared(Shared{2020});
598 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
599 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800600 r_take_str(rust::Str("2020"));
David Tolnay4037de22020-04-30 19:51:04 -0700601 r_take_sliceu8(rust::Slice<uint8_t>(
602 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
David Tolnay40226ab2020-03-03 00:05:35 -0800603 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800604 r_take_unique_ptr_string(
605 std::unique_ptr<std::string>(new std::string("2020")));
David Tolnay5df0a712020-09-24 15:58:28 -0400606 r_take_ref_vector(std::vector<uint8_t>{20, 2, 0});
David Tolnay80631e92020-09-24 16:07:30 -0400607 std::vector<uint64_t> empty_vector;
608 r_take_ref_empty_vector(empty_vector);
609 empty_vector.reserve(10);
610 r_take_ref_empty_vector(empty_vector);
Joel Galensonc03402a2020-04-23 17:31:09 -0700611 r_take_enum(Enum::AVal);
David Tolnayf306da42020-02-22 19:55:43 -0800612
David Tolnayb6c5ea72020-03-16 13:36:28 -0700613 ASSERT(r_try_return_primitive() == 2020);
614 try {
615 r_fail_return_primitive();
616 ASSERT(false);
617 } catch (const rust::Error &e) {
618 ASSERT(std::strcmp(e.what(), "rust error") == 0);
619 }
620
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700621 auto r2 = r_return_r2(2020);
622 ASSERT(r2->get() == 2020);
623 ASSERT(r2->set(2021) == 2021);
624 ASSERT(r2->get() == 2021);
625 ASSERT(r2->set(2020) == 2020);
626 ASSERT(r2->get() == 2020);
627
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700628 ASSERT(std::string(rAliasedFunction(2020)) == "2020");
629
David Tolnayf306da42020-02-22 19:55:43 -0800630 cxx_test_suite_set_correct();
631 return nullptr;
632}
633
David Tolnay97c72102020-01-25 16:49:00 -0800634} // namespace tests
Adrian Taylorddc146e2020-10-25 21:40:17 -0700635
636namespace other {
David Tolnaybf23e3e2020-10-30 21:09:16 -0700637void ns_c_take_trivial(::tests::D d) {
638 if (d.d == 30) {
639 cxx_test_suite_set_correct();
Adrian Taylorddc146e2020-10-25 21:40:17 -0700640 }
David Tolnaybf23e3e2020-10-30 21:09:16 -0700641}
Adrian Taylorddc146e2020-10-25 21:40:17 -0700642
David Tolnaybf23e3e2020-10-30 21:09:16 -0700643::tests::D ns_c_return_trivial() {
644 ::tests::D d;
645 d.d = 30;
646 return d;
647}
Adrian Taylorddc146e2020-10-25 21:40:17 -0700648
David Tolnaybf23e3e2020-10-30 21:09:16 -0700649void ns_c_take_ns_shared(::A::AShared shared) {
650 if (shared.z == 2020) {
651 cxx_test_suite_set_correct();
Adrian Taylorddc146e2020-10-25 21:40:17 -0700652 }
David Tolnaybf23e3e2020-10-30 21:09:16 -0700653}
Adrian Taylor0fac3212020-10-25 21:52:55 -0700654} // namespace other
655
656namespace I {
David Tolnaybf23e3e2020-10-30 21:09:16 -0700657uint32_t I::get() const { return a; }
Adrian Taylor0fac3212020-10-25 21:52:55 -0700658
David Tolnaybf23e3e2020-10-30 21:09:16 -0700659std::unique_ptr<I> ns_c_return_unique_ptr_ns() {
660 return std::unique_ptr<I>(new I());
661}
Adrian Taylor0fac3212020-10-25 21:52:55 -0700662} // namespace I