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