blob: 17fcfcabf08789b4d257c65f766f194cfb3ad4f6 [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());
Myron Ahneba35cf2020-02-05 19:41:51 +070035 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 Tolnay99642622020-03-25 13:07:35 -070074}
David Tolnayebef4a22020-03-17 15:33:47 -070075
David Tolnay99642622020-03-25 13:07:35 -070076#[test]
77fn test_c_try_return() {
David Tolnayebef4a22020-03-17 15:33:47 -070078 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 Ahneba35cf2020-02-05 19:41:51 +070084 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 Tolnay99642622020-03-25 13:07:35 -070096 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 Taylorec9430e2020-04-14 16:09:58 -070099 assert_eq!(b"2020", ffi::c_try_return_sliceu8(b"2020").unwrap());
David Tolnay99642622020-03-25 13:07:35 -0700100 assert_eq!("2020", ffi::c_try_return_rust_string().unwrap());
David Tolnay5e29b212020-04-17 15:02:16 -0700101 assert_eq!("2020", &*ffi::c_try_return_unique_ptr_string().unwrap());
David Tolnay3fd7f562020-01-26 17:47:11 -0800102}
103
104#[test]
105fn test_c_take() {
David Tolnay3fd7f562020-01-26 17:47:11 -0800106 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 Tolnaybe13d8a2020-03-06 15:45:39 -0800110 check!(ffi::c_take_box(Box::new(2020)));
David Tolnay5e29b212020-04-17 15:02:16 -0700111 check!(ffi::c_take_ref_c(&unique_ptr));
David Tolnay3fd7f562020-01-26 17:47:11 -0800112 check!(ffi::c_take_unique_ptr(unique_ptr));
113 check!(ffi::c_take_str("2020"));
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700114 check!(ffi::c_take_sliceu8(b"2020"));
David Tolnay3fd7f562020-01-26 17:47:11 -0800115 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 Ahneba35cf2020-02-05 19:41:51 +0700119 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 Tolnay3fd7f562020-01-26 17:47:11 -0800135}
David Tolnayf306da42020-02-22 19:55:43 -0800136
137#[test]
David Tolnay75dca2e2020-03-25 20:17:52 -0700138fn 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 Tolnayf306da42020-02-22 19:55:43 -0800150fn 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 Tolnaybe13d8a2020-03-06 15:45:39 -0800163
Joel Galenson3d4f6122020-04-07 15:54:05 -0700164#[test]
165fn test_c_method_calls() {
166 let mut unique_ptr = ffi::c_return_unique_ptr();
167
David Tolnay5e29b212020-04-17 15:02:16 -0700168 let old_value = unique_ptr.get();
Joel Galenson3d4f6122020-04-07 15:54:05 -0700169 assert_eq!(2020, old_value);
David Tolnay5e29b212020-04-17 15:02:16 -0700170 assert_eq!(2021, unique_ptr.set(2021));
171 assert_eq!(2021, unique_ptr.get());
Joel Galensone1e969d2020-04-21 12:50:20 -0700172 assert_eq!(old_value, unique_ptr.set2(old_value));
173 assert_eq!(old_value, unique_ptr.get2())
Joel Galenson3d4f6122020-04-07 15:54:05 -0700174}
175
David Tolnaybe13d8a2020-03-06 15:45:39 -0800176#[no_mangle]
177extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R {
178 Box::into_raw(Box::new(2020usize))
179}
David Tolnaya7d00e82020-03-06 15:50:14 -0800180
181#[no_mangle]
182unsafe extern "C" fn cxx_test_suite_r_is_correct(r: *const cxx_test_suite::R) -> bool {
183 *r == 2020
184}