blob: 2305766cd560ecbd419ac385da2c5c4c32b8ceb0 [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 Tolnay464aeeb2020-11-08 19:12:10 -080038size_t Shared::c_method_on_shared() const noexcept { return 2021; }
David Tolnay102c7ea2020-11-08 18:58:09 -080039
David Tolnayde5340e2020-04-25 14:03:21 -070040const std::vector<uint8_t> &C::get_v() const { return this->v; }
41
David Tolnay18d93b62020-08-27 00:55:48 -070042std::vector<uint8_t> &C::get_v() { return this->v; }
43
David Tolnayb8715772020-01-28 00:54:05 -080044size_t c_return_primitive() { return 2020; }
David Tolnayad5b8af2020-01-26 16:59:13 -080045
David Tolnayb8715772020-01-28 00:54:05 -080046Shared c_return_shared() { return Shared{2020}; }
David Tolnayad5b8af2020-01-26 16:59:13 -080047
Adrian Taylor9a158e42020-10-24 20:51:25 -070048::A::AShared c_return_ns_shared() { return ::A::AShared{2020}; }
49
50::A::B::ABShared c_return_nested_ns_shared() { return ::A::B::ABShared{2020}; }
51
David Tolnaybe13d8a2020-03-06 15:45:39 -080052rust::Box<R> c_return_box() {
53 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
54}
55
David Tolnayad5b8af2020-01-26 16:59:13 -080056std::unique_ptr<C> c_return_unique_ptr() {
57 return std::unique_ptr<C>(new C{2020});
58}
59
Adrian Taylord47af7a2020-10-26 13:18:48 -070060std::unique_ptr<::H::H> c_return_ns_unique_ptr() {
61 return std::unique_ptr<::H::H>(new ::H::H{"hello"});
62}
63
David Tolnayb8715772020-01-28 00:54:05 -080064const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080065
Adrian Taylor9a158e42020-10-24 20:51:25 -070066const size_t &c_return_ns_ref(const ::A::AShared &shared) { return shared.z; }
67
David Tolnaybf23e3e2020-10-30 21:09:16 -070068const size_t &c_return_nested_ns_ref(const ::A::B::ABShared &shared) {
69 return shared.z;
70}
Adrian Taylor9a158e42020-10-24 20:51:25 -070071
David Tolnay18d93b62020-08-27 00:55:48 -070072size_t &c_return_mut(Shared &shared) { return shared.z; }
73
David Tolnay750755e2020-03-01 13:04:08 -080074rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080075 (void)shared;
76 return "2020";
77}
78
David Tolnayeb952ba2020-04-14 15:02:24 -070079rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070080 (void)shared;
David Tolnay4037de22020-04-30 19:51:04 -070081 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA),
82 sizeof(SLICE_DATA));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070083}
84
David Tolnay750755e2020-03-01 13:04:08 -080085rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080086
87std::unique_ptr<std::string> c_return_unique_ptr_string() {
88 return std::unique_ptr<std::string>(new std::string("2020"));
89}
90
Myron Ahneba35cf2020-02-05 19:41:51 +070091std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
David Tolnay85db5a02020-04-25 13:17:27 -070092 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
93 vec->push_back(86);
94 vec->push_back(75);
95 vec->push_back(30);
96 vec->push_back(9);
97 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070098}
99
100std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
David Tolnay85db5a02020-04-25 13:17:27 -0700101 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
102 vec->push_back(86.0);
103 vec->push_back(75.0);
104 vec->push_back(30.0);
105 vec->push_back(9.5);
106 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +0700107}
108
David Tolnay47e239d2020-08-28 00:32:04 -0700109std::unique_ptr<std::vector<std::string>> c_return_unique_ptr_vector_string() {
110 return std::unique_ptr<std::vector<std::string>>(
111 new std::vector<std::string>());
112}
113
Myron Ahneba35cf2020-02-05 19:41:51 +0700114std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
David Tolnay85db5a02020-04-25 13:17:27 -0700115 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
116 vec->push_back(Shared{1010});
117 vec->push_back(Shared{1011});
118 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +0700119}
120
David Tolnay1bcc9fe2020-04-25 13:51:07 -0700121std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
122 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
123}
124
David Tolnayde5340e2020-04-25 14:03:21 -0700125const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
126 return c.get_v();
127}
128
David Tolnay18d93b62020-08-27 00:55:48 -0700129std::vector<uint8_t> &c_return_mut_vector(C &c) { return c.get_v(); }
130
David Tolnayb41e74c2020-04-25 15:06:18 -0700131rust::Vec<uint8_t> c_return_rust_vec() {
132 throw std::runtime_error("unimplemented");
133}
134
David Tolnay77989692020-04-25 15:57:47 -0700135const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
136 (void)c;
137 throw std::runtime_error("unimplemented");
138}
139
David Tolnay18d93b62020-08-27 00:55:48 -0700140rust::Vec<uint8_t> &c_return_mut_rust_vec(C &c) {
141 (void)c;
142 throw std::runtime_error("unimplemented");
143}
144
David Tolnay33f56ad2020-08-27 17:06:35 -0700145rust::Vec<rust::String> c_return_rust_vec_string() {
146 throw std::runtime_error("unimplemented");
147}
148
David Tolnay378ca502020-04-27 16:47:55 -0700149size_t c_return_identity(size_t n) { return n; }
Joel Galensonba676072020-04-27 15:55:45 -0700150
David Tolnay378ca502020-04-27 16:47:55 -0700151size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
Joel Galensonba676072020-04-27 15:55:45 -0700152
David Tolnayf6a89f22020-05-10 23:39:27 -0700153Enum c_return_enum(uint16_t n) {
154 if (n <= static_cast<uint16_t>(Enum::AVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700155 return Enum::AVal;
David Tolnayf6a89f22020-05-10 23:39:27 -0700156 } else if (n <= static_cast<uint16_t>(Enum::BVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700157 return Enum::BVal;
158 } else {
159 return Enum::CVal;
160 }
161}
162
Adrian Taylor9a158e42020-10-24 20:51:25 -0700163::A::AEnum c_return_ns_enum(uint16_t n) {
164 if (n <= static_cast<uint16_t>(::A::AEnum::AAVal)) {
165 return ::A::AEnum::AAVal;
166 } else if (n <= static_cast<uint16_t>(::A::AEnum::ABVal)) {
167 return ::A::AEnum::ABVal;
168 } else {
169 return ::A::AEnum::ACVal;
170 }
171}
172
173::A::B::ABEnum c_return_nested_ns_enum(uint16_t n) {
174 if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABAVal)) {
175 return ::A::B::ABEnum::ABAVal;
176 } else if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABBVal)) {
177 return ::A::B::ABEnum::ABBVal;
178 } else {
179 return ::A::B::ABEnum::ABCVal;
180 }
181}
182
David Tolnay3fd7f562020-01-26 17:47:11 -0800183void c_take_primitive(size_t n) {
184 if (n == 2020) {
185 cxx_test_suite_set_correct();
186 }
187}
David Tolnayad5b8af2020-01-26 16:59:13 -0800188
David Tolnay3fd7f562020-01-26 17:47:11 -0800189void c_take_shared(Shared shared) {
190 if (shared.z == 2020) {
191 cxx_test_suite_set_correct();
192 }
193}
David Tolnayad5b8af2020-01-26 16:59:13 -0800194
Adrian Taylor9a158e42020-10-24 20:51:25 -0700195void c_take_ns_shared(::A::AShared shared) {
196 if (shared.z == 2020) {
197 cxx_test_suite_set_correct();
198 }
199}
200
201void c_take_nested_ns_shared(::A::B::ABShared shared) {
202 if (shared.z == 2020) {
203 cxx_test_suite_set_correct();
204 }
205}
206
David Tolnay750755e2020-03-01 13:04:08 -0800207void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -0800208 if (cxx_test_suite_r_is_correct(&*r)) {
209 cxx_test_suite_set_correct();
210 }
David Tolnay3fd7f562020-01-26 17:47:11 -0800211}
David Tolnayad5b8af2020-01-26 16:59:13 -0800212
David Tolnay3fd7f562020-01-26 17:47:11 -0800213void c_take_unique_ptr(std::unique_ptr<C> c) {
214 if (c->get() == 2020) {
215 cxx_test_suite_set_correct();
216 }
217}
David Tolnayad5b8af2020-01-26 16:59:13 -0800218
David Tolnaya7d00e82020-03-06 15:50:14 -0800219void c_take_ref_r(const R &r) {
220 if (cxx_test_suite_r_is_correct(&r)) {
221 cxx_test_suite_set_correct();
222 }
223}
David Tolnayad5b8af2020-01-26 16:59:13 -0800224
David Tolnay3fd7f562020-01-26 17:47:11 -0800225void c_take_ref_c(const C &c) {
226 if (c.get() == 2020) {
227 cxx_test_suite_set_correct();
228 }
229}
David Tolnayad5b8af2020-01-26 16:59:13 -0800230
Adrian Taylord47af7a2020-10-26 13:18:48 -0700231void c_take_ref_ns_c(const ::H::H &h) {
232 if (h.h == "hello") {
233 cxx_test_suite_set_correct();
234 }
235}
236
David Tolnay750755e2020-03-01 13:04:08 -0800237void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800238 if (std::string(s) == "2020") {
239 cxx_test_suite_set_correct();
240 }
241}
David Tolnayad5b8af2020-01-26 16:59:13 -0800242
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700243void c_take_sliceu8(rust::Slice<uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700244 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
245 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700246 cxx_test_suite_set_correct();
247 }
248}
249
David Tolnay750755e2020-03-01 13:04:08 -0800250void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800251 if (std::string(s) == "2020") {
252 cxx_test_suite_set_correct();
253 }
254}
David Tolnayad5b8af2020-01-26 16:59:13 -0800255
David Tolnay3fd7f562020-01-26 17:47:11 -0800256void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
257 if (*s == "2020") {
258 cxx_test_suite_set_correct();
259 }
260}
David Tolnayad5b8af2020-01-26 16:59:13 -0800261
Myron Ahneba35cf2020-02-05 19:41:51 +0700262void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
263 if (v->size() == 4) {
264 cxx_test_suite_set_correct();
265 }
266}
267
268void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
269 if (v->size() == 4) {
270 cxx_test_suite_set_correct();
271 }
272}
273
David Tolnay47e239d2020-08-28 00:32:04 -0700274void c_take_unique_ptr_vector_string(
275 std::unique_ptr<std::vector<std::string>> v) {
276 (void)v;
277 cxx_test_suite_set_correct();
278}
279
Myron Ahneba35cf2020-02-05 19:41:51 +0700280void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
281 if (v->size() == 2) {
282 cxx_test_suite_set_correct();
283 }
284}
285
David Tolnay2244d1f2020-04-25 13:58:18 -0700286void c_take_ref_vector(const std::vector<uint8_t> &v) {
287 if (v.size() == 4) {
288 cxx_test_suite_set_correct();
289 }
290}
291
David Tolnayd2ce8a92020-04-25 16:16:45 -0700292void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
Myron Ahneba35cf2020-02-05 19:41:51 +0700293
David Tolnay61adf422020-08-26 20:51:27 -0700294void c_take_rust_vec_index(rust::Vec<uint8_t> v) {
295 try {
296 v.at(100);
297 } catch (const std::out_of_range &ex) {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700298 std::string expected = "rust::Vec index out of range";
David Tolnay61adf422020-08-26 20:51:27 -0700299 if (ex.what() == expected) {
300 cxx_test_suite_set_correct();
301 }
302 }
303}
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700304
David Tolnayd2ce8a92020-04-25 16:16:45 -0700305void c_take_rust_vec_shared(rust::Vec<Shared> v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700306 uint32_t sum = 0;
David Tolnayc87c2152020-04-24 17:07:41 -0700307 for (auto i : v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700308 sum += i.z;
309 }
310 if (sum == 2021) {
311 cxx_test_suite_set_correct();
312 }
313}
314
Adrian Taylor9a158e42020-10-24 20:51:25 -0700315void c_take_rust_vec_ns_shared(rust::Vec<::A::AShared> v) {
316 uint32_t sum = 0;
317 for (auto i : v) {
318 sum += i.z;
319 }
320 if (sum == 2021) {
321 cxx_test_suite_set_correct();
322 }
323}
324
325void c_take_rust_vec_nested_ns_shared(rust::Vec<::A::B::ABShared> v) {
326 uint32_t sum = 0;
327 for (auto i : v) {
328 sum += i.z;
329 }
330 if (sum == 2021) {
331 cxx_test_suite_set_correct();
332 }
333}
334
David Tolnay33f56ad2020-08-27 17:06:35 -0700335void c_take_rust_vec_string(rust::Vec<rust::String> v) {
336 (void)v;
337 cxx_test_suite_set_correct();
338}
339
myronahnda9be502020-04-29 05:47:23 +0700340void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
341 // Exercise requirements of ForwardIterator
342 // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
343 uint32_t sum = 0;
344 for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
345 sum += it->z;
346 }
347 if (sum == 2021) {
348 cxx_test_suite_set_correct();
349 }
350}
351
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700352void c_take_rust_vec_shared_index(rust::Vec<Shared> v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700353 if (v[0].z == 1010 && v.at(0).z == 1010 && v.front().z == 1010 &&
354 v[1].z == 1011 && v.at(1).z == 1011 && v.back().z == 1011) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700355 cxx_test_suite_set_correct();
356 }
357}
358
David Tolnayfb6b73c2020-11-10 14:32:16 -0800359void c_take_rust_vec_shared_push(rust::Vec<Shared> v) {
360 v.push_back(Shared{3});
361 v.emplace_back(Shared{2});
362 if (v[v.size() - 2].z == 3 && v.back().z == 2) {
363 cxx_test_suite_set_correct();
364 }
365}
366
David Tolnayd2ce8a92020-04-25 16:16:45 -0700367void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
368 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
369 if (sum == 200) {
370 cxx_test_suite_set_correct();
371 }
372}
373
David Tolnay33f56ad2020-08-27 17:06:35 -0700374void c_take_ref_rust_vec_string(const rust::Vec<rust::String> &v) {
375 (void)v;
376 cxx_test_suite_set_correct();
377}
378
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700379void c_take_ref_rust_vec_index(const rust::Vec<uint8_t> &v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700380 if (v[0] == 86 && v.at(0) == 86 && v.front() == 86 && v[1] == 75 &&
381 v.at(1) == 75 && v[3] == 9 && v.at(3) == 9 && v.back() == 9) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700382 cxx_test_suite_set_correct();
383 }
384}
385
myronahnda9be502020-04-29 05:47:23 +0700386void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
387 // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
388 // requirements for std::iterator_traits.
389 // https://en.cppreference.com/w/cpp/iterator/iterator_traits
David Tolnayf5aeea22020-04-30 19:34:51 -0700390 std::vector<uint8_t> stdv;
391 std::copy(v.begin(), v.end(), std::back_inserter(stdv));
392 uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
myronahnda9be502020-04-29 05:47:23 +0700393 if (sum == 200) {
394 cxx_test_suite_set_correct();
395 }
396}
397
David Tolnay701c6882020-11-02 10:44:39 -0800398const SharedString &c_take_ref_shared_string(const SharedString &s) {
399 if (std::string(s.msg) == "2020") {
400 cxx_test_suite_set_correct();
401 }
402 return s;
403}
404
David Tolnay75dca2e2020-03-25 20:17:52 -0700405void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
406 callback("2020");
407}
408
Joel Galensonc03402a2020-04-23 17:31:09 -0700409void c_take_enum(Enum e) {
410 if (e == Enum::AVal) {
411 cxx_test_suite_set_correct();
412 }
413}
414
Adrian Taylor9a158e42020-10-24 20:51:25 -0700415void c_take_ns_enum(::A::AEnum e) {
416 if (e == ::A::AEnum::AAVal) {
417 cxx_test_suite_set_correct();
418 }
419}
420
421void c_take_nested_ns_enum(::A::B::ABEnum e) {
422 if (e == ::A::B::ABEnum::ABAVal) {
423 cxx_test_suite_set_correct();
424 }
425}
426
David Tolnayebef4a22020-03-17 15:33:47 -0700427void c_try_return_void() {}
428
429size_t c_try_return_primitive() { return 2020; }
430
431size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
432
David Tolnay99642622020-03-25 13:07:35 -0700433rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700434
David Tolnay99642622020-03-25 13:07:35 -0700435const rust::String &c_try_return_ref(const rust::String &s) { return s; }
436
437rust::Str c_try_return_str(rust::Str s) { return s; }
438
Adrian Taylorec9430e2020-04-14 16:09:58 -0700439rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700440
David Tolnay99642622020-03-25 13:07:35 -0700441rust::String c_try_return_rust_string() { return c_return_rust_string(); }
442
443std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
444 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700445}
446
David Tolnay8b9d1762020-04-25 16:05:46 -0700447rust::Vec<uint8_t> c_try_return_rust_vec() {
448 throw std::runtime_error("unimplemented");
449}
450
David Tolnay33f56ad2020-08-27 17:06:35 -0700451rust::Vec<rust::String> c_try_return_rust_vec_string() {
452 throw std::runtime_error("unimplemented");
453}
454
David Tolnay77989692020-04-25 15:57:47 -0700455const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
456 (void)c;
457 throw std::runtime_error("unimplemented");
458}
459
David Tolnay2fe58c62020-03-06 16:23:09 -0800460extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800461 return std::unique_ptr<C>(new C{2020}).release();
462}
463
David Tolnay85db24862020-03-06 16:24:41 -0800464extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
465 return std::unique_ptr<std::string>(new std::string("2020")).release();
466}
467
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700468rust::String C::cOverloadedMethod(int32_t x) const {
469 return rust::String(std::to_string(x));
470}
471
472rust::String C::cOverloadedMethod(rust::Str x) const {
473 return rust::String(std::string(x));
474}
475
476rust::String cOverloadedFunction(int x) {
477 return rust::String(std::to_string(x));
478}
479
480rust::String cOverloadedFunction(rust::Str x) {
481 return rust::String(std::string(x));
482}
483
Adrian Taylor121cca42020-10-10 15:32:00 -0700484void c_take_trivial_ptr(std::unique_ptr<D> d) {
485 if (d->d == 30) {
486 cxx_test_suite_set_correct();
487 }
488}
489
David Tolnaybf23e3e2020-10-30 21:09:16 -0700490void c_take_trivial_ref(const D &d) {
Adrian Taylor121cca42020-10-10 15:32:00 -0700491 if (d.d == 30) {
492 cxx_test_suite_set_correct();
493 }
494}
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700495
Adrian Taylor121cca42020-10-10 15:32:00 -0700496void c_take_trivial(D d) {
497 if (d.d == 30) {
498 cxx_test_suite_set_correct();
499 }
500}
501
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700502void c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g) {
503 if (g->g == 30) {
504 cxx_test_suite_set_correct();
505 }
506}
507
David Tolnaybf23e3e2020-10-30 21:09:16 -0700508void c_take_trivial_ns_ref(const ::G::G &g) {
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700509 if (g.g == 30) {
510 cxx_test_suite_set_correct();
511 }
512}
513
514void c_take_trivial_ns(::G::G g) {
515 if (g.g == 30) {
516 cxx_test_suite_set_correct();
517 }
518}
519
Adrian Taylor121cca42020-10-10 15:32:00 -0700520void c_take_opaque_ptr(std::unique_ptr<E> e) {
521 if (e->e == 40) {
522 cxx_test_suite_set_correct();
523 }
524}
525
Adrian Taylor5e79c642020-10-24 21:09:42 -0700526void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f) {
527 if (f->f == 40) {
528 cxx_test_suite_set_correct();
529 }
530}
531
David Tolnaybf23e3e2020-10-30 21:09:16 -0700532void c_take_opaque_ref(const E &e) {
Adrian Taylor121cca42020-10-10 15:32:00 -0700533 if (e.e == 40 && e.e_str == "hello") {
534 cxx_test_suite_set_correct();
535 }
536}
537
David Tolnaybf23e3e2020-10-30 21:09:16 -0700538void c_take_opaque_ns_ref(const ::F::F &f) {
Adrian Taylor5e79c642020-10-24 21:09:42 -0700539 if (f.f == 40 && f.f_str == "hello") {
540 cxx_test_suite_set_correct();
541 }
542}
543
Adrian Taylor121cca42020-10-10 15:32:00 -0700544std::unique_ptr<D> c_return_trivial_ptr() {
545 auto d = std::unique_ptr<D>(new D());
546 d->d = 30;
547 return d;
548}
549
550D c_return_trivial() {
551 D d;
552 d.d = 30;
553 return d;
554}
555
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700556std::unique_ptr<::G::G> c_return_trivial_ns_ptr() {
557 auto g = std::unique_ptr<::G::G>(new ::G::G());
558 g->g = 30;
559 return g;
560}
561
562::G::G c_return_trivial_ns() {
563 ::G::G g;
564 g.g = 30;
565 return g;
566}
567
Adrian Taylor121cca42020-10-10 15:32:00 -0700568std::unique_ptr<E> c_return_opaque_ptr() {
569 auto e = std::unique_ptr<E>(new E());
570 e->e = 40;
571 e->e_str = std::string("hello");
572 return e;
573}
574
Adrian Taylor5e79c642020-10-24 21:09:42 -0700575std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
576 auto f = std::unique_ptr<::F::F>(new ::F::F());
577 f->f = 40;
578 f->f_str = std::string("hello");
579 return f;
580}
581
David Tolnayf306da42020-02-22 19:55:43 -0800582extern "C" const char *cxx_run_test() noexcept {
583#define STRINGIFY(x) #x
584#define TOSTRING(x) STRINGIFY(x)
585#define ASSERT(x) \
586 do { \
587 if (!(x)) { \
588 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
589 } \
590 } while (false)
591
592 ASSERT(r_return_primitive() == 2020);
593 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800594 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800595 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800596 ASSERT(r_return_ref(Shared{2020}) == 2020);
597 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
598 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800599 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700600 ASSERT(r_return_identity(2020) == 2020);
601 ASSERT(r_return_sum(2020, 1) == 2021);
Joel Galensonc03402a2020-04-23 17:31:09 -0700602 ASSERT(r_return_enum(0) == Enum::AVal);
603 ASSERT(r_return_enum(1) == Enum::BVal);
604 ASSERT(r_return_enum(2021) == Enum::CVal);
David Tolnayf306da42020-02-22 19:55:43 -0800605
606 r_take_primitive(2020);
607 r_take_shared(Shared{2020});
608 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
609 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800610 r_take_str(rust::Str("2020"));
David Tolnay4037de22020-04-30 19:51:04 -0700611 r_take_sliceu8(rust::Slice<uint8_t>(
612 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
David Tolnay40226ab2020-03-03 00:05:35 -0800613 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800614 r_take_unique_ptr_string(
615 std::unique_ptr<std::string>(new std::string("2020")));
David Tolnay5df0a712020-09-24 15:58:28 -0400616 r_take_ref_vector(std::vector<uint8_t>{20, 2, 0});
David Tolnay80631e92020-09-24 16:07:30 -0400617 std::vector<uint64_t> empty_vector;
618 r_take_ref_empty_vector(empty_vector);
619 empty_vector.reserve(10);
620 r_take_ref_empty_vector(empty_vector);
Joel Galensonc03402a2020-04-23 17:31:09 -0700621 r_take_enum(Enum::AVal);
David Tolnayf306da42020-02-22 19:55:43 -0800622
David Tolnayb6c5ea72020-03-16 13:36:28 -0700623 ASSERT(r_try_return_primitive() == 2020);
624 try {
625 r_fail_return_primitive();
626 ASSERT(false);
627 } catch (const rust::Error &e) {
628 ASSERT(std::strcmp(e.what(), "rust error") == 0);
629 }
630
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700631 auto r2 = r_return_r2(2020);
632 ASSERT(r2->get() == 2020);
633 ASSERT(r2->set(2021) == 2021);
634 ASSERT(r2->get() == 2021);
635 ASSERT(r2->set(2020) == 2020);
636 ASSERT(r2->get() == 2020);
David Tolnay74608d72020-11-10 08:49:20 -0800637 ASSERT(std::string(Shared{0}.r_method_on_shared()) == "2020");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700638
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700639 ASSERT(std::string(rAliasedFunction(2020)) == "2020");
640
David Tolnayf306da42020-02-22 19:55:43 -0800641 cxx_test_suite_set_correct();
642 return nullptr;
643}
644
David Tolnay97c72102020-01-25 16:49:00 -0800645} // namespace tests
Adrian Taylorddc146e2020-10-25 21:40:17 -0700646
647namespace other {
David Tolnaybf23e3e2020-10-30 21:09:16 -0700648void ns_c_take_trivial(::tests::D d) {
649 if (d.d == 30) {
650 cxx_test_suite_set_correct();
Adrian Taylorddc146e2020-10-25 21:40:17 -0700651 }
David Tolnaybf23e3e2020-10-30 21:09:16 -0700652}
Adrian Taylorddc146e2020-10-25 21:40:17 -0700653
David Tolnaybf23e3e2020-10-30 21:09:16 -0700654::tests::D ns_c_return_trivial() {
655 ::tests::D d;
656 d.d = 30;
657 return d;
658}
Adrian Taylorddc146e2020-10-25 21:40:17 -0700659
David Tolnaybf23e3e2020-10-30 21:09:16 -0700660void ns_c_take_ns_shared(::A::AShared shared) {
661 if (shared.z == 2020) {
662 cxx_test_suite_set_correct();
Adrian Taylorddc146e2020-10-25 21:40:17 -0700663 }
David Tolnaybf23e3e2020-10-30 21:09:16 -0700664}
Adrian Taylor0fac3212020-10-25 21:52:55 -0700665} // namespace other
666
667namespace I {
David Tolnaybf23e3e2020-10-30 21:09:16 -0700668uint32_t I::get() const { return a; }
Adrian Taylor0fac3212020-10-25 21:52:55 -0700669
David Tolnaybf23e3e2020-10-30 21:09:16 -0700670std::unique_ptr<I> ns_c_return_unique_ptr_ns() {
671 return std::unique_ptr<I>(new I());
672}
Adrian Taylor0fac3212020-10-25 21:52:55 -0700673} // namespace I