blob: d72bfd007665c0a69d0a79c7e82984b6fdfbe21a [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
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
David Tolnayeb952ba2020-04-14 15:02:24 -070037rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070038 (void)shared;
David Tolnay633b1f52020-04-14 16:33:14 -070039 return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA), 5);
Adrian Taylorf5dd5522020-04-13 16:50:14 -070040}
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) {
David Tolnay633b1f52020-04-14 16:33:14 -070091 if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
92 "2020") {
Adrian Taylorf5dd5522020-04-13 16:50:14 -070093 cxx_test_suite_set_correct();
94 }
95}
96
David Tolnay750755e2020-03-01 13:04:08 -080097void c_take_rust_string(rust::String s) {
David Tolnay3fd7f562020-01-26 17:47:11 -080098 if (std::string(s) == "2020") {
99 cxx_test_suite_set_correct();
100 }
101}
David Tolnayad5b8af2020-01-26 16:59:13 -0800102
David Tolnay3fd7f562020-01-26 17:47:11 -0800103void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
104 if (*s == "2020") {
105 cxx_test_suite_set_correct();
106 }
107}
David Tolnayad5b8af2020-01-26 16:59:13 -0800108
David Tolnay75dca2e2020-03-25 20:17:52 -0700109void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
110 callback("2020");
111}
112
David Tolnayebef4a22020-03-17 15:33:47 -0700113void c_try_return_void() {}
114
115size_t c_try_return_primitive() { return 2020; }
116
117size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }
118
David Tolnay99642622020-03-25 13:07:35 -0700119rust::Box<R> c_try_return_box() { return c_return_box(); }
Myron Ahn84849302020-03-25 22:00:58 +0700120
David Tolnay99642622020-03-25 13:07:35 -0700121const rust::String &c_try_return_ref(const rust::String &s) { return s; }
122
123rust::Str c_try_return_str(rust::Str s) { return s; }
124
Adrian Taylorec9430e2020-04-14 16:09:58 -0700125rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700126
David Tolnay99642622020-03-25 13:07:35 -0700127rust::String c_try_return_rust_string() { return c_return_rust_string(); }
128
129std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
130 return c_return_unique_ptr_string();
Myron Ahn84849302020-03-25 22:00:58 +0700131}
132
David Tolnay2fe58c62020-03-06 16:23:09 -0800133extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800134 return std::unique_ptr<C>(new C{2020}).release();
135}
136
David Tolnay85db24862020-03-06 16:24:41 -0800137extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
138 return std::unique_ptr<std::string>(new std::string("2020")).release();
139}
140
David Tolnayf306da42020-02-22 19:55:43 -0800141extern "C" const char *cxx_run_test() noexcept {
142#define STRINGIFY(x) #x
143#define TOSTRING(x) STRINGIFY(x)
144#define ASSERT(x) \
145 do { \
146 if (!(x)) { \
147 return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \
148 } \
149 } while (false)
150
151 ASSERT(r_return_primitive() == 2020);
152 ASSERT(r_return_shared().z == 2020);
David Tolnay5cd8d612020-03-06 15:56:30 -0800153 ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
David Tolnay4b3a66e2020-03-06 16:14:00 -0800154 ASSERT(r_return_unique_ptr()->get() == 2020);
David Tolnayf306da42020-02-22 19:55:43 -0800155 ASSERT(r_return_ref(Shared{2020}) == 2020);
156 ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
157 ASSERT(std::string(r_return_rust_string()) == "2020");
David Tolnay85db24862020-03-06 16:24:41 -0800158 ASSERT(*r_return_unique_ptr_string() == "2020");
David Tolnayf306da42020-02-22 19:55:43 -0800159
160 r_take_primitive(2020);
161 r_take_shared(Shared{2020});
162 r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
163 r_take_ref_c(C{2020});
David Tolnay750755e2020-03-01 13:04:08 -0800164 r_take_str(rust::Str("2020"));
David Tolnay633b1f52020-04-14 16:33:14 -0700165 r_take_sliceu8(
166 rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA), 5));
David Tolnay40226ab2020-03-03 00:05:35 -0800167 r_take_rust_string(rust::String("2020"));
David Tolnayf306da42020-02-22 19:55:43 -0800168 r_take_unique_ptr_string(
169 std::unique_ptr<std::string>(new std::string("2020")));
170
David Tolnayb6c5ea72020-03-16 13:36:28 -0700171 ASSERT(r_try_return_primitive() == 2020);
172 try {
173 r_fail_return_primitive();
174 ASSERT(false);
175 } catch (const rust::Error &e) {
176 ASSERT(std::strcmp(e.what(), "rust error") == 0);
177 }
178
David Tolnayf306da42020-02-22 19:55:43 -0800179 cxx_test_suite_set_correct();
180 return nullptr;
181}
182
David Tolnay97c72102020-01-25 16:49:00 -0800183} // namespace tests