Skip to content

Commit 7a5ba3d

Browse files
committed
Add expected output for fn_ptr tests
1 parent 537ccbe commit 7a5ba3d

26 files changed

Lines changed: 1776 additions & 0 deletions

tests/unit/out/refcount/fn_ptr.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::AsFd;
9+
use std::rc::{Rc, Weak};
10+
pub fn my_foo_0(p: AnyPtr) -> i32 {
11+
let p: Value<AnyPtr> = Rc::new(RefCell::new(p));
12+
return ((*p.borrow()).cast::<i32>().expect("ub:wrong type").read());
13+
}
14+
pub fn foo_1(fn_: FnPtr<fn(AnyPtr) -> i32>, pi: Ptr<i32>) -> i32 {
15+
let fn_: Value<FnPtr<fn(AnyPtr) -> i32>> = Rc::new(RefCell::new(fn_));
16+
let pi: Value<Ptr<i32>> = Rc::new(RefCell::new(pi));
17+
return ({
18+
let _arg0: AnyPtr = ((*pi.borrow()).clone() as Ptr<i32>).to_any();
19+
(*(*fn_.borrow()))(_arg0)
20+
});
21+
}
22+
pub fn main() {
23+
std::process::exit(main_0());
24+
}
25+
fn main_0() -> i32 {
26+
let fn_: Value<FnPtr<fn(AnyPtr) -> i32>> = Rc::new(RefCell::new(FnPtr::null()));
27+
assert!((*fn_.borrow()).is_null());
28+
assert!({
29+
let _lhs = (*fn_.borrow()).clone();
30+
_lhs != fn_ptr!(my_foo_0, fn(AnyPtr) -> i32)
31+
});
32+
(*fn_.borrow_mut()) = fn_ptr!(my_foo_0, fn(AnyPtr) -> i32);
33+
assert!(!((*fn_.borrow()).is_null()));
34+
assert!({
35+
let _lhs = (*fn_.borrow()).clone();
36+
_lhs == fn_ptr!(my_foo_0, fn(AnyPtr) -> i32)
37+
});
38+
let a: Value<i32> = Rc::new(RefCell::new(10));
39+
assert!({
40+
let _lhs = ({
41+
let _fn: FnPtr<fn(AnyPtr) -> i32> = (*fn_.borrow()).clone();
42+
let _pi: Ptr<i32> = (a.as_pointer());
43+
foo_1(_fn, _pi)
44+
});
45+
_lhs == (*a.borrow())
46+
});
47+
return 0;
48+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::AsFd;
9+
use std::rc::{Rc, Weak};
10+
pub fn add_0(a: i32, b: i32) -> i32 {
11+
let a: Value<i32> = Rc::new(RefCell::new(a));
12+
let b: Value<i32> = Rc::new(RefCell::new(b));
13+
return ((*a.borrow()) + (*b.borrow()));
14+
}
15+
pub fn sub_1(a: i32, b: i32) -> i32 {
16+
let a: Value<i32> = Rc::new(RefCell::new(a));
17+
let b: Value<i32> = Rc::new(RefCell::new(b));
18+
return ((*a.borrow()) - (*b.borrow()));
19+
}
20+
pub fn mul_2(a: i32, b: i32) -> i32 {
21+
let a: Value<i32> = Rc::new(RefCell::new(a));
22+
let b: Value<i32> = Rc::new(RefCell::new(b));
23+
return ((*a.borrow()) * (*b.borrow()));
24+
}
25+
pub fn main() {
26+
std::process::exit(main_0());
27+
}
28+
fn main_0() -> i32 {
29+
let ops: Value<Box<[FnPtr<fn(i32, i32) -> i32>]>> = Rc::new(RefCell::new(Box::new([
30+
fn_ptr!(add_0, fn(i32, i32) -> i32),
31+
fn_ptr!(sub_1, fn(i32, i32) -> i32),
32+
fn_ptr!(mul_2, fn(i32, i32) -> i32),
33+
])));
34+
assert!(
35+
(({
36+
let _arg0: i32 = 2;
37+
let _arg1: i32 = 3;
38+
(*(*ops.borrow())[(0) as usize])(_arg0, _arg1)
39+
}) == 5)
40+
);
41+
assert!(
42+
(({
43+
let _arg0: i32 = 7;
44+
let _arg1: i32 = 4;
45+
(*(*ops.borrow())[(1) as usize])(_arg0, _arg1)
46+
}) == 3)
47+
);
48+
assert!(
49+
(({
50+
let _arg0: i32 = 6;
51+
let _arg1: i32 = 5;
52+
(*(*ops.borrow())[(2) as usize])(_arg0, _arg1)
53+
}) == 30)
54+
);
55+
assert!(!(((*ops.borrow())[(0) as usize]).is_null()));
56+
assert!(((*ops.borrow())[(0) as usize] == fn_ptr!(add_0, fn(i32, i32) -> i32)));
57+
assert!(((*ops.borrow())[(0) as usize] != fn_ptr!(sub_1, fn(i32, i32) -> i32)));
58+
return 0;
59+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::AsFd;
9+
use std::rc::{Rc, Weak};
10+
pub fn double_it_0(x: Ptr<i32>) {
11+
let x: Value<Ptr<i32>> = Rc::new(RefCell::new(x));
12+
{
13+
let __ptr = (*x.borrow()).clone();
14+
let __tmp = __ptr.read() * 2;
15+
__ptr.write(__tmp)
16+
};
17+
}
18+
pub fn maybe_call_1(cb: FnPtr<fn(Ptr<i32>)>, x: Ptr<i32>) {
19+
let cb: Value<FnPtr<fn(Ptr<i32>)>> = Rc::new(RefCell::new(cb));
20+
let x: Value<Ptr<i32>> = Rc::new(RefCell::new(x));
21+
if !(*cb.borrow()).is_null() {
22+
({
23+
let _arg0: Ptr<i32> = (*x.borrow()).clone();
24+
(*(*cb.borrow()))(_arg0)
25+
});
26+
}
27+
}
28+
pub fn main() {
29+
std::process::exit(main_0());
30+
}
31+
fn main_0() -> i32 {
32+
let a: Value<i32> = Rc::new(RefCell::new(5));
33+
({
34+
let _cb: FnPtr<fn(Ptr<i32>)> = fn_ptr!(double_it_0, fn(Ptr::<i32>));
35+
let _x: Ptr<i32> = (a.as_pointer());
36+
maybe_call_1(_cb, _x)
37+
});
38+
assert!(((*a.borrow()) == 10));
39+
let b: Value<i32> = Rc::new(RefCell::new(5));
40+
({
41+
let _cb: FnPtr<fn(Ptr<i32>)> = FnPtr::null();
42+
let _x: Ptr<i32> = (b.as_pointer());
43+
maybe_call_1(_cb, _x)
44+
});
45+
assert!(((*b.borrow()) == 5));
46+
let fn_: Value<FnPtr<fn(Ptr<i32>)>> = Rc::new(RefCell::new(FnPtr::null()));
47+
if !!(*fn_.borrow()).is_null() {
48+
(*fn_.borrow_mut()) = (fn_ptr!(double_it_0, fn(Ptr::<i32>))).clone();
49+
}
50+
let c: Value<i32> = Rc::new(RefCell::new(3));
51+
if !(*fn_.borrow()).is_null() {
52+
({
53+
let _arg0: Ptr<i32> = (c.as_pointer());
54+
(*(*fn_.borrow()))(_arg0)
55+
});
56+
}
57+
assert!(((*c.borrow()) == 6));
58+
return 0;
59+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::AsFd;
9+
use std::rc::{Rc, Weak};
10+
pub fn double_it_0(x: i32) -> i32 {
11+
let x: Value<i32> = Rc::new(RefCell::new(x));
12+
return ((*x.borrow()) * 2);
13+
}
14+
pub fn test_roundtrip_1() {
15+
let fn_: Value<FnPtr<fn(i32) -> i32>> =
16+
Rc::new(RefCell::new(fn_ptr!(double_it_0, fn(i32) -> i32)));
17+
assert!(
18+
(({
19+
let _arg0: i32 = 5;
20+
(*(*fn_.borrow()))(_arg0)
21+
}) == 10)
22+
);
23+
let gfn: Value<FnPtr<fn()>> =
24+
Rc::new(RefCell::new(((*fn_.borrow()).cast::<fn()>(None)).clone()));
25+
assert!(!((*gfn.borrow()).is_null()));
26+
let fn2: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(
27+
((*gfn.borrow()).cast::<fn(i32) -> i32>(None)).clone(),
28+
));
29+
assert!(
30+
(({
31+
let _arg0: i32 = 5;
32+
(*(*fn2.borrow()))(_arg0)
33+
}) == 10)
34+
);
35+
assert!({
36+
let _lhs = (*fn2.borrow()).clone();
37+
_lhs == (*fn_.borrow()).clone()
38+
});
39+
}
40+
pub fn test_double_cast_2() {
41+
let fn_: Value<FnPtr<fn(i32) -> i32>> =
42+
Rc::new(RefCell::new(fn_ptr!(double_it_0, fn(i32) -> i32)));
43+
let fn2: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(
44+
((*fn_.borrow())
45+
.cast::<fn()>(None)
46+
.cast::<fn(i32) -> i32>(None))
47+
.clone(),
48+
));
49+
assert!(
50+
(({
51+
let _arg0: i32 = 5;
52+
(*(*fn2.borrow()))(_arg0)
53+
}) == 10)
54+
);
55+
assert!({
56+
let _lhs = (*fn2.borrow()).clone();
57+
_lhs == (*fn_.borrow()).clone()
58+
});
59+
}
60+
#[derive(Default)]
61+
pub struct Command {
62+
pub data: Value<AnyPtr>,
63+
}
64+
impl Clone for Command {
65+
fn clone(&self) -> Self {
66+
let mut this = Self {
67+
data: Rc::new(RefCell::new((*self.data.borrow()).clone())),
68+
};
69+
this
70+
}
71+
}
72+
impl ByteRepr for Command {}
73+
pub fn test_void_ptr_to_fn_3() {
74+
let cmd: Value<Command> = Rc::new(RefCell::new(<Command>::default()));
75+
(*(*cmd.borrow()).data.borrow_mut()) = fn_ptr!(double_it_0, fn(i32) -> i32).to_any();
76+
let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(
77+
((*(*cmd.borrow()).data.borrow())
78+
.cast_fn::<fn(i32) -> i32>()
79+
.expect("ub:wrong fn type"))
80+
.clone(),
81+
));
82+
assert!(
83+
(({
84+
let _arg0: i32 = 5;
85+
(*(*fn_.borrow()))(_arg0)
86+
}) == 10)
87+
);
88+
}
89+
pub fn add_offset_4(base: Ptr<i32>, offset: i32) -> i32 {
90+
let base: Value<Ptr<i32>> = Rc::new(RefCell::new(base));
91+
let offset: Value<i32> = Rc::new(RefCell::new(offset));
92+
return {
93+
let _lhs = ((*base.borrow()).read());
94+
_lhs + (*offset.borrow())
95+
};
96+
}
97+
pub fn test_call_through_cast_5() {
98+
let gfn: Value<FnPtr<fn(AnyPtr, i32) -> i32>> = Rc::new(RefCell::new(
99+
fn_ptr!(add_offset_4, fn(Ptr::<i32>, i32) -> i32).cast::<fn(AnyPtr, i32) -> i32>(Some(
100+
(|a0: AnyPtr, a1: i32| -> i32 { add_offset_4(a0.cast::<i32>().unwrap(), a1) })
101+
as fn(AnyPtr, i32) -> i32,
102+
)),
103+
));
104+
let val: Value<i32> = Rc::new(RefCell::new(100));
105+
let result: Value<i32> = Rc::new(RefCell::new(
106+
({
107+
let _arg0: AnyPtr = ((val.as_pointer()) as Ptr<i32>).to_any();
108+
let _arg1: i32 = 42;
109+
(*(*gfn.borrow()))(_arg0, _arg1)
110+
}),
111+
));
112+
assert!(((*result.borrow()) == 142));
113+
}
114+
pub fn main() {
115+
std::process::exit(main_0());
116+
}
117+
fn main_0() -> i32 {
118+
({ test_roundtrip_1() });
119+
({ test_double_cast_2() });
120+
({ test_void_ptr_to_fn_3() });
121+
({ test_call_through_cast_5() });
122+
return 0;
123+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::AsFd;
9+
use std::rc::{Rc, Weak};
10+
pub fn inc_0(x: i32) -> i32 {
11+
let x: Value<i32> = Rc::new(RefCell::new(x));
12+
return ((*x.borrow()) + 1);
13+
}
14+
pub fn dec_1(x: i32) -> i32 {
15+
let x: Value<i32> = Rc::new(RefCell::new(x));
16+
return ((*x.borrow()) - 1);
17+
}
18+
pub fn identity_2(x: i32) -> i32 {
19+
let x: Value<i32> = Rc::new(RefCell::new(x));
20+
return (*x.borrow());
21+
}
22+
pub fn pick_3(mode: i32) -> FnPtr<fn(i32) -> i32> {
23+
let mode: Value<i32> = Rc::new(RefCell::new(mode));
24+
return if ((*mode.borrow()) > 0) {
25+
fn_ptr!(inc_0, fn(i32) -> i32)
26+
} else {
27+
if ((*mode.borrow()) < 0) {
28+
fn_ptr!(dec_1, fn(i32) -> i32)
29+
} else {
30+
fn_ptr!(identity_2, fn(i32) -> i32)
31+
}
32+
};
33+
}
34+
pub fn apply_4(fn_: FnPtr<fn(i32) -> i32>, x: i32) -> i32 {
35+
let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(fn_));
36+
let x: Value<i32> = Rc::new(RefCell::new(x));
37+
let actual: Value<FnPtr<fn(i32) -> i32>> =
38+
Rc::new(RefCell::new(if !(*fn_.borrow()).is_null() {
39+
(*fn_.borrow()).clone()
40+
} else {
41+
fn_ptr!(identity_2, fn(i32) -> i32)
42+
}));
43+
return ({
44+
let _arg0: i32 = (*x.borrow());
45+
(*(*actual.borrow()))(_arg0)
46+
});
47+
}
48+
pub fn main() {
49+
std::process::exit(main_0());
50+
}
51+
fn main_0() -> i32 {
52+
assert!(
53+
(({
54+
let _arg0: i32 = 10;
55+
(*({
56+
let _mode: i32 = 1;
57+
pick_3(_mode)
58+
}))(_arg0)
59+
}) == 11)
60+
);
61+
assert!(
62+
(({
63+
let _arg0: i32 = 10;
64+
(*({
65+
let _mode: i32 = -1_i32;
66+
pick_3(_mode)
67+
}))(_arg0)
68+
}) == 9)
69+
);
70+
assert!(
71+
(({
72+
let _arg0: i32 = 10;
73+
(*({
74+
let _mode: i32 = 0;
75+
pick_3(_mode)
76+
}))(_arg0)
77+
}) == 10)
78+
);
79+
assert!(
80+
(({
81+
let _fn: FnPtr<fn(i32) -> i32> = fn_ptr!(inc_0, fn(i32) -> i32);
82+
let _x: i32 = 5;
83+
apply_4(_fn, _x)
84+
}) == 6)
85+
);
86+
assert!(
87+
(({
88+
let _fn: FnPtr<fn(i32) -> i32> = FnPtr::null();
89+
let _x: i32 = 5;
90+
apply_4(_fn, _x)
91+
}) == 5)
92+
);
93+
return 0;
94+
}

0 commit comments

Comments
 (0)