blob: 619485a307b26eb903569b32dd8d250fa60ee3ef [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 Tolnayebef4a22020-03-17 15:33:47 -07004#include <stdexcept>
David Tolnay97c72102020-01-25 16:49:00 -08005
David Tolnay2fe58c62020-03-06 16:23:09 -08006extern "C" void cxx_test_suite_set_correct() noexcept;
7extern "C" tests::R *cxx_test_suite_get_box() noexcept;
8extern "C" bool cxx_test_suite_r_is_correct(const tests::R *) noexcept;
David Tolnay3fd7f562020-01-26 17:47:11 -08009
David Tolnay97c72102020-01-25 16:49:00 -080010namespace tests {
11
David Tolnayeb952ba2020-04-14 15:02:24 -070012const char *SLICE_DATA = "2020";
Adrian Taylorf5dd5522020-04-13 16:50:14 -070013
David Tolnayad5b8af2020-01-26 16:59:13 -080014C::C(size_t n) : n(n) {}
15
David Tolnay3fd7f562020-01-26 17:47:11 -080016size_t C::get() const { return this->n; }
17
Joel Galensone1e969d2020-04-21 12:50:20 -070018size_t C::get2() const { return this->n; }
19
Joel Galenson3d4f6122020-04-07 15:54:05 -070020size_t C::set(size_t n) {
21 this->n = n;
22 return this->n;
23}
24
Joel Galensone1e969d2020-04-21 12:50:20 -070025size_t C::set2(size_t n) {
26 this->n = n;
27 return this->n;
28}
29
David Tolnayb8715772020-01-28 00:54:05 -080030size_t c_return_primitive() { return 2020; }
David Tolnayad5b8af2020-01-26 16:59:13 -080031
David Tolnayb8715772020-01-28 00:54:05 -080032Shared c_return_shared() { return Shared{2020}; }
David Tolnayad5b8af2020-01-26 16:59:13 -080033
David Tolnaybe13d8a2020-03-06 15:45:39 -080034rust::Box<R> c_return_box() {
35 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
36}
37
David Tolnayad5b8af2020-01-26 16:59:13 -080038std::unique_ptr<C> c_return_unique_ptr() {
39 return std::unique_ptr<C>(new C{2020});
40}
41
David Tolnayb8715772020-01-28 00:54:05 -080042const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080043
David Tolnay750755e2020-03-01 13:04:08 -080044rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080045 (void)shared;
46 return "2020";
47}
48
David Tolnayeb952ba2020-04-14 15:02:24 -070049rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070050 (void)shared;
David Tolnay633b1f52020-04-14 16:33:14 -070051 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA), 5);
Adrian Taylorf5dd5522020-04-13 16:50:14 -070052}
53
David Tolnay750755e2020-03-01 13:04:08 -080054rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080055
56std::unique_ptr<std::string> c_return_unique_ptr_string() {
57 return std::unique_ptr<std::string>(new std::string("2020"));
58}
59
David Tolnay3fd7f562020-01-26 17:47:11 -080060void c_take_primitive(size_t n) {
61 if (n == 2020) {
62 cxx_test_suite_set_correct();
63 }
64}
David Tolnayad5b8af2020-01-26 16:59:13 -080065
David Tolnay3fd7f562020-01-26 17:47:11 -080066void c_take_shared(Shared shared) {
67 if (shared.z == 2020) {
68 cxx_test_suite_set_correct();
69 }
70}
David Tolnayad5b8af2020-01-26 16:59:13 -080071
David Tolnay750755e2020-03-01 13:04:08 -080072void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -080073 if (cxx_test_suite_r_is_correct(&*r)) {
74 cxx_test_suite_set_correct();
75 }
David Tolnay3fd7f562020-01-26 17:47:11 -080076}
David Tolnayad5b8af2020-01-26 16:59:13 -080077
David Tolnay3fd7f562020-01-26 17:47:11 -080078void c_take_unique_ptr(std::unique_ptr<C> c) {
79 if (c->get() == 2020) {
80 cxx_test_suite_set_correct();
81 }
82}
David Tolnayad5b8af2020-01-26 16:59:13 -080083
David Tolnaya7d00e82020-03-06 15:50:14 -080084void c_take_ref_r(const R &r) {
85 if (cxx_test_suite_r_is_correct(&r)) {
86 cxx_test_suite_set_correct();
87 }
88}
David Tolnayad5b8af2020-01-26 16:59:13 -080089
David Tolnay3fd7f562020-01-26 17:47:11 -080090void c_take_ref_c(const C &c) {
91 if (c.get() == 2020) {
92 cxx_test_suite_set_correct();
93 }
94}
David Tolnayad5b8af2020-01-26 16:59:13 -080095
David Tolnay750755e2020-03-01 13:04:08 -080096void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -080097 if (std::string(s) == "2020") {
98 cxx_test_suite_set_correct();
99 }
100}
David Tolnayad5b8af2020-01-26 16:59:13 -0800101
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700102void c_take_sliceu8(rust::Slice<uint8_t> s) {
David Tolnay633b1f52020-04-14 16:33:14 -0700103 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
104 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700105 cxx_test_suite_set_correct();
106 }
107}
108
David Tolnay750755e2020-03-01 13:04:08 -0800109void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -0800110 if (std::string(s) == "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_unique_ptr_string(std::unique_ptr<std::string> s) {
116 if (*s == "2020") {
117 cxx_test_suite_set_correct();
118 }
119}
David Tolnayad5b8af2020-01-26 16:59:13 -0800120
David Tolnay75dca2e2020-03-25 20:17:52 -0700121void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
122 callback("2020");
123}
124
David Tolnayebef4a22020-03-17 15:33:47 -0700125void c_try_return_void() {}
126
127size_t c_try_return_primitive() { return 2020; }
128
129size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
130
David Tolnay99642622020-03-25 13:07:35 -0700131rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700132
David Tolnay99642622020-03-25 13:07:35 -0700133const rust::String &c_try_return_ref(const rust::String &s) { return s; }
134
135rust::Str c_try_return_str(rust::Str s) { return s; }
136
Adrian Taylorec9430e2020-04-14 16:09:58 -0700137rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700138
David Tolnay99642622020-03-25 13:07:35 -0700139rust::String c_try_return_rust_string() { return c_return_rust_string(); }
140
141std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
142 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700143}
144
David Tolnay2fe58c62020-03-06 16:23:09 -0800145extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800146 return std::unique_ptr<C>(new C{2020}).release();
147}
148
David Tolnay85db24862020-03-06 16:24:41 -0800149extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
150 return std::unique_ptr<std::string>(new std::string("2020")).release();
151}
152
David Tolnayf306da42020-02-22 19:55:43 -0800153extern "C" const char *cxx_run_test() noexcept {
154#define STRINGIFY(x) #x
155#define TOSTRING(x) STRINGIFY(x)
156#define ASSERT(x) \
157 do { \
158 if (!(x)) { \
159 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
160 } \
161 } while (false)
162
163 ASSERT(r_return_primitive() == 2020);
164 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800165 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800166 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800167 ASSERT(r_return_ref(Shared{2020}) == 2020);
168 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
169 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800170 ASSERT(*r_return_unique_ptr_string() == "2020");
David Tolnayf306da42020-02-22 19:55:43 -0800171
172 r_take_primitive(2020);
173 r_take_shared(Shared{2020});
174 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
175 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800176 r_take_str(rust::Str("2020"));
David Tolnay633b1f52020-04-14 16:33:14 -0700177 r_take_sliceu8(
178 rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA), 5));
David Tolnay40226ab2020-03-03 00:05:35 -0800179 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800180 r_take_unique_ptr_string(
181 std::unique_ptr<std::string>(new std::string("2020")));
182
David Tolnayb6c5ea72020-03-16 13:36:28 -0700183 ASSERT(r_try_return_primitive() == 2020);
184 try {
185 r_fail_return_primitive();
186 ASSERT(false);
187 } catch (const rust::Error &e) {
188 ASSERT(std::strcmp(e.what(), "rust error") == 0);
189 }
190
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700191 auto r2 = r_return_r2(2020);
192 ASSERT(r2->get() == 2020);
193 ASSERT(r2->set(2021) == 2021);
194 ASSERT(r2->get() == 2021);
195 ASSERT(r2->set(2020) == 2020);
196 ASSERT(r2->get() == 2020);
197
David Tolnayf306da42020-02-22 19:55:43 -0800198 cxx_test_suite_set_correct();
199 return nullptr;
200}
201
David Tolnay97c72102020-01-25 16:49:00 -0800202} // namespace tests