blob: 61ef701c90d4dc2ed4248d9d25fd3b720f842455 [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
David Tolnay1346ca32020-11-15 16:11:56 -080029size_t C::set_succeed(size_t n) { return this->set(n); }
myronahne3b78ea2020-05-23 01:08:13 +070030
31size_t C::get_fail() { throw std::runtime_error("unimplemented"); }
32
David Tolnay464aeeb2020-11-08 19:12:10 -080033size_t Shared::c_method_on_shared() const noexcept { return 2021; }
David Tolnay102c7ea2020-11-08 18:58:09 -080034
David Tolnayde5340e2020-04-25 14:03:21 -070035const std::vector<uint8_t> &C::get_v() const { return this->v; }
36
David Tolnay18d93b62020-08-27 00:55:48 -070037std::vector<uint8_t> &C::get_v() { return this->v; }
38
David Tolnayb8715772020-01-28 00:54:05 -080039size_t c_return_primitive() { return 2020; }
David Tolnayad5b8af2020-01-26 16:59:13 -080040
David Tolnayb8715772020-01-28 00:54:05 -080041Shared c_return_shared() { return Shared{2020}; }
David Tolnayad5b8af2020-01-26 16:59:13 -080042
Adrian Taylor9a158e42020-10-24 20:51:25 -070043::A::AShared c_return_ns_shared() { return ::A::AShared{2020}; }
44
45::A::B::ABShared c_return_nested_ns_shared() { return ::A::B::ABShared{2020}; }
46
David Tolnaybe13d8a2020-03-06 15:45:39 -080047rust::Box<R> c_return_box() {
48 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
49}
50
David Tolnayad5b8af2020-01-26 16:59:13 -080051std::unique_ptr<C> c_return_unique_ptr() {
52 return std::unique_ptr<C>(new C{2020});
53}
54
Adrian Taylord47af7a2020-10-26 13:18:48 -070055std::unique_ptr<::H::H> c_return_ns_unique_ptr() {
56 return std::unique_ptr<::H::H>(new ::H::H{"hello"});
57}
58
David Tolnayb8715772020-01-28 00:54:05 -080059const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080060
Adrian Taylor9a158e42020-10-24 20:51:25 -070061const size_t &c_return_ns_ref(const ::A::AShared &shared) { return shared.z; }
62
David Tolnaybf23e3e2020-10-30 21:09:16 -070063const size_t &c_return_nested_ns_ref(const ::A::B::ABShared &shared) {
64 return shared.z;
65}
Adrian Taylor9a158e42020-10-24 20:51:25 -070066
David Tolnay18d93b62020-08-27 00:55:48 -070067size_t &c_return_mut(Shared &shared) { return shared.z; }
68
David Tolnay750755e2020-03-01 13:04:08 -080069rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080070 (void)shared;
71 return "2020";
72}
73
David Tolnayce298232020-11-11 10:08:54 -080074rust::Slice<const uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070075 (void)shared;
David Tolnayce298232020-11-11 10:08:54 -080076 return rust::Slice<const uint8_t>(
77 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070078}
79
David Tolnay750755e2020-03-01 13:04:08 -080080rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080081
82std::unique_ptr<std::string> c_return_unique_ptr_string() {
83 return std::unique_ptr<std::string>(new std::string("2020"));
84}
85
Myron Ahneba35cf2020-02-05 19:41:51 +070086std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
David Tolnay85db5a02020-04-25 13:17:27 -070087 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
88 vec->push_back(86);
89 vec->push_back(75);
90 vec->push_back(30);
91 vec->push_back(9);
92 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070093}
94
95std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
David Tolnay85db5a02020-04-25 13:17:27 -070096 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
97 vec->push_back(86.0);
98 vec->push_back(75.0);
99 vec->push_back(30.0);
100 vec->push_back(9.5);
101 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +0700102}
103
David Tolnay47e239d2020-08-28 00:32:04 -0700104std::unique_ptr<std::vector<std::string>> c_return_unique_ptr_vector_string() {
105 return std::unique_ptr<std::vector<std::string>>(
106 new std::vector<std::string>());
107}
108
Myron Ahneba35cf2020-02-05 19:41:51 +0700109std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
David Tolnay85db5a02020-04-25 13:17:27 -0700110 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
111 vec->push_back(Shared{1010});
112 vec->push_back(Shared{1011});
113 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +0700114}
115
David Tolnay1bcc9fe2020-04-25 13:51:07 -0700116std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
117 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
118}
119
David Tolnayde5340e2020-04-25 14:03:21 -0700120const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
121 return c.get_v();
122}
123
David Tolnay18d93b62020-08-27 00:55:48 -0700124std::vector<uint8_t> &c_return_mut_vector(C &c) { return c.get_v(); }
125
David Tolnayb41e74c2020-04-25 15:06:18 -0700126rust::Vec<uint8_t> c_return_rust_vec() {
127 throw std::runtime_error("unimplemented");
128}
129
David Tolnay77989692020-04-25 15:57:47 -0700130const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
131 (void)c;
132 throw std::runtime_error("unimplemented");
133}
134
David Tolnay18d93b62020-08-27 00:55:48 -0700135rust::Vec<uint8_t> &c_return_mut_rust_vec(C &c) {
136 (void)c;
137 throw std::runtime_error("unimplemented");
138}
139
David Tolnay33f56ad2020-08-27 17:06:35 -0700140rust::Vec<rust::String> c_return_rust_vec_string() {
141 throw std::runtime_error("unimplemented");
142}
143
David Tolnay378ca502020-04-27 16:47:55 -0700144size_t c_return_identity(size_t n) { return n; }
Joel Galensonba676072020-04-27 15:55:45 -0700145
David Tolnay378ca502020-04-27 16:47:55 -0700146size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
Joel Galensonba676072020-04-27 15:55:45 -0700147
David Tolnayf6a89f22020-05-10 23:39:27 -0700148Enum c_return_enum(uint16_t n) {
149 if (n <= static_cast<uint16_t>(Enum::AVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700150 return Enum::AVal;
David Tolnayf6a89f22020-05-10 23:39:27 -0700151 } else if (n <= static_cast<uint16_t>(Enum::BVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700152 return Enum::BVal;
153 } else {
154 return Enum::CVal;
155 }
156}
157
Adrian Taylor9a158e42020-10-24 20:51:25 -0700158::A::AEnum c_return_ns_enum(uint16_t n) {
159 if (n <= static_cast<uint16_t>(::A::AEnum::AAVal)) {
160 return ::A::AEnum::AAVal;
161 } else if (n <= static_cast<uint16_t>(::A::AEnum::ABVal)) {
162 return ::A::AEnum::ABVal;
163 } else {
164 return ::A::AEnum::ACVal;
165 }
166}
167
168::A::B::ABEnum c_return_nested_ns_enum(uint16_t n) {
169 if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABAVal)) {
170 return ::A::B::ABEnum::ABAVal;
171 } else if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABBVal)) {
172 return ::A::B::ABEnum::ABBVal;
173 } else {
174 return ::A::B::ABEnum::ABCVal;
175 }
176}
177
David Tolnay3fd7f562020-01-26 17:47:11 -0800178void c_take_primitive(size_t n) {
179 if (n == 2020) {
180 cxx_test_suite_set_correct();
181 }
182}
David Tolnayad5b8af2020-01-26 16:59:13 -0800183
David Tolnay3fd7f562020-01-26 17:47:11 -0800184void c_take_shared(Shared shared) {
185 if (shared.z == 2020) {
186 cxx_test_suite_set_correct();
187 }
188}
David Tolnayad5b8af2020-01-26 16:59:13 -0800189
Adrian Taylor9a158e42020-10-24 20:51:25 -0700190void c_take_ns_shared(::A::AShared shared) {
191 if (shared.z == 2020) {
192 cxx_test_suite_set_correct();
193 }
194}
195
196void c_take_nested_ns_shared(::A::B::ABShared shared) {
197 if (shared.z == 2020) {
198 cxx_test_suite_set_correct();
199 }
200}
201
David Tolnay750755e2020-03-01 13:04:08 -0800202void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -0800203 if (cxx_test_suite_r_is_correct(&*r)) {
204 cxx_test_suite_set_correct();
205 }
David Tolnay3fd7f562020-01-26 17:47:11 -0800206}
David Tolnayad5b8af2020-01-26 16:59:13 -0800207
David Tolnay3fd7f562020-01-26 17:47:11 -0800208void c_take_unique_ptr(std::unique_ptr<C> c) {
209 if (c->get() == 2020) {
210 cxx_test_suite_set_correct();
211 }
212}
David Tolnayad5b8af2020-01-26 16:59:13 -0800213
David Tolnaya7d00e82020-03-06 15:50:14 -0800214void c_take_ref_r(const R &r) {
215 if (cxx_test_suite_r_is_correct(&r)) {
216 cxx_test_suite_set_correct();
217 }
218}
David Tolnayad5b8af2020-01-26 16:59:13 -0800219
David Tolnay3fd7f562020-01-26 17:47:11 -0800220void c_take_ref_c(const C &c) {
221 if (c.get() == 2020) {
222 cxx_test_suite_set_correct();
223 }
224}
David Tolnayad5b8af2020-01-26 16:59:13 -0800225
Adrian Taylord47af7a2020-10-26 13:18:48 -0700226void c_take_ref_ns_c(const ::H::H &h) {
227 if (h.h == "hello") {
228 cxx_test_suite_set_correct();
229 }
230}
231
David Tolnay750755e2020-03-01 13:04:08 -0800232void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800233 if (std::string(s) == "2020") {
234 cxx_test_suite_set_correct();
235 }
236}
David Tolnayad5b8af2020-01-26 16:59:13 -0800237
David Tolnayce298232020-11-11 10:08:54 -0800238void c_take_sliceu8(rust::Slice<const uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700239 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
240 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700241 cxx_test_suite_set_correct();
242 }
243}
244
David Tolnay750755e2020-03-01 13:04:08 -0800245void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800246 if (std::string(s) == "2020") {
247 cxx_test_suite_set_correct();
248 }
249}
David Tolnayad5b8af2020-01-26 16:59:13 -0800250
David Tolnay3fd7f562020-01-26 17:47:11 -0800251void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
252 if (*s == "2020") {
253 cxx_test_suite_set_correct();
254 }
255}
David Tolnayad5b8af2020-01-26 16:59:13 -0800256
Myron Ahneba35cf2020-02-05 19:41:51 +0700257void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
258 if (v->size() == 4) {
259 cxx_test_suite_set_correct();
260 }
261}
262
263void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
264 if (v->size() == 4) {
265 cxx_test_suite_set_correct();
266 }
267}
268
David Tolnay47e239d2020-08-28 00:32:04 -0700269void c_take_unique_ptr_vector_string(
270 std::unique_ptr<std::vector<std::string>> v) {
271 (void)v;
272 cxx_test_suite_set_correct();
273}
274
Myron Ahneba35cf2020-02-05 19:41:51 +0700275void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
276 if (v->size() == 2) {
277 cxx_test_suite_set_correct();
278 }
279}
280
David Tolnay2244d1f2020-04-25 13:58:18 -0700281void c_take_ref_vector(const std::vector<uint8_t> &v) {
282 if (v.size() == 4) {
283 cxx_test_suite_set_correct();
284 }
285}
286
David Tolnayd2ce8a92020-04-25 16:16:45 -0700287void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
Myron Ahneba35cf2020-02-05 19:41:51 +0700288
David Tolnay61adf422020-08-26 20:51:27 -0700289void c_take_rust_vec_index(rust::Vec<uint8_t> v) {
290 try {
291 v.at(100);
292 } catch (const std::out_of_range &ex) {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700293 std::string expected = "rust::Vec index out of range";
David Tolnay61adf422020-08-26 20:51:27 -0700294 if (ex.what() == expected) {
295 cxx_test_suite_set_correct();
296 }
297 }
298}
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700299
David Tolnayd2ce8a92020-04-25 16:16:45 -0700300void c_take_rust_vec_shared(rust::Vec<Shared> v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700301 uint32_t sum = 0;
David Tolnayc87c2152020-04-24 17:07:41 -0700302 for (auto i : v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700303 sum += i.z;
304 }
305 if (sum == 2021) {
306 cxx_test_suite_set_correct();
307 }
308}
309
Adrian Taylor9a158e42020-10-24 20:51:25 -0700310void c_take_rust_vec_ns_shared(rust::Vec<::A::AShared> v) {
311 uint32_t sum = 0;
312 for (auto i : v) {
313 sum += i.z;
314 }
315 if (sum == 2021) {
316 cxx_test_suite_set_correct();
317 }
318}
319
320void c_take_rust_vec_nested_ns_shared(rust::Vec<::A::B::ABShared> v) {
321 uint32_t sum = 0;
322 for (auto i : v) {
323 sum += i.z;
324 }
325 if (sum == 2021) {
326 cxx_test_suite_set_correct();
327 }
328}
329
David Tolnay33f56ad2020-08-27 17:06:35 -0700330void c_take_rust_vec_string(rust::Vec<rust::String> v) {
331 (void)v;
332 cxx_test_suite_set_correct();
333}
334
myronahnda9be502020-04-29 05:47:23 +0700335void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
336 // Exercise requirements of ForwardIterator
337 // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
338 uint32_t sum = 0;
339 for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
340 sum += it->z;
341 }
342 if (sum == 2021) {
343 cxx_test_suite_set_correct();
344 }
345}
346
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700347void c_take_rust_vec_shared_index(rust::Vec<Shared> v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700348 if (v[0].z == 1010 && v.at(0).z == 1010 && v.front().z == 1010 &&
349 v[1].z == 1011 && v.at(1).z == 1011 && v.back().z == 1011) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700350 cxx_test_suite_set_correct();
351 }
352}
353
David Tolnayfb6b73c2020-11-10 14:32:16 -0800354void c_take_rust_vec_shared_push(rust::Vec<Shared> v) {
355 v.push_back(Shared{3});
356 v.emplace_back(Shared{2});
357 if (v[v.size() - 2].z == 3 && v.back().z == 2) {
358 cxx_test_suite_set_correct();
359 }
360}
361
David Tolnayd2ce8a92020-04-25 16:16:45 -0700362void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
363 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
364 if (sum == 200) {
365 cxx_test_suite_set_correct();
366 }
367}
368
David Tolnay33f56ad2020-08-27 17:06:35 -0700369void c_take_ref_rust_vec_string(const rust::Vec<rust::String> &v) {
370 (void)v;
371 cxx_test_suite_set_correct();
372}
373
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700374void c_take_ref_rust_vec_index(const rust::Vec<uint8_t> &v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700375 if (v[0] == 86 && v.at(0) == 86 && v.front() == 86 && v[1] == 75 &&
376 v.at(1) == 75 && v[3] == 9 && v.at(3) == 9 && v.back() == 9) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700377 cxx_test_suite_set_correct();
378 }
379}
380
myronahnda9be502020-04-29 05:47:23 +0700381void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
382 // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
383 // requirements for std::iterator_traits.
384 // https://en.cppreference.com/w/cpp/iterator/iterator_traits
David Tolnayf5aeea22020-04-30 19:34:51 -0700385 std::vector<uint8_t> stdv;
386 std::copy(v.begin(), v.end(), std::back_inserter(stdv));
387 uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
myronahnda9be502020-04-29 05:47:23 +0700388 if (sum == 200) {
389 cxx_test_suite_set_correct();
390 }
391}
392
David Tolnay701c6882020-11-02 10:44:39 -0800393const SharedString &c_take_ref_shared_string(const SharedString &s) {
394 if (std::string(s.msg) == "2020") {
395 cxx_test_suite_set_correct();
396 }
397 return s;
398}
399
David Tolnay75dca2e2020-03-25 20:17:52 -0700400void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
401 callback("2020");
402}
403
Joel Galensonc03402a2020-04-23 17:31:09 -0700404void c_take_enum(Enum e) {
405 if (e == Enum::AVal) {
406 cxx_test_suite_set_correct();
407 }
408}
409
Adrian Taylor9a158e42020-10-24 20:51:25 -0700410void c_take_ns_enum(::A::AEnum e) {
411 if (e == ::A::AEnum::AAVal) {
412 cxx_test_suite_set_correct();
413 }
414}
415
416void c_take_nested_ns_enum(::A::B::ABEnum e) {
417 if (e == ::A::B::ABEnum::ABAVal) {
418 cxx_test_suite_set_correct();
419 }
420}
421
David Tolnayebef4a22020-03-17 15:33:47 -0700422void c_try_return_void() {}
423
424size_t c_try_return_primitive() { return 2020; }
425
426size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
427
David Tolnay99642622020-03-25 13:07:35 -0700428rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700429
David Tolnay99642622020-03-25 13:07:35 -0700430const rust::String &c_try_return_ref(const rust::String &s) { return s; }
431
432rust::Str c_try_return_str(rust::Str s) { return s; }
433
David Tolnayce298232020-11-11 10:08:54 -0800434rust::Slice<const uint8_t> c_try_return_sliceu8(rust::Slice<const uint8_t> s) {
435 return s;
436}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700437
David Tolnay99642622020-03-25 13:07:35 -0700438rust::String c_try_return_rust_string() { return c_return_rust_string(); }
439
440std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
441 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700442}
443
David Tolnay8b9d1762020-04-25 16:05:46 -0700444rust::Vec<uint8_t> c_try_return_rust_vec() {
445 throw std::runtime_error("unimplemented");
446}
447
David Tolnay33f56ad2020-08-27 17:06:35 -0700448rust::Vec<rust::String> c_try_return_rust_vec_string() {
449 throw std::runtime_error("unimplemented");
450}
451
David Tolnay77989692020-04-25 15:57:47 -0700452const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
453 (void)c;
454 throw std::runtime_error("unimplemented");
455}
456
David Tolnay2fe58c62020-03-06 16:23:09 -0800457extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800458 return std::unique_ptr<C>(new C{2020}).release();
459}
460
David Tolnay85db24862020-03-06 16:24:41 -0800461extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
462 return std::unique_ptr<std::string>(new std::string("2020")).release();
463}
464
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700465rust::String C::cOverloadedMethod(int32_t x) const {
466 return rust::String(std::to_string(x));
467}
468
469rust::String C::cOverloadedMethod(rust::Str x) const {
470 return rust::String(std::string(x));
471}
472
473rust::String cOverloadedFunction(int x) {
474 return rust::String(std::to_string(x));
475}
476
477rust::String cOverloadedFunction(rust::Str x) {
478 return rust::String(std::string(x));
479}
480
Adrian Taylor121cca42020-10-10 15:32:00 -0700481void c_take_trivial_ptr(std::unique_ptr<D> d) {
482 if (d->d == 30) {
483 cxx_test_suite_set_correct();
484 }
485}
486
David Tolnaybf23e3e2020-10-30 21:09:16 -0700487void c_take_trivial_ref(const D &d) {
Adrian Taylor121cca42020-10-10 15:32:00 -0700488 if (d.d == 30) {
489 cxx_test_suite_set_correct();
490 }
491}
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700492
Adrian Taylor121cca42020-10-10 15:32:00 -0700493void c_take_trivial(D d) {
494 if (d.d == 30) {
495 cxx_test_suite_set_correct();
496 }
497}
498
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700499void c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g) {
500 if (g->g == 30) {
501 cxx_test_suite_set_correct();
502 }
503}
504
David Tolnaybf23e3e2020-10-30 21:09:16 -0700505void c_take_trivial_ns_ref(const ::G::G &g) {
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700506 if (g.g == 30) {
507 cxx_test_suite_set_correct();
508 }
509}
510
511void c_take_trivial_ns(::G::G g) {
512 if (g.g == 30) {
513 cxx_test_suite_set_correct();
514 }
515}
516
Adrian Taylor121cca42020-10-10 15:32:00 -0700517void c_take_opaque_ptr(std::unique_ptr<E> e) {
518 if (e->e == 40) {
519 cxx_test_suite_set_correct();
520 }
521}
522
Adrian Taylor5e79c642020-10-24 21:09:42 -0700523void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f) {
524 if (f->f == 40) {
525 cxx_test_suite_set_correct();
526 }
527}
528
David Tolnaybf23e3e2020-10-30 21:09:16 -0700529void c_take_opaque_ref(const E &e) {
Adrian Taylor121cca42020-10-10 15:32:00 -0700530 if (e.e == 40 && e.e_str == "hello") {
531 cxx_test_suite_set_correct();
532 }
533}
534
David Tolnaybf23e3e2020-10-30 21:09:16 -0700535void c_take_opaque_ns_ref(const ::F::F &f) {
Adrian Taylor5e79c642020-10-24 21:09:42 -0700536 if (f.f == 40 && f.f_str == "hello") {
537 cxx_test_suite_set_correct();
538 }
539}
540
Adrian Taylor121cca42020-10-10 15:32:00 -0700541std::unique_ptr<D> c_return_trivial_ptr() {
542 auto d = std::unique_ptr<D>(new D());
543 d->d = 30;
544 return d;
545}
546
547D c_return_trivial() {
548 D d;
549 d.d = 30;
550 return d;
551}
552
Adrian Taylor585bb0b2020-10-26 13:17:17 -0700553std::unique_ptr<::G::G> c_return_trivial_ns_ptr() {
554 auto g = std::unique_ptr<::G::G>(new ::G::G());
555 g->g = 30;
556 return g;
557}
558
559::G::G c_return_trivial_ns() {
560 ::G::G g;
561 g.g = 30;
562 return g;
563}
564
Adrian Taylor121cca42020-10-10 15:32:00 -0700565std::unique_ptr<E> c_return_opaque_ptr() {
566 auto e = std::unique_ptr<E>(new E());
567 e->e = 40;
568 e->e_str = std::string("hello");
569 return e;
570}
571
Adrian Taylor5e79c642020-10-24 21:09:42 -0700572std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
573 auto f = std::unique_ptr<::F::F>(new ::F::F());
574 f->f = 40;
575 f->f_str = std::string("hello");
576 return f;
577}
578
David Tolnayf306da42020-02-22 19:55:43 -0800579extern "C" const char *cxx_run_test() noexcept {
580#define STRINGIFY(x) #x
581#define TOSTRING(x) STRINGIFY(x)
582#define ASSERT(x) \
583 do { \
584 if (!(x)) { \
585 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
586 } \
587 } while (false)
588
589 ASSERT(r_return_primitive() == 2020);
590 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800591 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800592 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800593 ASSERT(r_return_ref(Shared{2020}) == 2020);
594 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
595 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800596 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700597 ASSERT(r_return_identity(2020) == 2020);
598 ASSERT(r_return_sum(2020, 1) == 2021);
Joel Galensonc03402a2020-04-23 17:31:09 -0700599 ASSERT(r_return_enum(0) == Enum::AVal);
600 ASSERT(r_return_enum(1) == Enum::BVal);
601 ASSERT(r_return_enum(2021) == Enum::CVal);
David Tolnayf306da42020-02-22 19:55:43 -0800602
603 r_take_primitive(2020);
604 r_take_shared(Shared{2020});
605 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
606 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800607 r_take_str(rust::Str("2020"));
David Tolnayce298232020-11-11 10:08:54 -0800608 r_take_sliceu8(rust::Slice<const uint8_t>(
David Tolnay4037de22020-04-30 19:51:04 -0700609 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
David Tolnay40226ab2020-03-03 00:05:35 -0800610 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800611 r_take_unique_ptr_string(
612 std::unique_ptr<std::string>(new std::string("2020")));
David Tolnay5df0a712020-09-24 15:58:28 -0400613 r_take_ref_vector(std::vector<uint8_t>{20, 2, 0});
David Tolnay80631e92020-09-24 16:07:30 -0400614 std::vector<uint64_t> empty_vector;
615 r_take_ref_empty_vector(empty_vector);
616 empty_vector.reserve(10);
617 r_take_ref_empty_vector(empty_vector);
Joel Galensonc03402a2020-04-23 17:31:09 -0700618 r_take_enum(Enum::AVal);
David Tolnayf306da42020-02-22 19:55:43 -0800619
David Tolnayb6c5ea72020-03-16 13:36:28 -0700620 ASSERT(r_try_return_primitive() == 2020);
621 try {
622 r_fail_return_primitive();
623 ASSERT(false);
624 } catch (const rust::Error &e) {
625 ASSERT(std::strcmp(e.what(), "rust error") == 0);
626 }
627
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700628 auto r2 = r_return_r2(2020);
629 ASSERT(r2->get() == 2020);
630 ASSERT(r2->set(2021) == 2021);
631 ASSERT(r2->get() == 2021);
632 ASSERT(r2->set(2020) == 2020);
633 ASSERT(r2->get() == 2020);
David Tolnay74608d72020-11-10 08:49:20 -0800634 ASSERT(std::string(Shared{0}.r_method_on_shared()) == "2020");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700635
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700636 ASSERT(std::string(rAliasedFunction(2020)) == "2020");
637
David Tolnayf306da42020-02-22 19:55:43 -0800638 cxx_test_suite_set_correct();
639 return nullptr;
640}
641
David Tolnay97c72102020-01-25 16:49:00 -0800642} // namespace tests
Adrian Taylorddc146e2020-10-25 21:40:17 -0700643
644namespace other {
David Tolnaybf23e3e2020-10-30 21:09:16 -0700645void ns_c_take_trivial(::tests::D d) {
646 if (d.d == 30) {
647 cxx_test_suite_set_correct();
Adrian Taylorddc146e2020-10-25 21:40:17 -0700648 }
David Tolnaybf23e3e2020-10-30 21:09:16 -0700649}
Adrian Taylorddc146e2020-10-25 21:40:17 -0700650
David Tolnaybf23e3e2020-10-30 21:09:16 -0700651::tests::D ns_c_return_trivial() {
652 ::tests::D d;
653 d.d = 30;
654 return d;
655}
Adrian Taylorddc146e2020-10-25 21:40:17 -0700656
David Tolnaybf23e3e2020-10-30 21:09:16 -0700657void ns_c_take_ns_shared(::A::AShared shared) {
658 if (shared.z == 2020) {
659 cxx_test_suite_set_correct();
Adrian Taylorddc146e2020-10-25 21:40:17 -0700660 }
David Tolnaybf23e3e2020-10-30 21:09:16 -0700661}
Adrian Taylor0fac3212020-10-25 21:52:55 -0700662} // namespace other
663
664namespace I {
David Tolnaybf23e3e2020-10-30 21:09:16 -0700665uint32_t I::get() const { return a; }
Adrian Taylor0fac3212020-10-25 21:52:55 -0700666
David Tolnaybf23e3e2020-10-30 21:09:16 -0700667std::unique_ptr<I> ns_c_return_unique_ptr_ns() {
668 return std::unique_ptr<I>(new I());
669}
Adrian Taylor0fac3212020-10-25 21:52:55 -0700670} // namespace I