blob: a9cd8e165a32c12dd2ea26b747ae8692d3c61753 [file] [log] [blame]
Adrian Taylor121cca42020-10-10 15:32:00 -07001use cxx_test_suite::extra::ffi2;
David Tolnay362c9f92020-10-10 16:29:58 -07002use cxx_test_suite::ffi;
David Tolnay3fd7f562020-01-26 17:47:11 -08003use std::cell::Cell;
David Tolnayf306da42020-02-22 19:55:43 -08004use std::ffi::CStr;
David Tolnay3fd7f562020-01-26 17:47:11 -08005
6thread_local! {
7 static CORRECT: Cell<bool> = Cell::new(false);
8}
9
10#[no_mangle]
11extern "C" fn cxx_test_suite_set_correct() {
12 CORRECT.with(|correct| correct.set(true));
13}
14
David Tolnayf306da42020-02-22 19:55:43 -080015macro_rules! check {
16 ($run:expr) => {{
17 CORRECT.with(|correct| correct.set(false));
18 $run;
19 assert!(CORRECT.with(|correct| correct.get()), stringify!($run));
20 }};
21}
22
David Tolnay3fd7f562020-01-26 17:47:11 -080023#[test]
24fn test_c_return() {
25 let shared = ffi::Shared { z: 2020 };
26
27 assert_eq!(2020, ffi::c_return_primitive());
28 assert_eq!(2020, ffi::c_return_shared().z);
David Tolnaybe13d8a2020-03-06 15:45:39 -080029 assert_eq!(2020, *ffi::c_return_box());
David Tolnay3fd7f562020-01-26 17:47:11 -080030 ffi::c_return_unique_ptr();
31 assert_eq!(2020, *ffi::c_return_ref(&shared));
32 assert_eq!("2020", ffi::c_return_str(&shared));
Adrian Taylorec9430e2020-04-14 16:09:58 -070033 assert_eq!(b"2020\0", ffi::c_return_sliceu8(&shared));
David Tolnay3fd7f562020-01-26 17:47:11 -080034 assert_eq!("2020", ffi::c_return_rust_string());
David Tolnay5e29b212020-04-17 15:02:16 -070035 assert_eq!("2020", ffi::c_return_unique_ptr_string().to_str().unwrap());
David Tolnayc01d0a02020-04-24 13:30:44 -070036 assert_eq!(4, ffi::c_return_unique_ptr_vector_u8().len());
Myron Ahneba35cf2020-02-05 19:41:51 +070037 assert_eq!(
38 200_u8,
David Tolnay4f6dd4e2020-04-25 13:08:38 -070039 ffi::c_return_unique_ptr_vector_u8().into_iter().sum(),
Myron Ahneba35cf2020-02-05 19:41:51 +070040 );
41 assert_eq!(
42 200.5_f64,
David Tolnay4f6dd4e2020-04-25 13:08:38 -070043 ffi::c_return_unique_ptr_vector_f64().into_iter().sum(),
Myron Ahneba35cf2020-02-05 19:41:51 +070044 );
David Tolnayc01d0a02020-04-24 13:30:44 -070045 assert_eq!(2, ffi::c_return_unique_ptr_vector_shared().len());
Myron Ahneba35cf2020-02-05 19:41:51 +070046 assert_eq!(
47 2021_usize,
48 ffi::c_return_unique_ptr_vector_shared()
Myron Ahneba35cf2020-02-05 19:41:51 +070049 .into_iter()
50 .map(|o| o.z)
David Tolnay4f6dd4e2020-04-25 13:08:38 -070051 .sum(),
Myron Ahneba35cf2020-02-05 19:41:51 +070052 );
Joel Galensonba676072020-04-27 15:55:45 -070053 assert_eq!(2020, ffi::c_return_identity(2020));
54 assert_eq!(2021, ffi::c_return_sum(2020, 1));
Joel Galensonc03402a2020-04-23 17:31:09 -070055 match ffi::c_return_enum(0) {
Joel Galensondb1ec312020-05-01 13:57:32 -070056 enm @ ffi::Enum::AVal => assert_eq!(0, enm.repr),
Joel Galensonc03402a2020-04-23 17:31:09 -070057 _ => assert!(false),
58 }
59 match ffi::c_return_enum(1) {
Joel Galensondb1ec312020-05-01 13:57:32 -070060 enm @ ffi::Enum::BVal => assert_eq!(2020, enm.repr),
Joel Galensonc03402a2020-04-23 17:31:09 -070061 _ => assert!(false),
62 }
63 match ffi::c_return_enum(2021) {
Joel Galensondb1ec312020-05-01 13:57:32 -070064 enm @ ffi::Enum::CVal => assert_eq!(2021, enm.repr),
Joel Galensonc03402a2020-04-23 17:31:09 -070065 _ => assert!(false),
66 }
David Tolnay99642622020-03-25 13:07:35 -070067}
David Tolnayebef4a22020-03-17 15:33:47 -070068
David Tolnay99642622020-03-25 13:07:35 -070069#[test]
70fn test_c_try_return() {
David Tolnayebef4a22020-03-17 15:33:47 -070071 assert_eq!((), ffi::c_try_return_void().unwrap());
72 assert_eq!(2020, ffi::c_try_return_primitive().unwrap());
73 assert_eq!(
74 "logic error",
75 ffi::c_fail_return_primitive().unwrap_err().what(),
76 );
David Tolnay99642622020-03-25 13:07:35 -070077 assert_eq!(2020, *ffi::c_try_return_box().unwrap());
78 assert_eq!("2020", *ffi::c_try_return_ref(&"2020".to_owned()).unwrap());
79 assert_eq!("2020", ffi::c_try_return_str("2020").unwrap());
Adrian Taylorec9430e2020-04-14 16:09:58 -070080 assert_eq!(b"2020", ffi::c_try_return_sliceu8(b"2020").unwrap());
David Tolnay99642622020-03-25 13:07:35 -070081 assert_eq!("2020", ffi::c_try_return_rust_string().unwrap());
David Tolnay5e29b212020-04-17 15:02:16 -070082 assert_eq!("2020", &*ffi::c_try_return_unique_ptr_string().unwrap());
David Tolnay3fd7f562020-01-26 17:47:11 -080083}
84
85#[test]
86fn test_c_take() {
David Tolnay3fd7f562020-01-26 17:47:11 -080087 let unique_ptr = ffi::c_return_unique_ptr();
88
89 check!(ffi::c_take_primitive(2020));
90 check!(ffi::c_take_shared(ffi::Shared { z: 2020 }));
David Tolnaybe13d8a2020-03-06 15:45:39 -080091 check!(ffi::c_take_box(Box::new(2020)));
David Tolnay5e29b212020-04-17 15:02:16 -070092 check!(ffi::c_take_ref_c(&unique_ptr));
David Tolnaya62cca22020-05-07 21:52:16 -070093 check!(cxx_test_suite::module::ffi::c_take_unique_ptr(unique_ptr));
David Tolnay3fd7f562020-01-26 17:47:11 -080094 check!(ffi::c_take_str("2020"));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070095 check!(ffi::c_take_sliceu8(b"2020"));
David Tolnay3fd7f562020-01-26 17:47:11 -080096 check!(ffi::c_take_rust_string("2020".to_owned()));
97 check!(ffi::c_take_unique_ptr_string(
98 ffi::c_return_unique_ptr_string()
99 ));
Myron Ahneba35cf2020-02-05 19:41:51 +0700100 check!(ffi::c_take_unique_ptr_vector_u8(
101 ffi::c_return_unique_ptr_vector_u8()
102 ));
103 check!(ffi::c_take_unique_ptr_vector_f64(
104 ffi::c_return_unique_ptr_vector_f64()
105 ));
106 check!(ffi::c_take_unique_ptr_vector_shared(
107 ffi::c_return_unique_ptr_vector_shared()
108 ));
David Tolnay2244d1f2020-04-25 13:58:18 -0700109 check!(ffi::c_take_ref_vector(&ffi::c_return_unique_ptr_vector_u8()));
myronahnda9be502020-04-29 05:47:23 +0700110 let test_vec = [86_u8, 75_u8, 30_u8, 9_u8].to_vec();
111 check!(ffi::c_take_rust_vec(test_vec.clone()));
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700112 check!(ffi::c_take_rust_vec_index(test_vec.clone()));
David Tolnaya7ba6a62020-08-26 22:33:17 -0700113 let shared_test_vec = vec![ffi::Shared { z: 1010 }, ffi::Shared { z: 1011 }];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700114 check!(ffi::c_take_rust_vec_shared(shared_test_vec.clone()));
115 check!(ffi::c_take_rust_vec_shared_index(shared_test_vec.clone()));
David Tolnaya7ba6a62020-08-26 22:33:17 -0700116 check!(ffi::c_take_rust_vec_shared_forward_iterator(
117 shared_test_vec,
118 ));
myronahnda9be502020-04-29 05:47:23 +0700119 check!(ffi::c_take_ref_rust_vec(&test_vec));
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700120 check!(ffi::c_take_ref_rust_vec_index(&test_vec));
myronahnda9be502020-04-29 05:47:23 +0700121 check!(ffi::c_take_ref_rust_vec_copy(&test_vec));
Joel Galensonc03402a2020-04-23 17:31:09 -0700122 check!(ffi::c_take_enum(ffi::Enum::AVal));
David Tolnay3fd7f562020-01-26 17:47:11 -0800123}
David Tolnayf306da42020-02-22 19:55:43 -0800124
David Tolnay0c8c0f22020-07-21 17:57:46 -0700125/*
126// https://github.com/dtolnay/cxx/issues/232
David Tolnayf306da42020-02-22 19:55:43 -0800127#[test]
David Tolnay75dca2e2020-03-25 20:17:52 -0700128fn test_c_callback() {
129 fn callback(s: String) -> usize {
130 if s == "2020" {
131 cxx_test_suite_set_correct();
132 }
133 0
134 }
135
136 check!(ffi::c_take_callback(callback));
137}
David Tolnay0c8c0f22020-07-21 17:57:46 -0700138*/
David Tolnay75dca2e2020-03-25 20:17:52 -0700139
140#[test]
David Tolnayf306da42020-02-22 19:55:43 -0800141fn test_c_call_r() {
142 fn cxx_run_test() {
143 extern "C" {
144 fn cxx_run_test() -> *const i8;
145 }
146 let failure = unsafe { cxx_run_test() };
147 if !failure.is_null() {
Myron Ahnd963bf92020-05-17 12:32:42 +0700148 let msg = unsafe { CStr::from_ptr(failure as *mut std::os::raw::c_char) };
David Tolnayf306da42020-02-22 19:55:43 -0800149 eprintln!("{}", msg.to_string_lossy());
150 }
151 }
152 check!(cxx_run_test());
153}
David Tolnaybe13d8a2020-03-06 15:45:39 -0800154
Joel Galenson3d4f6122020-04-07 15:54:05 -0700155#[test]
156fn test_c_method_calls() {
157 let mut unique_ptr = ffi::c_return_unique_ptr();
158
David Tolnay5e29b212020-04-17 15:02:16 -0700159 let old_value = unique_ptr.get();
Joel Galenson3d4f6122020-04-07 15:54:05 -0700160 assert_eq!(2020, old_value);
David Tolnay5e29b212020-04-17 15:02:16 -0700161 assert_eq!(2021, unique_ptr.set(2021));
162 assert_eq!(2021, unique_ptr.get());
Joel Galensone1e969d2020-04-21 12:50:20 -0700163 assert_eq!(old_value, unique_ptr.set2(old_value));
myronahne3b78ea2020-05-23 01:08:13 +0700164 assert_eq!(old_value, unique_ptr.get2());
David Tolnayae714362020-05-22 11:12:26 -0700165 assert_eq!(2022, unique_ptr.set_succeed(2022).unwrap());
myronahne3b78ea2020-05-23 01:08:13 +0700166 assert!(unique_ptr.get_fail().is_err());
Joel Galenson3d4f6122020-04-07 15:54:05 -0700167}
168
Joel Galensondb1ec312020-05-01 13:57:32 -0700169#[test]
170fn test_enum_representations() {
171 assert_eq!(0, ffi::Enum::AVal.repr);
172 assert_eq!(2020, ffi::Enum::BVal.repr);
173 assert_eq!(2021, ffi::Enum::CVal.repr);
174}
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}
David Tolnay3bbcdbb2020-10-09 19:29:44 -0700185
186#[test]
187fn test_rust_name_attribute() {
188 assert_eq!("2020", ffi::i32_overloaded_function(2020));
189 assert_eq!("2020", ffi::str_overloaded_function("2020"));
190 let unique_ptr = ffi::c_return_unique_ptr();
191 assert_eq!("2020", unique_ptr.i32_overloaded_method(2020));
192 assert_eq!("2020", unique_ptr.str_overloaded_method("2020"));
193}
Adrian Taylor121cca42020-10-10 15:32:00 -0700194
195#[test]
196fn test_extern_trivial() {
197 let d = ffi2::c_return_trivial();
198 check!(ffi2::c_take_trivial_ref(&d));
199 check!(ffi2::c_take_trivial(d));
200 let d = ffi2::c_return_trivial_ptr();
201 check!(ffi2::c_take_trivial_ptr(d));
202}
203
204#[test]
205fn test_extern_opaque() {
206 let e = ffi2::c_return_opaque_ptr();
207 check!(ffi2::c_take_opaque_ref(e.as_ref().unwrap()));
208 check!(ffi2::c_take_opaque_ptr(e));
209}