Skip to content

Commit 77430e4

Browse files
committed
Add lambda tests
1 parent a9d98fe commit 77430e4

4 files changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 apply_0(fn_: impl Fn(i32) -> i32, x: i32) -> i32 {
11+
let fn_: Value<_> = Rc::new(RefCell::new(fn_));
12+
let x: Value<i32> = Rc::new(RefCell::new(x));
13+
return ({
14+
let _x: i32 = (*x.borrow());
15+
(*fn_.borrow())(_x)
16+
})
17+
.clone();
18+
}
19+
pub fn apply_1(fn_: impl Fn(i32) -> i32, x: i32) -> i32 {
20+
let fn_: Value<_> = Rc::new(RefCell::new(fn_));
21+
let x: Value<i32> = Rc::new(RefCell::new(x));
22+
return ({
23+
let _x: i32 = (*x.borrow());
24+
(*fn_.borrow())(_x)
25+
})
26+
.clone();
27+
}
28+
pub fn main() {
29+
std::process::exit(main_0());
30+
}
31+
fn main_0() -> i32 {
32+
let base: Value<i32> = Rc::new(RefCell::new(10));
33+
let add_base: Value<_> = Rc::new(RefCell::new(
34+
(|x: i32| {
35+
let x: Value<i32> = Rc::new(RefCell::new(x));
36+
return ((*x.borrow()) + (*base.borrow()));
37+
}),
38+
));
39+
assert!(
40+
(({
41+
let _fn: _ = (*add_base.borrow()).clone();
42+
let _x: i32 = 5;
43+
apply_0(_fn, _x)
44+
}) == 15)
45+
);
46+
(*base.borrow_mut()) = 100;
47+
assert!(
48+
(({
49+
let _fn: _ = (*add_base.borrow()).clone();
50+
let _x: i32 = 5;
51+
apply_0(_fn, _x)
52+
}) == 105)
53+
);
54+
let factor: Value<i32> = Rc::new(RefCell::new(3));
55+
let scale: Value<_> = Rc::new(RefCell::new(
56+
(|x: i32| {
57+
let x: Value<i32> = Rc::new(RefCell::new(x));
58+
return ((*x.borrow()) * (*factor.borrow()));
59+
}),
60+
));
61+
assert!(
62+
(({
63+
let _fn: _ = (*scale.borrow()).clone();
64+
let _x: i32 = 4;
65+
apply_1(_fn, _x)
66+
}) == 12)
67+
);
68+
return 0;
69+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 main() {
11+
std::process::exit(main_0());
12+
}
13+
fn main_0() -> i32 {
14+
let x: Value<i32> = Rc::new(RefCell::new(10));
15+
let outer: Value<_> = Rc::new(RefCell::new(
16+
(|y: i32| {
17+
let y: Value<i32> = Rc::new(RefCell::new(y));
18+
let inner: Value<_> = Rc::new(RefCell::new(
19+
(|z: i32| {
20+
let z: Value<i32> = Rc::new(RefCell::new(z));
21+
return (((*x.borrow()) + (*y.borrow())) + (*z.borrow()));
22+
}),
23+
));
24+
return ({
25+
let _z: i32 = 1;
26+
(*inner.borrow())(_z)
27+
})
28+
.clone();
29+
}),
30+
));
31+
assert!(
32+
(({
33+
let _y: i32 = 20;
34+
(*outer.borrow())(_y)
35+
}) == 31)
36+
);
37+
(*x.borrow_mut()) = 100;
38+
assert!(
39+
(({
40+
let _y: i32 = 20;
41+
(*outer.borrow())(_y)
42+
}) == 121)
43+
);
44+
return 0;
45+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
9+
use std::rc::Rc;
10+
pub unsafe fn apply_0(mut fn_: impl Fn(i32) -> i32, mut x: i32) -> i32 {
11+
return (unsafe {
12+
let _x: i32 = x;
13+
fn_(_x)
14+
});
15+
}
16+
pub unsafe fn apply_1(mut fn_: impl Fn(i32) -> i32, mut x: i32) -> i32 {
17+
return (unsafe {
18+
let _x: i32 = x;
19+
fn_(_x)
20+
});
21+
}
22+
pub fn main() {
23+
unsafe {
24+
std::process::exit(main_0() as i32);
25+
}
26+
}
27+
unsafe fn main_0() -> i32 {
28+
let mut base: i32 = 10;
29+
assert!(
30+
((unsafe {
31+
let _fn: _ = (|x: i32| {
32+
return ((x) + (base));
33+
})
34+
.clone();
35+
let _x: i32 = 5;
36+
apply_0(_fn, _x)
37+
}) == (15))
38+
);
39+
base = 100;
40+
assert!(
41+
((unsafe {
42+
let _fn: _ = (|x: i32| {
43+
return ((x) + (base));
44+
})
45+
.clone();
46+
let _x: i32 = 5;
47+
apply_0(_fn, _x)
48+
}) == (105))
49+
);
50+
let mut factor: i32 = 3;
51+
assert!(
52+
((unsafe {
53+
let _fn: _ = (|x: i32| {
54+
return ((x) * (factor));
55+
})
56+
.clone();
57+
let _x: i32 = 4;
58+
apply_1(_fn, _x)
59+
}) == (12))
60+
);
61+
return 0;
62+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
9+
use std::rc::Rc;
10+
pub fn main() {
11+
unsafe {
12+
std::process::exit(main_0() as i32);
13+
}
14+
}
15+
unsafe fn main_0() -> i32 {
16+
let mut x: i32 = 10;
17+
assert!(
18+
((unsafe {
19+
let _y: i32 = 20;
20+
(|y: i32| {
21+
return (unsafe {
22+
let _z: i32 = 1;
23+
(|z: i32| {
24+
return (((x) + (y)) + (z));
25+
})(_z)
26+
});
27+
})(_y)
28+
}) == (31))
29+
);
30+
x = 100;
31+
assert!(
32+
((unsafe {
33+
let _y: i32 = 20;
34+
(|y: i32| {
35+
return (unsafe {
36+
let _z: i32 = 1;
37+
(|z: i32| {
38+
return (((x) + (y)) + (z));
39+
})(_z)
40+
});
41+
})(_y)
42+
}) == (121))
43+
);
44+
return 0;
45+
}

0 commit comments

Comments
 (0)