blob: bf5e76ddc3a7fb2f0cbc067cd4d36d018e5b2754 [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
Adrian Taylorf5dd5522020-04-13 16:50:14 -070012const char* SLICE_DATA = "2020";
13
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
David Tolnayb8715772020-01-28 00:54:05 -080018size_t c_return_primitive() { return 2020; }
David Tolnayad5b8af2020-01-26 16:59:13 -080019
David Tolnayb8715772020-01-28 00:54:05 -080020Shared c_return_shared() { return Shared{2020}; }
David Tolnayad5b8af2020-01-26 16:59:13 -080021
David Tolnaybe13d8a2020-03-06 15:45:39 -080022rust::Box<R> c_return_box() {
23 return rust::Box<R>::from_raw(cxx_test_suite_get_box());
24}
25
David Tolnayad5b8af2020-01-26 16:59:13 -080026std::unique_ptr<C> c_return_unique_ptr() {
27 return std::unique_ptr<C>(new C{2020});
28}
29
David Tolnayb8715772020-01-28 00:54:05 -080030const size_t &c_return_ref(const Shared &shared) { return shared.z; }
David Tolnayad5b8af2020-01-26 16:59:13 -080031
David Tolnay750755e2020-03-01 13:04:08 -080032rust::Str c_return_str(const Shared &shared) {
David Tolnayad5b8af2020-01-26 16:59:13 -080033 (void)shared;
34 return "2020";
35}
36
Adrian Taylorf5dd5522020-04-13 16:50:14 -070037rust::Slice<uint8_t> c_return_sliceu8(const Shared& shared) {
38 (void)shared;
39 return rust::Slice<uint8_t>((const unsigned char*)SLICE_DATA, 5);
40}
41
David Tolnay750755e2020-03-01 13:04:08 -080042rust::String c_return_rust_string() { return "2020"; }
David Tolnayad5b8af2020-01-26 16:59:13 -080043
44std::unique_ptr<std::string> c_return_unique_ptr_string() {
45 return std::unique_ptr<std::string>(new std::string("2020"));
46}
47
David Tolnay3fd7f562020-01-26 17:47:11 -080048void c_take_primitive(size_t n) {
49 if (n == 2020) {
50 cxx_test_suite_set_correct();
51 }
52}
David Tolnayad5b8af2020-01-26 16:59:13 -080053
David Tolnay3fd7f562020-01-26 17:47:11 -080054void c_take_shared(Shared shared) {
55 if (shared.z == 2020) {
56 cxx_test_suite_set_correct();
57 }
58}
David Tolnayad5b8af2020-01-26 16:59:13 -080059
David Tolnay750755e2020-03-01 13:04:08 -080060void c_take_box(rust::Box<R> r) {
David Tolnaya7d00e82020-03-06 15:50:14 -080061 if (cxx_test_suite_r_is_correct(&*r)) {
62 cxx_test_suite_set_correct();
63 }
David Tolnay3fd7f562020-01-26 17:47:11 -080064}
David Tolnayad5b8af2020-01-26 16:59:13 -080065
David Tolnay3fd7f562020-01-26 17:47:11 -080066void c_take_unique_ptr(std::unique_ptr<C> c) {
67 if (c->get() == 2020) {
68 cxx_test_suite_set_correct();
69 }
70}
David Tolnayad5b8af2020-01-26 16:59:13 -080071
David Tolnaya7d00e82020-03-06 15:50:14 -080072void c_take_ref_r(const R &r) {
73 if (cxx_test_suite_r_is_correct(&r)) {
74 cxx_test_suite_set_correct();
75 }
76}
David Tolnayad5b8af2020-01-26 16:59:13 -080077
David Tolnay3fd7f562020-01-26 17:47:11 -080078void c_take_ref_c(const C &c) {
79 if (c.get() == 2020) {
80 cxx_test_suite_set_correct();
81 }
82}
David Tolnayad5b8af2020-01-26 16:59:13 -080083
David Tolnay750755e2020-03-01 13:04:08 -080084void c_take_str(rust::Str s) {
David Tolnay3fd7f562020-01-26 17:47:11 -080085 if (std::string(s) == "2020") {
86 cxx_test_suite_set_correct();
87 }
88}
David Tolnayad5b8af2020-01-26 16:59:13 -080089
Adrian Taylorf5dd5522020-04-13 16:50:14 -070090void c_take_sliceu8(rust::Slice<uint8_t> s) {
91 if (std::string((const char*)s.data(), s.size()) == "2020") {
92 cxx_test_suite_set_correct();
93 }
94}
95
David Tolnay750755e2020-03-01 13:04:08 -080096void c_take_rust_string(rust::String 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
David Tolnay3fd7f562020-01-26 17:47:11 -0800102void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
103 if (*s == "2020") {
104 cxx_test_suite_set_correct();
105 }
106}
David Tolnayad5b8af2020-01-26 16:59:13 -0800107
David Tolnay75dca2e2020-03-25 20:17:52 -0700108void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
109 callback("2020");
110}
111
David Tolnayebef4a22020-03-17 15:33:47 -0700112void c_try_return_void() {}
113
114size_t c_try_return_primitive() { return 2020; }
115
116size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
117
David Tolnay99642622020-03-25 13:07:35 -0700118rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700119
David Tolnay99642622020-03-25 13:07:35 -0700120const rust::String &c_try_return_ref(const rust::String &s) { return s; }
121
122rust::Str c_try_return_str(rust::Str s) { return s; }
123
Adrian Taylorec9430e2020-04-14 16:09:58 -0700124rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700125
David Tolnay99642622020-03-25 13:07:35 -0700126rust::String c_try_return_rust_string() { return c_return_rust_string(); }
127
128std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
129 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700130}
131
David Tolnay2fe58c62020-03-06 16:23:09 -0800132extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800133 return std::unique_ptr<C>(new C{2020}).release();
134}
135
David Tolnay85db24862020-03-06 16:24:41 -0800136extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
137 return std::unique_ptr<std::string>(new std::string("2020")).release();
138}
139
David Tolnayf306da42020-02-22 19:55:43 -0800140extern "C" const char *cxx_run_test() noexcept {
141#define STRINGIFY(x) #x
142#define TOSTRING(x) STRINGIFY(x)
143#define ASSERT(x) \
144 do { \
145 if (!(x)) { \
146 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
147 } \
148 } while (false)
149
150 ASSERT(r_return_primitive() == 2020);
151 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800152 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800153 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800154 ASSERT(r_return_ref(Shared{2020}) == 2020);
155 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
156 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800157 ASSERT(*r_return_unique_ptr_string() == "2020");
David Tolnayf306da42020-02-22 19:55:43 -0800158
159 r_take_primitive(2020);
160 r_take_shared(Shared{2020});
161 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
162 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800163 r_take_str(rust::Str("2020"));
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700164 r_take_sliceu8(rust::Slice<uint8_t>((const unsigned char*)SLICE_DATA, 5));
David Tolnay40226ab2020-03-03 00:05:35 -0800165 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800166 r_take_unique_ptr_string(
167 std::unique_ptr<std::string>(new std::string("2020")));
168
David Tolnayb6c5ea72020-03-16 13:36:28 -0700169 ASSERT(r_try_return_primitive() == 2020);
170 try {
171 r_fail_return_primitive();
172 ASSERT(false);
173 } catch (const rust::Error &e) {
174 ASSERT(std::strcmp(e.what(), "rust error") == 0);
175 }
176
David Tolnayf306da42020-02-22 19:55:43 -0800177 cxx_test_suite_set_correct();
178 return nullptr;
179}
180
David Tolnay97c72102020-01-25 16:49:00 -0800181} // namespace tests