| 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 | 4037de2 | 2020-04-30 19:51:04 -0700 | [diff] [blame^] | 13 | static constexpr 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 | de5340e | 2020-04-25 14:03:21 -0700 | [diff] [blame] | 31 | const std::vector<uint8_t> &C::get_v() const { return this->v; } |
| 32 | |
| David Tolnay | b871577 | 2020-01-28 00:54:05 -0800 | [diff] [blame] | 33 | size_t c_return_primitive() { return 2020; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 34 | |
| David Tolnay | b871577 | 2020-01-28 00:54:05 -0800 | [diff] [blame] | 35 | Shared c_return_shared() { return Shared{2020}; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 36 | |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 37 | rust::Box<R> c_return_box() { |
| 38 | return rust::Box<R>::from_raw(cxx_test_suite_get_box()); |
| 39 | } |
| 40 | |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 41 | std::unique_ptr<C> c_return_unique_ptr() { |
| 42 | return std::unique_ptr<C>(new C{2020}); |
| 43 | } |
| 44 | |
| David Tolnay | b871577 | 2020-01-28 00:54:05 -0800 | [diff] [blame] | 45 | const size_t &c_return_ref(const Shared &shared) { return shared.z; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 46 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 47 | rust::Str c_return_str(const Shared &shared) { |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 48 | (void)shared; |
| 49 | return "2020"; |
| 50 | } |
| 51 | |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 52 | rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) { |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 53 | (void)shared; |
| David Tolnay | 4037de2 | 2020-04-30 19:51:04 -0700 | [diff] [blame^] | 54 | return rust::Slice<uint8_t>(reinterpret_cast<const uint8_t *>(SLICE_DATA), |
| 55 | sizeof(SLICE_DATA)); |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 58 | rust::String c_return_rust_string() { return "2020"; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 59 | |
| 60 | std::unique_ptr<std::string> c_return_unique_ptr_string() { |
| 61 | return std::unique_ptr<std::string>(new std::string("2020")); |
| 62 | } |
| 63 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 64 | 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] | 65 | auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>()); |
| 66 | vec->push_back(86); |
| 67 | vec->push_back(75); |
| 68 | vec->push_back(30); |
| 69 | vec->push_back(9); |
| 70 | return vec; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() { |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 74 | auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>()); |
| 75 | vec->push_back(86.0); |
| 76 | vec->push_back(75.0); |
| 77 | vec->push_back(30.0); |
| 78 | vec->push_back(9.5); |
| 79 | return vec; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() { |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 83 | auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>()); |
| 84 | vec->push_back(Shared{1010}); |
| 85 | vec->push_back(Shared{1011}); |
| 86 | return vec; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 87 | } |
| 88 | |
| David Tolnay | 1bcc9fe | 2020-04-25 13:51:07 -0700 | [diff] [blame] | 89 | std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() { |
| 90 | return std::unique_ptr<std::vector<C>>(new std::vector<C>()); |
| 91 | } |
| 92 | |
| David Tolnay | de5340e | 2020-04-25 14:03:21 -0700 | [diff] [blame] | 93 | const std::vector<uint8_t> &c_return_ref_vector(const C &c) { |
| 94 | return c.get_v(); |
| 95 | } |
| 96 | |
| David Tolnay | b41e74c | 2020-04-25 15:06:18 -0700 | [diff] [blame] | 97 | rust::Vec<uint8_t> c_return_rust_vec() { |
| 98 | throw std::runtime_error("unimplemented"); |
| 99 | } |
| 100 | |
| David Tolnay | 7798969 | 2020-04-25 15:57:47 -0700 | [diff] [blame] | 101 | const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) { |
| 102 | (void)c; |
| 103 | throw std::runtime_error("unimplemented"); |
| 104 | } |
| 105 | |
| David Tolnay | 378ca50 | 2020-04-27 16:47:55 -0700 | [diff] [blame] | 106 | size_t c_return_identity(size_t n) { return n; } |
| Joel Galenson | ba67607 | 2020-04-27 15:55:45 -0700 | [diff] [blame] | 107 | |
| David Tolnay | 378ca50 | 2020-04-27 16:47:55 -0700 | [diff] [blame] | 108 | size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; } |
| Joel Galenson | ba67607 | 2020-04-27 15:55:45 -0700 | [diff] [blame] | 109 | |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 110 | Enum c_return_enum(uint32_t n) { |
| 111 | if (n <= static_cast<uint32_t>(Enum::AVal)) { |
| 112 | return Enum::AVal; |
| 113 | } else if (n <= static_cast<uint32_t>(Enum::BVal)) { |
| 114 | return Enum::BVal; |
| 115 | } else { |
| 116 | return Enum::CVal; |
| 117 | } |
| 118 | } |
| 119 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 120 | void c_take_primitive(size_t n) { |
| 121 | if (n == 2020) { |
| 122 | cxx_test_suite_set_correct(); |
| 123 | } |
| 124 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 125 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 126 | void c_take_shared(Shared shared) { |
| 127 | if (shared.z == 2020) { |
| 128 | cxx_test_suite_set_correct(); |
| 129 | } |
| 130 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 131 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 132 | void c_take_box(rust::Box<R> r) { |
| David Tolnay | a7d00e8 | 2020-03-06 15:50:14 -0800 | [diff] [blame] | 133 | if (cxx_test_suite_r_is_correct(&*r)) { |
| 134 | cxx_test_suite_set_correct(); |
| 135 | } |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 136 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 137 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 138 | void c_take_unique_ptr(std::unique_ptr<C> c) { |
| 139 | if (c->get() == 2020) { |
| 140 | cxx_test_suite_set_correct(); |
| 141 | } |
| 142 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 143 | |
| David Tolnay | a7d00e8 | 2020-03-06 15:50:14 -0800 | [diff] [blame] | 144 | void c_take_ref_r(const R &r) { |
| 145 | if (cxx_test_suite_r_is_correct(&r)) { |
| 146 | cxx_test_suite_set_correct(); |
| 147 | } |
| 148 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 149 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 150 | void c_take_ref_c(const C &c) { |
| 151 | if (c.get() == 2020) { |
| 152 | cxx_test_suite_set_correct(); |
| 153 | } |
| 154 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 155 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 156 | void c_take_str(rust::Str s) { |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 157 | if (std::string(s) == "2020") { |
| 158 | cxx_test_suite_set_correct(); |
| 159 | } |
| 160 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 161 | |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 162 | void c_take_sliceu8(rust::Slice<uint8_t> s) { |
| David Tolnay | 633b1f5 | 2020-04-14 16:33:14 -0700 | [diff] [blame] | 163 | if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) == |
| 164 | "2020") { |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 165 | cxx_test_suite_set_correct(); |
| 166 | } |
| 167 | } |
| 168 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 169 | void c_take_rust_string(rust::String s) { |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 170 | if (std::string(s) == "2020") { |
| 171 | cxx_test_suite_set_correct(); |
| 172 | } |
| 173 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 174 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 175 | void c_take_unique_ptr_string(std::unique_ptr<std::string> s) { |
| 176 | if (*s == "2020") { |
| 177 | cxx_test_suite_set_correct(); |
| 178 | } |
| 179 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 180 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 181 | void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) { |
| 182 | if (v->size() == 4) { |
| 183 | cxx_test_suite_set_correct(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) { |
| 188 | if (v->size() == 4) { |
| 189 | cxx_test_suite_set_correct(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) { |
| 194 | if (v->size() == 2) { |
| 195 | cxx_test_suite_set_correct(); |
| 196 | } |
| 197 | } |
| 198 | |
| David Tolnay | 2244d1f | 2020-04-25 13:58:18 -0700 | [diff] [blame] | 199 | void c_take_ref_vector(const std::vector<uint8_t> &v) { |
| 200 | if (v.size() == 4) { |
| 201 | cxx_test_suite_set_correct(); |
| 202 | } |
| 203 | } |
| 204 | |
| David Tolnay | d2ce8a9 | 2020-04-25 16:16:45 -0700 | [diff] [blame] | 205 | void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 206 | |
| David Tolnay | d2ce8a9 | 2020-04-25 16:16:45 -0700 | [diff] [blame] | 207 | void c_take_rust_vec_shared(rust::Vec<Shared> v) { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 208 | uint32_t sum = 0; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 209 | for (auto i : v) { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 210 | sum += i.z; |
| 211 | } |
| 212 | if (sum == 2021) { |
| 213 | cxx_test_suite_set_correct(); |
| 214 | } |
| 215 | } |
| 216 | |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 217 | void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) { |
| 218 | // Exercise requirements of ForwardIterator |
| 219 | // https://en.cppreference.com/w/cpp/named_req/ForwardIterator |
| 220 | uint32_t sum = 0; |
| 221 | for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) { |
| 222 | sum += it->z; |
| 223 | } |
| 224 | if (sum == 2021) { |
| 225 | cxx_test_suite_set_correct(); |
| 226 | } |
| 227 | } |
| 228 | |
| David Tolnay | d2ce8a9 | 2020-04-25 16:16:45 -0700 | [diff] [blame] | 229 | void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) { |
| 230 | uint8_t sum = std::accumulate(v.begin(), v.end(), 0); |
| 231 | if (sum == 200) { |
| 232 | cxx_test_suite_set_correct(); |
| 233 | } |
| 234 | } |
| 235 | |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 236 | void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) { |
| 237 | // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the |
| 238 | // requirements for std::iterator_traits. |
| 239 | // https://en.cppreference.com/w/cpp/iterator/iterator_traits |
| David Tolnay | f5aeea2 | 2020-04-30 19:34:51 -0700 | [diff] [blame] | 240 | std::vector<uint8_t> stdv; |
| 241 | std::copy(v.begin(), v.end(), std::back_inserter(stdv)); |
| 242 | uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0); |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 243 | if (sum == 200) { |
| 244 | cxx_test_suite_set_correct(); |
| 245 | } |
| 246 | } |
| 247 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 248 | void c_take_callback(rust::Fn<size_t(rust::String)> callback) { |
| 249 | callback("2020"); |
| 250 | } |
| 251 | |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 252 | void c_take_enum(Enum e) { |
| 253 | if (e == Enum::AVal) { |
| 254 | cxx_test_suite_set_correct(); |
| 255 | } |
| 256 | } |
| 257 | |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 258 | void c_try_return_void() {} |
| 259 | |
| 260 | size_t c_try_return_primitive() { return 2020; } |
| 261 | |
| 262 | size_t c_fail_return_primitive() { throw std::logic_error("logic error"); } |
| 263 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 264 | rust::Box<R> c_try_return_box() { return c_return_box(); } |
| Myron Ahn | 8484930 | 2020-03-25 22:00:58 +0700 | [diff] [blame] | 265 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 266 | const rust::String &c_try_return_ref(const rust::String &s) { return s; } |
| 267 | |
| 268 | rust::Str c_try_return_str(rust::Str s) { return s; } |
| 269 | |
| Adrian Taylor | ec9430e | 2020-04-14 16:09:58 -0700 | [diff] [blame] | 270 | 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] | 271 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 272 | rust::String c_try_return_rust_string() { return c_return_rust_string(); } |
| 273 | |
| 274 | std::unique_ptr<std::string> c_try_return_unique_ptr_string() { |
| 275 | return c_return_unique_ptr_string(); |
| Myron Ahn | 8484930 | 2020-03-25 22:00:58 +0700 | [diff] [blame] | 276 | } |
| 277 | |
| David Tolnay | 8b9d176 | 2020-04-25 16:05:46 -0700 | [diff] [blame] | 278 | rust::Vec<uint8_t> c_try_return_rust_vec() { |
| 279 | throw std::runtime_error("unimplemented"); |
| 280 | } |
| 281 | |
| David Tolnay | 7798969 | 2020-04-25 15:57:47 -0700 | [diff] [blame] | 282 | const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) { |
| 283 | (void)c; |
| 284 | throw std::runtime_error("unimplemented"); |
| 285 | } |
| 286 | |
| David Tolnay | 2fe58c6 | 2020-03-06 16:23:09 -0800 | [diff] [blame] | 287 | extern "C" C *cxx_test_suite_get_unique_ptr() noexcept { |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 288 | return std::unique_ptr<C>(new C{2020}).release(); |
| 289 | } |
| 290 | |
| David Tolnay | 85db2486 | 2020-03-06 16:24:41 -0800 | [diff] [blame] | 291 | extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept { |
| 292 | return std::unique_ptr<std::string>(new std::string("2020")).release(); |
| 293 | } |
| 294 | |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 295 | extern "C" const char *cxx_run_test() noexcept { |
| 296 | #define STRINGIFY(x) #x |
| 297 | #define TOSTRING(x) STRINGIFY(x) |
| 298 | #define ASSERT(x) \ |
| 299 | do { \ |
| 300 | if (!(x)) { \ |
| 301 | return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \ |
| 302 | } \ |
| 303 | } while (false) |
| 304 | |
| 305 | ASSERT(r_return_primitive() == 2020); |
| 306 | ASSERT(r_return_shared().z == 2020); |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 307 | ASSERT(cxx_test_suite_r_is_correct(&*r_return_box())); |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 308 | ASSERT(r_return_unique_ptr()->get() == 2020); |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 309 | ASSERT(r_return_ref(Shared{2020}) == 2020); |
| 310 | ASSERT(std::string(r_return_str(Shared{2020})) == "2020"); |
| 311 | ASSERT(std::string(r_return_rust_string()) == "2020"); |
| David Tolnay | 85db2486 | 2020-03-06 16:24:41 -0800 | [diff] [blame] | 312 | ASSERT(*r_return_unique_ptr_string() == "2020"); |
| Joel Galenson | ba67607 | 2020-04-27 15:55:45 -0700 | [diff] [blame] | 313 | ASSERT(r_return_identity(2020) == 2020); |
| 314 | ASSERT(r_return_sum(2020, 1) == 2021); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 315 | ASSERT(r_return_enum(0) == Enum::AVal); |
| 316 | ASSERT(r_return_enum(1) == Enum::BVal); |
| 317 | ASSERT(r_return_enum(2021) == Enum::CVal); |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 318 | |
| 319 | r_take_primitive(2020); |
| 320 | r_take_shared(Shared{2020}); |
| 321 | r_take_unique_ptr(std::unique_ptr<C>(new C{2020})); |
| 322 | r_take_ref_c(C{2020}); |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 323 | r_take_str(rust::Str("2020")); |
| David Tolnay | 4037de2 | 2020-04-30 19:51:04 -0700 | [diff] [blame^] | 324 | r_take_sliceu8(rust::Slice<uint8_t>( |
| 325 | reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA))); |
| David Tolnay | 40226ab | 2020-03-03 00:05:35 -0800 | [diff] [blame] | 326 | r_take_rust_string(rust::String("2020")); |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 327 | r_take_unique_ptr_string( |
| 328 | std::unique_ptr<std::string>(new std::string("2020"))); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 329 | r_take_enum(Enum::AVal); |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 330 | |
| David Tolnay | b6c5ea7 | 2020-03-16 13:36:28 -0700 | [diff] [blame] | 331 | ASSERT(r_try_return_primitive() == 2020); |
| 332 | try { |
| 333 | r_fail_return_primitive(); |
| 334 | ASSERT(false); |
| 335 | } catch (const rust::Error &e) { |
| 336 | ASSERT(std::strcmp(e.what(), "rust error") == 0); |
| 337 | } |
| 338 | |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 339 | auto r2 = r_return_r2(2020); |
| 340 | ASSERT(r2->get() == 2020); |
| 341 | ASSERT(r2->set(2021) == 2021); |
| 342 | ASSERT(r2->get() == 2021); |
| 343 | ASSERT(r2->set(2020) == 2020); |
| 344 | ASSERT(r2->get() == 2020); |
| 345 | |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 346 | cxx_test_suite_set_correct(); |
| 347 | return nullptr; |
| 348 | } |
| 349 | |
| David Tolnay | 97c7210 | 2020-01-25 16:49:00 -0800 | [diff] [blame] | 350 | } // namespace tests |