blob: db51435558c392e256ee4d1d989592080d0b9c4a [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
David Tolnayb8715772020-01-28 00:54:05 -080058const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080059
Adrian Taylor9a158e42020-10-24 20:51:25 -070060const size_t &c_return_ns_ref(const ::A::AShared &shared) { return shared.z; }
61
62const size_t &c_return_nested_ns_ref(const ::A::B::ABShared &shared) { return shared.z; }
63
David Tolnay18d93b62020-08-27 00:55:48 -070064size_t &c_return_mut(Shared &shared) { return shared.z; }
65
David Tolnay750755e2020-03-01 13:04:08 -080066rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080067 (void)shared;
68 return "2020";
69}
70
David Tolnayeb952ba2020-04-14 15:02:24 -070071rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070072 (void)shared;
David Tolnay4037de22020-04-30 19:51:04 -070073 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA),
74 sizeof(SLICE_DATA));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070075}
76
David Tolnay750755e2020-03-01 13:04:08 -080077rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080078
79std::unique_ptr<std::string> c_return_unique_ptr_string() {
80 return std::unique_ptr<std::string>(new std::string("2020"));
81}
82
Myron Ahneba35cf2020-02-05 19:41:51 +070083std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
David Tolnay85db5a02020-04-25 13:17:27 -070084 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
85 vec->push_back(86);
86 vec->push_back(75);
87 vec->push_back(30);
88 vec->push_back(9);
89 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070090}
91
92std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
David Tolnay85db5a02020-04-25 13:17:27 -070093 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
94 vec->push_back(86.0);
95 vec->push_back(75.0);
96 vec->push_back(30.0);
97 vec->push_back(9.5);
98 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070099}
100
David Tolnay47e239d2020-08-28 00:32:04 -0700101std::unique_ptr<std::vector<std::string>> c_return_unique_ptr_vector_string() {
102 return std::unique_ptr<std::vector<std::string>>(
103 new std::vector<std::string>());
104}
105
Myron Ahneba35cf2020-02-05 19:41:51 +0700106std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
David Tolnay85db5a02020-04-25 13:17:27 -0700107 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
108 vec->push_back(Shared{1010});
109 vec->push_back(Shared{1011});
110 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +0700111}
112
David Tolnay1bcc9fe2020-04-25 13:51:07 -0700113std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
114 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
115}
116
David Tolnayde5340e2020-04-25 14:03:21 -0700117const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
118 return c.get_v();
119}
120
David Tolnay18d93b62020-08-27 00:55:48 -0700121std::vector<uint8_t> &c_return_mut_vector(C &c) { return c.get_v(); }
122
David Tolnayb41e74c2020-04-25 15:06:18 -0700123rust::Vec<uint8_t> c_return_rust_vec() {
124 throw std::runtime_error("unimplemented");
125}
126
David Tolnay77989692020-04-25 15:57:47 -0700127const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
128 (void)c;
129 throw std::runtime_error("unimplemented");
130}
131
David Tolnay18d93b62020-08-27 00:55:48 -0700132rust::Vec<uint8_t> &c_return_mut_rust_vec(C &c) {
133 (void)c;
134 throw std::runtime_error("unimplemented");
135}
136
David Tolnay33f56ad2020-08-27 17:06:35 -0700137rust::Vec<rust::String> c_return_rust_vec_string() {
138 throw std::runtime_error("unimplemented");
139}
140
David Tolnay378ca502020-04-27 16:47:55 -0700141size_t c_return_identity(size_t n) { return n; }
Joel Galensonba676072020-04-27 15:55:45 -0700142
David Tolnay378ca502020-04-27 16:47:55 -0700143size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
Joel Galensonba676072020-04-27 15:55:45 -0700144
David Tolnayf6a89f22020-05-10 23:39:27 -0700145Enum c_return_enum(uint16_t n) {
146 if (n <= static_cast<uint16_t>(Enum::AVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700147 return Enum::AVal;
David Tolnayf6a89f22020-05-10 23:39:27 -0700148 } else if (n <= static_cast<uint16_t>(Enum::BVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700149 return Enum::BVal;
150 } else {
151 return Enum::CVal;
152 }
153}
154
Adrian Taylor9a158e42020-10-24 20:51:25 -0700155::A::AEnum c_return_ns_enum(uint16_t n) {
156 if (n <= static_cast<uint16_t>(::A::AEnum::AAVal)) {
157 return ::A::AEnum::AAVal;
158 } else if (n <= static_cast<uint16_t>(::A::AEnum::ABVal)) {
159 return ::A::AEnum::ABVal;
160 } else {
161 return ::A::AEnum::ACVal;
162 }
163}
164
165::A::B::ABEnum c_return_nested_ns_enum(uint16_t n) {
166 if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABAVal)) {
167 return ::A::B::ABEnum::ABAVal;
168 } else if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABBVal)) {
169 return ::A::B::ABEnum::ABBVal;
170 } else {
171 return ::A::B::ABEnum::ABCVal;
172 }
173}
174
David Tolnay3fd7f562020-01-26 17:47:11 -0800175void c_take_primitive(size_t n) {
176 if (n == 2020) {
177 cxx_test_suite_set_correct();
178 }
179}
David Tolnayad5b8af2020-01-26 16:59:13 -0800180
David Tolnay3fd7f562020-01-26 17:47:11 -0800181void c_take_shared(Shared shared) {
182 if (shared.z == 2020) {
183 cxx_test_suite_set_correct();
184 }
185}
David Tolnayad5b8af2020-01-26 16:59:13 -0800186
Adrian Taylor9a158e42020-10-24 20:51:25 -0700187void c_take_ns_shared(::A::AShared shared) {
188 if (shared.z == 2020) {
189 cxx_test_suite_set_correct();
190 }
191}
192
193void c_take_nested_ns_shared(::A::B::ABShared shared) {
194 if (shared.z == 2020) {
195 cxx_test_suite_set_correct();
196 }
197}
198
David Tolnay750755e2020-03-01 13:04:08 -0800199void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -0800200 if (cxx_test_suite_r_is_correct(&*r)) {
201 cxx_test_suite_set_correct();
202 }
David Tolnay3fd7f562020-01-26 17:47:11 -0800203}
David Tolnayad5b8af2020-01-26 16:59:13 -0800204
David Tolnay3fd7f562020-01-26 17:47:11 -0800205void c_take_unique_ptr(std::unique_ptr<C> c) {
206 if (c->get() == 2020) {
207 cxx_test_suite_set_correct();
208 }
209}
David Tolnayad5b8af2020-01-26 16:59:13 -0800210
David Tolnaya7d00e82020-03-06 15:50:14 -0800211void c_take_ref_r(const R &r) {
212 if (cxx_test_suite_r_is_correct(&r)) {
213 cxx_test_suite_set_correct();
214 }
215}
David Tolnayad5b8af2020-01-26 16:59:13 -0800216
David Tolnay3fd7f562020-01-26 17:47:11 -0800217void c_take_ref_c(const C &c) {
218 if (c.get() == 2020) {
219 cxx_test_suite_set_correct();
220 }
221}
David Tolnayad5b8af2020-01-26 16:59:13 -0800222
David Tolnay750755e2020-03-01 13:04:08 -0800223void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800224 if (std::string(s) == "2020") {
225 cxx_test_suite_set_correct();
226 }
227}
David Tolnayad5b8af2020-01-26 16:59:13 -0800228
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700229void c_take_sliceu8(rust::Slice<uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700230 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
231 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700232 cxx_test_suite_set_correct();
233 }
234}
235
David Tolnay750755e2020-03-01 13:04:08 -0800236void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800237 if (std::string(s) == "2020") {
238 cxx_test_suite_set_correct();
239 }
240}
David Tolnayad5b8af2020-01-26 16:59:13 -0800241
David Tolnay3fd7f562020-01-26 17:47:11 -0800242void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
243 if (*s == "2020") {
244 cxx_test_suite_set_correct();
245 }
246}
David Tolnayad5b8af2020-01-26 16:59:13 -0800247
Myron Ahneba35cf2020-02-05 19:41:51 +0700248void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
249 if (v->size() == 4) {
250 cxx_test_suite_set_correct();
251 }
252}
253
254void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
255 if (v->size() == 4) {
256 cxx_test_suite_set_correct();
257 }
258}
259
David Tolnay47e239d2020-08-28 00:32:04 -0700260void c_take_unique_ptr_vector_string(
261 std::unique_ptr<std::vector<std::string>> v) {
262 (void)v;
263 cxx_test_suite_set_correct();
264}
265
Myron Ahneba35cf2020-02-05 19:41:51 +0700266void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
267 if (v->size() == 2) {
268 cxx_test_suite_set_correct();
269 }
270}
271
David Tolnay2244d1f2020-04-25 13:58:18 -0700272void c_take_ref_vector(const std::vector<uint8_t> &v) {
273 if (v.size() == 4) {
274 cxx_test_suite_set_correct();
275 }
276}
277
David Tolnayd2ce8a92020-04-25 16:16:45 -0700278void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
Myron Ahneba35cf2020-02-05 19:41:51 +0700279
David Tolnay61adf422020-08-26 20:51:27 -0700280void c_take_rust_vec_index(rust::Vec<uint8_t> v) {
281 try {
282 v.at(100);
283 } catch (const std::out_of_range &ex) {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700284 std::string expected = "rust::Vec index out of range";
David Tolnay61adf422020-08-26 20:51:27 -0700285 if (ex.what() == expected) {
286 cxx_test_suite_set_correct();
287 }
288 }
289}
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700290
David Tolnayd2ce8a92020-04-25 16:16:45 -0700291void c_take_rust_vec_shared(rust::Vec<Shared> v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700292 uint32_t sum = 0;
David Tolnayc87c2152020-04-24 17:07:41 -0700293 for (auto i : v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700294 sum += i.z;
295 }
296 if (sum == 2021) {
297 cxx_test_suite_set_correct();
298 }
299}
300
Adrian Taylor9a158e42020-10-24 20:51:25 -0700301void c_take_rust_vec_ns_shared(rust::Vec<::A::AShared> v) {
302 uint32_t sum = 0;
303 for (auto i : v) {
304 sum += i.z;
305 }
306 if (sum == 2021) {
307 cxx_test_suite_set_correct();
308 }
309}
310
311void c_take_rust_vec_nested_ns_shared(rust::Vec<::A::B::ABShared> 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
David Tolnay33f56ad2020-08-27 17:06:35 -0700321void c_take_rust_vec_string(rust::Vec<rust::String> v) {
322 (void)v;
323 cxx_test_suite_set_correct();
324}
325
myronahnda9be502020-04-29 05:47:23 +0700326void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
327 // Exercise requirements of ForwardIterator
328 // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
329 uint32_t sum = 0;
330 for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
331 sum += it->z;
332 }
333 if (sum == 2021) {
334 cxx_test_suite_set_correct();
335 }
336}
337
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700338void c_take_rust_vec_shared_index(rust::Vec<Shared> v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700339 if (v[0].z == 1010 && v.at(0).z == 1010 && v.front().z == 1010 &&
340 v[1].z == 1011 && v.at(1).z == 1011 && v.back().z == 1011) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700341 cxx_test_suite_set_correct();
342 }
343}
344
David Tolnayd2ce8a92020-04-25 16:16:45 -0700345void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
346 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
347 if (sum == 200) {
348 cxx_test_suite_set_correct();
349 }
350}
351
David Tolnay33f56ad2020-08-27 17:06:35 -0700352void c_take_ref_rust_vec_string(const rust::Vec<rust::String> &v) {
353 (void)v;
354 cxx_test_suite_set_correct();
355}
356
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700357void c_take_ref_rust_vec_index(const rust::Vec<uint8_t> &v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700358 if (v[0] == 86 && v.at(0) == 86 && v.front() == 86 && v[1] == 75 &&
359 v.at(1) == 75 && v[3] == 9 && v.at(3) == 9 && v.back() == 9) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700360 cxx_test_suite_set_correct();
361 }
362}
363
myronahnda9be502020-04-29 05:47:23 +0700364void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
365 // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
366 // requirements for std::iterator_traits.
367 // https://en.cppreference.com/w/cpp/iterator/iterator_traits
David Tolnayf5aeea22020-04-30 19:34:51 -0700368 std::vector<uint8_t> stdv;
369 std::copy(v.begin(), v.end(), std::back_inserter(stdv));
370 uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
myronahnda9be502020-04-29 05:47:23 +0700371 if (sum == 200) {
372 cxx_test_suite_set_correct();
373 }
374}
375
David Tolnay0c8c0f22020-07-21 17:57:46 -0700376/*
377// https://github.com/dtolnay/cxx/issues/232
David Tolnay75dca2e2020-03-25 20:17:52 -0700378void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
379 callback("2020");
380}
David Tolnay0c8c0f22020-07-21 17:57:46 -0700381*/
David Tolnay75dca2e2020-03-25 20:17:52 -0700382
Joel Galensonc03402a2020-04-23 17:31:09 -0700383void c_take_enum(Enum e) {
384 if (e == Enum::AVal) {
385 cxx_test_suite_set_correct();
386 }
387}
388
Adrian Taylor9a158e42020-10-24 20:51:25 -0700389void c_take_ns_enum(::A::AEnum e) {
390 if (e == ::A::AEnum::AAVal) {
391 cxx_test_suite_set_correct();
392 }
393}
394
395void c_take_nested_ns_enum(::A::B::ABEnum e) {
396 if (e == ::A::B::ABEnum::ABAVal) {
397 cxx_test_suite_set_correct();
398 }
399}
400
David Tolnayebef4a22020-03-17 15:33:47 -0700401void c_try_return_void() {}
402
403size_t c_try_return_primitive() { return 2020; }
404
405size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
406
David Tolnay99642622020-03-25 13:07:35 -0700407rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700408
David Tolnay99642622020-03-25 13:07:35 -0700409const rust::String &c_try_return_ref(const rust::String &s) { return s; }
410
411rust::Str c_try_return_str(rust::Str s) { return s; }
412
Adrian Taylorec9430e2020-04-14 16:09:58 -0700413rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700414
David Tolnay99642622020-03-25 13:07:35 -0700415rust::String c_try_return_rust_string() { return c_return_rust_string(); }
416
417std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
418 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700419}
420
David Tolnay8b9d1762020-04-25 16:05:46 -0700421rust::Vec<uint8_t> c_try_return_rust_vec() {
422 throw std::runtime_error("unimplemented");
423}
424
David Tolnay33f56ad2020-08-27 17:06:35 -0700425rust::Vec<rust::String> c_try_return_rust_vec_string() {
426 throw std::runtime_error("unimplemented");
427}
428
David Tolnay77989692020-04-25 15:57:47 -0700429const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
430 (void)c;
431 throw std::runtime_error("unimplemented");
432}
433
David Tolnay2fe58c62020-03-06 16:23:09 -0800434extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800435 return std::unique_ptr<C>(new C{2020}).release();
436}
437
David Tolnay85db24862020-03-06 16:24:41 -0800438extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
439 return std::unique_ptr<std::string>(new std::string("2020")).release();
440}
441
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700442rust::String C::cOverloadedMethod(int32_t x) const {
443 return rust::String(std::to_string(x));
444}
445
446rust::String C::cOverloadedMethod(rust::Str x) const {
447 return rust::String(std::string(x));
448}
449
450rust::String cOverloadedFunction(int x) {
451 return rust::String(std::to_string(x));
452}
453
454rust::String cOverloadedFunction(rust::Str x) {
455 return rust::String(std::string(x));
456}
457
Adrian Taylor121cca42020-10-10 15:32:00 -0700458void c_take_trivial_ptr(std::unique_ptr<D> d) {
459 if (d->d == 30) {
460 cxx_test_suite_set_correct();
461 }
462}
463
464void c_take_trivial_ref(const D& d) {
465 if (d.d == 30) {
466 cxx_test_suite_set_correct();
467 }
468}
469void c_take_trivial(D d) {
470 if (d.d == 30) {
471 cxx_test_suite_set_correct();
472 }
473}
474
475void c_take_opaque_ptr(std::unique_ptr<E> e) {
476 if (e->e == 40) {
477 cxx_test_suite_set_correct();
478 }
479}
480
Adrian Taylor5e79c642020-10-24 21:09:42 -0700481void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f) {
482 if (f->f == 40) {
483 cxx_test_suite_set_correct();
484 }
485}
486
Adrian Taylor121cca42020-10-10 15:32:00 -0700487void c_take_opaque_ref(const E& e) {
488 if (e.e == 40 && e.e_str == "hello") {
489 cxx_test_suite_set_correct();
490 }
491}
492
Adrian Taylor5e79c642020-10-24 21:09:42 -0700493void c_take_opaque_ns_ref(const ::F::F& f) {
494 if (f.f == 40 && f.f_str == "hello") {
495 cxx_test_suite_set_correct();
496 }
497}
498
499
Adrian Taylor121cca42020-10-10 15:32:00 -0700500std::unique_ptr<D> c_return_trivial_ptr() {
501 auto d = std::unique_ptr<D>(new D());
502 d->d = 30;
503 return d;
504}
505
506D c_return_trivial() {
507 D d;
508 d.d = 30;
509 return d;
510}
511
512std::unique_ptr<E> c_return_opaque_ptr() {
513 auto e = std::unique_ptr<E>(new E());
514 e->e = 40;
515 e->e_str = std::string("hello");
516 return e;
517}
518
Adrian Taylor5e79c642020-10-24 21:09:42 -0700519std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
520 auto f = std::unique_ptr<::F::F>(new ::F::F());
521 f->f = 40;
522 f->f_str = std::string("hello");
523 return f;
524}
525
David Tolnayf306da42020-02-22 19:55:43 -0800526extern "C" const char *cxx_run_test() noexcept {
527#define STRINGIFY(x) #x
528#define TOSTRING(x) STRINGIFY(x)
529#define ASSERT(x) \
530 do { \
531 if (!(x)) { \
532 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
533 } \
534 } while (false)
535
536 ASSERT(r_return_primitive() == 2020);
537 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800538 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800539 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800540 ASSERT(r_return_ref(Shared{2020}) == 2020);
541 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
542 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800543 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700544 ASSERT(r_return_identity(2020) == 2020);
545 ASSERT(r_return_sum(2020, 1) == 2021);
Joel Galensonc03402a2020-04-23 17:31:09 -0700546 ASSERT(r_return_enum(0) == Enum::AVal);
547 ASSERT(r_return_enum(1) == Enum::BVal);
548 ASSERT(r_return_enum(2021) == Enum::CVal);
David Tolnayf306da42020-02-22 19:55:43 -0800549
550 r_take_primitive(2020);
551 r_take_shared(Shared{2020});
552 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
553 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800554 r_take_str(rust::Str("2020"));
David Tolnay4037de22020-04-30 19:51:04 -0700555 r_take_sliceu8(rust::Slice<uint8_t>(
556 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
David Tolnay40226ab2020-03-03 00:05:35 -0800557 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800558 r_take_unique_ptr_string(
559 std::unique_ptr<std::string>(new std::string("2020")));
David Tolnay5df0a712020-09-24 15:58:28 -0400560 r_take_ref_vector(std::vector<uint8_t>{20, 2, 0});
David Tolnay80631e92020-09-24 16:07:30 -0400561 std::vector<uint64_t> empty_vector;
562 r_take_ref_empty_vector(empty_vector);
563 empty_vector.reserve(10);
564 r_take_ref_empty_vector(empty_vector);
Joel Galensonc03402a2020-04-23 17:31:09 -0700565 r_take_enum(Enum::AVal);
David Tolnayf306da42020-02-22 19:55:43 -0800566
David Tolnayb6c5ea72020-03-16 13:36:28 -0700567 ASSERT(r_try_return_primitive() == 2020);
568 try {
569 r_fail_return_primitive();
570 ASSERT(false);
571 } catch (const rust::Error &e) {
572 ASSERT(std::strcmp(e.what(), "rust error") == 0);
573 }
574
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700575 auto r2 = r_return_r2(2020);
576 ASSERT(r2->get() == 2020);
577 ASSERT(r2->set(2021) == 2021);
578 ASSERT(r2->get() == 2021);
579 ASSERT(r2->set(2020) == 2020);
580 ASSERT(r2->get() == 2020);
581
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700582 ASSERT(std::string(rAliasedFunction(2020)) == "2020");
583
David Tolnayf306da42020-02-22 19:55:43 -0800584 cxx_test_suite_set_correct();
585 return nullptr;
586}
587
David Tolnay97c72102020-01-25 16:49:00 -0800588} // namespace tests