blob: 6a6fc3e8f01cf2b5d1185f976d2cef590e28ef70 [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));
32 assert_eq!("2020", ffi::c_return_rust_string());
33 assert_eq!(
34 "2020",
35 ffi::c_return_unique_ptr_string()
36 .as_ref()
37 .unwrap()
38 .to_str()
39 .unwrap()
40 );
David Tolnay99642622020-03-25 13:07:35 -070041}
David Tolnayebef4a22020-03-17 15:33:47 -070042
David Tolnay99642622020-03-25 13:07:35 -070043#[test]
44fn test_c_try_return() {
David Tolnayebef4a22020-03-17 15:33:47 -070045 assert_eq!((), ffi::c_try_return_void().unwrap());
46 assert_eq!(2020, ffi::c_try_return_primitive().unwrap());
47 assert_eq!(
48 "logic error",
49 ffi::c_fail_return_primitive().unwrap_err().what(),
50 );
David Tolnay99642622020-03-25 13:07:35 -070051 assert_eq!(2020, *ffi::c_try_return_box().unwrap());
52 assert_eq!("2020", *ffi::c_try_return_ref(&"2020".to_owned()).unwrap());
53 assert_eq!("2020", ffi::c_try_return_str("2020").unwrap());
54 assert_eq!("2020", ffi::c_try_return_rust_string().unwrap());
Myron Ahn84849302020-03-25 22:00:58 +070055 assert_eq!(
David Tolnay99642622020-03-25 13:07:35 -070056 "2020",
57 ffi::c_try_return_unique_ptr_string()
58 .unwrap()
59 .as_ref()
60 .unwrap()
Myron Ahn84849302020-03-25 22:00:58 +070061 );
David Tolnay3fd7f562020-01-26 17:47:11 -080062}
63
64#[test]
65fn test_c_take() {
David Tolnay3fd7f562020-01-26 17:47:11 -080066 let unique_ptr = ffi::c_return_unique_ptr();
67
68 check!(ffi::c_take_primitive(2020));
69 check!(ffi::c_take_shared(ffi::Shared { z: 2020 }));
David Tolnaybe13d8a2020-03-06 15:45:39 -080070 check!(ffi::c_take_box(Box::new(2020)));
David Tolnay3fd7f562020-01-26 17:47:11 -080071 check!(ffi::c_take_ref_c(unique_ptr.as_ref().unwrap()));
72 check!(ffi::c_take_unique_ptr(unique_ptr));
73 check!(ffi::c_take_str("2020"));
74 check!(ffi::c_take_rust_string("2020".to_owned()));
75 check!(ffi::c_take_unique_ptr_string(
76 ffi::c_return_unique_ptr_string()
77 ));
78}
David Tolnayf306da42020-02-22 19:55:43 -080079
80#[test]
81fn test_c_call_r() {
82 fn cxx_run_test() {
83 extern "C" {
84 fn cxx_run_test() -> *const i8;
85 }
86 let failure = unsafe { cxx_run_test() };
87 if !failure.is_null() {
88 let msg = unsafe { CStr::from_ptr(failure) };
89 eprintln!("{}", msg.to_string_lossy());
90 }
91 }
92 check!(cxx_run_test());
93}
David Tolnaybe13d8a2020-03-06 15:45:39 -080094
95#[no_mangle]
96extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R {
97 Box::into_raw(Box::new(2020usize))
98}
David Tolnaya7d00e82020-03-06 15:50:14 -080099
100#[no_mangle]
101unsafe extern "C" fn cxx_test_suite_r_is_correct(r: *const cxx_test_suite::R) -> bool {
102 *r == 2020
103}