blob: 2b5431fa6b5f745a7d35e2ac7ed4b633b57990c7 [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 Tolnayeb952ba2020-04-14 15:02:24 -070013const 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
David Tolnayde5340e2020-04-25 14:03:21 -070031const std::vector<uint8_t> &C::get_v() const { return this->v; }
32
David Tolnayb8715772020-01-28 00:54:05 -080033size_t c_return_primitive() { return 2020; }
David Tolnayad5b8af2020-01-26 16:59:13 -080034
David Tolnayb8715772020-01-28 00:54:05 -080035Shared c_return_shared() { return Shared{2020}; }
David Tolnayad5b8af2020-01-26 16:59:13 -080036
David Tolnaybe13d8a2020-03-06 15:45:39 -080037rust::Box<R> c_return_box() {
38 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
39}
40
David Tolnayad5b8af2020-01-26 16:59:13 -080041std::unique_ptr<C> c_return_unique_ptr() {
42 return std::unique_ptr<C>(new C{2020});
43}
44
David Tolnayb8715772020-01-28 00:54:05 -080045const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080046
David Tolnay750755e2020-03-01 13:04:08 -080047rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080048 (void)shared;
49 return "2020";
50}
51
David Tolnayeb952ba2020-04-14 15:02:24 -070052rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070053 (void)shared;
David Tolnay633b1f52020-04-14 16:33:14 -070054 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA), 5);
Adrian Taylorf5dd5522020-04-13 16:50:14 -070055}
56
David Tolnay750755e2020-03-01 13:04:08 -080057rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080058
59std::unique_ptr<std::string> c_return_unique_ptr_string() {
60 return std::unique_ptr<std::string>(new std::string("2020"));
61}
62
Myron Ahneba35cf2020-02-05 19:41:51 +070063std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
David Tolnay85db5a02020-04-25 13:17:27 -070064 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
65 vec->push_back(86);
66 vec->push_back(75);
67 vec->push_back(30);
68 vec->push_back(9);
69 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070070}
71
72std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
David Tolnay85db5a02020-04-25 13:17:27 -070073 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
74 vec->push_back(86.0);
75 vec->push_back(75.0);
76 vec->push_back(30.0);
77 vec->push_back(9.5);
78 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070079}
80
81std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
David Tolnay85db5a02020-04-25 13:17:27 -070082 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
83 vec->push_back(Shared{1010});
84 vec->push_back(Shared{1011});
85 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070086}
87
David Tolnay1bcc9fe2020-04-25 13:51:07 -070088std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
89 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
90}
91
David Tolnayde5340e2020-04-25 14:03:21 -070092const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
93 return c.get_v();
94}
95
David Tolnayb41e74c2020-04-25 15:06:18 -070096rust::Vec<uint8_t> c_return_rust_vec() {
97 throw std::runtime_error("unimplemented");
98}
99
David Tolnay77989692020-04-25 15:57:47 -0700100const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
101 (void)c;
102 throw std::runtime_error("unimplemented");
103}
104
David Tolnay378ca502020-04-27 16:47:55 -0700105size_t c_return_identity(size_t n) { return n; }
Joel Galensonba676072020-04-27 15:55:45 -0700106
David Tolnay378ca502020-04-27 16:47:55 -0700107size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }
Joel Galensonba676072020-04-27 15:55:45 -0700108
David Tolnay3fd7f562020-01-26 17:47:11 -0800109void c_take_primitive(size_t n) {
110 if (n == 2020) {
111 cxx_test_suite_set_correct();
112 }
113}
David Tolnayad5b8af2020-01-26 16:59:13 -0800114
David Tolnay3fd7f562020-01-26 17:47:11 -0800115void c_take_shared(Shared shared) {
116 if (shared.z == 2020) {
117 cxx_test_suite_set_correct();
118 }
119}
David Tolnayad5b8af2020-01-26 16:59:13 -0800120
David Tolnay750755e2020-03-01 13:04:08 -0800121void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -0800122 if (cxx_test_suite_r_is_correct(&*r)) {
123 cxx_test_suite_set_correct();
124 }
David Tolnay3fd7f562020-01-26 17:47:11 -0800125}
David Tolnayad5b8af2020-01-26 16:59:13 -0800126
David Tolnay3fd7f562020-01-26 17:47:11 -0800127void c_take_unique_ptr(std::unique_ptr<C> c) {
128 if (c->get() == 2020) {
129 cxx_test_suite_set_correct();
130 }
131}
David Tolnayad5b8af2020-01-26 16:59:13 -0800132
David Tolnaya7d00e82020-03-06 15:50:14 -0800133void c_take_ref_r(const R &r) {
134 if (cxx_test_suite_r_is_correct(&r)) {
135 cxx_test_suite_set_correct();
136 }
137}
David Tolnayad5b8af2020-01-26 16:59:13 -0800138
David Tolnay3fd7f562020-01-26 17:47:11 -0800139void c_take_ref_c(const C &c) {
140 if (c.get() == 2020) {
141 cxx_test_suite_set_correct();
142 }
143}
David Tolnayad5b8af2020-01-26 16:59:13 -0800144
David Tolnay750755e2020-03-01 13:04:08 -0800145void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800146 if (std::string(s) == "2020") {
147 cxx_test_suite_set_correct();
148 }
149}
David Tolnayad5b8af2020-01-26 16:59:13 -0800150
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700151void c_take_sliceu8(rust::Slice<uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700152 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
153 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700154 cxx_test_suite_set_correct();
155 }
156}
157
David Tolnay750755e2020-03-01 13:04:08 -0800158void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800159 if (std::string(s) == "2020") {
160 cxx_test_suite_set_correct();
161 }
162}
David Tolnayad5b8af2020-01-26 16:59:13 -0800163
David Tolnay3fd7f562020-01-26 17:47:11 -0800164void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
165 if (*s == "2020") {
166 cxx_test_suite_set_correct();
167 }
168}
David Tolnayad5b8af2020-01-26 16:59:13 -0800169
Myron Ahneba35cf2020-02-05 19:41:51 +0700170void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
171 if (v->size() == 4) {
172 cxx_test_suite_set_correct();
173 }
174}
175
176void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
177 if (v->size() == 4) {
178 cxx_test_suite_set_correct();
179 }
180}
181
182void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
183 if (v->size() == 2) {
184 cxx_test_suite_set_correct();
185 }
186}
187
David Tolnay2244d1f2020-04-25 13:58:18 -0700188void c_take_ref_vector(const std::vector<uint8_t> &v) {
189 if (v.size() == 4) {
190 cxx_test_suite_set_correct();
191 }
192}
193
David Tolnayd2ce8a92020-04-25 16:16:45 -0700194void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }
Myron Ahneba35cf2020-02-05 19:41:51 +0700195
David Tolnayd2ce8a92020-04-25 16:16:45 -0700196void c_take_rust_vec_shared(rust::Vec<Shared> v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700197 uint32_t sum = 0;
David Tolnayc87c2152020-04-24 17:07:41 -0700198 for (auto i : v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700199 sum += i.z;
200 }
201 if (sum == 2021) {
202 cxx_test_suite_set_correct();
203 }
204}
205
myronahnda9be502020-04-29 05:47:23 +0700206void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
207 // Exercise requirements of ForwardIterator
208 // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
209 uint32_t sum = 0;
210 for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
211 sum += it->z;
212 }
213 if (sum == 2021) {
214 cxx_test_suite_set_correct();
215 }
216}
217
David Tolnayd2ce8a92020-04-25 16:16:45 -0700218void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
219 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
220 if (sum == 200) {
221 cxx_test_suite_set_correct();
222 }
223}
224
myronahnda9be502020-04-29 05:47:23 +0700225void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
226 // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
227 // requirements for std::iterator_traits.
228 // https://en.cppreference.com/w/cpp/iterator/iterator_traits
229 std::vector<uint8_t> cxx_v;
230 std::copy(v.begin(), v.end(), back_inserter(cxx_v));
231 uint8_t sum = std::accumulate(cxx_v.begin(), cxx_v.end(), 0);
232 if (sum == 200) {
233 cxx_test_suite_set_correct();
234 }
235}
236
David Tolnay75dca2e2020-03-25 20:17:52 -0700237void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
238 callback("2020");
239}
240
David Tolnayebef4a22020-03-17 15:33:47 -0700241void c_try_return_void() {}
242
243size_t c_try_return_primitive() { return 2020; }
244
245size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
246
David Tolnay99642622020-03-25 13:07:35 -0700247rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700248
David Tolnay99642622020-03-25 13:07:35 -0700249const rust::String &c_try_return_ref(const rust::String &s) { return s; }
250
251rust::Str c_try_return_str(rust::Str s) { return s; }
252
Adrian Taylorec9430e2020-04-14 16:09:58 -0700253rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700254
David Tolnay99642622020-03-25 13:07:35 -0700255rust::String c_try_return_rust_string() { return c_return_rust_string(); }
256
257std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
258 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700259}
260
David Tolnay8b9d1762020-04-25 16:05:46 -0700261rust::Vec<uint8_t> c_try_return_rust_vec() {
262 throw std::runtime_error("unimplemented");
263}
264
David Tolnay77989692020-04-25 15:57:47 -0700265const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
266 (void)c;
267 throw std::runtime_error("unimplemented");
268}
269
David Tolnay2fe58c62020-03-06 16:23:09 -0800270extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800271 return std::unique_ptr<C>(new C{2020}).release();
272}
273
David Tolnay85db24862020-03-06 16:24:41 -0800274extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
275 return std::unique_ptr<std::string>(new std::string("2020")).release();
276}
277
David Tolnayf306da42020-02-22 19:55:43 -0800278extern "C" const char *cxx_run_test() noexcept {
279#define STRINGIFY(x) #x
280#define TOSTRING(x) STRINGIFY(x)
281#define ASSERT(x) \
282 do { \
283 if (!(x)) { \
284 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
285 } \
286 } while (false)
287
288 ASSERT(r_return_primitive() == 2020);
289 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800290 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800291 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800292 ASSERT(r_return_ref(Shared{2020}) == 2020);
293 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
294 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800295 ASSERT(*r_return_unique_ptr_string() == "2020");
Joel Galensonba676072020-04-27 15:55:45 -0700296 ASSERT(r_return_identity(2020) == 2020);
297 ASSERT(r_return_sum(2020, 1) == 2021);
David Tolnayf306da42020-02-22 19:55:43 -0800298
299 r_take_primitive(2020);
300 r_take_shared(Shared{2020});
301 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
302 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800303 r_take_str(rust::Str("2020"));
David Tolnay633b1f52020-04-14 16:33:14 -0700304 r_take_sliceu8(
305 rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA), 5));
David Tolnay40226ab2020-03-03 00:05:35 -0800306 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800307 r_take_unique_ptr_string(
308 std::unique_ptr<std::string>(new std::string("2020")));
309
David Tolnayb6c5ea72020-03-16 13:36:28 -0700310 ASSERT(r_try_return_primitive() == 2020);
311 try {
312 r_fail_return_primitive();
313 ASSERT(false);
314 } catch (const rust::Error &e) {
315 ASSERT(std::strcmp(e.what(), "rust error") == 0);
316 }
317
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700318 auto r2 = r_return_r2(2020);
319 ASSERT(r2->get() == 2020);
320 ASSERT(r2->set(2021) == 2021);
321 ASSERT(r2->get() == 2021);
322 ASSERT(r2->set(2020) == 2020);
323 ASSERT(r2->get() == 2020);
324
David Tolnayf306da42020-02-22 19:55:43 -0800325 cxx_test_suite_set_correct();
326 return nullptr;
327}
328
David Tolnay97c72102020-01-25 16:49:00 -0800329} // namespace tests