blob: d70d5a0c6b5d8182d0d038baec2ab4927b74ec01 [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 Tolnay37dd7e12020-04-25 12:51:59 -07004#include <numeric>
David Tolnayebef4a22020-03-17 15:33:47 -07005#include <stdexcept>
David Tolnay97c72102020-01-25 16:49:00 -08006
David Tolnay2fe58c62020-03-06 16:23:09 -08007extern "C" void cxx_test_suite_set_correct() noexcept;
8extern "C" tests::R *cxx_test_suite_get_box() noexcept;
9extern "C" bool cxx_test_suite_r_is_correct(const tests::R *) noexcept;
David Tolnay3fd7f562020-01-26 17:47:11 -080010
David Tolnay97c72102020-01-25 16:49:00 -080011namespace tests {
12
David Tolnay4037de22020-04-30 19:51:04 -070013static constexpr char SLICE_DATA[] = "2020";
Adrian Taylorf5dd5522020-04-13 16:50:14 -070014
David Tolnayad5b8af2020-01-26 16:59:13 -080015C::C(size_t n) : n(n) {}
16
David Tolnay3fd7f562020-01-26 17:47:11 -080017size_t C::get() const { return this->n; }
18
Joel Galensone1e969d2020-04-21 12:50:20 -070019size_t C::get2() const { return this->n; }
20
Joel Galenson3d4f6122020-04-07 15:54:05 -070021size_t C::set(size_t n) {
22 this->n = n;
23 return this->n;
24}
25
Joel Galensone1e969d2020-04-21 12:50:20 -070026size_t C::set2(size_t n) {
27 this->n = n;
28 return this->n;
29}
30
myronahne3b78ea2020-05-23 01:08:13 +070031size_t C::set_succeed(size_t n) { return this->set2(n); }
32
33size_t C::get_fail() { throw std::runtime_error("unimplemented"); }
34
David Tolnayde5340e2020-04-25 14:03:21 -070035const std::vector<uint8_t> &C::get_v() const { return this->v; }
36
David Tolnayb8715772020-01-28 00:54:05 -080037size_t c_return_primitive() { return 2020; }
David Tolnayad5b8af2020-01-26 16:59:13 -080038
David Tolnayb8715772020-01-28 00:54:05 -080039Shared c_return_shared() { return Shared{2020}; }
David Tolnayad5b8af2020-01-26 16:59:13 -080040
David Tolnaybe13d8a2020-03-06 15:45:39 -080041rust::Box<R> c_return_box() {
42 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
43}
44
David Tolnayad5b8af2020-01-26 16:59:13 -080045std::unique_ptr<C> c_return_unique_ptr() {
46 return std::unique_ptr<C>(new C{2020});
47}
48
David Tolnayb8715772020-01-28 00:54:05 -080049const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080050
David Tolnay750755e2020-03-01 13:04:08 -080051rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080052 (void)shared;
53 return "2020";
54}
55
David Tolnayeb952ba2020-04-14 15:02:24 -070056rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070057 (void)shared;
David Tolnay4037de22020-04-30 19:51:04 -070058 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA),
59 sizeof(SLICE_DATA));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070060}
61
David Tolnay750755e2020-03-01 13:04:08 -080062rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080063
64std::unique_ptr<std::string> c_return_unique_ptr_string() {
65 return std::unique_ptr<std::string>(new std::string("2020"));
66}
67
Myron Ahneba35cf2020-02-05 19:41:51 +070068std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
David Tolnay85db5a02020-04-25 13:17:27 -070069 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
70 vec->push_back(86);
71 vec->push_back(75);
72 vec->push_back(30);
73 vec->push_back(9);
74 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070075}
76
77std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
David Tolnay85db5a02020-04-25 13:17:27 -070078 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
79 vec->push_back(86.0);
80 vec->push_back(75.0);
81 vec->push_back(30.0);
82 vec->push_back(9.5);
83 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070084}
85
86std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
David Tolnay85db5a02020-04-25 13:17:27 -070087 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
88 vec->push_back(Shared{1010});
89 vec->push_back(Shared{1011});
90 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070091}
92
David Tolnay1bcc9fe2020-04-25 13:51:07 -070093std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
94 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
95}
96
David Tolnayde5340e2020-04-25 14:03:21 -070097const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
98 return c.get_v();
99}
100
David Tolnayb41e74c2020-04-25 15:06:18 -0700101rust::Vec<uint8_t> c_return_rust_vec() {
102 throw std::runtime_error("unimplemented");
103}
104
David Tolnay77989692020-04-25 15:57:47 -0700105const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
106 (void)c;
107 throw std::runtime_error("unimplemented");
108}
109
David Tolnay378ca502020-04-27 16:47:55 -0700110size_t c_return_identity(size_t n) { return n; }
Joel Galensonba676072020-04-27 15:55:45 -0700111
David Tolnay378ca502020-04-27 16:47:55 -0700112size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
Joel Galensonba676072020-04-27 15:55:45 -0700113
David Tolnayf6a89f22020-05-10 23:39:27 -0700114Enum c_return_enum(uint16_t n) {
115 if (n <= static_cast<uint16_t>(Enum::AVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700116 return Enum::AVal;
David Tolnayf6a89f22020-05-10 23:39:27 -0700117 } else if (n <= static_cast<uint16_t>(Enum::BVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700118 return Enum::BVal;
119 } else {
120 return Enum::CVal;
121 }
122}
123
David Tolnay3fd7f562020-01-26 17:47:11 -0800124void c_take_primitive(size_t n) {
125 if (n == 2020) {
126 cxx_test_suite_set_correct();
127 }
128}
David Tolnayad5b8af2020-01-26 16:59:13 -0800129
David Tolnay3fd7f562020-01-26 17:47:11 -0800130void c_take_shared(Shared shared) {
131 if (shared.z == 2020) {
132 cxx_test_suite_set_correct();
133 }
134}
David Tolnayad5b8af2020-01-26 16:59:13 -0800135
David Tolnay750755e2020-03-01 13:04:08 -0800136void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -0800137 if (cxx_test_suite_r_is_correct(&*r)) {
138 cxx_test_suite_set_correct();
139 }
David Tolnay3fd7f562020-01-26 17:47:11 -0800140}
David Tolnayad5b8af2020-01-26 16:59:13 -0800141
David Tolnay3fd7f562020-01-26 17:47:11 -0800142void c_take_unique_ptr(std::unique_ptr<C> c) {
143 if (c->get() == 2020) {
144 cxx_test_suite_set_correct();
145 }
146}
David Tolnayad5b8af2020-01-26 16:59:13 -0800147
David Tolnaya7d00e82020-03-06 15:50:14 -0800148void c_take_ref_r(const R &r) {
149 if (cxx_test_suite_r_is_correct(&r)) {
150 cxx_test_suite_set_correct();
151 }
152}
David Tolnayad5b8af2020-01-26 16:59:13 -0800153
David Tolnay3fd7f562020-01-26 17:47:11 -0800154void c_take_ref_c(const C &c) {
155 if (c.get() == 2020) {
156 cxx_test_suite_set_correct();
157 }
158}
David Tolnayad5b8af2020-01-26 16:59:13 -0800159
David Tolnay750755e2020-03-01 13:04:08 -0800160void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800161 if (std::string(s) == "2020") {
162 cxx_test_suite_set_correct();
163 }
164}
David Tolnayad5b8af2020-01-26 16:59:13 -0800165
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700166void c_take_sliceu8(rust::Slice<uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700167 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
168 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700169 cxx_test_suite_set_correct();
170 }
171}
172
David Tolnay750755e2020-03-01 13:04:08 -0800173void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800174 if (std::string(s) == "2020") {
175 cxx_test_suite_set_correct();
176 }
177}
David Tolnayad5b8af2020-01-26 16:59:13 -0800178
David Tolnay3fd7f562020-01-26 17:47:11 -0800179void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
180 if (*s == "2020") {
181 cxx_test_suite_set_correct();
182 }
183}
David Tolnayad5b8af2020-01-26 16:59:13 -0800184
Myron Ahneba35cf2020-02-05 19:41:51 +0700185void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
186 if (v->size() == 4) {
187 cxx_test_suite_set_correct();
188 }
189}
190
191void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
192 if (v->size() == 4) {
193 cxx_test_suite_set_correct();
194 }
195}
196
197void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
198 if (v->size() == 2) {
199 cxx_test_suite_set_correct();
200 }
201}
202
David Tolnay2244d1f2020-04-25 13:58:18 -0700203void c_take_ref_vector(const std::vector<uint8_t> &v) {
204 if (v.size() == 4) {
205 cxx_test_suite_set_correct();
206 }
207}
208
David Tolnayd2ce8a92020-04-25 16:16:45 -0700209void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
Myron Ahneba35cf2020-02-05 19:41:51 +0700210
David Tolnayd2ce8a92020-04-25 16:16:45 -0700211void c_take_rust_vec_shared(rust::Vec<Shared> v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700212 uint32_t sum = 0;
David Tolnayc87c2152020-04-24 17:07:41 -0700213 for (auto i : v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700214 sum += i.z;
215 }
216 if (sum == 2021) {
217 cxx_test_suite_set_correct();
218 }
219}
220
myronahnda9be502020-04-29 05:47:23 +0700221void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
222 // Exercise requirements of ForwardIterator
223 // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
224 uint32_t sum = 0;
225 for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
226 sum += it->z;
227 }
228 if (sum == 2021) {
229 cxx_test_suite_set_correct();
230 }
231}
232
David Tolnayd2ce8a92020-04-25 16:16:45 -0700233void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
234 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
235 if (sum == 200) {
236 cxx_test_suite_set_correct();
237 }
238}
239
myronahnda9be502020-04-29 05:47:23 +0700240void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
241 // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
242 // requirements for std::iterator_traits.
243 // https://en.cppreference.com/w/cpp/iterator/iterator_traits
David Tolnayf5aeea22020-04-30 19:34:51 -0700244 std::vector<uint8_t> stdv;
245 std::copy(v.begin(), v.end(), std::back_inserter(stdv));
246 uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
myronahnda9be502020-04-29 05:47:23 +0700247 if (sum == 200) {
248 cxx_test_suite_set_correct();
249 }
250}
251
David Tolnay0c8c0f22020-07-21 17:57:46 -0700252/*
253// https://github.com/dtolnay/cxx/issues/232
David Tolnay75dca2e2020-03-25 20:17:52 -0700254void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
255 callback("2020");
256}
David Tolnay0c8c0f22020-07-21 17:57:46 -0700257*/
David Tolnay75dca2e2020-03-25 20:17:52 -0700258
Joel Galensonc03402a2020-04-23 17:31:09 -0700259void c_take_enum(Enum e) {
260 if (e == Enum::AVal) {
261 cxx_test_suite_set_correct();
262 }
263}
264
David Tolnayebef4a22020-03-17 15:33:47 -0700265void c_try_return_void() {}
266
267size_t c_try_return_primitive() { return 2020; }
268
269size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
270
David Tolnay99642622020-03-25 13:07:35 -0700271rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700272
David Tolnay99642622020-03-25 13:07:35 -0700273const rust::String &c_try_return_ref(const rust::String &s) { return s; }
274
275rust::Str c_try_return_str(rust::Str s) { return s; }
276
Adrian Taylorec9430e2020-04-14 16:09:58 -0700277rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700278
David Tolnay99642622020-03-25 13:07:35 -0700279rust::String c_try_return_rust_string() { return c_return_rust_string(); }
280
281std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
282 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700283}
284
David Tolnay8b9d1762020-04-25 16:05:46 -0700285rust::Vec<uint8_t> c_try_return_rust_vec() {
286 throw std::runtime_error("unimplemented");
287}
288
David Tolnay77989692020-04-25 15:57:47 -0700289const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
290 (void)c;
291 throw std::runtime_error("unimplemented");
292}
293
David Tolnay2fe58c62020-03-06 16:23:09 -0800294extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800295 return std::unique_ptr<C>(new C{2020}).release();
296}
297
David Tolnay85db24862020-03-06 16:24:41 -0800298extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
299 return std::unique_ptr<std::string>(new std::string("2020")).release();
300}
301
David Tolnayf306da42020-02-22 19:55:43 -0800302extern "C" const char *cxx_run_test() noexcept {
303#define STRINGIFY(x) #x
304#define TOSTRING(x) STRINGIFY(x)
305#define ASSERT(x) \
306 do { \
307 if (!(x)) { \
308 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
309 } \
310 } while (false)
311
312 ASSERT(r_return_primitive() == 2020);
313 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800314 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800315 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800316 ASSERT(r_return_ref(Shared{2020}) == 2020);
317 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
318 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800319 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700320 ASSERT(r_return_identity(2020) == 2020);
321 ASSERT(r_return_sum(2020, 1) == 2021);
Joel Galensonc03402a2020-04-23 17:31:09 -0700322 ASSERT(r_return_enum(0) == Enum::AVal);
323 ASSERT(r_return_enum(1) == Enum::BVal);
324 ASSERT(r_return_enum(2021) == Enum::CVal);
David Tolnayf306da42020-02-22 19:55:43 -0800325
326 r_take_primitive(2020);
327 r_take_shared(Shared{2020});
328 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
329 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800330 r_take_str(rust::Str("2020"));
David Tolnay4037de22020-04-30 19:51:04 -0700331 r_take_sliceu8(rust::Slice<uint8_t>(
332 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
David Tolnay40226ab2020-03-03 00:05:35 -0800333 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800334 r_take_unique_ptr_string(
335 std::unique_ptr<std::string>(new std::string("2020")));
Joel Galensonc03402a2020-04-23 17:31:09 -0700336 r_take_enum(Enum::AVal);
David Tolnayf306da42020-02-22 19:55:43 -0800337
David Tolnayb6c5ea72020-03-16 13:36:28 -0700338 ASSERT(r_try_return_primitive() == 2020);
339 try {
340 r_fail_return_primitive();
341 ASSERT(false);
342 } catch (const rust::Error &e) {
343 ASSERT(std::strcmp(e.what(), "rust error") == 0);
344 }
345
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700346 auto r2 = r_return_r2(2020);
347 ASSERT(r2->get() == 2020);
348 ASSERT(r2->set(2021) == 2021);
349 ASSERT(r2->get() == 2021);
350 ASSERT(r2->set(2020) == 2020);
351 ASSERT(r2->get() == 2020);
352
David Tolnayf306da42020-02-22 19:55:43 -0800353 cxx_test_suite_set_correct();
354 return nullptr;
355}
356
David Tolnay97c72102020-01-25 16:49:00 -0800357} // namespace tests