blob: 4fd52d351b4d2d688e087656ecdc9323de975383 [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));
David Tolnay99642622020-03-25 13:07:35 -070054}
David Tolnayebef4a22020-03-17 15:33:47 -070055
David Tolnay99642622020-03-25 13:07:35 -070056#[test]
57fn test_c_try_return() {
David Tolnayebef4a22020-03-17 15:33:47 -070058 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 Tolnay99642622020-03-25 13:07:35 -070064 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 Taylorec9430e2020-04-14 16:09:58 -070067 assert_eq!(b"2020", ffi::c_try_return_sliceu8(b"2020").unwrap());
David Tolnay99642622020-03-25 13:07:35 -070068 assert_eq!("2020", ffi::c_try_return_rust_string().unwrap());
David Tolnay5e29b212020-04-17 15:02:16 -070069 assert_eq!("2020", &*ffi::c_try_return_unique_ptr_string().unwrap());
David Tolnay3fd7f562020-01-26 17:47:11 -080070}
71
72#[test]
73fn test_c_take() {
David Tolnay3fd7f562020-01-26 17:47:11 -080074 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 Tolnaybe13d8a2020-03-06 15:45:39 -080078 check!(ffi::c_take_box(Box::new(2020)));
David Tolnay5e29b212020-04-17 15:02:16 -070079 check!(ffi::c_take_ref_c(&unique_ptr));
David Tolnay3fd7f562020-01-26 17:47:11 -080080 check!(ffi::c_take_unique_ptr(unique_ptr));
81 check!(ffi::c_take_str("2020"));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070082 check!(ffi::c_take_sliceu8(b"2020"));
David Tolnay3fd7f562020-01-26 17:47:11 -080083 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 Ahneba35cf2020-02-05 19:41:51 +070087 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 Tolnay2244d1f2020-04-25 13:58:18 -070096 check!(ffi::c_take_ref_vector(&ffi::c_return_unique_ptr_vector_u8()));
David Tolnayd2ce8a92020-04-25 16:16:45 -070097 check!(ffi::c_take_rust_vec([86_u8, 75_u8, 30_u8, 9_u8].to_vec()));
98 check!(ffi::c_take_rust_vec_shared(vec![
Myron Ahneba35cf2020-02-05 19:41:51 +070099 ffi::Shared { z: 1010 },
100 ffi::Shared { z: 1011 }
101 ]));
David Tolnayd2ce8a92020-04-25 16:16:45 -0700102 check!(ffi::c_take_ref_rust_vec(
103 &[86_u8, 75_u8, 30_u8, 9_u8].to_vec()
104 ));
David Tolnay3fd7f562020-01-26 17:47:11 -0800105}
David Tolnayf306da42020-02-22 19:55:43 -0800106
107#[test]
David Tolnay75dca2e2020-03-25 20:17:52 -0700108fn test_c_callback() {
109 fn callback(s: String) -> usize {
110 if s == "2020" {
111 cxx_test_suite_set_correct();
112 }
113 0
114 }
115
116 check!(ffi::c_take_callback(callback));
117}
118
119#[test]
David Tolnayf306da42020-02-22 19:55:43 -0800120fn test_c_call_r() {
121 fn cxx_run_test() {
122 extern "C" {
123 fn cxx_run_test() -> *const i8;
124 }
125 let failure = unsafe { cxx_run_test() };
126 if !failure.is_null() {
127 let msg = unsafe { CStr::from_ptr(failure) };
128 eprintln!("{}", msg.to_string_lossy());
129 }
130 }
131 check!(cxx_run_test());
132}
David Tolnaybe13d8a2020-03-06 15:45:39 -0800133
Joel Galenson3d4f6122020-04-07 15:54:05 -0700134#[test]
135fn test_c_method_calls() {
136 let mut unique_ptr = ffi::c_return_unique_ptr();
137
David Tolnay5e29b212020-04-17 15:02:16 -0700138 let old_value = unique_ptr.get();
Joel Galenson3d4f6122020-04-07 15:54:05 -0700139 assert_eq!(2020, old_value);
David Tolnay5e29b212020-04-17 15:02:16 -0700140 assert_eq!(2021, unique_ptr.set(2021));
141 assert_eq!(2021, unique_ptr.get());
Joel Galensone1e969d2020-04-21 12:50:20 -0700142 assert_eq!(old_value, unique_ptr.set2(old_value));
143 assert_eq!(old_value, unique_ptr.get2())
Joel Galenson3d4f6122020-04-07 15:54:05 -0700144}
145
David Tolnaybe13d8a2020-03-06 15:45:39 -0800146#[no_mangle]
147extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R {
148 Box::into_raw(Box::new(2020usize))
149}
David Tolnaya7d00e82020-03-06 15:50:14 -0800150
151#[no_mangle]
152unsafe extern "C" fn cxx_test_suite_r_is_correct(r: *const cxx_test_suite::R) -> bool {
153 *r == 2020
154}