blob: 1cd85dfd123e7d5a0ce1d642b5b1de82e0f3c486 [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 Tolnay99642622020-03-25 13:07:35 -070035}
David Tolnayebef4a22020-03-17 15:33:47 -070036
David Tolnay99642622020-03-25 13:07:35 -070037#[test]
38fn test_c_try_return() {
David Tolnayebef4a22020-03-17 15:33:47 -070039 assert_eq!((), ffi::c_try_return_void().unwrap());
40 assert_eq!(2020, ffi::c_try_return_primitive().unwrap());
41 assert_eq!(
42 "logic error",
43 ffi::c_fail_return_primitive().unwrap_err().what(),
44 );
David Tolnay99642622020-03-25 13:07:35 -070045 assert_eq!(2020, *ffi::c_try_return_box().unwrap());
46 assert_eq!("2020", *ffi::c_try_return_ref(&"2020".to_owned()).unwrap());
47 assert_eq!("2020", ffi::c_try_return_str("2020").unwrap());
Adrian Taylorec9430e2020-04-14 16:09:58 -070048 assert_eq!(b"2020", ffi::c_try_return_sliceu8(b"2020").unwrap());
David Tolnay99642622020-03-25 13:07:35 -070049 assert_eq!("2020", ffi::c_try_return_rust_string().unwrap());
David Tolnay5e29b212020-04-17 15:02:16 -070050 assert_eq!("2020", &*ffi::c_try_return_unique_ptr_string().unwrap());
David Tolnay3fd7f562020-01-26 17:47:11 -080051}
52
53#[test]
54fn test_c_take() {
David Tolnay3fd7f562020-01-26 17:47:11 -080055 let unique_ptr = ffi::c_return_unique_ptr();
56
57 check!(ffi::c_take_primitive(2020));
58 check!(ffi::c_take_shared(ffi::Shared { z: 2020 }));
David Tolnaybe13d8a2020-03-06 15:45:39 -080059 check!(ffi::c_take_box(Box::new(2020)));
David Tolnay5e29b212020-04-17 15:02:16 -070060 check!(ffi::c_take_ref_c(&unique_ptr));
David Tolnay3fd7f562020-01-26 17:47:11 -080061 check!(ffi::c_take_unique_ptr(unique_ptr));
62 check!(ffi::c_take_str("2020"));
Adrian Taylorf5dd5522020-04-13 16:50:14 -070063 check!(ffi::c_take_sliceu8(b"2020"));
David Tolnay3fd7f562020-01-26 17:47:11 -080064 check!(ffi::c_take_rust_string("2020".to_owned()));
65 check!(ffi::c_take_unique_ptr_string(
66 ffi::c_return_unique_ptr_string()
67 ));
68}
David Tolnayf306da42020-02-22 19:55:43 -080069
70#[test]
David Tolnay75dca2e2020-03-25 20:17:52 -070071fn test_c_callback() {
72 fn callback(s: String) -> usize {
73 if s == "2020" {
74 cxx_test_suite_set_correct();
75 }
76 0
77 }
78
79 check!(ffi::c_take_callback(callback));
80}
81
82#[test]
David Tolnayf306da42020-02-22 19:55:43 -080083fn test_c_call_r() {
84 fn cxx_run_test() {
85 extern "C" {
86 fn cxx_run_test() -> *const i8;
87 }
88 let failure = unsafe { cxx_run_test() };
89 if !failure.is_null() {
90 let msg = unsafe { CStr::from_ptr(failure) };
91 eprintln!("{}", msg.to_string_lossy());
92 }
93 }
94 check!(cxx_run_test());
95}
David Tolnaybe13d8a2020-03-06 15:45:39 -080096
Joel Galenson3d4f6122020-04-07 15:54:05 -070097#[test]
98fn test_c_method_calls() {
99 let mut unique_ptr = ffi::c_return_unique_ptr();
100
David Tolnay5e29b212020-04-17 15:02:16 -0700101 let old_value = unique_ptr.get();
Joel Galenson3d4f6122020-04-07 15:54:05 -0700102 assert_eq!(2020, old_value);
David Tolnay5e29b212020-04-17 15:02:16 -0700103 assert_eq!(2021, unique_ptr.set(2021));
104 assert_eq!(2021, unique_ptr.get());
Joel Galensone1e969d2020-04-21 12:50:20 -0700105 assert_eq!(old_value, unique_ptr.set2(old_value));
106 assert_eq!(old_value, unique_ptr.get2())
Joel Galenson3d4f6122020-04-07 15:54:05 -0700107}
108
David Tolnaybe13d8a2020-03-06 15:45:39 -0800109#[no_mangle]
110extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R {
111 Box::into_raw(Box::new(2020usize))
112}
David Tolnaya7d00e82020-03-06 15:50:14 -0800113
114#[no_mangle]
115unsafe extern "C" fn cxx_test_suite_r_is_correct(r: *const cxx_test_suite::R) -> bool {
116 *r == 2020
117}