| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 1 | use cxx_test_suite::ffi; |
| 2 | use std::cell::Cell; |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 3 | use std::ffi::CStr; |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 4 | |
| 5 | thread_local! { |
| 6 | static CORRECT: Cell<bool> = Cell::new(false); |
| 7 | } |
| 8 | |
| 9 | #[no_mangle] |
| 10 | extern "C" fn cxx_test_suite_set_correct() { |
| 11 | CORRECT.with(|correct| correct.set(true)); |
| 12 | } |
| 13 | |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 14 | macro_rules! check { |
| 15 | ($run:expr) => {{ |
| 16 | CORRECT.with(|correct| correct.set(false)); |
| 17 | $run; |
| 18 | assert!(CORRECT.with(|correct| correct.get()), stringify!($run)); |
| 19 | }}; |
| 20 | } |
| 21 | |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 22 | #[test] |
| 23 | fn test_c_return() { |
| 24 | let shared = ffi::Shared { z: 2020 }; |
| 25 | |
| 26 | assert_eq!(2020, ffi::c_return_primitive()); |
| 27 | assert_eq!(2020, ffi::c_return_shared().z); |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 28 | assert_eq!(2020, *ffi::c_return_box()); |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 29 | ffi::c_return_unique_ptr(); |
| 30 | assert_eq!(2020, *ffi::c_return_ref(&shared)); |
| 31 | assert_eq!("2020", ffi::c_return_str(&shared)); |
| 32 | assert_eq!("2020", ffi::c_return_rust_string()); |
| 33 | assert_eq!( |
| 34 | "2020", |
| 35 | ffi::c_return_unique_ptr_string() |
| 36 | .as_ref() |
| 37 | .unwrap() |
| 38 | .to_str() |
| 39 | .unwrap() |
| 40 | ); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 41 | |
| 42 | assert_eq!((), ffi::c_try_return_void().unwrap()); |
| 43 | assert_eq!(2020, ffi::c_try_return_primitive().unwrap()); |
| 44 | assert_eq!( |
| 45 | "logic error", |
| 46 | ffi::c_fail_return_primitive().unwrap_err().what(), |
| 47 | ); |
| Myron Ahn | 8484930 | 2020-03-25 22:00:58 +0700 | [diff] [blame^] | 48 | assert_eq!( |
| 49 | "ok", |
| 50 | ffi::c_try_return_string() |
| 51 | .unwrap() |
| 52 | .as_ref() |
| 53 | .unwrap() |
| 54 | .to_string() |
| 55 | ); |
| 56 | assert_eq!( |
| 57 | "logic error getting string", |
| 58 | ffi::c_fail_return_string().unwrap_err().what(), |
| 59 | ); |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn test_c_take() { |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 64 | let unique_ptr = ffi::c_return_unique_ptr(); |
| 65 | |
| 66 | check!(ffi::c_take_primitive(2020)); |
| 67 | check!(ffi::c_take_shared(ffi::Shared { z: 2020 })); |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 68 | check!(ffi::c_take_box(Box::new(2020))); |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 69 | check!(ffi::c_take_ref_c(unique_ptr.as_ref().unwrap())); |
| 70 | check!(ffi::c_take_unique_ptr(unique_ptr)); |
| 71 | check!(ffi::c_take_str("2020")); |
| 72 | check!(ffi::c_take_rust_string("2020".to_owned())); |
| 73 | check!(ffi::c_take_unique_ptr_string( |
| 74 | ffi::c_return_unique_ptr_string() |
| 75 | )); |
| 76 | } |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 77 | |
| 78 | #[test] |
| 79 | fn test_c_call_r() { |
| 80 | fn cxx_run_test() { |
| 81 | extern "C" { |
| 82 | fn cxx_run_test() -> *const i8; |
| 83 | } |
| 84 | let failure = unsafe { cxx_run_test() }; |
| 85 | if !failure.is_null() { |
| 86 | let msg = unsafe { CStr::from_ptr(failure) }; |
| 87 | eprintln!("{}", msg.to_string_lossy()); |
| 88 | } |
| 89 | } |
| 90 | check!(cxx_run_test()); |
| 91 | } |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 92 | |
| 93 | #[no_mangle] |
| 94 | extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R { |
| 95 | Box::into_raw(Box::new(2020usize)) |
| 96 | } |
| David Tolnay | a7d00e8 | 2020-03-06 15:50:14 -0800 | [diff] [blame] | 97 | |
| 98 | #[no_mangle] |
| 99 | unsafe extern "C" fn cxx_test_suite_r_is_correct(r: *const cxx_test_suite::R) -> bool { |
| 100 | *r == 2020 |
| 101 | } |