blob: 983cf955a3324f19e87c967209fc6c6c19d7601d [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
David Tolnaybe13d8a2020-03-06 15:45:39 -080046rust::Box<R> c_return_box() {
47 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
48}
49
David Tolnayad5b8af2020-01-26 16:59:13 -080050std::unique_ptr<C> c_return_unique_ptr() {
51 return std::unique_ptr<C>(new C{2020});
52}
53
David Tolnayb8715772020-01-28 00:54:05 -080054const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080055
David Tolnay18d93b62020-08-27 00:55:48 -070056size_t &c_return_mut(Shared &shared) { return shared.z; }
57
David Tolnay750755e2020-03-01 13:04:08 -080058rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080059 (void)shared;
60 return "2020";
61}
62
David Tolnayeb952ba2020-04-14 15:02:24 -070063rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070064 (void)shared;
David Tolnay4037de22020-04-30 19:51:04 -070065 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA),
66 sizeof(SLICE_DATA));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070067}
68
David Tolnay750755e2020-03-01 13:04:08 -080069rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080070
71std::unique_ptr<std::string> c_return_unique_ptr_string() {
72 return std::unique_ptr<std::string>(new std::string("2020"));
73}
74
Myron Ahneba35cf2020-02-05 19:41:51 +070075std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
David Tolnay85db5a02020-04-25 13:17:27 -070076 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
77 vec->push_back(86);
78 vec->push_back(75);
79 vec->push_back(30);
80 vec->push_back(9);
81 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070082}
83
84std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
David Tolnay85db5a02020-04-25 13:17:27 -070085 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
86 vec->push_back(86.0);
87 vec->push_back(75.0);
88 vec->push_back(30.0);
89 vec->push_back(9.5);
90 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070091}
92
David Tolnay47e239d2020-08-28 00:32:04 -070093std::unique_ptr<std::vector<std::string>> c_return_unique_ptr_vector_string() {
94 return std::unique_ptr<std::vector<std::string>>(
95 new std::vector<std::string>());
96}
97
Myron Ahneba35cf2020-02-05 19:41:51 +070098std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
David Tolnay85db5a02020-04-25 13:17:27 -070099 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
100 vec->push_back(Shared{1010});
101 vec->push_back(Shared{1011});
102 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +0700103}
104
David Tolnay1bcc9fe2020-04-25 13:51:07 -0700105std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
106 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
107}
108
David Tolnayde5340e2020-04-25 14:03:21 -0700109const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
110 return c.get_v();
111}
112
David Tolnay18d93b62020-08-27 00:55:48 -0700113std::vector<uint8_t> &c_return_mut_vector(C &c) { return c.get_v(); }
114
David Tolnayb41e74c2020-04-25 15:06:18 -0700115rust::Vec<uint8_t> c_return_rust_vec() {
116 throw std::runtime_error("unimplemented");
117}
118
David Tolnay77989692020-04-25 15:57:47 -0700119const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
120 (void)c;
121 throw std::runtime_error("unimplemented");
122}
123
David Tolnay18d93b62020-08-27 00:55:48 -0700124rust::Vec<uint8_t> &c_return_mut_rust_vec(C &c) {
125 (void)c;
126 throw std::runtime_error("unimplemented");
127}
128
David Tolnay33f56ad2020-08-27 17:06:35 -0700129rust::Vec<rust::String> c_return_rust_vec_string() {
130 throw std::runtime_error("unimplemented");
131}
132
David Tolnay378ca502020-04-27 16:47:55 -0700133size_t c_return_identity(size_t n) { return n; }
Joel Galensonba676072020-04-27 15:55:45 -0700134
David Tolnay378ca502020-04-27 16:47:55 -0700135size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
Joel Galensonba676072020-04-27 15:55:45 -0700136
David Tolnayf6a89f22020-05-10 23:39:27 -0700137Enum c_return_enum(uint16_t n) {
138 if (n <= static_cast<uint16_t>(Enum::AVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700139 return Enum::AVal;
David Tolnayf6a89f22020-05-10 23:39:27 -0700140 } else if (n <= static_cast<uint16_t>(Enum::BVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700141 return Enum::BVal;
142 } else {
143 return Enum::CVal;
144 }
145}
146
David Tolnay3fd7f562020-01-26 17:47:11 -0800147void c_take_primitive(size_t n) {
148 if (n == 2020) {
149 cxx_test_suite_set_correct();
150 }
151}
David Tolnayad5b8af2020-01-26 16:59:13 -0800152
David Tolnay3fd7f562020-01-26 17:47:11 -0800153void c_take_shared(Shared shared) {
154 if (shared.z == 2020) {
155 cxx_test_suite_set_correct();
156 }
157}
David Tolnayad5b8af2020-01-26 16:59:13 -0800158
David Tolnay750755e2020-03-01 13:04:08 -0800159void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -0800160 if (cxx_test_suite_r_is_correct(&*r)) {
161 cxx_test_suite_set_correct();
162 }
David Tolnay3fd7f562020-01-26 17:47:11 -0800163}
David Tolnayad5b8af2020-01-26 16:59:13 -0800164
David Tolnay3fd7f562020-01-26 17:47:11 -0800165void c_take_unique_ptr(std::unique_ptr<C> c) {
166 if (c->get() == 2020) {
167 cxx_test_suite_set_correct();
168 }
169}
David Tolnayad5b8af2020-01-26 16:59:13 -0800170
David Tolnaya7d00e82020-03-06 15:50:14 -0800171void c_take_ref_r(const R &r) {
172 if (cxx_test_suite_r_is_correct(&r)) {
173 cxx_test_suite_set_correct();
174 }
175}
David Tolnayad5b8af2020-01-26 16:59:13 -0800176
David Tolnay3fd7f562020-01-26 17:47:11 -0800177void c_take_ref_c(const C &c) {
178 if (c.get() == 2020) {
179 cxx_test_suite_set_correct();
180 }
181}
David Tolnayad5b8af2020-01-26 16:59:13 -0800182
David Tolnay750755e2020-03-01 13:04:08 -0800183void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800184 if (std::string(s) == "2020") {
185 cxx_test_suite_set_correct();
186 }
187}
David Tolnayad5b8af2020-01-26 16:59:13 -0800188
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700189void c_take_sliceu8(rust::Slice<uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700190 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
191 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700192 cxx_test_suite_set_correct();
193 }
194}
195
David Tolnay750755e2020-03-01 13:04:08 -0800196void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800197 if (std::string(s) == "2020") {
198 cxx_test_suite_set_correct();
199 }
200}
David Tolnayad5b8af2020-01-26 16:59:13 -0800201
David Tolnay3fd7f562020-01-26 17:47:11 -0800202void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
203 if (*s == "2020") {
204 cxx_test_suite_set_correct();
205 }
206}
David Tolnayad5b8af2020-01-26 16:59:13 -0800207
Myron Ahneba35cf2020-02-05 19:41:51 +0700208void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
209 if (v->size() == 4) {
210 cxx_test_suite_set_correct();
211 }
212}
213
214void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
215 if (v->size() == 4) {
216 cxx_test_suite_set_correct();
217 }
218}
219
David Tolnay47e239d2020-08-28 00:32:04 -0700220void c_take_unique_ptr_vector_string(
221 std::unique_ptr<std::vector<std::string>> v) {
222 (void)v;
223 cxx_test_suite_set_correct();
224}
225
Myron Ahneba35cf2020-02-05 19:41:51 +0700226void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
227 if (v->size() == 2) {
228 cxx_test_suite_set_correct();
229 }
230}
231
David Tolnay2244d1f2020-04-25 13:58:18 -0700232void c_take_ref_vector(const std::vector<uint8_t> &v) {
233 if (v.size() == 4) {
234 cxx_test_suite_set_correct();
235 }
236}
237
David Tolnayd2ce8a92020-04-25 16:16:45 -0700238void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
Myron Ahneba35cf2020-02-05 19:41:51 +0700239
David Tolnay61adf422020-08-26 20:51:27 -0700240void c_take_rust_vec_index(rust::Vec<uint8_t> v) {
241 try {
242 v.at(100);
243 } catch (const std::out_of_range &ex) {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700244 std::string expected = "rust::Vec index out of range";
David Tolnay61adf422020-08-26 20:51:27 -0700245 if (ex.what() == expected) {
246 cxx_test_suite_set_correct();
247 }
248 }
249}
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700250
David Tolnayd2ce8a92020-04-25 16:16:45 -0700251void c_take_rust_vec_shared(rust::Vec<Shared> v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700252 uint32_t sum = 0;
David Tolnayc87c2152020-04-24 17:07:41 -0700253 for (auto i : v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700254 sum += i.z;
255 }
256 if (sum == 2021) {
257 cxx_test_suite_set_correct();
258 }
259}
260
David Tolnay33f56ad2020-08-27 17:06:35 -0700261void c_take_rust_vec_string(rust::Vec<rust::String> v) {
262 (void)v;
263 cxx_test_suite_set_correct();
264}
265
myronahnda9be502020-04-29 05:47:23 +0700266void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
267 // Exercise requirements of ForwardIterator
268 // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
269 uint32_t sum = 0;
270 for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
271 sum += it->z;
272 }
273 if (sum == 2021) {
274 cxx_test_suite_set_correct();
275 }
276}
277
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700278void c_take_rust_vec_shared_index(rust::Vec<Shared> v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700279 if (v[0].z == 1010 && v.at(0).z == 1010 && v.front().z == 1010 &&
280 v[1].z == 1011 && v.at(1).z == 1011 && v.back().z == 1011) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700281 cxx_test_suite_set_correct();
282 }
283}
284
David Tolnayd2ce8a92020-04-25 16:16:45 -0700285void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
286 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
287 if (sum == 200) {
288 cxx_test_suite_set_correct();
289 }
290}
291
David Tolnay33f56ad2020-08-27 17:06:35 -0700292void c_take_ref_rust_vec_string(const rust::Vec<rust::String> &v) {
293 (void)v;
294 cxx_test_suite_set_correct();
295}
296
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700297void c_take_ref_rust_vec_index(const rust::Vec<uint8_t> &v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700298 if (v[0] == 86 && v.at(0) == 86 && v.front() == 86 && v[1] == 75 &&
299 v.at(1) == 75 && v[3] == 9 && v.at(3) == 9 && v.back() == 9) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700300 cxx_test_suite_set_correct();
301 }
302}
303
myronahnda9be502020-04-29 05:47:23 +0700304void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
305 // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
306 // requirements for std::iterator_traits.
307 // https://en.cppreference.com/w/cpp/iterator/iterator_traits
David Tolnayf5aeea22020-04-30 19:34:51 -0700308 std::vector<uint8_t> stdv;
309 std::copy(v.begin(), v.end(), std::back_inserter(stdv));
310 uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
myronahnda9be502020-04-29 05:47:23 +0700311 if (sum == 200) {
312 cxx_test_suite_set_correct();
313 }
314}
315
David Tolnay0c8c0f22020-07-21 17:57:46 -0700316/*
317// https://github.com/dtolnay/cxx/issues/232
David Tolnay75dca2e2020-03-25 20:17:52 -0700318void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
319 callback("2020");
320}
David Tolnay0c8c0f22020-07-21 17:57:46 -0700321*/
David Tolnay75dca2e2020-03-25 20:17:52 -0700322
Joel Galensonc03402a2020-04-23 17:31:09 -0700323void c_take_enum(Enum e) {
324 if (e == Enum::AVal) {
325 cxx_test_suite_set_correct();
326 }
327}
328
David Tolnayebef4a22020-03-17 15:33:47 -0700329void c_try_return_void() {}
330
331size_t c_try_return_primitive() { return 2020; }
332
333size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
334
David Tolnay99642622020-03-25 13:07:35 -0700335rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700336
David Tolnay99642622020-03-25 13:07:35 -0700337const rust::String &c_try_return_ref(const rust::String &s) { return s; }
338
339rust::Str c_try_return_str(rust::Str s) { return s; }
340
Adrian Taylorec9430e2020-04-14 16:09:58 -0700341rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700342
David Tolnay99642622020-03-25 13:07:35 -0700343rust::String c_try_return_rust_string() { return c_return_rust_string(); }
344
345std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
346 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700347}
348
David Tolnay8b9d1762020-04-25 16:05:46 -0700349rust::Vec<uint8_t> c_try_return_rust_vec() {
350 throw std::runtime_error("unimplemented");
351}
352
David Tolnay33f56ad2020-08-27 17:06:35 -0700353rust::Vec<rust::String> c_try_return_rust_vec_string() {
354 throw std::runtime_error("unimplemented");
355}
356
David Tolnay77989692020-04-25 15:57:47 -0700357const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
358 (void)c;
359 throw std::runtime_error("unimplemented");
360}
361
David Tolnay2fe58c62020-03-06 16:23:09 -0800362extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800363 return std::unique_ptr<C>(new C{2020}).release();
364}
365
David Tolnay85db24862020-03-06 16:24:41 -0800366extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
367 return std::unique_ptr<std::string>(new std::string("2020")).release();
368}
369
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700370rust::String C::cOverloadedMethod(int32_t x) const {
371 return rust::String(std::to_string(x));
372}
373
374rust::String C::cOverloadedMethod(rust::Str x) const {
375 return rust::String(std::string(x));
376}
377
378rust::String cOverloadedFunction(int x) {
379 return rust::String(std::to_string(x));
380}
381
382rust::String cOverloadedFunction(rust::Str x) {
383 return rust::String(std::string(x));
384}
385
Adrian Taylor121cca42020-10-10 15:32:00 -0700386void c_take_trivial_ptr(std::unique_ptr<D> d) {
387 if (d->d == 30) {
388 cxx_test_suite_set_correct();
389 }
390}
391
392void c_take_trivial_ref(const D& d) {
393 if (d.d == 30) {
394 cxx_test_suite_set_correct();
395 }
396}
397void c_take_trivial(D d) {
398 if (d.d == 30) {
399 cxx_test_suite_set_correct();
400 }
401}
402
403void c_take_opaque_ptr(std::unique_ptr<E> e) {
404 if (e->e == 40) {
405 cxx_test_suite_set_correct();
406 }
407}
408
409void c_take_opaque_ref(const E& e) {
410 if (e.e == 40 && e.e_str == "hello") {
411 cxx_test_suite_set_correct();
412 }
413}
414
415std::unique_ptr<D> c_return_trivial_ptr() {
416 auto d = std::unique_ptr<D>(new D());
417 d->d = 30;
418 return d;
419}
420
421D c_return_trivial() {
422 D d;
423 d.d = 30;
424 return d;
425}
426
427std::unique_ptr<E> c_return_opaque_ptr() {
428 auto e = std::unique_ptr<E>(new E());
429 e->e = 40;
430 e->e_str = std::string("hello");
431 return e;
432}
433
David Tolnayf306da42020-02-22 19:55:43 -0800434extern "C" const char *cxx_run_test() noexcept {
435#define STRINGIFY(x) #x
436#define TOSTRING(x) STRINGIFY(x)
437#define ASSERT(x) \
438 do { \
439 if (!(x)) { \
440 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
441 } \
442 } while (false)
443
444 ASSERT(r_return_primitive() == 2020);
445 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800446 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800447 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800448 ASSERT(r_return_ref(Shared{2020}) == 2020);
449 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
450 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800451 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700452 ASSERT(r_return_identity(2020) == 2020);
453 ASSERT(r_return_sum(2020, 1) == 2021);
Joel Galensonc03402a2020-04-23 17:31:09 -0700454 ASSERT(r_return_enum(0) == Enum::AVal);
455 ASSERT(r_return_enum(1) == Enum::BVal);
456 ASSERT(r_return_enum(2021) == Enum::CVal);
David Tolnayf306da42020-02-22 19:55:43 -0800457
458 r_take_primitive(2020);
459 r_take_shared(Shared{2020});
460 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
461 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800462 r_take_str(rust::Str("2020"));
David Tolnay4037de22020-04-30 19:51:04 -0700463 r_take_sliceu8(rust::Slice<uint8_t>(
464 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
David Tolnay40226ab2020-03-03 00:05:35 -0800465 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800466 r_take_unique_ptr_string(
467 std::unique_ptr<std::string>(new std::string("2020")));
David Tolnay5df0a712020-09-24 15:58:28 -0400468 r_take_ref_vector(std::vector<uint8_t>{20, 2, 0});
David Tolnay80631e92020-09-24 16:07:30 -0400469 std::vector<uint64_t> empty_vector;
470 r_take_ref_empty_vector(empty_vector);
471 empty_vector.reserve(10);
472 r_take_ref_empty_vector(empty_vector);
Joel Galensonc03402a2020-04-23 17:31:09 -0700473 r_take_enum(Enum::AVal);
David Tolnayf306da42020-02-22 19:55:43 -0800474
David Tolnayb6c5ea72020-03-16 13:36:28 -0700475 ASSERT(r_try_return_primitive() == 2020);
476 try {
477 r_fail_return_primitive();
478 ASSERT(false);
479 } catch (const rust::Error &e) {
480 ASSERT(std::strcmp(e.what(), "rust error") == 0);
481 }
482
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700483 auto r2 = r_return_r2(2020);
484 ASSERT(r2->get() == 2020);
485 ASSERT(r2->set(2021) == 2021);
486 ASSERT(r2->get() == 2021);
487 ASSERT(r2->set(2020) == 2020);
488 ASSERT(r2->get() == 2020);
489
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700490 ASSERT(std::string(rAliasedFunction(2020)) == "2020");
491
David Tolnayf306da42020-02-22 19:55:43 -0800492 cxx_test_suite_set_correct();
493 return nullptr;
494}
495
David Tolnay97c72102020-01-25 16:49:00 -0800496} // namespace tests