blob: 372641c165d620b8d46872dfb80d4e7bebe3d124 [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 Tolnayb8715772020-01-28 00:54:05 -080031size_t c_return_primitive() { return 2020; }
David Tolnayad5b8af2020-01-26 16:59:13 -080032
David Tolnayb8715772020-01-28 00:54:05 -080033Shared c_return_shared() { return Shared{2020}; }
David Tolnayad5b8af2020-01-26 16:59:13 -080034
David Tolnaybe13d8a2020-03-06 15:45:39 -080035rust::Box<R> c_return_box() {
36 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
37}
38
David Tolnayad5b8af2020-01-26 16:59:13 -080039std::unique_ptr<C> c_return_unique_ptr() {
40 return std::unique_ptr<C>(new C{2020});
41}
42
David Tolnayb8715772020-01-28 00:54:05 -080043const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080044
David Tolnay750755e2020-03-01 13:04:08 -080045rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080046 (void)shared;
47 return "2020";
48}
49
David Tolnayeb952ba2020-04-14 15:02:24 -070050rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070051 (void)shared;
David Tolnay633b1f52020-04-14 16:33:14 -070052 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA), 5);
Adrian Taylorf5dd5522020-04-13 16:50:14 -070053}
54
David Tolnay750755e2020-03-01 13:04:08 -080055rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080056
57std::unique_ptr<std::string> c_return_unique_ptr_string() {
58 return std::unique_ptr<std::string>(new std::string("2020"));
59}
60
Myron Ahneba35cf2020-02-05 19:41:51 +070061std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
David Tolnay85db5a02020-04-25 13:17:27 -070062 auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
63 vec->push_back(86);
64 vec->push_back(75);
65 vec->push_back(30);
66 vec->push_back(9);
67 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070068}
69
70std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
David Tolnay85db5a02020-04-25 13:17:27 -070071 auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
72 vec->push_back(86.0);
73 vec->push_back(75.0);
74 vec->push_back(30.0);
75 vec->push_back(9.5);
76 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070077}
78
79std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
David Tolnay85db5a02020-04-25 13:17:27 -070080 auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
81 vec->push_back(Shared{1010});
82 vec->push_back(Shared{1011});
83 return vec;
Myron Ahneba35cf2020-02-05 19:41:51 +070084}
85
David Tolnay1bcc9fe2020-04-25 13:51:07 -070086std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
87 return std::unique_ptr<std::vector<C>>(new std::vector<C>());
88}
89
David Tolnay3fd7f562020-01-26 17:47:11 -080090void c_take_primitive(size_t n) {
91 if (n == 2020) {
92 cxx_test_suite_set_correct();
93 }
94}
David Tolnayad5b8af2020-01-26 16:59:13 -080095
David Tolnay3fd7f562020-01-26 17:47:11 -080096void c_take_shared(Shared shared) {
97 if (shared.z == 2020) {
98 cxx_test_suite_set_correct();
99 }
100}
David Tolnayad5b8af2020-01-26 16:59:13 -0800101
David Tolnay750755e2020-03-01 13:04:08 -0800102void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -0800103 if (cxx_test_suite_r_is_correct(&*r)) {
104 cxx_test_suite_set_correct();
105 }
David Tolnay3fd7f562020-01-26 17:47:11 -0800106}
David Tolnayad5b8af2020-01-26 16:59:13 -0800107
David Tolnay3fd7f562020-01-26 17:47:11 -0800108void c_take_unique_ptr(std::unique_ptr<C> c) {
109 if (c->get() == 2020) {
110 cxx_test_suite_set_correct();
111 }
112}
David Tolnayad5b8af2020-01-26 16:59:13 -0800113
David Tolnaya7d00e82020-03-06 15:50:14 -0800114void c_take_ref_r(const R &r) {
115 if (cxx_test_suite_r_is_correct(&r)) {
116 cxx_test_suite_set_correct();
117 }
118}
David Tolnayad5b8af2020-01-26 16:59:13 -0800119
David Tolnay3fd7f562020-01-26 17:47:11 -0800120void c_take_ref_c(const C &c) {
121 if (c.get() == 2020) {
122 cxx_test_suite_set_correct();
123 }
124}
David Tolnayad5b8af2020-01-26 16:59:13 -0800125
David Tolnay750755e2020-03-01 13:04:08 -0800126void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800127 if (std::string(s) == "2020") {
128 cxx_test_suite_set_correct();
129 }
130}
David Tolnayad5b8af2020-01-26 16:59:13 -0800131
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700132void c_take_sliceu8(rust::Slice<uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700133 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
134 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700135 cxx_test_suite_set_correct();
136 }
137}
138
David Tolnay750755e2020-03-01 13:04:08 -0800139void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800140 if (std::string(s) == "2020") {
141 cxx_test_suite_set_correct();
142 }
143}
David Tolnayad5b8af2020-01-26 16:59:13 -0800144
David Tolnay3fd7f562020-01-26 17:47:11 -0800145void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
146 if (*s == "2020") {
147 cxx_test_suite_set_correct();
148 }
149}
David Tolnayad5b8af2020-01-26 16:59:13 -0800150
Myron Ahneba35cf2020-02-05 19:41:51 +0700151void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
152 if (v->size() == 4) {
153 cxx_test_suite_set_correct();
154 }
155}
156
157void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
158 if (v->size() == 4) {
159 cxx_test_suite_set_correct();
160 }
161}
162
163void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
164 if (v->size() == 2) {
165 cxx_test_suite_set_correct();
166 }
167}
168
David Tolnayd1413042020-04-25 13:10:22 -0700169void c_take_vec_u8(const rust::Vec<uint8_t> &v) {
David Tolnayc87c2152020-04-24 17:07:41 -0700170 uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
Myron Ahneba35cf2020-02-05 19:41:51 +0700171 if (sum == 200) {
172 cxx_test_suite_set_correct();
173 }
174}
175
David Tolnayd1413042020-04-25 13:10:22 -0700176void c_take_vec_shared(const rust::Vec<Shared> &v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700177 uint32_t sum = 0;
David Tolnayc87c2152020-04-24 17:07:41 -0700178 for (auto i : v) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700179 sum += i.z;
180 }
181 if (sum == 2021) {
182 cxx_test_suite_set_correct();
183 }
184}
185
David Tolnay75dca2e2020-03-25 20:17:52 -0700186void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
187 callback("2020");
188}
189
David Tolnayebef4a22020-03-17 15:33:47 -0700190void c_try_return_void() {}
191
192size_t c_try_return_primitive() { return 2020; }
193
194size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
195
David Tolnay99642622020-03-25 13:07:35 -0700196rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700197
David Tolnay99642622020-03-25 13:07:35 -0700198const rust::String &c_try_return_ref(const rust::String &s) { return s; }
199
200rust::Str c_try_return_str(rust::Str s) { return s; }
201
Adrian Taylorec9430e2020-04-14 16:09:58 -0700202rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700203
David Tolnay99642622020-03-25 13:07:35 -0700204rust::String c_try_return_rust_string() { return c_return_rust_string(); }
205
206std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
207 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700208}
209
David Tolnay2fe58c62020-03-06 16:23:09 -0800210extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800211 return std::unique_ptr<C>(new C{2020}).release();
212}
213
David Tolnay85db24862020-03-06 16:24:41 -0800214extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
215 return std::unique_ptr<std::string>(new std::string("2020")).release();
216}
217
David Tolnayf306da42020-02-22 19:55:43 -0800218extern "C" const char *cxx_run_test() noexcept {
219#define STRINGIFY(x) #x
220#define TOSTRING(x) STRINGIFY(x)
221#define ASSERT(x) \
222 do { \
223 if (!(x)) { \
224 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
225 } \
226 } while (false)
227
228 ASSERT(r_return_primitive() == 2020);
229 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800230 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800231 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800232 ASSERT(r_return_ref(Shared{2020}) == 2020);
233 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
234 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800235 ASSERT(*r_return_unique_ptr_string() == "2020");
David Tolnayf306da42020-02-22 19:55:43 -0800236
237 r_take_primitive(2020);
238 r_take_shared(Shared{2020});
239 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
240 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800241 r_take_str(rust::Str("2020"));
David Tolnay633b1f52020-04-14 16:33:14 -0700242 r_take_sliceu8(
243 rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA), 5));
David Tolnay40226ab2020-03-03 00:05:35 -0800244 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800245 r_take_unique_ptr_string(
246 std::unique_ptr<std::string>(new std::string("2020")));
247
David Tolnayb6c5ea72020-03-16 13:36:28 -0700248 ASSERT(r_try_return_primitive() == 2020);
249 try {
250 r_fail_return_primitive();
251 ASSERT(false);
252 } catch (const rust::Error &e) {
253 ASSERT(std::strcmp(e.what(), "rust error") == 0);
254 }
255
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700256 auto r2 = r_return_r2(2020);
257 ASSERT(r2->get() == 2020);
258 ASSERT(r2->set(2021) == 2021);
259 ASSERT(r2->get() == 2021);
260 ASSERT(r2->set(2020) == 2020);
261 ASSERT(r2->get() == 2020);
262
David Tolnayf306da42020-02-22 19:55:43 -0800263 cxx_test_suite_set_correct();
264 return nullptr;
265}
266
David Tolnay97c72102020-01-25 16:49:00 -0800267} // namespace tests