blob: 4ab407b9abc68d34fd395974c9d1e30e3ea7e4db [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 Tolnayb8715772020-01-28 00:54:05 -080038size_t c_return_primitive() { return 2020; }
David Tolnayad5b8af2020-01-26 16:59:13 -080039
David Tolnayb8715772020-01-28 00:54:05 -080040Shared c_return_shared() { return Shared{2020}; }
David Tolnayad5b8af2020-01-26 16:59:13 -080041
David Tolnaybe13d8a2020-03-06 15:45:39 -080042rust::Box<R> c_return_box() {
43 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
44}
45
David Tolnayad5b8af2020-01-26 16:59:13 -080046std::unique_ptr<C> c_return_unique_ptr() {
47 return std::unique_ptr<C>(new C{2020});
48}
49
David Tolnayb8715772020-01-28 00:54:05 -080050const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080051
David Tolnay750755e2020-03-01 13:04:08 -080052rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080053 (void)shared;
54 return "2020";
55}
56
David Tolnayeb952ba2020-04-14 15:02:24 -070057rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070058 (void)shared;
David Tolnay4037de22020-04-30 19:51:04 -070059 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA),
60 sizeof(SLICE_DATA));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070061}
62
David Tolnay750755e2020-03-01 13:04:08 -080063rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080064
65std::unique_ptr<std::string> c_return_unique_ptr_string() {
66 return std::unique_ptr<std::string>(new std::string("2020"));
67}
68
Myron Ahneba35cf2020-02-05 19:41:51 +070069std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
David Tolnay85db5a02020-04-25 13:17:27 -070070 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
71 vec->push_back(86);
72 vec->push_back(75);
73 vec->push_back(30);
74 vec->push_back(9);
75 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070076}
77
78std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
David Tolnay85db5a02020-04-25 13:17:27 -070079 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
80 vec->push_back(86.0);
81 vec->push_back(75.0);
82 vec->push_back(30.0);
83 vec->push_back(9.5);
84 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070085}
86
87std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
David Tolnay85db5a02020-04-25 13:17:27 -070088 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
89 vec->push_back(Shared{1010});
90 vec->push_back(Shared{1011});
91 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070092}
93
David Tolnay1bcc9fe2020-04-25 13:51:07 -070094std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
95 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
96}
97
David Tolnayde5340e2020-04-25 14:03:21 -070098const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
99 return c.get_v();
100}
101
David Tolnayb41e74c2020-04-25 15:06:18 -0700102rust::Vec<uint8_t> c_return_rust_vec() {
103 throw std::runtime_error("unimplemented");
104}
105
David Tolnay77989692020-04-25 15:57:47 -0700106const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
107 (void)c;
108 throw std::runtime_error("unimplemented");
109}
110
David Tolnay378ca502020-04-27 16:47:55 -0700111size_t c_return_identity(size_t n) { return n; }
Joel Galensonba676072020-04-27 15:55:45 -0700112
David Tolnay378ca502020-04-27 16:47:55 -0700113size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
Joel Galensonba676072020-04-27 15:55:45 -0700114
David Tolnayf6a89f22020-05-10 23:39:27 -0700115Enum c_return_enum(uint16_t n) {
116 if (n <= static_cast<uint16_t>(Enum::AVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700117 return Enum::AVal;
David Tolnayf6a89f22020-05-10 23:39:27 -0700118 } else if (n <= static_cast<uint16_t>(Enum::BVal)) {
Joel Galensonc03402a2020-04-23 17:31:09 -0700119 return Enum::BVal;
120 } else {
121 return Enum::CVal;
122 }
123}
124
David Tolnay3fd7f562020-01-26 17:47:11 -0800125void c_take_primitive(size_t n) {
126 if (n == 2020) {
127 cxx_test_suite_set_correct();
128 }
129}
David Tolnayad5b8af2020-01-26 16:59:13 -0800130
David Tolnay3fd7f562020-01-26 17:47:11 -0800131void c_take_shared(Shared shared) {
132 if (shared.z == 2020) {
133 cxx_test_suite_set_correct();
134 }
135}
David Tolnayad5b8af2020-01-26 16:59:13 -0800136
David Tolnay750755e2020-03-01 13:04:08 -0800137void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -0800138 if (cxx_test_suite_r_is_correct(&*r)) {
139 cxx_test_suite_set_correct();
140 }
David Tolnay3fd7f562020-01-26 17:47:11 -0800141}
David Tolnayad5b8af2020-01-26 16:59:13 -0800142
David Tolnay3fd7f562020-01-26 17:47:11 -0800143void c_take_unique_ptr(std::unique_ptr<C> c) {
144 if (c->get() == 2020) {
145 cxx_test_suite_set_correct();
146 }
147}
David Tolnayad5b8af2020-01-26 16:59:13 -0800148
David Tolnaya7d00e82020-03-06 15:50:14 -0800149void c_take_ref_r(const R &r) {
150 if (cxx_test_suite_r_is_correct(&r)) {
151 cxx_test_suite_set_correct();
152 }
153}
David Tolnayad5b8af2020-01-26 16:59:13 -0800154
David Tolnay3fd7f562020-01-26 17:47:11 -0800155void c_take_ref_c(const C &c) {
156 if (c.get() == 2020) {
157 cxx_test_suite_set_correct();
158 }
159}
David Tolnayad5b8af2020-01-26 16:59:13 -0800160
David Tolnay750755e2020-03-01 13:04:08 -0800161void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800162 if (std::string(s) == "2020") {
163 cxx_test_suite_set_correct();
164 }
165}
David Tolnayad5b8af2020-01-26 16:59:13 -0800166
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700167void c_take_sliceu8(rust::Slice<uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700168 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
169 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700170 cxx_test_suite_set_correct();
171 }
172}
173
David Tolnay750755e2020-03-01 13:04:08 -0800174void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800175 if (std::string(s) == "2020") {
176 cxx_test_suite_set_correct();
177 }
178}
David Tolnayad5b8af2020-01-26 16:59:13 -0800179
David Tolnay3fd7f562020-01-26 17:47:11 -0800180void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
181 if (*s == "2020") {
182 cxx_test_suite_set_correct();
183 }
184}
David Tolnayad5b8af2020-01-26 16:59:13 -0800185
Myron Ahneba35cf2020-02-05 19:41:51 +0700186void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
187 if (v->size() == 4) {
188 cxx_test_suite_set_correct();
189 }
190}
191
192void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
193 if (v->size() == 4) {
194 cxx_test_suite_set_correct();
195 }
196}
197
198void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
199 if (v->size() == 2) {
200 cxx_test_suite_set_correct();
201 }
202}
203
David Tolnay2244d1f2020-04-25 13:58:18 -0700204void c_take_ref_vector(const std::vector<uint8_t> &v) {
205 if (v.size() == 4) {
206 cxx_test_suite_set_correct();
207 }
208}
209
David Tolnayd2ce8a92020-04-25 16:16:45 -0700210void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
Myron Ahneba35cf2020-02-05 19:41:51 +0700211
David Tolnay61adf422020-08-26 20:51:27 -0700212void c_take_rust_vec_index(rust::Vec<uint8_t> v) {
213 try {
214 v.at(100);
215 } catch (const std::out_of_range &ex) {
216 std::string expected = "Vec";
217 if (ex.what() == expected) {
218 cxx_test_suite_set_correct();
219 }
220 }
221}
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700222
David Tolnayd2ce8a92020-04-25 16:16:45 -0700223void c_take_rust_vec_shared(rust::Vec<Shared> v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700224 uint32_t sum = 0;
David Tolnayc87c2152020-04-24 17:07:41 -0700225 for (auto i : v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700226 sum += i.z;
227 }
228 if (sum == 2021) {
229 cxx_test_suite_set_correct();
230 }
231}
232
myronahnda9be502020-04-29 05:47:23 +0700233void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
234 // Exercise requirements of ForwardIterator
235 // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
236 uint32_t sum = 0;
237 for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
238 sum += it->z;
239 }
240 if (sum == 2021) {
241 cxx_test_suite_set_correct();
242 }
243}
244
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700245void c_take_rust_vec_shared_index(rust::Vec<Shared> v) {
246 if (v[0].z == 1010 &&
247 v.at(0).z == 1010 &&
248 v.front().z == 1010 &&
249 v[1].z == 1011 &&
250 v.at(1).z == 1011 &&
251 v.back().z == 1011) {
252 cxx_test_suite_set_correct();
253 }
254}
255
David Tolnayd2ce8a92020-04-25 16:16:45 -0700256void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
257 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
258 if (sum == 200) {
259 cxx_test_suite_set_correct();
260 }
261}
262
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700263void c_take_ref_rust_vec_index(const rust::Vec<uint8_t> &v) {
264 if (v[0] == 86 &&
265 v.at(0) == 86 &&
266 v.front() == 86 &&
267 v[1] == 75 &&
268 v.at(1) == 75 &&
269 v[3] == 9 &&
270 v.at(3) == 9 &&
271 v.back() == 9) {
272 cxx_test_suite_set_correct();
273 }
274}
275
myronahnda9be502020-04-29 05:47:23 +0700276void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
277 // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
278 // requirements for std::iterator_traits.
279 // https://en.cppreference.com/w/cpp/iterator/iterator_traits
David Tolnayf5aeea22020-04-30 19:34:51 -0700280 std::vector<uint8_t> stdv;
281 std::copy(v.begin(), v.end(), std::back_inserter(stdv));
282 uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
myronahnda9be502020-04-29 05:47:23 +0700283 if (sum == 200) {
284 cxx_test_suite_set_correct();
285 }
286}
287
David Tolnay0c8c0f22020-07-21 17:57:46 -0700288/*
289// https://github.com/dtolnay/cxx/issues/232
David Tolnay75dca2e2020-03-25 20:17:52 -0700290void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
291 callback("2020");
292}
David Tolnay0c8c0f22020-07-21 17:57:46 -0700293*/
David Tolnay75dca2e2020-03-25 20:17:52 -0700294
Joel Galensonc03402a2020-04-23 17:31:09 -0700295void c_take_enum(Enum e) {
296 if (e == Enum::AVal) {
297 cxx_test_suite_set_correct();
298 }
299}
300
David Tolnayebef4a22020-03-17 15:33:47 -0700301void c_try_return_void() {}
302
303size_t c_try_return_primitive() { return 2020; }
304
305size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
306
David Tolnay99642622020-03-25 13:07:35 -0700307rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700308
David Tolnay99642622020-03-25 13:07:35 -0700309const rust::String &c_try_return_ref(const rust::String &s) { return s; }
310
311rust::Str c_try_return_str(rust::Str s) { return s; }
312
Adrian Taylorec9430e2020-04-14 16:09:58 -0700313rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700314
David Tolnay99642622020-03-25 13:07:35 -0700315rust::String c_try_return_rust_string() { return c_return_rust_string(); }
316
317std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
318 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700319}
320
David Tolnay8b9d1762020-04-25 16:05:46 -0700321rust::Vec<uint8_t> c_try_return_rust_vec() {
322 throw std::runtime_error("unimplemented");
323}
324
David Tolnay77989692020-04-25 15:57:47 -0700325const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
326 (void)c;
327 throw std::runtime_error("unimplemented");
328}
329
David Tolnay2fe58c62020-03-06 16:23:09 -0800330extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800331 return std::unique_ptr<C>(new C{2020}).release();
332}
333
David Tolnay85db24862020-03-06 16:24:41 -0800334extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
335 return std::unique_ptr<std::string>(new std::string("2020")).release();
336}
337
David Tolnayf306da42020-02-22 19:55:43 -0800338extern "C" const char *cxx_run_test() noexcept {
339#define STRINGIFY(x) #x
340#define TOSTRING(x) STRINGIFY(x)
341#define ASSERT(x) \
342 do { \
343 if (!(x)) { \
344 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
345 } \
346 } while (false)
347
348 ASSERT(r_return_primitive() == 2020);
349 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800350 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800351 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800352 ASSERT(r_return_ref(Shared{2020}) == 2020);
353 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
354 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800355 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700356 ASSERT(r_return_identity(2020) == 2020);
357 ASSERT(r_return_sum(2020, 1) == 2021);
Joel Galensonc03402a2020-04-23 17:31:09 -0700358 ASSERT(r_return_enum(0) == Enum::AVal);
359 ASSERT(r_return_enum(1) == Enum::BVal);
360 ASSERT(r_return_enum(2021) == Enum::CVal);
David Tolnayf306da42020-02-22 19:55:43 -0800361
362 r_take_primitive(2020);
363 r_take_shared(Shared{2020});
364 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
365 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800366 r_take_str(rust::Str("2020"));
David Tolnay4037de22020-04-30 19:51:04 -0700367 r_take_sliceu8(rust::Slice<uint8_t>(
368 reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
David Tolnay40226ab2020-03-03 00:05:35 -0800369 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800370 r_take_unique_ptr_string(
371 std::unique_ptr<std::string>(new std::string("2020")));
Joel Galensonc03402a2020-04-23 17:31:09 -0700372 r_take_enum(Enum::AVal);
David Tolnayf306da42020-02-22 19:55:43 -0800373
David Tolnayb6c5ea72020-03-16 13:36:28 -0700374 ASSERT(r_try_return_primitive() == 2020);
375 try {
376 r_fail_return_primitive();
377 ASSERT(false);
378 } catch (const rust::Error &e) {
379 ASSERT(std::strcmp(e.what(), "rust error") == 0);
380 }
381
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700382 auto r2 = r_return_r2(2020);
383 ASSERT(r2->get() == 2020);
384 ASSERT(r2->set(2021) == 2021);
385 ASSERT(r2->get() == 2021);
386 ASSERT(r2->set(2020) == 2020);
387 ASSERT(r2->get() == 2020);
388
David Tolnayf306da42020-02-22 19:55:43 -0800389 cxx_test_suite_set_correct();
390 return nullptr;
391}
392
David Tolnay97c72102020-01-25 16:49:00 -0800393} // namespace tests