blob: 1f0f8ff15761263b31e2cf45bcce0a34c3f18a35 [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 Tolnay37dd7e12020-04-25 12:51:59 -07005#include <numeric>
David Tolnayebef4a22020-03-17 15:33:47 -07006#include <stdexcept>
Adrian Taylor121cca42020-10-10 15:32:00 -07007#include <memory>
8#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
66const size_t &c_return_nested_ns_ref(const ::A::B::ABShared &shared) { return shared.z; }
67
David Tolnay18d93b62020-08-27 00:55:48 -070068size_t &c_return_mut(Shared &shared) { return shared.z; }
69
David Tolnay750755e2020-03-01 13:04:08 -080070rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080071 (void)shared;
72 return "2020";
73}
74
David Tolnayeb952ba2020-04-14 15:02:24 -070075rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070076 (void)shared;
David Tolnay4037de22020-04-30 19:51:04 -070077 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA),
78 sizeof(SLICE_DATA));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070079}
80
David Tolnay750755e2020-03-01 13:04:08 -080081rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080082
83std::unique_ptr<std::string> c_return_unique_ptr_string() {
84 return std::unique_ptr<std::string>(new std::string("2020"));
85}
86
Myron Ahneba35cf2020-02-05 19:41:51 +070087std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
David Tolnay85db5a02020-04-25 13:17:27 -070088 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
89 vec->push_back(86);
90 vec->push_back(75);
91 vec->push_back(30);
92 vec->push_back(9);
93 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070094}
95
96std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
David Tolnay85db5a02020-04-25 13:17:27 -070097 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
98 vec->push_back(86.0);
99 vec->push_back(75.0);
100 vec->push_back(30.0);
101 vec->push_back(9.5);
102 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +0700103}
104
David Tolnay47e239d2020-08-28 00:32:04 -0700105std::unique_ptr<std::vector<std::string>> c_return_unique_ptr_vector_string() {
106 return std::unique_ptr<std::vector<std::string>>(
107 new std::vector<std::string>());
108}
109
Myron Ahneba35cf2020-02-05 19:41:51 +0700110std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
David Tolnay85db5a02020-04-25 13:17:27 -0700111 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
112 vec->push_back(Shared{1010});
113 vec->push_back(Shared{1011});
114 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +0700115}
116
David Tolnay1bcc9fe2020-04-25 13:51:07 -0700117std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
118 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
119}
120
David Tolnayde5340e2020-04-25 14:03:21 -0700121const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
122 return c.get_v();
123}
124
David Tolnay18d93b62020-08-27 00:55:48 -0700125std::vector<uint8_t> &c_return_mut_vector(C &c) { return c.get_v(); }
126
David Tolnayb41e74c2020-04-25 15:06:18 -0700127rust::Vec<uint8_t> c_return_rust_vec() {
128 throw std::runtime_error("unimplemented");
129}
130
David Tolnay77989692020-04-25 15:57:47 -0700131const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
132 (void)c;
133 throw std::runtime_error("unimplemented");
134}
135
David Tolnay18d93b62020-08-27 00:55:48 -0700136rust::Vec<uint8_t> &c_return_mut_rust_vec(C &c) {
137 (void)c;
138 throw std::runtime_error("unimplemented");
139}
140
David Tolnay33f56ad2020-08-27 17:06:35 -0700141rust::Vec<rust::String> c_return_rust_vec_string() {
142 throw std::runtime_error("unimplemented");
143}
144
David Tolnay378ca502020-04-27 16:47:55 -0700145size_t c_return_identity(size_t n) { return n; }
Joel Galensonba676072020-04-27 15:55:45 -0700146
David Tolnay378ca502020-04-27 16:47:55 -0700147size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
Joel Galensonba676072020-04-27 15:55:45 -0700148
David Tolnayf6a89f22020-05-10 23:39:27 -0700149Enum c_return_enum(uint16_t n) {
150 if (n <= static_cast<uint16_t>(Enum::AVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700151 return Enum::AVal;
David Tolnayf6a89f22020-05-10 23:39:27 -0700152 } else if (n <= static_cast<uint16_t>(Enum::BVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700153 return Enum::BVal;
154 } else {
155 return Enum::CVal;
156 }
157}
158
Adrian Taylor9a158e42020-10-24 20:51:25 -0700159::A::AEnum c_return_ns_enum(uint16_t n) {
160 if (n <= static_cast<uint16_t>(::A::AEnum::AAVal)) {
161 return ::A::AEnum::AAVal;
162 } else if (n <= static_cast<uint16_t>(::A::AEnum::ABVal)) {
163 return ::A::AEnum::ABVal;
164 } else {
165 return ::A::AEnum::ACVal;
166 }
167}
168
169::A::B::ABEnum c_return_nested_ns_enum(uint16_t n) {
170 if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABAVal)) {
171 return ::A::B::ABEnum::ABAVal;
172 } else if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABBVal)) {
173 return ::A::B::ABEnum::ABBVal;
174 } else {
175 return ::A::B::ABEnum::ABCVal;
176 }
177}
178
David Tolnay3fd7f562020-01-26 17:47:11 -0800179void c_take_primitive(size_t n) {
180 if (n == 2020) {
181 cxx_test_suite_set_correct();
182 }
183}
David Tolnayad5b8af2020-01-26 16:59:13 -0800184
David Tolnay3fd7f562020-01-26 17:47:11 -0800185void c_take_shared(Shared shared) {
186 if (shared.z == 2020) {
187 cxx_test_suite_set_correct();
188 }
189}
David Tolnayad5b8af2020-01-26 16:59:13 -0800190
Adrian Taylor9a158e42020-10-24 20:51:25 -0700191void c_take_ns_shared(::A::AShared shared) {
192 if (shared.z == 2020) {
193 cxx_test_suite_set_correct();
194 }
195}
196
197void c_take_nested_ns_shared(::A::B::ABShared shared) {
198 if (shared.z == 2020) {
199 cxx_test_suite_set_correct();
200 }
201}
202
David Tolnay750755e2020-03-01 13:04:08 -0800203void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -0800204 if (cxx_test_suite_r_is_correct(&*r)) {
205 cxx_test_suite_set_correct();
206 }
David Tolnay3fd7f562020-01-26 17:47:11 -0800207}
David Tolnayad5b8af2020-01-26 16:59:13 -0800208
David Tolnay3fd7f562020-01-26 17:47:11 -0800209void c_take_unique_ptr(std::unique_ptr<C> c) {
210 if (c->get() == 2020) {
211 cxx_test_suite_set_correct();
212 }
213}
David Tolnayad5b8af2020-01-26 16:59:13 -0800214
David Tolnaya7d00e82020-03-06 15:50:14 -0800215void c_take_ref_r(const R &r) {
216 if (cxx_test_suite_r_is_correct(&r)) {
217 cxx_test_suite_set_correct();
218 }
219}
David Tolnayad5b8af2020-01-26 16:59:13 -0800220
David Tolnay3fd7f562020-01-26 17:47:11 -0800221void c_take_ref_c(const C &c) {
222 if (c.get() == 2020) {
223 cxx_test_suite_set_correct();
224 }
225}
David Tolnayad5b8af2020-01-26 16:59:13 -0800226
Adrian Taylord47af7a2020-10-26 13:18:48 -0700227void c_take_ref_ns_c(const ::H::H &h) {
228 if (h.h == "hello") {
229 cxx_test_suite_set_correct();
230 }
231}
232
David Tolnay750755e2020-03-01 13:04:08 -0800233void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800234 if (std::string(s) == "2020") {
235 cxx_test_suite_set_correct();
236 }
237}
David Tolnayad5b8af2020-01-26 16:59:13 -0800238
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700239void c_take_sliceu8(rust::Slice<uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700240 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
241 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700242 cxx_test_suite_set_correct();
243 }
244}
245
David Tolnay750755e2020-03-01 13:04:08 -0800246void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800247 if (std::string(s) == "2020") {
248 cxx_test_suite_set_correct();
249 }
250}
David Tolnayad5b8af2020-01-26 16:59:13 -0800251
David Tolnay3fd7f562020-01-26 17:47:11 -0800252void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
253 if (*s == "2020") {
254 cxx_test_suite_set_correct();
255 }
256}
David Tolnayad5b8af2020-01-26 16:59:13 -0800257
Myron Ahneba35cf2020-02-05 19:41:51 +0700258void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
259 if (v->size() == 4) {
260 cxx_test_suite_set_correct();
261 }
262}
263
264void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
265 if (v->size() == 4) {
266 cxx_test_suite_set_correct();
267 }
268}
269
David Tolnay47e239d2020-08-28 00:32:04 -0700270void c_take_unique_ptr_vector_string(
271 std::unique_ptr<std::vector<std::string>> v) {
272 (void)v;
273 cxx_test_suite_set_correct();
274}
275
Myron Ahneba35cf2020-02-05 19:41:51 +0700276void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
277 if (v->size() == 2) {
278 cxx_test_suite_set_correct();
279 }
280}
281
David Tolnay2244d1f2020-04-25 13:58:18 -0700282void c_take_ref_vector(const std::vector<uint8_t> &v) {
283 if (v.size() == 4) {
284 cxx_test_suite_set_correct();
285 }
286}
287
David Tolnayd2ce8a92020-04-25 16:16:45 -0700288void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
Myron Ahneba35cf2020-02-05 19:41:51 +0700289
David Tolnay61adf422020-08-26 20:51:27 -0700290void c_take_rust_vec_index(rust::Vec<uint8_t> v) {
291 try {
292 v.at(100);
293 } catch (const std::out_of_range &ex) {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700294 std::string expected = "rust::Vec index out of range";
David Tolnay61adf422020-08-26 20:51:27 -0700295 if (ex.what() == expected) {
296 cxx_test_suite_set_correct();
297 }
298 }
299}
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700300
David Tolnayd2ce8a92020-04-25 16:16:45 -0700301void c_take_rust_vec_shared(rust::Vec<Shared> v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700302 uint32_t sum = 0;
David Tolnayc87c2152020-04-24 17:07:41 -0700303 for (auto i : v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700304 sum += i.z;
305 }
306 if (sum == 2021) {
307 cxx_test_suite_set_correct();
308 }
309}
310
Adrian Taylor9a158e42020-10-24 20:51:25 -0700311void c_take_rust_vec_ns_shared(rust::Vec<::A::AShared> v) {
312 uint32_t sum = 0;
313 for (auto i : v) {
314 sum += i.z;
315 }
316 if (sum == 2021) {
317 cxx_test_suite_set_correct();
318 }
319}
320
321void c_take_rust_vec_nested_ns_shared(rust::Vec<::A::B::ABShared> v) {
322 uint32_t sum = 0;
323 for (auto i : v) {
324 sum += i.z;
325 }
326 if (sum == 2021) {
327 cxx_test_suite_set_correct();
328 }
329}
330
David Tolnay33f56ad2020-08-27 17:06:35 -0700331void c_take_rust_vec_string(rust::Vec<rust::String> v) {
332 (void)v;
333 cxx_test_suite_set_correct();
334}
335
myronahnda9be502020-04-29 05:47:23 +0700336void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
337 // Exercise requirements of ForwardIterator
338 // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
339 uint32_t sum = 0;
340 for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
341 sum += it->z;
342 }
343 if (sum == 2021) {
344 cxx_test_suite_set_correct();
345 }
346}
347
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700348void c_take_rust_vec_shared_index(rust::Vec<Shared> v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700349 if (v[0].z == 1010 && v.at(0).z == 1010 && v.front().z == 1010 &&
350 v[1].z == 1011 && v.at(1).z == 1011 && v.back().z == 1011) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700351 cxx_test_suite_set_correct();
352 }
353}
354
David Tolnayd2ce8a92020-04-25 16:16:45 -0700355void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
356 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
357 if (sum == 200) {
358 cxx_test_suite_set_correct();
359 }
360}
361
David Tolnay33f56ad2020-08-27 17:06:35 -0700362void c_take_ref_rust_vec_string(const rust::Vec<rust::String> &v) {
363 (void)v;
364 cxx_test_suite_set_correct();
365}
366
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700367void c_take_ref_rust_vec_index(const rust::Vec<uint8_t> &v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700368 if (v[0] == 86 && v.at(0) == 86 && v.front() == 86 && v[1] == 75 &&
369 v.at(1) == 75 && v[3] == 9 && v.at(3) == 9 && v.back() == 9) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700370 cxx_test_suite_set_correct();
371 }
372}
373
myronahnda9be502020-04-29 05:47:23 +0700374void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
375 // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
376 // requirements for std::iterator_traits.
377 // https://en.cppreference.com/w/cpp/iterator/iterator_traits
David Tolnayf5aeea22020-04-30 19:34:51 -0700378 std::vector<uint8_t> stdv;
379 std::copy(v.begin(), v.end(), std::back_inserter(stdv));
380 uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
myronahnda9be502020-04-29 05:47:23 +0700381 if (sum == 200) {
382 cxx_test_suite_set_correct();
383 }
384}
385
David Tolnay0c8c0f22020-07-21 17:57:46 -0700386/*
387// https://github.com/dtolnay/cxx/issues/232
David Tolnay75dca2e2020-03-25 20:17:52 -0700388void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
389 callback("2020");
390}
David Tolnay0c8c0f22020-07-21 17:57:46 -0700391*/
David Tolnay75dca2e2020-03-25 20:17:52 -0700392
Joel Galensonc03402a2020-04-23 17:31:09 -0700393void c_take_enum(Enum e) {
394 if (e == Enum::AVal) {
395 cxx_test_suite_set_correct();
396 }
397}
398
Adrian Taylor9a158e42020-10-24 20:51:25 -0700399void c_take_ns_enum(::A::AEnum e) {
400 if (e == ::A::AEnum::AAVal) {
401 cxx_test_suite_set_correct();
402 }
403}
404
405void c_take_nested_ns_enum(::A::B::ABEnum e) {
406 if (e == ::A::B::ABEnum::ABAVal) {
407 cxx_test_suite_set_correct();
408 }
409}
410
David Tolnayebef4a22020-03-17 15:33:47 -0700411void c_try_return_void() {}
412
413size_t c_try_return_primitive() { return 2020; }
414
415size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
416
David Tolnay99642622020-03-25 13:07:35 -0700417rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700418
David Tolnay99642622020-03-25 13:07:35 -0700419const rust::String &c_try_return_ref(const rust::String &s) { return s; }
420
421rust::Str c_try_return_str(rust::Str s) { return s; }
422
Adrian Taylorec9430e2020-04-14 16:09:58 -0700423rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700424
David Tolnay99642622020-03-25 13:07:35 -0700425rust::String c_try_return_rust_string() { return c_return_rust_string(); }
426
427std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
428 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700429}
430
David Tolnay8b9d1762020-04-25 16:05:46 -0700431rust::Vec<uint8_t> c_try_return_rust_vec() {
432 throw std::runtime_error("unimplemented");
433}
434
David Tolnay33f56ad2020-08-27 17:06:35 -0700435rust::Vec<rust::String> c_try_return_rust_vec_string() {
436 throw std::runtime_error("unimplemented");
437}
438
David Tolnay77989692020-04-25 15:57:47 -0700439const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
440 (void)c;
441 throw std::runtime_error("unimplemented");
442}
443
David Tolnay2fe58c62020-03-06 16:23:09 -0800444extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800445 return std::unique_ptr<C>(new C{2020}).release();
446}
447
David Tolnay85db24862020-03-06 16:24:41 -0800448extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
449 return std::unique_ptr<std::string>(new std::string("2020")).release();
450}
451
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700452rust::String C::cOverloadedMethod(int32_t x) const {
453 return rust::String(std::to_string(x));
454}
455
456rust::String C::cOverloadedMethod(rust::Str x) const {
457 return rust::String(std::string(x));
458}
459
460rust::String cOverloadedFunction(int x) {
461 return rust::String(std::to_string(x));
462}
463
464rust::String cOverloadedFunction(rust::Str x) {
465 return rust::String(std::string(x));
466}
467
Adrian Taylor121cca42020-10-10 15:32:00 -0700468void c_take_trivial_ptr(std::unique_ptr<D> d) {
469 if (d->d == 30) {
470 cxx_test_suite_set_correct();
471 }
472}
473
474void c_take_trivial_ref(const D& d) {
475 if (d.d == 30) {
476 cxx_test_suite_set_correct();
477 }
478}
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700479
Adrian Taylor121cca42020-10-10 15:32:00 -0700480void c_take_trivial(D d) {
481 if (d.d == 30) {
482 cxx_test_suite_set_correct();
483 }
484}
485
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700486
487void c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g) {
488 if (g->g == 30) {
489 cxx_test_suite_set_correct();
490 }
491}
492
493void c_take_trivial_ns_ref(const ::G::G& g) {
494 if (g.g == 30) {
495 cxx_test_suite_set_correct();
496 }
497}
498
499void c_take_trivial_ns(::G::G g) {
500 if (g.g == 30) {
501 cxx_test_suite_set_correct();
502 }
503}
504
Adrian Taylor121cca42020-10-10 15:32:00 -0700505void c_take_opaque_ptr(std::unique_ptr<E> e) {
506 if (e->e == 40) {
507 cxx_test_suite_set_correct();
508 }
509}
510
Adrian Taylor5e79c642020-10-24 21:09:42 -0700511void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f) {
512 if (f->f == 40) {
513 cxx_test_suite_set_correct();
514 }
515}
516
Adrian Taylor121cca42020-10-10 15:32:00 -0700517void c_take_opaque_ref(const E& e) {
518 if (e.e == 40 && e.e_str == "hello") {
519 cxx_test_suite_set_correct();
520 }
521}
522
Adrian Taylor5e79c642020-10-24 21:09:42 -0700523void c_take_opaque_ns_ref(const ::F::F& f) {
524 if (f.f == 40 && f.f_str == "hello") {
525 cxx_test_suite_set_correct();
526 }
527}
528
Adrian Taylor121cca42020-10-10 15:32:00 -0700529std::unique_ptr<D> c_return_trivial_ptr() {
530 auto d = std::unique_ptr<D>(new D());
531 d->d = 30;
532 return d;
533}
534
535D c_return_trivial() {
536 D d;
537 d.d = 30;
538 return d;
539}
540
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700541std::unique_ptr<::G::G> c_return_trivial_ns_ptr() {
542 auto g = std::unique_ptr<::G::G>(new ::G::G());
543 g->g = 30;
544 return g;
545}
546
547::G::G c_return_trivial_ns() {
548 ::G::G g;
549 g.g = 30;
550 return g;
551}
552
Adrian Taylor121cca42020-10-10 15:32:00 -0700553std::unique_ptr<E> c_return_opaque_ptr() {
554 auto e = std::unique_ptr<E>(new E());
555 e->e = 40;
556 e->e_str = std::string("hello");
557 return e;
558}
559
Adrian Taylor5e79c642020-10-24 21:09:42 -0700560std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
561 auto f = std::unique_ptr<::F::F>(new ::F::F());
562 f->f = 40;
563 f->f_str = std::string("hello");
564 return f;
565}
566
David Tolnayf306da42020-02-22 19:55:43 -0800567extern "C" const char *cxx_run_test() noexcept {
568#define STRINGIFY(x) #x
569#define TOSTRING(x) STRINGIFY(x)
570#define ASSERT(x) \
571 do { \
572 if (!(x)) { \
573 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
574 } \
575 } while (false)
576
577 ASSERT(r_return_primitive() == 2020);
578 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800579 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800580 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800581 ASSERT(r_return_ref(Shared{2020}) == 2020);
582 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
583 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800584 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700585 ASSERT(r_return_identity(2020) == 2020);
586 ASSERT(r_return_sum(2020, 1) == 2021);
Joel Galensonc03402a2020-04-23 17:31:09 -0700587 ASSERT(r_return_enum(0) == Enum::AVal);
588 ASSERT(r_return_enum(1) == Enum::BVal);
589 ASSERT(r_return_enum(2021) == Enum::CVal);
David Tolnayf306da42020-02-22 19:55:43 -0800590
591 r_take_primitive(2020);
592 r_take_shared(Shared{2020});
593 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
594 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800595 r_take_str(rust::Str("2020"));
David Tolnay4037de22020-04-30 19:51:04 -0700596 r_take_sliceu8(rust::Slice<uint8_t>(
597 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
David Tolnay40226ab2020-03-03 00:05:35 -0800598 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800599 r_take_unique_ptr_string(
600 std::unique_ptr<std::string>(new std::string("2020")));
David Tolnay5df0a712020-09-24 15:58:28 -0400601 r_take_ref_vector(std::vector<uint8_t>{20, 2, 0});
David Tolnay80631e92020-09-24 16:07:30 -0400602 std::vector<uint64_t> empty_vector;
603 r_take_ref_empty_vector(empty_vector);
604 empty_vector.reserve(10);
605 r_take_ref_empty_vector(empty_vector);
Joel Galensonc03402a2020-04-23 17:31:09 -0700606 r_take_enum(Enum::AVal);
David Tolnayf306da42020-02-22 19:55:43 -0800607
David Tolnayb6c5ea72020-03-16 13:36:28 -0700608 ASSERT(r_try_return_primitive() == 2020);
609 try {
610 r_fail_return_primitive();
611 ASSERT(false);
612 } catch (const rust::Error &e) {
613 ASSERT(std::strcmp(e.what(), "rust error") == 0);
614 }
615
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700616 auto r2 = r_return_r2(2020);
617 ASSERT(r2->get() == 2020);
618 ASSERT(r2->set(2021) == 2021);
619 ASSERT(r2->get() == 2021);
620 ASSERT(r2->set(2020) == 2020);
621 ASSERT(r2->get() == 2020);
622
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700623 ASSERT(std::string(rAliasedFunction(2020)) == "2020");
624
David Tolnayf306da42020-02-22 19:55:43 -0800625 cxx_test_suite_set_correct();
626 return nullptr;
627}
628
David Tolnay97c72102020-01-25 16:49:00 -0800629} // namespace tests
Adrian Taylorddc146e2020-10-25 21:40:17 -0700630
631namespace other {
632
633 void ns_c_take_trivial(::tests::D d) {
634 if (d.d == 30) {
635 cxx_test_suite_set_correct();
636 }
637 }
638
639 ::tests::D ns_c_return_trivial() {
640 ::tests::D d;
641 d.d = 30;
642 return d;
643 }
644
645 void ns_c_take_ns_shared(::A::AShared shared) {
646 if (shared.z == 2020) {
647 cxx_test_suite_set_correct();
648 }
649 }
650}