| David Tolnay | 97c7210 | 2020-01-25 16:49:00 -0800 | [diff] [blame] | 1 | #include "tests/ffi/tests.h" |
| David Tolnay | b871577 | 2020-01-28 00:54:05 -0800 | [diff] [blame] | 2 | #include "tests/ffi/lib.rs" |
| David Tolnay | b6c5ea7 | 2020-03-16 13:36:28 -0700 | [diff] [blame^] | 3 | #include <cstring> |
| David Tolnay | 97c7210 | 2020-01-25 16:49:00 -0800 | [diff] [blame] | 4 | |
| David Tolnay | 2fe58c6 | 2020-03-06 16:23:09 -0800 | [diff] [blame] | 5 | extern "C" void cxx_test_suite_set_correct() noexcept; |
| 6 | extern "C" tests::R *cxx_test_suite_get_box() noexcept; |
| 7 | 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] | 8 | |
| David Tolnay | 97c7210 | 2020-01-25 16:49:00 -0800 | [diff] [blame] | 9 | namespace tests { |
| 10 | |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 11 | C::C(size_t n) : n(n) {} |
| 12 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 13 | size_t C::get() const { return this->n; } |
| 14 | |
| David Tolnay | b871577 | 2020-01-28 00:54:05 -0800 | [diff] [blame] | 15 | size_t c_return_primitive() { return 2020; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 16 | |
| David Tolnay | b871577 | 2020-01-28 00:54:05 -0800 | [diff] [blame] | 17 | Shared c_return_shared() { return Shared{2020}; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 18 | |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 19 | rust::Box<R> c_return_box() { |
| 20 | return rust::Box<R>::from_raw(cxx_test_suite_get_box()); |
| 21 | } |
| 22 | |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 23 | std::unique_ptr<C> c_return_unique_ptr() { |
| 24 | return std::unique_ptr<C>(new C{2020}); |
| 25 | } |
| 26 | |
| David Tolnay | b871577 | 2020-01-28 00:54:05 -0800 | [diff] [blame] | 27 | const size_t &c_return_ref(const Shared &shared) { return shared.z; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 28 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 29 | rust::Str c_return_str(const Shared &shared) { |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 30 | (void)shared; |
| 31 | return "2020"; |
| 32 | } |
| 33 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 34 | rust::String c_return_rust_string() { return "2020"; } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 35 | |
| 36 | std::unique_ptr<std::string> c_return_unique_ptr_string() { |
| 37 | return std::unique_ptr<std::string>(new std::string("2020")); |
| 38 | } |
| 39 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 40 | void c_take_primitive(size_t n) { |
| 41 | if (n == 2020) { |
| 42 | cxx_test_suite_set_correct(); |
| 43 | } |
| 44 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 45 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 46 | void c_take_shared(Shared shared) { |
| 47 | if (shared.z == 2020) { |
| 48 | cxx_test_suite_set_correct(); |
| 49 | } |
| 50 | } |
| 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 | void c_take_box(rust::Box<R> r) { |
| David Tolnay | a7d00e8 | 2020-03-06 15:50:14 -0800 | [diff] [blame] | 53 | if (cxx_test_suite_r_is_correct(&*r)) { |
| 54 | cxx_test_suite_set_correct(); |
| 55 | } |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 56 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 57 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 58 | void c_take_unique_ptr(std::unique_ptr<C> c) { |
| 59 | if (c->get() == 2020) { |
| 60 | cxx_test_suite_set_correct(); |
| 61 | } |
| 62 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 63 | |
| David Tolnay | a7d00e8 | 2020-03-06 15:50:14 -0800 | [diff] [blame] | 64 | void c_take_ref_r(const R &r) { |
| 65 | if (cxx_test_suite_r_is_correct(&r)) { |
| 66 | cxx_test_suite_set_correct(); |
| 67 | } |
| 68 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 69 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 70 | void c_take_ref_c(const C &c) { |
| 71 | if (c.get() == 2020) { |
| 72 | cxx_test_suite_set_correct(); |
| 73 | } |
| 74 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 75 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 76 | void c_take_str(rust::Str s) { |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 77 | if (std::string(s) == "2020") { |
| 78 | cxx_test_suite_set_correct(); |
| 79 | } |
| 80 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 81 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 82 | void c_take_rust_string(rust::String s) { |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 83 | if (std::string(s) == "2020") { |
| 84 | cxx_test_suite_set_correct(); |
| 85 | } |
| 86 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 87 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 88 | void c_take_unique_ptr_string(std::unique_ptr<std::string> s) { |
| 89 | if (*s == "2020") { |
| 90 | cxx_test_suite_set_correct(); |
| 91 | } |
| 92 | } |
| David Tolnay | ad5b8af | 2020-01-26 16:59:13 -0800 | [diff] [blame] | 93 | |
| David Tolnay | 2fe58c6 | 2020-03-06 16:23:09 -0800 | [diff] [blame] | 94 | extern "C" C *cxx_test_suite_get_unique_ptr() noexcept { |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 95 | return std::unique_ptr<C>(new C{2020}).release(); |
| 96 | } |
| 97 | |
| David Tolnay | 85db2486 | 2020-03-06 16:24:41 -0800 | [diff] [blame] | 98 | extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept { |
| 99 | return std::unique_ptr<std::string>(new std::string("2020")).release(); |
| 100 | } |
| 101 | |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 102 | extern "C" const char *cxx_run_test() noexcept { |
| 103 | #define STRINGIFY(x) #x |
| 104 | #define TOSTRING(x) STRINGIFY(x) |
| 105 | #define ASSERT(x) \ |
| 106 | do { \ |
| 107 | if (!(x)) { \ |
| 108 | return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__); \ |
| 109 | } \ |
| 110 | } while (false) |
| 111 | |
| 112 | ASSERT(r_return_primitive() == 2020); |
| 113 | ASSERT(r_return_shared().z == 2020); |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 114 | ASSERT(cxx_test_suite_r_is_correct(&*r_return_box())); |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 115 | ASSERT(r_return_unique_ptr()->get() == 2020); |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 116 | ASSERT(r_return_ref(Shared{2020}) == 2020); |
| 117 | ASSERT(std::string(r_return_str(Shared{2020})) == "2020"); |
| 118 | ASSERT(std::string(r_return_rust_string()) == "2020"); |
| David Tolnay | 85db2486 | 2020-03-06 16:24:41 -0800 | [diff] [blame] | 119 | ASSERT(*r_return_unique_ptr_string() == "2020"); |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 120 | |
| 121 | r_take_primitive(2020); |
| 122 | r_take_shared(Shared{2020}); |
| 123 | r_take_unique_ptr(std::unique_ptr<C>(new C{2020})); |
| 124 | r_take_ref_c(C{2020}); |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 125 | r_take_str(rust::Str("2020")); |
| David Tolnay | 40226ab | 2020-03-03 00:05:35 -0800 | [diff] [blame] | 126 | r_take_rust_string(rust::String("2020")); |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 127 | r_take_unique_ptr_string( |
| 128 | std::unique_ptr<std::string>(new std::string("2020"))); |
| 129 | |
| David Tolnay | b6c5ea7 | 2020-03-16 13:36:28 -0700 | [diff] [blame^] | 130 | ASSERT(r_try_return_primitive() == 2020); |
| 131 | try { |
| 132 | r_fail_return_primitive(); |
| 133 | ASSERT(false); |
| 134 | } catch (const rust::Error &e) { |
| 135 | ASSERT(std::strcmp(e.what(), "rust error") == 0); |
| 136 | } |
| 137 | |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 138 | cxx_test_suite_set_correct(); |
| 139 | return nullptr; |
| 140 | } |
| 141 | |
| David Tolnay | 97c7210 | 2020-01-25 16:49:00 -0800 | [diff] [blame] | 142 | } // namespace tests |