blob: 4abc456429ab442e40fcd669591ff0f23060ca96 [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 Tolnay0c8c0f22020-07-21 17:57:46 -0700388/*
389// https://github.com/dtolnay/cxx/issues/232
David Tolnay75dca2e2020-03-25 20:17:52 -0700390void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
391 callback("2020");
392}
David Tolnay0c8c0f22020-07-21 17:57:46 -0700393*/
David Tolnay75dca2e2020-03-25 20:17:52 -0700394
Joel Galensonc03402a2020-04-23 17:31:09 -0700395void c_take_enum(Enum e) {
396 if (e == Enum::AVal) {
397 cxx_test_suite_set_correct();
398 }
399}
400
Adrian Taylor9a158e42020-10-24 20:51:25 -0700401void c_take_ns_enum(::A::AEnum e) {
402 if (e == ::A::AEnum::AAVal) {
403 cxx_test_suite_set_correct();
404 }
405}
406
407void c_take_nested_ns_enum(::A::B::ABEnum e) {
408 if (e == ::A::B::ABEnum::ABAVal) {
409 cxx_test_suite_set_correct();
410 }
411}
412
David Tolnayebef4a22020-03-17 15:33:47 -0700413void c_try_return_void() {}
414
415size_t c_try_return_primitive() { return 2020; }
416
417size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
418
David Tolnay99642622020-03-25 13:07:35 -0700419rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700420
David Tolnay99642622020-03-25 13:07:35 -0700421const rust::String &c_try_return_ref(const rust::String &s) { return s; }
422
423rust::Str c_try_return_str(rust::Str s) { return s; }
424
Adrian Taylorec9430e2020-04-14 16:09:58 -0700425rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700426
David Tolnay99642622020-03-25 13:07:35 -0700427rust::String c_try_return_rust_string() { return c_return_rust_string(); }
428
429std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
430 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700431}
432
David Tolnay8b9d1762020-04-25 16:05:46 -0700433rust::Vec<uint8_t> c_try_return_rust_vec() {
434 throw std::runtime_error("unimplemented");
435}
436
David Tolnay33f56ad2020-08-27 17:06:35 -0700437rust::Vec<rust::String> c_try_return_rust_vec_string() {
438 throw std::runtime_error("unimplemented");
439}
440
David Tolnay77989692020-04-25 15:57:47 -0700441const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
442 (void)c;
443 throw std::runtime_error("unimplemented");
444}
445
David Tolnay2fe58c62020-03-06 16:23:09 -0800446extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800447 return std::unique_ptr<C>(new C{2020}).release();
448}
449
David Tolnay85db24862020-03-06 16:24:41 -0800450extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
451 return std::unique_ptr<std::string>(new std::string("2020")).release();
452}
453
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700454rust::String C::cOverloadedMethod(int32_t x) const {
455 return rust::String(std::to_string(x));
456}
457
458rust::String C::cOverloadedMethod(rust::Str x) const {
459 return rust::String(std::string(x));
460}
461
462rust::String cOverloadedFunction(int x) {
463 return rust::String(std::to_string(x));
464}
465
466rust::String cOverloadedFunction(rust::Str x) {
467 return rust::String(std::string(x));
468}
469
Adrian Taylor121cca42020-10-10 15:32:00 -0700470void c_take_trivial_ptr(std::unique_ptr<D> d) {
471 if (d->d == 30) {
472 cxx_test_suite_set_correct();
473 }
474}
475
David Tolnaybf23e3e2020-10-30 21:09:16 -0700476void c_take_trivial_ref(const D &d) {
Adrian Taylor121cca42020-10-10 15:32:00 -0700477 if (d.d == 30) {
478 cxx_test_suite_set_correct();
479 }
480}
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700481
Adrian Taylor121cca42020-10-10 15:32:00 -0700482void c_take_trivial(D d) {
483 if (d.d == 30) {
484 cxx_test_suite_set_correct();
485 }
486}
487
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700488void c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g) {
489 if (g->g == 30) {
490 cxx_test_suite_set_correct();
491 }
492}
493
David Tolnaybf23e3e2020-10-30 21:09:16 -0700494void c_take_trivial_ns_ref(const ::G::G &g) {
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700495 if (g.g == 30) {
496 cxx_test_suite_set_correct();
497 }
498}
499
500void c_take_trivial_ns(::G::G g) {
501 if (g.g == 30) {
502 cxx_test_suite_set_correct();
503 }
504}
505
Adrian Taylor121cca42020-10-10 15:32:00 -0700506void c_take_opaque_ptr(std::unique_ptr<E> e) {
507 if (e->e == 40) {
508 cxx_test_suite_set_correct();
509 }
510}
511
Adrian Taylor5e79c642020-10-24 21:09:42 -0700512void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f) {
513 if (f->f == 40) {
514 cxx_test_suite_set_correct();
515 }
516}
517
David Tolnaybf23e3e2020-10-30 21:09:16 -0700518void c_take_opaque_ref(const E &e) {
Adrian Taylor121cca42020-10-10 15:32:00 -0700519 if (e.e == 40 && e.e_str == "hello") {
520 cxx_test_suite_set_correct();
521 }
522}
523
David Tolnaybf23e3e2020-10-30 21:09:16 -0700524void c_take_opaque_ns_ref(const ::F::F &f) {
Adrian Taylor5e79c642020-10-24 21:09:42 -0700525 if (f.f == 40 && f.f_str == "hello") {
526 cxx_test_suite_set_correct();
527 }
528}
529
Adrian Taylor121cca42020-10-10 15:32:00 -0700530std::unique_ptr<D> c_return_trivial_ptr() {
531 auto d = std::unique_ptr<D>(new D());
532 d->d = 30;
533 return d;
534}
535
536D c_return_trivial() {
537 D d;
538 d.d = 30;
539 return d;
540}
541
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700542std::unique_ptr<::G::G> c_return_trivial_ns_ptr() {
543 auto g = std::unique_ptr<::G::G>(new ::G::G());
544 g->g = 30;
545 return g;
546}
547
548::G::G c_return_trivial_ns() {
549 ::G::G g;
550 g.g = 30;
551 return g;
552}
553
Adrian Taylor121cca42020-10-10 15:32:00 -0700554std::unique_ptr<E> c_return_opaque_ptr() {
555 auto e = std::unique_ptr<E>(new E());
556 e->e = 40;
557 e->e_str = std::string("hello");
558 return e;
559}
560
Adrian Taylor5e79c642020-10-24 21:09:42 -0700561std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
562 auto f = std::unique_ptr<::F::F>(new ::F::F());
563 f->f = 40;
564 f->f_str = std::string("hello");
565 return f;
566}
567
David Tolnayf306da42020-02-22 19:55:43 -0800568extern "C" const char *cxx_run_test() noexcept {
569#define STRINGIFY(x) #x
570#define TOSTRING(x) STRINGIFY(x)
571#define ASSERT(x) \
572 do { \
573 if (!(x)) { \
574 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
575 } \
576 } while (false)
577
578 ASSERT(r_return_primitive() == 2020);
579 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800580 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800581 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800582 ASSERT(r_return_ref(Shared{2020}) == 2020);
583 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
584 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800585 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700586 ASSERT(r_return_identity(2020) == 2020);
587 ASSERT(r_return_sum(2020, 1) == 2021);
Joel Galensonc03402a2020-04-23 17:31:09 -0700588 ASSERT(r_return_enum(0) == Enum::AVal);
589 ASSERT(r_return_enum(1) == Enum::BVal);
590 ASSERT(r_return_enum(2021) == Enum::CVal);
David Tolnayf306da42020-02-22 19:55:43 -0800591
592 r_take_primitive(2020);
593 r_take_shared(Shared{2020});
594 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
595 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800596 r_take_str(rust::Str("2020"));
David Tolnay4037de22020-04-30 19:51:04 -0700597 r_take_sliceu8(rust::Slice<uint8_t>(
598 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
David Tolnay40226ab2020-03-03 00:05:35 -0800599 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800600 r_take_unique_ptr_string(
601 std::unique_ptr<std::string>(new std::string("2020")));
David Tolnay5df0a712020-09-24 15:58:28 -0400602 r_take_ref_vector(std::vector<uint8_t>{20, 2, 0});
David Tolnay80631e92020-09-24 16:07:30 -0400603 std::vector<uint64_t> empty_vector;
604 r_take_ref_empty_vector(empty_vector);
605 empty_vector.reserve(10);
606 r_take_ref_empty_vector(empty_vector);
Joel Galensonc03402a2020-04-23 17:31:09 -0700607 r_take_enum(Enum::AVal);
David Tolnayf306da42020-02-22 19:55:43 -0800608
David Tolnayb6c5ea72020-03-16 13:36:28 -0700609 ASSERT(r_try_return_primitive() == 2020);
610 try {
611 r_fail_return_primitive();
612 ASSERT(false);
613 } catch (const rust::Error &e) {
614 ASSERT(std::strcmp(e.what(), "rust error") == 0);
615 }
616
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700617 auto r2 = r_return_r2(2020);
618 ASSERT(r2->get() == 2020);
619 ASSERT(r2->set(2021) == 2021);
620 ASSERT(r2->get() == 2021);
621 ASSERT(r2->set(2020) == 2020);
622 ASSERT(r2->get() == 2020);
623
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700624 ASSERT(std::string(rAliasedFunction(2020)) == "2020");
625
David Tolnayf306da42020-02-22 19:55:43 -0800626 cxx_test_suite_set_correct();
627 return nullptr;
628}
629
David Tolnay97c72102020-01-25 16:49:00 -0800630} // namespace tests
Adrian Taylorddc146e2020-10-25 21:40:17 -0700631
632namespace other {
David Tolnaybf23e3e2020-10-30 21:09:16 -0700633void ns_c_take_trivial(::tests::D d) {
634 if (d.d == 30) {
635 cxx_test_suite_set_correct();
Adrian Taylorddc146e2020-10-25 21:40:17 -0700636 }
David Tolnaybf23e3e2020-10-30 21:09:16 -0700637}
Adrian Taylorddc146e2020-10-25 21:40:17 -0700638
David Tolnaybf23e3e2020-10-30 21:09:16 -0700639::tests::D ns_c_return_trivial() {
640 ::tests::D d;
641 d.d = 30;
642 return d;
643}
Adrian Taylorddc146e2020-10-25 21:40:17 -0700644
David Tolnaybf23e3e2020-10-30 21:09:16 -0700645void ns_c_take_ns_shared(::A::AShared shared) {
646 if (shared.z == 2020) {
647 cxx_test_suite_set_correct();
Adrian Taylorddc146e2020-10-25 21:40:17 -0700648 }
David Tolnaybf23e3e2020-10-30 21:09:16 -0700649}
Adrian Taylor0fac3212020-10-25 21:52:55 -0700650} // namespace other
651
652namespace I {
David Tolnaybf23e3e2020-10-30 21:09:16 -0700653uint32_t I::get() const { return a; }
Adrian Taylor0fac3212020-10-25 21:52:55 -0700654
David Tolnaybf23e3e2020-10-30 21:09:16 -0700655std::unique_ptr<I> ns_c_return_unique_ptr_ns() {
656 return std::unique_ptr<I>(new I());
657}
Adrian Taylor0fac3212020-10-25 21:52:55 -0700658} // namespace I