blob: 91bd25e41d5f66203925c7f1044c685da29880a3 [file] [log] [blame]
David Tolnay3fd7f562020-01-26 17:47:11 -08001use cxx_test_suite::ffi;
2use std::cell::Cell;
David Tolnayf306da42020-02-22 19:55:43 -08003use std::ffi::CStr;
David Tolnay3fd7f562020-01-26 17:47:11 -08004
5thread_local! {
6 static CORRECT: Cell<bool> = Cell::new(false);
7}
8
9#[no_mangle]
10extern "C" fn cxx_test_suite_set_correct() {
11 CORRECT.with(|correct| correct.set(true));
12}
13
David Tolnayf306da42020-02-22 19:55:43 -080014macro_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 Tolnay3fd7f562020-01-26 17:47:11 -080022#[test]
23fn 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 Tolnaybe13d8a2020-03-06 15:45:39 -080028 assert_eq!(2020, *ffi::c_return_box());
David Tolnay3fd7f562020-01-26 17:47:11 -080029 ffi::c_return_unique_ptr();
30 assert_eq!(2020, *ffi::c_return_ref(&shared));
31 assert_eq!("2020", ffi::c_return_str(&shared));
Adrian Taylorec9430e2020-04-14 16:09:58 -070032 assert_eq!(b"2020\0", ffi::c_return_sliceu8(&shared));
David Tolnay3fd7f562020-01-26 17:47:11 -080033 assert_eq!("2020", ffi::c_return_rust_string());
David Tolnay5e29b212020-04-17 15:02:16 -070034 assert_eq!("2020", ffi::c_return_unique_ptr_string().to_str().unwrap());
David Tolnayc01d0a02020-04-24 13:30:44 -070035 assert_eq!(4, ffi::c_return_unique_ptr_vector_u8().len());
Myron Ahneba35cf2020-02-05 19:41:51 +070036 assert_eq!(
37 200_u8,
David Tolnay4f6dd4e2020-04-25 13:08:38 -070038 ffi::c_return_unique_ptr_vector_u8().into_iter().sum(),
Myron Ahneba35cf2020-02-05 19:41:51 +070039 );
40 assert_eq!(
41 200.5_f64,
David Tolnay4f6dd4e2020-04-25 13:08:38 -070042 ffi::c_return_unique_ptr_vector_f64().into_iter().sum(),
Myron Ahneba35cf2020-02-05 19:41:51 +070043 );
David Tolnayc01d0a02020-04-24 13:30:44 -070044 assert_eq!(2, ffi::c_return_unique_ptr_vector_shared().len());
Myron Ahneba35cf2020-02-05 19:41:51 +070045 assert_eq!(
46 2021_usize,
47 ffi::c_return_unique_ptr_vector_shared()
Myron Ahneba35cf2020-02-05 19:41:51 +070048 .into_iter()
49 .map(|o| o.z)
David Tolnay4f6dd4e2020-04-25 13:08:38 -070050 .sum(),
Myron Ahneba35cf2020-02-05 19:41:51 +070051 );
Joel Galensonba676072020-04-27 15:55:45 -070052 assert_eq!(2020, ffi::c_return_identity(2020));
53 assert_eq!(2021, ffi::c_return_sum(2020, 1));
Joel Galensonc03402a2020-04-23 17:31:09 -070054 match ffi::c_return_enum(0) {
Joel Galensondb1ec312020-05-01 13:57:32 -070055 enm @ ffi::Enum::AVal => assert_eq!(0, enm.repr),
Joel Galensonc03402a2020-04-23 17:31:09 -070056 _ => assert!(false),
57 }
58 match ffi::c_return_enum(1) {
Joel Galensondb1ec312020-05-01 13:57:32 -070059 enm @ ffi::Enum::BVal => assert_eq!(2020, enm.repr),
Joel Galensonc03402a2020-04-23 17:31:09 -070060 _ => assert!(false),
61 }
62 match ffi::c_return_enum(2021) {
Joel Galensondb1ec312020-05-01 13:57:32 -070063 enm @ ffi::Enum::CVal => assert_eq!(2021, enm.repr),
Joel Galensonc03402a2020-04-23 17:31:09 -070064 _ => assert!(false),
65 }
David Tolnay99642622020-03-25 13:07:35 -070066}
David Tolnayebef4a22020-03-17 15:33:47 -070067
David Tolnay99642622020-03-25 13:07:35 -070068#[test]
69fn test_c_try_return() {
David Tolnayebef4a22020-03-17 15:33:47 -070070 assert_eq!((), ffi::c_try_return_void().unwrap());
71 assert_eq!(2020, ffi::c_try_return_primitive().unwrap());
72 assert_eq!(
73 "logic error",
74 ffi::c_fail_return_primitive().unwrap_err().what(),
75 );
David Tolnay99642622020-03-25 13:07:35 -070076 assert_eq!(2020, *ffi::c_try_return_box().unwrap());
77 assert_eq!("2020", *ffi::c_try_return_ref(&"2020".to_owned()).unwrap());
78 assert_eq!("2020", ffi::c_try_return_str("2020").unwrap());
Adrian Taylorec9430e2020-04-14 16:09:58 -070079 assert_eq!(b"2020", ffi::c_try_return_sliceu8(b"2020").unwrap());
David Tolnay99642622020-03-25 13:07:35 -070080 assert_eq!("2020", ffi::c_try_return_rust_string().unwrap());
David Tolnay5e29b212020-04-17 15:02:16 -070081 assert_eq!("2020", &*ffi::c_try_return_unique_ptr_string().unwrap());
David Tolnay3fd7f562020-01-26 17:47:11 -080082}
83
84#[test]
85fn test_c_take() {
David Tolnay3fd7f562020-01-26 17:47:11 -080086 let unique_ptr = ffi::c_return_unique_ptr();
87
88 check!(ffi::c_take_primitive(2020));
89 check!(ffi::c_take_shared(ffi::Shared { z: 2020 }));
David Tolnaybe13d8a2020-03-06 15:45:39 -080090 check!(ffi::c_take_box(Box::new(2020)));
David Tolnay5e29b212020-04-17 15:02:16 -070091 check!(ffi::c_take_ref_c(&unique_ptr));
David Tolnaya62cca22020-05-07 21:52:16 -070092 check!(cxx_test_suite::module::ffi::c_take_unique_ptr(unique_ptr));
David Tolnay3fd7f562020-01-26 17:47:11 -080093 check!(ffi::c_take_str("2020"));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070094 check!(ffi::c_take_sliceu8(b"2020"));
David Tolnay3fd7f562020-01-26 17:47:11 -080095 check!(ffi::c_take_rust_string("2020".to_owned()));
96 check!(ffi::c_take_unique_ptr_string(
97 ffi::c_return_unique_ptr_string()
98 ));
Myron Ahneba35cf2020-02-05 19:41:51 +070099 check!(ffi::c_take_unique_ptr_vector_u8(
100 ffi::c_return_unique_ptr_vector_u8()
101 ));
102 check!(ffi::c_take_unique_ptr_vector_f64(
103 ffi::c_return_unique_ptr_vector_f64()
104 ));
105 check!(ffi::c_take_unique_ptr_vector_shared(
106 ffi::c_return_unique_ptr_vector_shared()
107 ));
David Tolnay2244d1f2020-04-25 13:58:18 -0700108 check!(ffi::c_take_ref_vector(&ffi::c_return_unique_ptr_vector_u8()));
myronahnda9be502020-04-29 05:47:23 +0700109 let test_vec = [86_u8, 75_u8, 30_u8, 9_u8].to_vec();
110 check!(ffi::c_take_rust_vec(test_vec.clone()));
David Tolnayd2ce8a92020-04-25 16:16:45 -0700111 check!(ffi::c_take_rust_vec_shared(vec![
Myron Ahneba35cf2020-02-05 19:41:51 +0700112 ffi::Shared { z: 1010 },
113 ffi::Shared { z: 1011 }
114 ]));
myronahnda9be502020-04-29 05:47:23 +0700115 check!(ffi::c_take_rust_vec_shared_forward_iterator(vec![
116 ffi::Shared { z: 1010 },
117 ffi::Shared { z: 1011 }
118 ]));
119 check!(ffi::c_take_ref_rust_vec(&test_vec));
120 check!(ffi::c_take_ref_rust_vec_copy(&test_vec));
Joel Galensonc03402a2020-04-23 17:31:09 -0700121 check!(ffi::c_take_enum(ffi::Enum::AVal));
David Tolnay3fd7f562020-01-26 17:47:11 -0800122}
David Tolnayf306da42020-02-22 19:55:43 -0800123
124#[test]
David Tolnay75dca2e2020-03-25 20:17:52 -0700125fn test_c_callback() {
126 fn callback(s: String) -> usize {
127 if s == "2020" {
128 cxx_test_suite_set_correct();
129 }
130 0
131 }
132
133 check!(ffi::c_take_callback(callback));
134}
135
136#[test]
David Tolnayf306da42020-02-22 19:55:43 -0800137fn test_c_call_r() {
138 fn cxx_run_test() {
139 extern "C" {
140 fn cxx_run_test() -> *const i8;
141 }
142 let failure = unsafe { cxx_run_test() };
143 if !failure.is_null() {
Myron Ahnd963bf92020-05-17 12:32:42 +0700144 let msg = unsafe { CStr::from_ptr(failure as *mut std::os::raw::c_char) };
David Tolnayf306da42020-02-22 19:55:43 -0800145 eprintln!("{}", msg.to_string_lossy());
146 }
147 }
148 check!(cxx_run_test());
149}
David Tolnaybe13d8a2020-03-06 15:45:39 -0800150
Joel Galenson3d4f6122020-04-07 15:54:05 -0700151#[test]
152fn test_c_method_calls() {
153 let mut unique_ptr = ffi::c_return_unique_ptr();
154
David Tolnay5e29b212020-04-17 15:02:16 -0700155 let old_value = unique_ptr.get();
Joel Galenson3d4f6122020-04-07 15:54:05 -0700156 assert_eq!(2020, old_value);
David Tolnay5e29b212020-04-17 15:02:16 -0700157 assert_eq!(2021, unique_ptr.set(2021));
158 assert_eq!(2021, unique_ptr.get());
Joel Galensone1e969d2020-04-21 12:50:20 -0700159 assert_eq!(old_value, unique_ptr.set2(old_value));
160 assert_eq!(old_value, unique_ptr.get2())
Joel Galenson3d4f6122020-04-07 15:54:05 -0700161}
162
Joel Galensondb1ec312020-05-01 13:57:32 -0700163#[test]
164fn test_enum_representations() {
165 assert_eq!(0, ffi::Enum::AVal.repr);
166 assert_eq!(2020, ffi::Enum::BVal.repr);
167 assert_eq!(2021, ffi::Enum::CVal.repr);
168}
169
David Tolnaybe13d8a2020-03-06 15:45:39 -0800170#[no_mangle]
171extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R {
172 Box::into_raw(Box::new(2020usize))
173}
David Tolnaya7d00e82020-03-06 15:50:14 -0800174
175#[no_mangle]
176unsafe extern "C" fn cxx_test_suite_r_is_correct(r: *const cxx_test_suite::R) -> bool {
177 *r == 2020
178}