| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame^] | 1 | use cxx_test_suite::ffi; |
| 2 | use std::cell::Cell; |
| 3 | |
| 4 | thread_local! { |
| 5 | static CORRECT: Cell<bool> = Cell::new(false); |
| 6 | } |
| 7 | |
| 8 | #[no_mangle] |
| 9 | extern "C" fn cxx_test_suite_set_correct() { |
| 10 | CORRECT.with(|correct| correct.set(true)); |
| 11 | } |
| 12 | |
| 13 | #[test] |
| 14 | fn test_c_return() { |
| 15 | let shared = ffi::Shared { z: 2020 }; |
| 16 | |
| 17 | assert_eq!(2020, ffi::c_return_primitive()); |
| 18 | assert_eq!(2020, ffi::c_return_shared().z); |
| 19 | ffi::c_return_unique_ptr(); |
| 20 | assert_eq!(2020, *ffi::c_return_ref(&shared)); |
| 21 | assert_eq!("2020", ffi::c_return_str(&shared)); |
| 22 | assert_eq!("2020", ffi::c_return_rust_string()); |
| 23 | assert_eq!( |
| 24 | "2020", |
| 25 | ffi::c_return_unique_ptr_string() |
| 26 | .as_ref() |
| 27 | .unwrap() |
| 28 | .to_str() |
| 29 | .unwrap() |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn test_c_take() { |
| 35 | macro_rules! check { |
| 36 | ($run:expr) => {{ |
| 37 | CORRECT.with(|correct| correct.set(false)); |
| 38 | $run; |
| 39 | assert!(CORRECT.with(|correct| correct.get()), stringify!($run)); |
| 40 | }}; |
| 41 | } |
| 42 | |
| 43 | let unique_ptr = ffi::c_return_unique_ptr(); |
| 44 | |
| 45 | check!(ffi::c_take_primitive(2020)); |
| 46 | check!(ffi::c_take_shared(ffi::Shared { z: 2020 })); |
| 47 | check!(ffi::c_take_box(Box::new(()))); |
| 48 | check!(ffi::c_take_ref_c(unique_ptr.as_ref().unwrap())); |
| 49 | check!(ffi::c_take_unique_ptr(unique_ptr)); |
| 50 | check!(ffi::c_take_str("2020")); |
| 51 | check!(ffi::c_take_rust_string("2020".to_owned())); |
| 52 | check!(ffi::c_take_unique_ptr_string( |
| 53 | ffi::c_return_unique_ptr_string() |
| 54 | )); |
| 55 | } |