| 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)); |
| Adrian Taylor | ec9430e | 2020-04-14 16:09:58 -0700 | [diff] [blame] | 32 | assert_eq!(b"2020\0", ffi::c_return_sliceu8(&shared)); |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 33 | assert_eq!("2020", ffi::c_return_rust_string()); |
| 34 | assert_eq!( |
| 35 | "2020", |
| 36 | ffi::c_return_unique_ptr_string() |
| 37 | .as_ref() |
| 38 | .unwrap() |
| 39 | .to_str() |
| 40 | .unwrap() |
| 41 | ); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 42 | } |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 43 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 44 | #[test] |
| 45 | fn test_c_try_return() { |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 46 | assert_eq!((), ffi::c_try_return_void().unwrap()); |
| 47 | assert_eq!(2020, ffi::c_try_return_primitive().unwrap()); |
| 48 | assert_eq!( |
| 49 | "logic error", |
| 50 | ffi::c_fail_return_primitive().unwrap_err().what(), |
| 51 | ); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 52 | assert_eq!(2020, *ffi::c_try_return_box().unwrap()); |
| 53 | assert_eq!("2020", *ffi::c_try_return_ref(&"2020".to_owned()).unwrap()); |
| 54 | assert_eq!("2020", ffi::c_try_return_str("2020").unwrap()); |
| Adrian Taylor | ec9430e | 2020-04-14 16:09:58 -0700 | [diff] [blame] | 55 | assert_eq!(b"2020", ffi::c_try_return_sliceu8(b"2020").unwrap()); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 56 | assert_eq!("2020", ffi::c_try_return_rust_string().unwrap()); |
| Myron Ahn | 8484930 | 2020-03-25 22:00:58 +0700 | [diff] [blame] | 57 | assert_eq!( |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 58 | "2020", |
| 59 | ffi::c_try_return_unique_ptr_string() |
| 60 | .unwrap() |
| 61 | .as_ref() |
| 62 | .unwrap() |
| Myron Ahn | 8484930 | 2020-03-25 22:00:58 +0700 | [diff] [blame] | 63 | ); |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn test_c_take() { |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 68 | let unique_ptr = ffi::c_return_unique_ptr(); |
| 69 | |
| 70 | check!(ffi::c_take_primitive(2020)); |
| 71 | check!(ffi::c_take_shared(ffi::Shared { z: 2020 })); |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 72 | check!(ffi::c_take_box(Box::new(2020))); |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 73 | check!(ffi::c_take_ref_c(unique_ptr.as_ref().unwrap())); |
| 74 | check!(ffi::c_take_unique_ptr(unique_ptr)); |
| 75 | check!(ffi::c_take_str("2020")); |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 76 | check!(ffi::c_take_sliceu8(b"2020")); |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 77 | check!(ffi::c_take_rust_string("2020".to_owned())); |
| 78 | check!(ffi::c_take_unique_ptr_string( |
| 79 | ffi::c_return_unique_ptr_string() |
| 80 | )); |
| 81 | } |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 82 | |
| 83 | #[test] |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 84 | fn test_c_callback() { |
| 85 | fn callback(s: String) -> usize { |
| 86 | if s == "2020" { |
| 87 | cxx_test_suite_set_correct(); |
| 88 | } |
| 89 | 0 |
| 90 | } |
| 91 | |
| 92 | check!(ffi::c_take_callback(callback)); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 96 | fn test_c_call_r() { |
| 97 | fn cxx_run_test() { |
| 98 | extern "C" { |
| 99 | fn cxx_run_test() -> *const i8; |
| 100 | } |
| 101 | let failure = unsafe { cxx_run_test() }; |
| 102 | if !failure.is_null() { |
| 103 | let msg = unsafe { CStr::from_ptr(failure) }; |
| 104 | eprintln!("{}", msg.to_string_lossy()); |
| 105 | } |
| 106 | } |
| 107 | check!(cxx_run_test()); |
| 108 | } |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 109 | |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 110 | #[test] |
| 111 | fn test_c_method_calls() { |
| 112 | let mut unique_ptr = ffi::c_return_unique_ptr(); |
| 113 | |
| 114 | let old_value = unique_ptr.as_ref().unwrap().get(); |
| 115 | assert_eq!(2020, old_value); |
| 116 | assert_eq!(2021, unique_ptr.as_mut().unwrap().set(2021)); |
| 117 | assert_eq!(2021, unique_ptr.as_ref().unwrap().get()); |
| 118 | assert_eq!(old_value, unique_ptr.as_mut().unwrap().set(old_value)); |
| 119 | assert_eq!(old_value, unique_ptr.as_ref().unwrap().get()) |
| 120 | } |
| 121 | |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 122 | #[no_mangle] |
| 123 | extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R { |
| 124 | Box::into_raw(Box::new(2020usize)) |
| 125 | } |
| David Tolnay | a7d00e8 | 2020-03-06 15:50:14 -0800 | [diff] [blame] | 126 | |
| 127 | #[no_mangle] |
| 128 | unsafe extern "C" fn cxx_test_suite_r_is_correct(r: *const cxx_test_suite::R) -> bool { |
| 129 | *r == 2020 |
| 130 | } |