blob: b33c675d023ee0589c8cde6da0a585dffffe7dc7 [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 );
David Tolnay99642622020-03-25 13:07:35 -070084 assert_eq!(2020, *ffi::c_try_return_box().unwrap());
85 assert_eq!("2020", *ffi::c_try_return_ref(&"2020".to_owned()).unwrap());
86 assert_eq!("2020", ffi::c_try_return_str("2020").unwrap());
Adrian Taylorec9430e2020-04-14 16:09:58 -070087 assert_eq!(b"2020", ffi::c_try_return_sliceu8(b"2020").unwrap());
David Tolnay99642622020-03-25 13:07:35 -070088 assert_eq!("2020", ffi::c_try_return_rust_string().unwrap());
David Tolnay5e29b212020-04-17 15:02:16 -070089 assert_eq!("2020", &*ffi::c_try_return_unique_ptr_string().unwrap());
David Tolnay3fd7f562020-01-26 17:47:11 -080090}
91
92#[test]
93fn test_c_take() {
David Tolnay3fd7f562020-01-26 17:47:11 -080094 let unique_ptr = ffi::c_return_unique_ptr();
95
96 check!(ffi::c_take_primitive(2020));
97 check!(ffi::c_take_shared(ffi::Shared { z: 2020 }));
David Tolnaybe13d8a2020-03-06 15:45:39 -080098 check!(ffi::c_take_box(Box::new(2020)));
David Tolnay5e29b212020-04-17 15:02:16 -070099 check!(ffi::c_take_ref_c(&unique_ptr));
David Tolnay3fd7f562020-01-26 17:47:11 -0800100 check!(ffi::c_take_unique_ptr(unique_ptr));
101 check!(ffi::c_take_str("2020"));
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700102 check!(ffi::c_take_sliceu8(b"2020"));
David Tolnay3fd7f562020-01-26 17:47:11 -0800103 check!(ffi::c_take_rust_string("2020".to_owned()));
104 check!(ffi::c_take_unique_ptr_string(
105 ffi::c_return_unique_ptr_string()
106 ));
Myron Ahneba35cf2020-02-05 19:41:51 +0700107 check!(ffi::c_take_unique_ptr_vector_u8(
108 ffi::c_return_unique_ptr_vector_u8()
109 ));
110 check!(ffi::c_take_unique_ptr_vector_f64(
111 ffi::c_return_unique_ptr_vector_f64()
112 ));
113 check!(ffi::c_take_unique_ptr_vector_shared(
114 ffi::c_return_unique_ptr_vector_shared()
115 ));
Myron Ahneba35cf2020-02-05 19:41:51 +0700116 check!(ffi::c_take_vec_u8(&[86_u8, 75_u8, 30_u8, 9_u8].to_vec()));
Myron Ahneba35cf2020-02-05 19:41:51 +0700117 check!(ffi::c_take_vec_shared(&vec![
118 ffi::Shared { z: 1010 },
119 ffi::Shared { z: 1011 }
120 ]));
David Tolnay3fd7f562020-01-26 17:47:11 -0800121}
David Tolnayf306da42020-02-22 19:55:43 -0800122
123#[test]
David Tolnay75dca2e2020-03-25 20:17:52 -0700124fn test_c_callback() {
125 fn callback(s: String) -> usize {
126 if s == "2020" {
127 cxx_test_suite_set_correct();
128 }
129 0
130 }
131
132 check!(ffi::c_take_callback(callback));
133}
134
135#[test]
David Tolnayf306da42020-02-22 19:55:43 -0800136fn test_c_call_r() {
137 fn cxx_run_test() {
138 extern "C" {
139 fn cxx_run_test() -> *const i8;
140 }
141 let failure = unsafe { cxx_run_test() };
142 if !failure.is_null() {
143 let msg = unsafe { CStr::from_ptr(failure) };
144 eprintln!("{}", msg.to_string_lossy());
145 }
146 }
147 check!(cxx_run_test());
148}
David Tolnaybe13d8a2020-03-06 15:45:39 -0800149
Joel Galenson3d4f6122020-04-07 15:54:05 -0700150#[test]
151fn test_c_method_calls() {
152 let mut unique_ptr = ffi::c_return_unique_ptr();
153
David Tolnay5e29b212020-04-17 15:02:16 -0700154 let old_value = unique_ptr.get();
Joel Galenson3d4f6122020-04-07 15:54:05 -0700155 assert_eq!(2020, old_value);
David Tolnay5e29b212020-04-17 15:02:16 -0700156 assert_eq!(2021, unique_ptr.set(2021));
157 assert_eq!(2021, unique_ptr.get());
Joel Galensone1e969d2020-04-21 12:50:20 -0700158 assert_eq!(old_value, unique_ptr.set2(old_value));
159 assert_eq!(old_value, unique_ptr.get2())
Joel Galenson3d4f6122020-04-07 15:54:05 -0700160}
161
David Tolnaybe13d8a2020-03-06 15:45:39 -0800162#[no_mangle]
163extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R {
164 Box::into_raw(Box::new(2020usize))
165}
David Tolnaya7d00e82020-03-06 15:50:14 -0800166
167#[no_mangle]
168unsafe extern "C" fn cxx_test_suite_r_is_correct(r: *const cxx_test_suite::R) -> bool {
169 *r == 2020
170}