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