Skip to content

Commit 119ddea

Browse files
committed
Add vaarg func pointer test
1 parent 4d885f9 commit 119ddea

3 files changed

Lines changed: 208 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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(x: i32) -> i32 {
10+
let x: Value<i32> = Rc::new(RefCell::new(x));
11+
return ((*x.borrow()) * (*x.borrow()));
12+
}
13+
pub fn negate_1(x: i32) -> i32 {
14+
let x: Value<i32> = Rc::new(RefCell::new(x));
15+
return -(*x.borrow());
16+
}
17+
pub fn add_2(a: i32, b: i32) -> i32 {
18+
let a: Value<i32> = Rc::new(RefCell::new(a));
19+
let b: Value<i32> = Rc::new(RefCell::new(b));
20+
return ((*a.borrow()) + (*b.borrow()));
21+
}
22+
pub fn apply_unary_3(x: i32, args: &[VaArg]) -> i32 {
23+
let x: Value<i32> = Rc::new(RefCell::new(x));
24+
let ap: Value<VaList> = Rc::new(RefCell::new(VaList::default()));
25+
(*ap.borrow_mut()) = VaList::new(args);
26+
let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(
27+
((*ap.borrow_mut()).arg::<FnPtr<fn(i32) -> i32>>()).clone(),
28+
));
29+
let result: Value<i32> = Rc::new(RefCell::new(
30+
({
31+
let _arg0: i32 = (*x.borrow());
32+
(*(*fn_.borrow()))(_arg0)
33+
}),
34+
));
35+
return (*result.borrow());
36+
}
37+
pub fn apply_binary_4(a: i32, b: i32, args: &[VaArg]) -> i32 {
38+
let a: Value<i32> = Rc::new(RefCell::new(a));
39+
let b: Value<i32> = Rc::new(RefCell::new(b));
40+
let ap: Value<VaList> = Rc::new(RefCell::new(VaList::default()));
41+
(*ap.borrow_mut()) = VaList::new(args);
42+
let fn_: Value<FnPtr<fn(i32, i32) -> i32>> = Rc::new(RefCell::new(
43+
((*ap.borrow_mut()).arg::<FnPtr<fn(i32, i32) -> i32>>()).clone(),
44+
));
45+
let result: Value<i32> = Rc::new(RefCell::new(
46+
({
47+
let _arg0: i32 = (*a.borrow());
48+
let _arg1: i32 = (*b.borrow());
49+
(*(*fn_.borrow()))(_arg0, _arg1)
50+
}),
51+
));
52+
return (*result.borrow());
53+
}
54+
pub fn main() {
55+
std::process::exit(main_0());
56+
}
57+
fn main_0() -> i32 {
58+
assert!(
59+
(((({
60+
let _x: i32 = 5;
61+
apply_unary_3(_x, &[FnPtr::<fn(i32) -> i32>::new(square_0).into()])
62+
}) == 25) as i32)
63+
!= 0)
64+
);
65+
assert!(
66+
(((({
67+
let _x: i32 = 7;
68+
apply_unary_3(_x, &[FnPtr::<fn(i32) -> i32>::new(negate_1).into()])
69+
}) == -7_i32) as i32)
70+
!= 0)
71+
);
72+
assert!(
73+
(((({
74+
let _a: i32 = 3;
75+
let _b: i32 = 4;
76+
apply_binary_4(_a, _b, &[FnPtr::<fn(i32, i32) -> i32>::new(add_2).into()])
77+
}) == 7) as i32)
78+
!= 0)
79+
);
80+
return 0;
81+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 negate_1(mut x: i32) -> i32 {
13+
return -x;
14+
}
15+
pub unsafe fn add_2(mut a: i32, mut b: i32) -> i32 {
16+
return ((a) + (b));
17+
}
18+
pub unsafe fn apply_unary_3(mut x: i32, args: &[VaArg]) -> i32 {
19+
let mut ap: VaList = VaList::default();
20+
ap = VaList::new(args);
21+
let mut fn_: Option<unsafe fn(i32) -> i32> = std::mem::transmute::<
22+
*mut ::libc::c_void,
23+
Option<unsafe fn(i32) -> i32>,
24+
>(ap.arg::<*mut ::libc::c_void>());
25+
let mut result: i32 = (unsafe {
26+
let _arg0: i32 = x;
27+
(fn_).unwrap()(_arg0)
28+
});
29+
return result;
30+
}
31+
pub unsafe fn apply_binary_4(mut a: i32, mut b: i32, args: &[VaArg]) -> i32 {
32+
let mut ap: VaList = VaList::default();
33+
ap = VaList::new(args);
34+
let mut fn_: Option<unsafe fn(i32, i32) -> i32> = std::mem::transmute::<
35+
*mut ::libc::c_void,
36+
Option<unsafe fn(i32, i32) -> i32>,
37+
>(ap.arg::<*mut ::libc::c_void>());
38+
let mut result: i32 = (unsafe {
39+
let _arg0: i32 = a;
40+
let _arg1: i32 = b;
41+
(fn_).unwrap()(_arg0, _arg1)
42+
});
43+
return result;
44+
}
45+
pub fn main() {
46+
unsafe {
47+
std::process::exit(main_0() as i32);
48+
}
49+
}
50+
unsafe fn main_0() -> i32 {
51+
assert!(
52+
((((unsafe {
53+
let _x: i32 = 5;
54+
apply_unary_3(
55+
_x,
56+
&[
57+
(Some(square_0).map_or(::std::ptr::null_mut(), |f| f as *mut ::libc::c_void))
58+
.into(),
59+
],
60+
)
61+
}) == (25)) as i32)
62+
!= 0)
63+
);
64+
assert!(
65+
((((unsafe {
66+
let _x: i32 = 7;
67+
apply_unary_3(
68+
_x,
69+
&[
70+
(Some(negate_1).map_or(::std::ptr::null_mut(), |f| f as *mut ::libc::c_void))
71+
.into(),
72+
],
73+
)
74+
}) == (-7_i32)) as i32)
75+
!= 0)
76+
);
77+
assert!(
78+
((((unsafe {
79+
let _a: i32 = 3;
80+
let _b: i32 = 4;
81+
apply_binary_4(
82+
_a,
83+
_b,
84+
&[
85+
(Some(add_2).map_or(::std::ptr::null_mut(), |f| f as *mut ::libc::c_void))
86+
.into(),
87+
],
88+
)
89+
}) == (7)) as i32)
90+
!= 0)
91+
);
92+
return 0;
93+
}

tests/unit/va_arg_fn_ptr.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <assert.h>
2+
#include <stdarg.h>
3+
4+
typedef int (*unary)(int);
5+
typedef int (*binary)(int, int);
6+
7+
int square(int x) { return x * x; }
8+
int negate(int x) { return -x; }
9+
int add(int a, int b) { return a + b; }
10+
11+
int apply_unary(int x, ...) {
12+
va_list ap;
13+
va_start(ap, x);
14+
unary fn = va_arg(ap, unary);
15+
int result = fn(x);
16+
va_end(ap);
17+
return result;
18+
}
19+
20+
int apply_binary(int a, int b, ...) {
21+
va_list ap;
22+
va_start(ap, b);
23+
binary fn = va_arg(ap, binary);
24+
int result = fn(a, b);
25+
va_end(ap);
26+
return result;
27+
}
28+
29+
int main() {
30+
assert(apply_unary(5, square) == 25);
31+
assert(apply_unary(7, negate) == -7);
32+
assert(apply_binary(3, 4, add) == 7);
33+
return 0;
34+
}

0 commit comments

Comments
 (0)