| David Tolnay | 97c7210 | 2020-01-25 16:49:00 -0800 | [diff] [blame] | 1 | #include "tests/ffi/tests.h" |
| David Tolnay | 1a2683a | 2020-03-17 19:09:29 -0700 | [diff] [blame] | 2 | #include "tests/ffi/lib.rs.h" |
| David Tolnay | b6c5ea7 | 2020-03-16 13:36:28 -0700 | [diff] [blame] | 3 | #include <cstring> |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 4 | #include <numeric> |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 5 | #include <stdexcept> |
| David Tolnay | 97c7210 | 2020-01-25 16:49:00 -0800 | [diff] [blame] | 6 | |
| David Tolnay | 2fe58c6 | 2020-03-06 16:23:09 -0800 | [diff] [blame] | 7 | extern "C" void cxx_test_suite_set_correct() noexcept; |
| 8 | extern "C" tests::R *cxx_test_suite_get_box() noexcept; |
| 9 | extern "C" bool cxx_test_suite_r_is_correct(const tests::R *) noexcept; |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 10 | |
| David Tolnay | 97c7210 | 2020-01-25 16:49:00 -0800 | [diff] [blame] | 11 | namespace tests { |
| 12 | |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 13 | const char *SLICE_DATA = "2020"; |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 14 | |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 15 | C::C(size_t n) : n(n) {} |
| 16 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 17 | size_t C::get() const { return this->n; } |
| 18 | |
| Joel Galenson | e1e969d | 2020-04-21 12:50:20 -0700 | [diff] [blame] | 19 | size_t C::get2() const { return this->n; } |
| 20 | |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 21 | size_t C::set(size_t n) { |
| 22 | this->n = n; |
| 23 | return this->n; |
| 24 | } |
| 25 | |
| Joel Galenson | e1e969d | 2020-04-21 12:50:20 -0700 | [diff] [blame] | 26 | size_t C::set2(size_t n) { |
| 27 | this->n = n; |
| 28 | return this->n; |
| 29 | } |
| 30 | |
| David Tolnay | b871577 | 2020-01-28 00:54:05 -0800 | [diff] [blame] | 31 | size_t c_return_primitive() { return 2020; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 32 | |
| David Tolnay | b871577 | 2020-01-28 00:54:05 -0800 | [diff] [blame] | 33 | Shared c_return_shared() { return Shared{2020}; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 34 | |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 35 | rust::Box<R> c_return_box() { |
| 36 | return rust::Box<R>::from_raw(cxx_test_suite_get_box()); |
| 37 | } |
| 38 | |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 39 | std::unique_ptr<C> c_return_unique_ptr() { |
| 40 | return std::unique_ptr<C>(new C{2020}); |
| 41 | } |
| 42 | |
| David Tolnay | b871577 | 2020-01-28 00:54:05 -0800 | [diff] [blame] | 43 | const size_t &c_return_ref(const Shared &shared) { return shared.z; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 44 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 45 | rust::Str c_return_str(const Shared &shared) { |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 46 | (void)shared; |
| 47 | return "2020"; |
| 48 | } |
| 49 | |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 50 | rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) { |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 51 | (void)shared; |
| David Tolnay | 633b1f5 | 2020-04-14 16:33:14 -0700 | [diff] [blame] | 52 | return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA), 5); |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 55 | rust::String c_return_rust_string() { return "2020"; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 56 | |
| 57 | std::unique_ptr<std::string> c_return_unique_ptr_string() { |
| 58 | return std::unique_ptr<std::string>(new std::string("2020")); |
| 59 | } |
| 60 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 61 | std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() { |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 62 | 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 Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() { |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 71 | 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 Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() { |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 80 | 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 Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 84 | } |
| 85 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 86 | void c_take_primitive(size_t n) { |
| 87 | if (n == 2020) { |
| 88 | cxx_test_suite_set_correct(); |
| 89 | } |
| 90 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 91 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 92 | void c_take_shared(Shared shared) { |
| 93 | if (shared.z == 2020) { |
| 94 | cxx_test_suite_set_correct(); |
| 95 | } |
| 96 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 97 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 98 | void c_take_box(rust::Box<R> r) { |
| David Tolnay | a7d00e8 | 2020-03-06 15:50:14 -0800 | [diff] [blame] | 99 | if (cxx_test_suite_r_is_correct(&*r)) { |
| 100 | cxx_test_suite_set_correct(); |
| 101 | } |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 102 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 103 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 104 | void c_take_unique_ptr(std::unique_ptr<C> c) { |
| 105 | if (c->get() == 2020) { |
| 106 | cxx_test_suite_set_correct(); |
| 107 | } |
| 108 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 109 | |
| David Tolnay | a7d00e8 | 2020-03-06 15:50:14 -0800 | [diff] [blame] | 110 | void c_take_ref_r(const R &r) { |
| 111 | if (cxx_test_suite_r_is_correct(&r)) { |
| 112 | cxx_test_suite_set_correct(); |
| 113 | } |
| 114 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 115 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 116 | void c_take_ref_c(const C &c) { |
| 117 | if (c.get() == 2020) { |
| 118 | cxx_test_suite_set_correct(); |
| 119 | } |
| 120 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 121 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 122 | void c_take_str(rust::Str s) { |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 123 | if (std::string(s) == "2020") { |
| 124 | cxx_test_suite_set_correct(); |
| 125 | } |
| 126 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 127 | |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 128 | void c_take_sliceu8(rust::Slice<uint8_t> s) { |
| David Tolnay | 633b1f5 | 2020-04-14 16:33:14 -0700 | [diff] [blame] | 129 | if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) == |
| 130 | "2020") { |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 131 | cxx_test_suite_set_correct(); |
| 132 | } |
| 133 | } |
| 134 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 135 | void c_take_rust_string(rust::String s) { |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 136 | if (std::string(s) == "2020") { |
| 137 | cxx_test_suite_set_correct(); |
| 138 | } |
| 139 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 140 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 141 | void c_take_unique_ptr_string(std::unique_ptr<std::string> s) { |
| 142 | if (*s == "2020") { |
| 143 | cxx_test_suite_set_correct(); |
| 144 | } |
| 145 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 146 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 147 | void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) { |
| 148 | if (v->size() == 4) { |
| 149 | cxx_test_suite_set_correct(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) { |
| 154 | if (v->size() == 4) { |
| 155 | cxx_test_suite_set_correct(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) { |
| 160 | if (v->size() == 2) { |
| 161 | cxx_test_suite_set_correct(); |
| 162 | } |
| 163 | } |
| 164 | |
| David Tolnay | d141304 | 2020-04-25 13:10:22 -0700 | [diff] [blame] | 165 | void c_take_vec_u8(const rust::Vec<uint8_t> &v) { |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame^] | 166 | uint8_t sum = std::accumulate(v.begin(), v.end(), 0); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 167 | if (sum == 200) { |
| 168 | cxx_test_suite_set_correct(); |
| 169 | } |
| 170 | } |
| 171 | |
| David Tolnay | d141304 | 2020-04-25 13:10:22 -0700 | [diff] [blame] | 172 | void c_take_vec_shared(const rust::Vec<Shared> &v) { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 173 | uint32_t sum = 0; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame^] | 174 | for (auto i : v) { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 175 | sum += i.z; |
| 176 | } |
| 177 | if (sum == 2021) { |
| 178 | cxx_test_suite_set_correct(); |
| 179 | } |
| 180 | } |
| 181 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 182 | void c_take_callback(rust::Fn<size_t(rust::String)> callback) { |
| 183 | callback("2020"); |
| 184 | } |
| 185 | |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 186 | void c_try_return_void() {} |
| 187 | |
| 188 | size_t c_try_return_primitive() { return 2020; } |
| 189 | |
| 190 | size_t c_fail_return_primitive() { throw std::logic_error("logic error"); } |
| 191 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 192 | rust::Box<R> c_try_return_box() { return c_return_box(); } |
| Myron Ahn | 8484930 | 2020-03-25 22:00:58 +0700 | [diff] [blame] | 193 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 194 | const rust::String &c_try_return_ref(const rust::String &s) { return s; } |
| 195 | |
| 196 | rust::Str c_try_return_str(rust::Str s) { return s; } |
| 197 | |
| Adrian Taylor | ec9430e | 2020-04-14 16:09:58 -0700 | [diff] [blame] | 198 | rust::Slice<uint8_t> c_try_return_sliceu8(rust::Slice<uint8_t> s) { return s; } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 199 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 200 | rust::String c_try_return_rust_string() { return c_return_rust_string(); } |
| 201 | |
| 202 | std::unique_ptr<std::string> c_try_return_unique_ptr_string() { |
| 203 | return c_return_unique_ptr_string(); |
| Myron Ahn | 8484930 | 2020-03-25 22:00:58 +0700 | [diff] [blame] | 204 | } |
| 205 | |
| David Tolnay | 2fe58c6 | 2020-03-06 16:23:09 -0800 | [diff] [blame] | 206 | extern "C" C *cxx_test_suite_get_unique_ptr() noexcept { |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 207 | return std::unique_ptr<C>(new C{2020}).release(); |
| 208 | } |
| 209 | |
| David Tolnay | 85db2486 | 2020-03-06 16:24:41 -0800 | [diff] [blame] | 210 | extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept { |
| 211 | return std::unique_ptr<std::string>(new std::string("2020")).release(); |
| 212 | } |
| 213 | |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 214 | extern "C" const char *cxx_run_test() noexcept { |
| 215 | #define STRINGIFY(x) #x |
| 216 | #define TOSTRING(x) STRINGIFY(x) |
| 217 | #define ASSERT(x) \ |
| 218 | do { \ |
| 219 | if (!(x)) { \ |
| 220 | return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \ |
| 221 | } \ |
| 222 | } while (false) |
| 223 | |
| 224 | ASSERT(r_return_primitive() == 2020); |
| 225 | ASSERT(r_return_shared().z == 2020); |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 226 | ASSERT(cxx_test_suite_r_is_correct(&*r_return_box())); |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 227 | ASSERT(r_return_unique_ptr()->get() == 2020); |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 228 | ASSERT(r_return_ref(Shared{2020}) == 2020); |
| 229 | ASSERT(std::string(r_return_str(Shared{2020})) == "2020"); |
| 230 | ASSERT(std::string(r_return_rust_string()) == "2020"); |
| David Tolnay | 85db2486 | 2020-03-06 16:24:41 -0800 | [diff] [blame] | 231 | ASSERT(*r_return_unique_ptr_string() == "2020"); |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 232 | |
| 233 | r_take_primitive(2020); |
| 234 | r_take_shared(Shared{2020}); |
| 235 | r_take_unique_ptr(std::unique_ptr<C>(new C{2020})); |
| 236 | r_take_ref_c(C{2020}); |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 237 | r_take_str(rust::Str("2020")); |
| David Tolnay | 633b1f5 | 2020-04-14 16:33:14 -0700 | [diff] [blame] | 238 | r_take_sliceu8( |
| 239 | rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA), 5)); |
| David Tolnay | 40226ab | 2020-03-03 00:05:35 -0800 | [diff] [blame] | 240 | r_take_rust_string(rust::String("2020")); |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 241 | r_take_unique_ptr_string( |
| 242 | std::unique_ptr<std::string>(new std::string("2020"))); |
| 243 | |
| David Tolnay | b6c5ea7 | 2020-03-16 13:36:28 -0700 | [diff] [blame] | 244 | ASSERT(r_try_return_primitive() == 2020); |
| 245 | try { |
| 246 | r_fail_return_primitive(); |
| 247 | ASSERT(false); |
| 248 | } catch (const rust::Error &e) { |
| 249 | ASSERT(std::strcmp(e.what(), "rust error") == 0); |
| 250 | } |
| 251 | |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 252 | auto r2 = r_return_r2(2020); |
| 253 | ASSERT(r2->get() == 2020); |
| 254 | ASSERT(r2->set(2021) == 2021); |
| 255 | ASSERT(r2->get() == 2021); |
| 256 | ASSERT(r2->set(2020) == 2020); |
| 257 | ASSERT(r2->get() == 2020); |
| 258 | |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 259 | cxx_test_suite_set_correct(); |
| 260 | return nullptr; |
| 261 | } |
| 262 | |
| David Tolnay | 97c7210 | 2020-01-25 16:49:00 -0800 | [diff] [blame] | 263 | } // namespace tests |