blob: b45e8aeccfebe245d0776792a05477a93ee51694 [file] [log] [blame]
David Tolnay97c72102020-01-25 16:49:00 -08001#include "tests/ffi/tests.h"
David Tolnay1a2683a2020-03-17 19:09:29 -07002#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>
David Tolnay97c72102020-01-25 16:49:00 -08007
David Tolnay2fe58c62020-03-06 16:23:09 -08008extern "C" void cxx_test_suite_set_correct() noexcept;
9extern "C" tests::R *cxx_test_suite_get_box() noexcept;
10extern "C" bool cxx_test_suite_r_is_correct(const tests::R *) noexcept;
David Tolnay3fd7f562020-01-26 17:47:11 -080011
David Tolnay97c72102020-01-25 16:49:00 -080012namespace tests {
13
David Tolnay4037de22020-04-30 19:51:04 -070014static constexpr char SLICE_DATA[] = "2020";
Adrian Taylorf5dd5522020-04-13 16:50:14 -070015
David Tolnayad5b8af2020-01-26 16:59:13 -080016C::C(size_t n) : n(n) {}
17
David Tolnay3fd7f562020-01-26 17:47:11 -080018size_t C::get() const { return this->n; }
19
Joel Galensone1e969d2020-04-21 12:50:20 -070020size_t C::get2() const { return this->n; }
21
Joel Galenson3d4f6122020-04-07 15:54:05 -070022size_t C::set(size_t n) {
23 this->n = n;
24 return this->n;
25}
26
Joel Galensone1e969d2020-04-21 12:50:20 -070027size_t C::set2(size_t n) {
28 this->n = n;
29 return this->n;
30}
31
myronahne3b78ea2020-05-23 01:08:13 +070032size_t C::set_succeed(size_t n) { return this->set2(n); }
33
34size_t C::get_fail() { throw std::runtime_error("unimplemented"); }
35
David Tolnayde5340e2020-04-25 14:03:21 -070036const std::vector<uint8_t> &C::get_v() const { return this->v; }
37
David Tolnay18d93b62020-08-27 00:55:48 -070038std::vector<uint8_t> &C::get_v() { return this->v; }
39
David Tolnayb8715772020-01-28 00:54:05 -080040size_t c_return_primitive() { return 2020; }
David Tolnayad5b8af2020-01-26 16:59:13 -080041
David Tolnayb8715772020-01-28 00:54:05 -080042Shared c_return_shared() { return Shared{2020}; }
David Tolnayad5b8af2020-01-26 16:59:13 -080043
David Tolnaybe13d8a2020-03-06 15:45:39 -080044rust::Box<R> c_return_box() {
45 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
46}
47
David Tolnayad5b8af2020-01-26 16:59:13 -080048std::unique_ptr<C> c_return_unique_ptr() {
49 return std::unique_ptr<C>(new C{2020});
50}
51
David Tolnayb8715772020-01-28 00:54:05 -080052const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080053
David Tolnay18d93b62020-08-27 00:55:48 -070054size_t &c_return_mut(Shared &shared) { return shared.z; }
55
David Tolnay750755e2020-03-01 13:04:08 -080056rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080057 (void)shared;
58 return "2020";
59}
60
David Tolnayeb952ba2020-04-14 15:02:24 -070061rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070062 (void)shared;
David Tolnay4037de22020-04-30 19:51:04 -070063 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA),
64 sizeof(SLICE_DATA));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070065}
66
David Tolnay750755e2020-03-01 13:04:08 -080067rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080068
69std::unique_ptr<std::string> c_return_unique_ptr_string() {
70 return std::unique_ptr<std::string>(new std::string("2020"));
71}
72
Myron Ahneba35cf2020-02-05 19:41:51 +070073std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
David Tolnay85db5a02020-04-25 13:17:27 -070074 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
75 vec->push_back(86);
76 vec->push_back(75);
77 vec->push_back(30);
78 vec->push_back(9);
79 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070080}
81
82std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
David Tolnay85db5a02020-04-25 13:17:27 -070083 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
84 vec->push_back(86.0);
85 vec->push_back(75.0);
86 vec->push_back(30.0);
87 vec->push_back(9.5);
88 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070089}
90
91std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
David Tolnay85db5a02020-04-25 13:17:27 -070092 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
93 vec->push_back(Shared{1010});
94 vec->push_back(Shared{1011});
95 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070096}
97
David Tolnay1bcc9fe2020-04-25 13:51:07 -070098std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
99 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
100}
101
David Tolnayde5340e2020-04-25 14:03:21 -0700102const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
103 return c.get_v();
104}
105
David Tolnay18d93b62020-08-27 00:55:48 -0700106std::vector<uint8_t> &c_return_mut_vector(C &c) { return c.get_v(); }
107
David Tolnayb41e74c2020-04-25 15:06:18 -0700108rust::Vec<uint8_t> c_return_rust_vec() {
109 throw std::runtime_error("unimplemented");
110}
111
David Tolnay77989692020-04-25 15:57:47 -0700112const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
113 (void)c;
114 throw std::runtime_error("unimplemented");
115}
116
David Tolnay18d93b62020-08-27 00:55:48 -0700117rust::Vec<uint8_t> &c_return_mut_rust_vec(C &c) {
118 (void)c;
119 throw std::runtime_error("unimplemented");
120}
121
David Tolnay33f56ad2020-08-27 17:06:35 -0700122rust::Vec<rust::String> c_return_rust_vec_string() {
123 throw std::runtime_error("unimplemented");
124}
125
David Tolnay378ca502020-04-27 16:47:55 -0700126size_t c_return_identity(size_t n) { return n; }
Joel Galensonba676072020-04-27 15:55:45 -0700127
David Tolnay378ca502020-04-27 16:47:55 -0700128size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
Joel Galensonba676072020-04-27 15:55:45 -0700129
David Tolnayf6a89f22020-05-10 23:39:27 -0700130Enum c_return_enum(uint16_t n) {
131 if (n <= static_cast<uint16_t>(Enum::AVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700132 return Enum::AVal;
David Tolnayf6a89f22020-05-10 23:39:27 -0700133 } else if (n <= static_cast<uint16_t>(Enum::BVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700134 return Enum::BVal;
135 } else {
136 return Enum::CVal;
137 }
138}
139
David Tolnay3fd7f562020-01-26 17:47:11 -0800140void c_take_primitive(size_t n) {
141 if (n == 2020) {
142 cxx_test_suite_set_correct();
143 }
144}
David Tolnayad5b8af2020-01-26 16:59:13 -0800145
David Tolnay3fd7f562020-01-26 17:47:11 -0800146void c_take_shared(Shared shared) {
147 if (shared.z == 2020) {
148 cxx_test_suite_set_correct();
149 }
150}
David Tolnayad5b8af2020-01-26 16:59:13 -0800151
David Tolnay750755e2020-03-01 13:04:08 -0800152void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -0800153 if (cxx_test_suite_r_is_correct(&*r)) {
154 cxx_test_suite_set_correct();
155 }
David Tolnay3fd7f562020-01-26 17:47:11 -0800156}
David Tolnayad5b8af2020-01-26 16:59:13 -0800157
David Tolnay3fd7f562020-01-26 17:47:11 -0800158void c_take_unique_ptr(std::unique_ptr<C> c) {
159 if (c->get() == 2020) {
160 cxx_test_suite_set_correct();
161 }
162}
David Tolnayad5b8af2020-01-26 16:59:13 -0800163
David Tolnaya7d00e82020-03-06 15:50:14 -0800164void c_take_ref_r(const R &r) {
165 if (cxx_test_suite_r_is_correct(&r)) {
166 cxx_test_suite_set_correct();
167 }
168}
David Tolnayad5b8af2020-01-26 16:59:13 -0800169
David Tolnay3fd7f562020-01-26 17:47:11 -0800170void c_take_ref_c(const C &c) {
171 if (c.get() == 2020) {
172 cxx_test_suite_set_correct();
173 }
174}
David Tolnayad5b8af2020-01-26 16:59:13 -0800175
David Tolnay750755e2020-03-01 13:04:08 -0800176void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800177 if (std::string(s) == "2020") {
178 cxx_test_suite_set_correct();
179 }
180}
David Tolnayad5b8af2020-01-26 16:59:13 -0800181
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700182void c_take_sliceu8(rust::Slice<uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700183 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
184 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700185 cxx_test_suite_set_correct();
186 }
187}
188
David Tolnay750755e2020-03-01 13:04:08 -0800189void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800190 if (std::string(s) == "2020") {
191 cxx_test_suite_set_correct();
192 }
193}
David Tolnayad5b8af2020-01-26 16:59:13 -0800194
David Tolnay3fd7f562020-01-26 17:47:11 -0800195void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
196 if (*s == "2020") {
197 cxx_test_suite_set_correct();
198 }
199}
David Tolnayad5b8af2020-01-26 16:59:13 -0800200
Myron Ahneba35cf2020-02-05 19:41:51 +0700201void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
202 if (v->size() == 4) {
203 cxx_test_suite_set_correct();
204 }
205}
206
207void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
208 if (v->size() == 4) {
209 cxx_test_suite_set_correct();
210 }
211}
212
213void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
214 if (v->size() == 2) {
215 cxx_test_suite_set_correct();
216 }
217}
218
David Tolnay2244d1f2020-04-25 13:58:18 -0700219void c_take_ref_vector(const std::vector<uint8_t> &v) {
220 if (v.size() == 4) {
221 cxx_test_suite_set_correct();
222 }
223}
224
David Tolnayd2ce8a92020-04-25 16:16:45 -0700225void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
Myron Ahneba35cf2020-02-05 19:41:51 +0700226
David Tolnay61adf422020-08-26 20:51:27 -0700227void c_take_rust_vec_index(rust::Vec<uint8_t> v) {
228 try {
229 v.at(100);
230 } catch (const std::out_of_range &ex) {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700231 std::string expected = "rust::Vec index out of range";
David Tolnay61adf422020-08-26 20:51:27 -0700232 if (ex.what() == expected) {
233 cxx_test_suite_set_correct();
234 }
235 }
236}
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700237
David Tolnayd2ce8a92020-04-25 16:16:45 -0700238void c_take_rust_vec_shared(rust::Vec<Shared> v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700239 uint32_t sum = 0;
David Tolnayc87c2152020-04-24 17:07:41 -0700240 for (auto i : v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700241 sum += i.z;
242 }
243 if (sum == 2021) {
244 cxx_test_suite_set_correct();
245 }
246}
247
David Tolnay33f56ad2020-08-27 17:06:35 -0700248void c_take_rust_vec_string(rust::Vec<rust::String> v) {
249 (void)v;
250 cxx_test_suite_set_correct();
251}
252
myronahnda9be502020-04-29 05:47:23 +0700253void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
254 // Exercise requirements of ForwardIterator
255 // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
256 uint32_t sum = 0;
257 for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
258 sum += it->z;
259 }
260 if (sum == 2021) {
261 cxx_test_suite_set_correct();
262 }
263}
264
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700265void c_take_rust_vec_shared_index(rust::Vec<Shared> v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700266 if (v[0].z == 1010 && v.at(0).z == 1010 && v.front().z == 1010 &&
267 v[1].z == 1011 && v.at(1).z == 1011 && v.back().z == 1011) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700268 cxx_test_suite_set_correct();
269 }
270}
271
David Tolnayd2ce8a92020-04-25 16:16:45 -0700272void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
273 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
274 if (sum == 200) {
275 cxx_test_suite_set_correct();
276 }
277}
278
David Tolnay33f56ad2020-08-27 17:06:35 -0700279void c_take_ref_rust_vec_string(const rust::Vec<rust::String> &v) {
280 (void)v;
281 cxx_test_suite_set_correct();
282}
283
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700284void c_take_ref_rust_vec_index(const rust::Vec<uint8_t> &v) {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700285 if (v[0] == 86 && v.at(0) == 86 && v.front() == 86 && v[1] == 75 &&
286 v.at(1) == 75 && v[3] == 9 && v.at(3) == 9 && v.back() == 9) {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700287 cxx_test_suite_set_correct();
288 }
289}
290
myronahnda9be502020-04-29 05:47:23 +0700291void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
292 // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
293 // requirements for std::iterator_traits.
294 // https://en.cppreference.com/w/cpp/iterator/iterator_traits
David Tolnayf5aeea22020-04-30 19:34:51 -0700295 std::vector<uint8_t> stdv;
296 std::copy(v.begin(), v.end(), std::back_inserter(stdv));
297 uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
myronahnda9be502020-04-29 05:47:23 +0700298 if (sum == 200) {
299 cxx_test_suite_set_correct();
300 }
301}
302
David Tolnay0c8c0f22020-07-21 17:57:46 -0700303/*
304// https://github.com/dtolnay/cxx/issues/232
David Tolnay75dca2e2020-03-25 20:17:52 -0700305void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
306 callback("2020");
307}
David Tolnay0c8c0f22020-07-21 17:57:46 -0700308*/
David Tolnay75dca2e2020-03-25 20:17:52 -0700309
Joel Galensonc03402a2020-04-23 17:31:09 -0700310void c_take_enum(Enum e) {
311 if (e == Enum::AVal) {
312 cxx_test_suite_set_correct();
313 }
314}
315
David Tolnayebef4a22020-03-17 15:33:47 -0700316void c_try_return_void() {}
317
318size_t c_try_return_primitive() { return 2020; }
319
320size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
321
David Tolnay99642622020-03-25 13:07:35 -0700322rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700323
David Tolnay99642622020-03-25 13:07:35 -0700324const rust::String &c_try_return_ref(const rust::String &s) { return s; }
325
326rust::Str c_try_return_str(rust::Str s) { return s; }
327
Adrian Taylorec9430e2020-04-14 16:09:58 -0700328rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700329
David Tolnay99642622020-03-25 13:07:35 -0700330rust::String c_try_return_rust_string() { return c_return_rust_string(); }
331
332std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
333 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700334}
335
David Tolnay8b9d1762020-04-25 16:05:46 -0700336rust::Vec<uint8_t> c_try_return_rust_vec() {
337 throw std::runtime_error("unimplemented");
338}
339
David Tolnay33f56ad2020-08-27 17:06:35 -0700340rust::Vec<rust::String> c_try_return_rust_vec_string() {
341 throw std::runtime_error("unimplemented");
342}
343
David Tolnay77989692020-04-25 15:57:47 -0700344const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
345 (void)c;
346 throw std::runtime_error("unimplemented");
347}
348
David Tolnay2fe58c62020-03-06 16:23:09 -0800349extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800350 return std::unique_ptr<C>(new C{2020}).release();
351}
352
David Tolnay85db24862020-03-06 16:24:41 -0800353extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
354 return std::unique_ptr<std::string>(new std::string("2020")).release();
355}
356
David Tolnayf306da42020-02-22 19:55:43 -0800357extern "C" const char *cxx_run_test() noexcept {
358#define STRINGIFY(x) #x
359#define TOSTRING(x) STRINGIFY(x)
360#define ASSERT(x) \
361 do { \
362 if (!(x)) { \
363 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
364 } \
365 } while (false)
366
367 ASSERT(r_return_primitive() == 2020);
368 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800369 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800370 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800371 ASSERT(r_return_ref(Shared{2020}) == 2020);
372 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
373 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800374 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700375 ASSERT(r_return_identity(2020) == 2020);
376 ASSERT(r_return_sum(2020, 1) == 2021);
Joel Galensonc03402a2020-04-23 17:31:09 -0700377 ASSERT(r_return_enum(0) == Enum::AVal);
378 ASSERT(r_return_enum(1) == Enum::BVal);
379 ASSERT(r_return_enum(2021) == Enum::CVal);
David Tolnayf306da42020-02-22 19:55:43 -0800380
381 r_take_primitive(2020);
382 r_take_shared(Shared{2020});
383 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
384 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800385 r_take_str(rust::Str("2020"));
David Tolnay4037de22020-04-30 19:51:04 -0700386 r_take_sliceu8(rust::Slice<uint8_t>(
387 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
David Tolnay40226ab2020-03-03 00:05:35 -0800388 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800389 r_take_unique_ptr_string(
390 std::unique_ptr<std::string>(new std::string("2020")));
Joel Galensonc03402a2020-04-23 17:31:09 -0700391 r_take_enum(Enum::AVal);
David Tolnayf306da42020-02-22 19:55:43 -0800392
David Tolnayb6c5ea72020-03-16 13:36:28 -0700393 ASSERT(r_try_return_primitive() == 2020);
394 try {
395 r_fail_return_primitive();
396 ASSERT(false);
397 } catch (const rust::Error &e) {
398 ASSERT(std::strcmp(e.what(), "rust error") == 0);
399 }
400
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700401 auto r2 = r_return_r2(2020);
402 ASSERT(r2->get() == 2020);
403 ASSERT(r2->set(2021) == 2021);
404 ASSERT(r2->get() == 2021);
405 ASSERT(r2->set(2020) == 2020);
406 ASSERT(r2->get() == 2020);
407
David Tolnayf306da42020-02-22 19:55:43 -0800408 cxx_test_suite_set_correct();
409 return nullptr;
410}
411
David Tolnay97c72102020-01-25 16:49:00 -0800412} // namespace tests