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