Skip to content

Commit b91ca5b

Browse files
committed
Update tests
1 parent 84f9b95 commit b91ca5b

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

‎tests/unit/out/refcount/fn_ref.rs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn square_0(mut x: i32) -> i32 {
10+
return (x * x);
11+
}
12+
pub fn twice_1(mut x: i32) -> i32 {
13+
return (2 * x);
14+
}
15+
pub fn call_ref_2(f: FnPtr<fn(i32) -> i32>, mut x: i32) -> i32 {
16+
return ({ f.call(x) });
17+
}
18+
pub fn call_deduced_3(f: FnPtr<fn(i32) -> i32>, mut x: i32) -> i32 {
19+
return ({ f.call(x) });
20+
}
21+
pub fn call_forwarded_4(f: FnPtr<fn(i32) -> i32>, mut x: i32) -> i32 {
22+
return ({ f.call(x) });
23+
}
24+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
25+
#[byte_size(8)]
26+
pub struct Holder {
27+
#[offset(0)]
28+
#[byte_size(8)]
29+
pub f: FnPtr<fn(i32) -> i32>,
30+
}
31+
pub fn main() {
32+
__cpp2rust_init_globals();
33+
std::process::exit(main_0());
34+
}
35+
fn main_0() -> i32 {
36+
assert!((({ call_ref_2(FnPtr::<fn(i32) -> i32>::new(square_0), 3,) }) == 9));
37+
assert!((({ call_ref_2(FnPtr::<fn(i32) -> i32>::new(twice_1), 3,) }) == 6));
38+
let r: FnPtr<fn(i32) -> i32> = FnPtr::<fn(i32) -> i32>::new(square_0);
39+
assert!((({ r.call(4,) }) == 16));
40+
assert!((({ call_ref_2((r).clone(), 5,) }) == 25));
41+
assert!((({ call_deduced_3(FnPtr::<fn(i32) -> i32>::new(twice_1), 7,) }) == 14));
42+
assert!((({ call_forwarded_4(FnPtr::<fn(i32) -> i32>::new(square_0), 6,) }) == 36));
43+
let h: Value<Holder> = Rc::new(RefCell::new(Holder {
44+
f: FnPtr::<fn(i32) -> i32>::new(twice_1),
45+
}));
46+
assert!((({ HolderImpl::run(&h.as_pointer(), 8,) }) == 16));
47+
return 0;
48+
}
49+
pub trait HolderImpl {
50+
fn run(&self, x: i32) -> i32;
51+
}
52+
impl HolderImpl for Ptr<Holder> {
53+
fn run(&self, mut x: i32) -> i32 {
54+
return ({ (*self).with(|__s| __s.f.clone()).call(x) });
55+
}
56+
}
57+
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/fn_ref.rs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn square_0(mut x: i32) -> i32 {
10+
return ((x) * (x));
11+
}
12+
pub unsafe fn twice_1(mut x: i32) -> i32 {
13+
return ((2) * (x));
14+
}
15+
pub unsafe fn call_ref_2(f: Option<unsafe fn(i32) -> i32>, mut x: i32) -> i32 {
16+
return (unsafe { (f).unwrap()(x) });
17+
}
18+
pub unsafe fn call_deduced_3(f: Option<unsafe fn(i32) -> i32>, mut x: i32) -> i32 {
19+
return (unsafe { (f).unwrap()(x) });
20+
}
21+
pub unsafe fn call_forwarded_4(f: Option<unsafe fn(i32) -> i32>, mut x: i32) -> i32 {
22+
return (unsafe { (f).unwrap()(x) });
23+
}
24+
#[repr(C)]
25+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
26+
pub struct Holder {
27+
pub f: Option<unsafe fn(i32) -> i32>,
28+
}
29+
impl Holder {
30+
pub unsafe fn run(&self, mut x: i32) -> i32 {
31+
return (unsafe { (self.f).unwrap()(x) });
32+
}
33+
}
34+
pub fn main() {
35+
unsafe {
36+
__cpp2rust_init_globals();
37+
std::process::exit(main_0() as i32);
38+
}
39+
}
40+
unsafe fn main_0() -> i32 {
41+
assert!(((unsafe { call_ref_2(Some(square_0), 3,) }) == (9)));
42+
assert!(((unsafe { call_ref_2(Some(twice_1), 3,) }) == (6)));
43+
let r: Option<unsafe fn(i32) -> i32> = Some(square_0);
44+
assert!(((unsafe { (r).unwrap()(4,) }) == (16)));
45+
assert!(((unsafe { call_ref_2(r, 5,) }) == (25)));
46+
assert!(((unsafe { call_deduced_3(Some(twice_1), 7,) }) == (14)));
47+
assert!(((unsafe { call_forwarded_4(Some(square_0), 6,) }) == (36)));
48+
let mut h: Holder = Holder { f: Some(twice_1) };
49+
assert!(((unsafe { Holder::run(&h, 8,) }) == (16)));
50+
return 0;
51+
}
52+
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)