| 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()); |
| David Tolnay | 5e29b21 | 2020-04-17 15:02:16 -0700 | [diff] [blame] | 34 | assert_eq!("2020", ffi::c_return_unique_ptr_string().to_str().unwrap()); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 35 | assert_eq!( |
| 36 | 4, |
| 37 | ffi::c_return_unique_ptr_vector_u8() |
| 38 | .as_ref() |
| 39 | .unwrap() |
| 40 | .size() |
| 41 | ); |
| 42 | assert_eq!( |
| 43 | 200_u8, |
| 44 | ffi::c_return_unique_ptr_vector_u8() |
| 45 | .as_ref() |
| 46 | .unwrap() |
| 47 | .into_iter() |
| 48 | .sum() |
| 49 | ); |
| 50 | assert_eq!( |
| 51 | 200.5_f64, |
| 52 | ffi::c_return_unique_ptr_vector_f64() |
| 53 | .as_ref() |
| 54 | .unwrap() |
| 55 | .into_iter() |
| 56 | .sum() |
| 57 | ); |
| 58 | assert_eq!( |
| 59 | 2, |
| 60 | ffi::c_return_unique_ptr_vector_shared() |
| 61 | .as_ref() |
| 62 | .unwrap() |
| 63 | .size() |
| 64 | ); |
| 65 | assert_eq!( |
| 66 | 2021_usize, |
| 67 | ffi::c_return_unique_ptr_vector_shared() |
| 68 | .as_ref() |
| 69 | .unwrap() |
| 70 | .into_iter() |
| 71 | .map(|o| o.z) |
| 72 | .sum() |
| 73 | ); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 74 | } |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 75 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 76 | #[test] |
| 77 | fn test_c_try_return() { |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 78 | assert_eq!((), ffi::c_try_return_void().unwrap()); |
| 79 | assert_eq!(2020, ffi::c_try_return_primitive().unwrap()); |
| 80 | assert_eq!( |
| 81 | "logic error", |
| 82 | ffi::c_fail_return_primitive().unwrap_err().what(), |
| 83 | ); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 84 | assert_eq!( |
| 85 | "ok", |
| 86 | ffi::c_try_return_string() |
| 87 | .unwrap() |
| 88 | .as_ref() |
| 89 | .unwrap() |
| 90 | .to_string() |
| 91 | ); |
| 92 | assert_eq!( |
| 93 | "logic error getting string", |
| 94 | ffi::c_fail_return_string().unwrap_err().what(), |
| 95 | ); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 96 | assert_eq!(2020, *ffi::c_try_return_box().unwrap()); |
| 97 | assert_eq!("2020", *ffi::c_try_return_ref(&"2020".to_owned()).unwrap()); |
| 98 | assert_eq!("2020", ffi::c_try_return_str("2020").unwrap()); |
| Adrian Taylor | ec9430e | 2020-04-14 16:09:58 -0700 | [diff] [blame] | 99 | assert_eq!(b"2020", ffi::c_try_return_sliceu8(b"2020").unwrap()); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 100 | assert_eq!("2020", ffi::c_try_return_rust_string().unwrap()); |
| David Tolnay | 5e29b21 | 2020-04-17 15:02:16 -0700 | [diff] [blame] | 101 | assert_eq!("2020", &*ffi::c_try_return_unique_ptr_string().unwrap()); |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn test_c_take() { |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 106 | let unique_ptr = ffi::c_return_unique_ptr(); |
| 107 | |
| 108 | check!(ffi::c_take_primitive(2020)); |
| 109 | check!(ffi::c_take_shared(ffi::Shared { z: 2020 })); |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 110 | check!(ffi::c_take_box(Box::new(2020))); |
| David Tolnay | 5e29b21 | 2020-04-17 15:02:16 -0700 | [diff] [blame] | 111 | check!(ffi::c_take_ref_c(&unique_ptr)); |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 112 | check!(ffi::c_take_unique_ptr(unique_ptr)); |
| 113 | check!(ffi::c_take_str("2020")); |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 114 | check!(ffi::c_take_sliceu8(b"2020")); |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 115 | check!(ffi::c_take_rust_string("2020".to_owned())); |
| 116 | check!(ffi::c_take_unique_ptr_string( |
| 117 | ffi::c_return_unique_ptr_string() |
| 118 | )); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 119 | check!(ffi::c_take_unique_ptr_vector_u8( |
| 120 | ffi::c_return_unique_ptr_vector_u8() |
| 121 | )); |
| 122 | check!(ffi::c_take_unique_ptr_vector_f64( |
| 123 | ffi::c_return_unique_ptr_vector_f64() |
| 124 | )); |
| 125 | check!(ffi::c_take_unique_ptr_vector_shared( |
| 126 | ffi::c_return_unique_ptr_vector_shared() |
| 127 | )); |
| 128 | |
| 129 | check!(ffi::c_take_vec_u8(&[86_u8, 75_u8, 30_u8, 9_u8].to_vec())); |
| 130 | |
| 131 | check!(ffi::c_take_vec_shared(&vec![ |
| 132 | ffi::Shared { z: 1010 }, |
| 133 | ffi::Shared { z: 1011 } |
| 134 | ])); |
| David Tolnay | 3fd7f56 | 2020-01-26 17:47:11 -0800 | [diff] [blame] | 135 | } |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 136 | |
| 137 | #[test] |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 138 | fn test_c_callback() { |
| 139 | fn callback(s: String) -> usize { |
| 140 | if s == "2020" { |
| 141 | cxx_test_suite_set_correct(); |
| 142 | } |
| 143 | 0 |
| 144 | } |
| 145 | |
| 146 | check!(ffi::c_take_callback(callback)); |
| 147 | } |
| 148 | |
| 149 | #[test] |
| David Tolnay | f306da4 | 2020-02-22 19:55:43 -0800 | [diff] [blame] | 150 | fn test_c_call_r() { |
| 151 | fn cxx_run_test() { |
| 152 | extern "C" { |
| 153 | fn cxx_run_test() -> *const i8; |
| 154 | } |
| 155 | let failure = unsafe { cxx_run_test() }; |
| 156 | if !failure.is_null() { |
| 157 | let msg = unsafe { CStr::from_ptr(failure) }; |
| 158 | eprintln!("{}", msg.to_string_lossy()); |
| 159 | } |
| 160 | } |
| 161 | check!(cxx_run_test()); |
| 162 | } |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 163 | |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 164 | #[test] |
| 165 | fn test_c_method_calls() { |
| 166 | let mut unique_ptr = ffi::c_return_unique_ptr(); |
| 167 | |
| David Tolnay | 5e29b21 | 2020-04-17 15:02:16 -0700 | [diff] [blame] | 168 | let old_value = unique_ptr.get(); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 169 | assert_eq!(2020, old_value); |
| David Tolnay | 5e29b21 | 2020-04-17 15:02:16 -0700 | [diff] [blame] | 170 | assert_eq!(2021, unique_ptr.set(2021)); |
| 171 | assert_eq!(2021, unique_ptr.get()); |
| Joel Galenson | e1e969d | 2020-04-21 12:50:20 -0700 | [diff] [blame] | 172 | assert_eq!(old_value, unique_ptr.set2(old_value)); |
| 173 | assert_eq!(old_value, unique_ptr.get2()) |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| David Tolnay | be13d8a | 2020-03-06 15:45:39 -0800 | [diff] [blame] | 176 | #[no_mangle] |
| 177 | extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R { |
| 178 | Box::into_raw(Box::new(2020usize)) |
| 179 | } |
| David Tolnay | a7d00e8 | 2020-03-06 15:50:14 -0800 | [diff] [blame] | 180 | |
| 181 | #[no_mangle] |
| 182 | unsafe extern "C" fn cxx_test_suite_r_is_correct(r: *const cxx_test_suite::R) -> bool { |
| 183 | *r == 2020 |
| 184 | } |