blob: a03ec2b75f02dcdc5610e0bf838beb23dabc1341 [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 Tolnay75dca2e2020-03-25 20:17:52 -0700388void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
389 callback("2020");
390}
391
Joel Galensonc03402a2020-04-23 17:31:09 -0700392void c_take_enum(Enum e) {
393 if (e == Enum::AVal) {
394 cxx_test_suite_set_correct();
395 }
396}
397
Adrian Taylor9a158e42020-10-24 20:51:25 -0700398void c_take_ns_enum(::A::AEnum e) {
399 if (e == ::A::AEnum::AAVal) {
400 cxx_test_suite_set_correct();
401 }
402}
403
404void c_take_nested_ns_enum(::A::B::ABEnum e) {
405 if (e == ::A::B::ABEnum::ABAVal) {
406 cxx_test_suite_set_correct();
407 }
408}
409
David Tolnayebef4a22020-03-17 15:33:47 -0700410void c_try_return_void() {}
411
412size_t c_try_return_primitive() { return 2020; }
413
414size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
415
David Tolnay99642622020-03-25 13:07:35 -0700416rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700417
David Tolnay99642622020-03-25 13:07:35 -0700418const rust::String &c_try_return_ref(const rust::String &s) { return s; }
419
420rust::Str c_try_return_str(rust::Str s) { return s; }
421
Adrian Taylorec9430e2020-04-14 16:09:58 -0700422rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700423
David Tolnay99642622020-03-25 13:07:35 -0700424rust::String c_try_return_rust_string() { return c_return_rust_string(); }
425
426std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
427 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700428}
429
David Tolnay8b9d1762020-04-25 16:05:46 -0700430rust::Vec<uint8_t> c_try_return_rust_vec() {
431 throw std::runtime_error("unimplemented");
432}
433
David Tolnay33f56ad2020-08-27 17:06:35 -0700434rust::Vec<rust::String> c_try_return_rust_vec_string() {
435 throw std::runtime_error("unimplemented");
436}
437
David Tolnay77989692020-04-25 15:57:47 -0700438const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
439 (void)c;
440 throw std::runtime_error("unimplemented");
441}
442
David Tolnay2fe58c62020-03-06 16:23:09 -0800443extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800444 return std::unique_ptr<C>(new C{2020}).release();
445}
446
David Tolnay85db24862020-03-06 16:24:41 -0800447extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
448 return std::unique_ptr<std::string>(new std::string("2020")).release();
449}
450
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700451rust::String C::cOverloadedMethod(int32_t x) const {
452 return rust::String(std::to_string(x));
453}
454
455rust::String C::cOverloadedMethod(rust::Str x) const {
456 return rust::String(std::string(x));
457}
458
459rust::String cOverloadedFunction(int x) {
460 return rust::String(std::to_string(x));
461}
462
463rust::String cOverloadedFunction(rust::Str x) {
464 return rust::String(std::string(x));
465}
466
Adrian Taylor121cca42020-10-10 15:32:00 -0700467void c_take_trivial_ptr(std::unique_ptr<D> d) {
468 if (d->d == 30) {
469 cxx_test_suite_set_correct();
470 }
471}
472
David Tolnaybf23e3e2020-10-30 21:09:16 -0700473void c_take_trivial_ref(const D &d) {
Adrian Taylor121cca42020-10-10 15:32:00 -0700474 if (d.d == 30) {
475 cxx_test_suite_set_correct();
476 }
477}
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700478
Adrian Taylor121cca42020-10-10 15:32:00 -0700479void c_take_trivial(D d) {
480 if (d.d == 30) {
481 cxx_test_suite_set_correct();
482 }
483}
484
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700485void c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g) {
486 if (g->g == 30) {
487 cxx_test_suite_set_correct();
488 }
489}
490
David Tolnaybf23e3e2020-10-30 21:09:16 -0700491void c_take_trivial_ns_ref(const ::G::G &g) {
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700492 if (g.g == 30) {
493 cxx_test_suite_set_correct();
494 }
495}
496
497void c_take_trivial_ns(::G::G g) {
498 if (g.g == 30) {
499 cxx_test_suite_set_correct();
500 }
501}
502
Adrian Taylor121cca42020-10-10 15:32:00 -0700503void c_take_opaque_ptr(std::unique_ptr<E> e) {
504 if (e->e == 40) {
505 cxx_test_suite_set_correct();
506 }
507}
508
Adrian Taylor5e79c642020-10-24 21:09:42 -0700509void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f) {
510 if (f->f == 40) {
511 cxx_test_suite_set_correct();
512 }
513}
514
David Tolnaybf23e3e2020-10-30 21:09:16 -0700515void c_take_opaque_ref(const E &e) {
Adrian Taylor121cca42020-10-10 15:32:00 -0700516 if (e.e == 40 && e.e_str == "hello") {
517 cxx_test_suite_set_correct();
518 }
519}
520
David Tolnaybf23e3e2020-10-30 21:09:16 -0700521void c_take_opaque_ns_ref(const ::F::F &f) {
Adrian Taylor5e79c642020-10-24 21:09:42 -0700522 if (f.f == 40 && f.f_str == "hello") {
523 cxx_test_suite_set_correct();
524 }
525}
526
Adrian Taylor121cca42020-10-10 15:32:00 -0700527std::unique_ptr<D> c_return_trivial_ptr() {
528 auto d = std::unique_ptr<D>(new D());
529 d->d = 30;
530 return d;
531}
532
533D c_return_trivial() {
534 D d;
535 d.d = 30;
536 return d;
537}
538
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700539std::unique_ptr<::G::G> c_return_trivial_ns_ptr() {
540 auto g = std::unique_ptr<::G::G>(new ::G::G());
541 g->g = 30;
542 return g;
543}
544
545::G::G c_return_trivial_ns() {
546 ::G::G g;
547 g.g = 30;
548 return g;
549}
550
Adrian Taylor121cca42020-10-10 15:32:00 -0700551std::unique_ptr<E> c_return_opaque_ptr() {
552 auto e = std::unique_ptr<E>(new E());
553 e->e = 40;
554 e->e_str = std::string("hello");
555 return e;
556}
557
Adrian Taylor5e79c642020-10-24 21:09:42 -0700558std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
559 auto f = std::unique_ptr<::F::F>(new ::F::F());
560 f->f = 40;
561 f->f_str = std::string("hello");
562 return f;
563}
564
David Tolnayf306da42020-02-22 19:55:43 -0800565extern "C" const char *cxx_run_test() noexcept {
566#define STRINGIFY(x) #x
567#define TOSTRING(x) STRINGIFY(x)
568#define ASSERT(x) \
569 do { \
570 if (!(x)) { \
571 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
572 } \
573 } while (false)
574
575 ASSERT(r_return_primitive() == 2020);
576 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800577 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800578 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800579 ASSERT(r_return_ref(Shared{2020}) == 2020);
580 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
581 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800582 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700583 ASSERT(r_return_identity(2020) == 2020);
584 ASSERT(r_return_sum(2020, 1) == 2021);
Joel Galensonc03402a2020-04-23 17:31:09 -0700585 ASSERT(r_return_enum(0) == Enum::AVal);
586 ASSERT(r_return_enum(1) == Enum::BVal);
587 ASSERT(r_return_enum(2021) == Enum::CVal);
David Tolnayf306da42020-02-22 19:55:43 -0800588
589 r_take_primitive(2020);
590 r_take_shared(Shared{2020});
591 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
592 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800593 r_take_str(rust::Str("2020"));
David Tolnay4037de22020-04-30 19:51:04 -0700594 r_take_sliceu8(rust::Slice<uint8_t>(
595 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
David Tolnay40226ab2020-03-03 00:05:35 -0800596 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800597 r_take_unique_ptr_string(
598 std::unique_ptr<std::string>(new std::string("2020")));
David Tolnay5df0a712020-09-24 15:58:28 -0400599 r_take_ref_vector(std::vector<uint8_t>{20, 2, 0});
David Tolnay80631e92020-09-24 16:07:30 -0400600 std::vector<uint64_t> empty_vector;
601 r_take_ref_empty_vector(empty_vector);
602 empty_vector.reserve(10);
603 r_take_ref_empty_vector(empty_vector);
Joel Galensonc03402a2020-04-23 17:31:09 -0700604 r_take_enum(Enum::AVal);
David Tolnayf306da42020-02-22 19:55:43 -0800605
David Tolnayb6c5ea72020-03-16 13:36:28 -0700606 ASSERT(r_try_return_primitive() == 2020);
607 try {
608 r_fail_return_primitive();
609 ASSERT(false);
610 } catch (const rust::Error &e) {
611 ASSERT(std::strcmp(e.what(), "rust error") == 0);
612 }
613
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700614 auto r2 = r_return_r2(2020);
615 ASSERT(r2->get() == 2020);
616 ASSERT(r2->set(2021) == 2021);
617 ASSERT(r2->get() == 2021);
618 ASSERT(r2->set(2020) == 2020);
619 ASSERT(r2->get() == 2020);
620
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700621 ASSERT(std::string(rAliasedFunction(2020)) == "2020");
622
David Tolnayf306da42020-02-22 19:55:43 -0800623 cxx_test_suite_set_correct();
624 return nullptr;
625}
626
David Tolnay97c72102020-01-25 16:49:00 -0800627} // namespace tests
Adrian Taylorddc146e2020-10-25 21:40:17 -0700628
629namespace other {
David Tolnaybf23e3e2020-10-30 21:09:16 -0700630void ns_c_take_trivial(::tests::D d) {
631 if (d.d == 30) {
632 cxx_test_suite_set_correct();
Adrian Taylorddc146e2020-10-25 21:40:17 -0700633 }
David Tolnaybf23e3e2020-10-30 21:09:16 -0700634}
Adrian Taylorddc146e2020-10-25 21:40:17 -0700635
David Tolnaybf23e3e2020-10-30 21:09:16 -0700636::tests::D ns_c_return_trivial() {
637 ::tests::D d;
638 d.d = 30;
639 return d;
640}
Adrian Taylorddc146e2020-10-25 21:40:17 -0700641
David Tolnaybf23e3e2020-10-30 21:09:16 -0700642void ns_c_take_ns_shared(::A::AShared shared) {
643 if (shared.z == 2020) {
644 cxx_test_suite_set_correct();
Adrian Taylorddc146e2020-10-25 21:40:17 -0700645 }
David Tolnaybf23e3e2020-10-30 21:09:16 -0700646}
Adrian Taylor0fac3212020-10-25 21:52:55 -0700647} // namespace other
648
649namespace I {
David Tolnaybf23e3e2020-10-30 21:09:16 -0700650uint32_t I::get() const { return a; }
Adrian Taylor0fac3212020-10-25 21:52:55 -0700651
David Tolnaybf23e3e2020-10-30 21:09:16 -0700652std::unique_ptr<I> ns_c_return_unique_ptr_ns() {
653 return std::unique_ptr<I>(new I());
654}
Adrian Taylor0fac3212020-10-25 21:52:55 -0700655} // namespace I