Skip to content

Commit 84ac34f

Browse files
committed
Update tests
1 parent 6b21d14 commit 84ac34f

34 files changed

Lines changed: 790 additions & 92 deletions

‎tests/unit/lambda_as_template_argument.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// translation-fail
21
#include <assert.h>
32

43
template <typename F> int apply_int(F fn, int x) { return fn(x); }

‎tests/unit/lambda_generic.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// translation-fail
21
#include <assert.h>
32

43
int main() {

‎tests/unit/lambda_generic_to_fn_ptr.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// translation-fail
21
#include <assert.h>
32

43
int main() {

‎tests/unit/lambda_self_recursive.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
2-
// translation-fail
32
#include <assert.h>
43

54
int main() {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 apply_int_0(fn_: FnPtr<fn(i32) -> i32>, mut x: i32) -> i32 {
10+
let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(fn_));
11+
return ({ (*fn_.borrow()).call(x) });
12+
}
13+
pub fn apply_int_1(fn_: FnPtr<Generic>, mut x: i32) -> i32 {
14+
let fn_: Value<FnPtr<Generic>> = Rc::new(RefCell::new(fn_));
15+
return ({ (*fn_.borrow()).spec::<fn(i32) -> i32>().call(x) });
16+
}
17+
pub fn apply_int_2(fn_: FnPtr<Generic>, mut x: i32) -> i32 {
18+
let fn_: Value<FnPtr<Generic>> = Rc::new(RefCell::new(fn_));
19+
return ({ (*fn_.borrow()).spec::<fn(i32) -> i32>().call(x) });
20+
}
21+
pub fn apply_int_3(fn_: FnPtr<Generic>, mut x: i32) -> i32 {
22+
let fn_: Value<FnPtr<Generic>> = Rc::new(RefCell::new(fn_));
23+
return ({ (*fn_.borrow()).spec::<fn(i32) -> i32>().call(x) });
24+
}
25+
pub fn apply_double_4(fn_: FnPtr<Generic>, mut x: f64) -> f64 {
26+
let fn_: Value<FnPtr<Generic>> = Rc::new(RefCell::new(fn_));
27+
return ({ (*fn_.borrow()).spec::<fn(f64) -> f64>().call(x) });
28+
}
29+
pub fn apply_double_5(fn_: FnPtr<Generic>, mut x: f64) -> f64 {
30+
let fn_: Value<FnPtr<Generic>> = Rc::new(RefCell::new(fn_));
31+
return ({ (*fn_.borrow()).spec::<fn(f64) -> f64>().call(x) });
32+
}
33+
pub fn apply_twice_6(fn_: FnPtr<fn(i32) -> i32>, mut x: i32) -> i32 {
34+
let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(fn_));
35+
return ({
36+
let _x: i32 = ({ (*fn_.borrow()).call(x) });
37+
(*fn_.borrow()).call(_x)
38+
})
39+
.clone();
40+
}
41+
pub fn main() {
42+
__cpp2rust_init_globals();
43+
std::process::exit(main_0());
44+
}
45+
fn main_0() -> i32 {
46+
let factor: Value<i32> = Rc::new(RefCell::new(3));
47+
let scale: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(lambda!(
48+
{
49+
let factor: Value<i32> = Rc::new(RefCell::new((*factor.borrow())));
50+
},
51+
|x: i32| -> i32 {
52+
return (x * (*factor.borrow()));
53+
},
54+
)));
55+
assert!((({ apply_twice_6((*scale.borrow()).copy_from(), 4,) }) == 36));
56+
assert!(
57+
(({
58+
apply_int_0(
59+
FnPtr::<fn(i32) -> i32>::new(|x: i32| -> i32 {
60+
{
61+
return -x;
62+
}
63+
}),
64+
9,
65+
)
66+
}) == -9_i32)
67+
);
68+
let generic_scale: Value<FnPtr<Generic>> = Rc::new(RefCell::new(lambda!(
69+
Generic,
70+
{
71+
let factor: Value<i32> = Rc::new(RefCell::new((*factor.borrow())));
72+
},
73+
|x: i32| -> i32 {
74+
return (x * (*factor.borrow()));
75+
},
76+
|x: f64| -> f64 {
77+
return (x * ((*factor.borrow()) as f64));
78+
},
79+
)));
80+
assert!((({ apply_int_1((*generic_scale.borrow()).copy_from(), 4,) }) == 12));
81+
assert!((({ apply_double_4((*generic_scale.borrow()).copy_from(), 1.5_f64,) }) == 4.5_f64));
82+
assert!(
83+
(({
84+
apply_int_2(
85+
lambda!(Generic, {}, |x: i32| -> i32 {
86+
return -x;
87+
},),
88+
9,
89+
)
90+
}) == -9_i32)
91+
);
92+
let offset: Value<FnPtr<Generic>> = Rc::new(RefCell::new(lambda!(
93+
Generic,
94+
{
95+
let factor: Value<i32> = Rc::new(RefCell::new((*factor.borrow())));
96+
},
97+
|x: i32| -> i32 {
98+
return (x + (*factor.borrow()));
99+
},
100+
|x: f64| -> f64 {
101+
return (x + ((*factor.borrow()) as f64));
102+
},
103+
)));
104+
assert!((({ apply_int_3((*offset.borrow()).copy_from(), 4,) }) == 7));
105+
assert!((({ apply_double_5((*offset.borrow()).copy_from(), 1.5_f64,) }) == 4.5_f64));
106+
return 0;
107+
}
108+
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn main_0() -> i32 {
8888
},
8989
|| -> i32 {
9090
return (({ (*c.borrow()).copies } * 10) + { (*c.borrow()).moves });
91-
}
91+
},
9292
)));
9393
assert!((({ (*f.borrow()).call() }) == 10));
9494
let g: Value<FnPtr<fn() -> i32>> = Rc::new(RefCell::new((*f.borrow()).copy_from()));
@@ -105,7 +105,7 @@ fn main_0() -> i32 {
105105
},
106106
|| -> Counted {
107107
return Counted::copy_from({ c.as_pointer() });
108-
}
108+
},
109109
)
110110
.call()
111111
})
@@ -127,7 +127,7 @@ fn main_0() -> i32 {
127127
return ({ (*arr.borrow())[(0) as usize].copies } + {
128128
(*arr.borrow())[(1) as usize].copies
129129
});
130-
}
130+
},
131131
)));
132132
assert!((({ (*a.borrow()).call() }) == 2));
133133
let a2: Value<FnPtr<fn() -> i32>> = Rc::new(RefCell::new((*a.borrow()).copy_from()));
@@ -139,7 +139,7 @@ fn main_0() -> i32 {
139139
{
140140
let d: Value<Dropped> = Rc::new(RefCell::new(Dropped::new()));
141141
},
142-
|| {}
142+
|| {},
143143
)));
144144
let _dtor_m = ScopedDestructor::new(&m, |__p| __p.with(|__f| __f.destroy()));
145145
let m2: Value<FnPtr<fn()>> = Rc::new(RefCell::new((*m.borrow()).move_from()));
@@ -151,7 +151,7 @@ fn main_0() -> i32 {
151151
{
152152
let d: Value<Dropped> = Rc::new(RefCell::new(Dropped::new()));
153153
},
154-
|| {}
154+
|| {},
155155
)));
156156
let _dtor_k = ScopedDestructor::new(&k, |__p| __p.with(|__f| __f.destroy()));
157157
let k2: Value<FnPtr<fn()>> = Rc::new(RefCell::new((*k.borrow()).copy_from()));
@@ -163,15 +163,15 @@ fn main_0() -> i32 {
163163
{
164164
let d: Value<Dropped> = Rc::new(RefCell::new(Dropped::new()));
165165
},
166-
|| {}
166+
|| {},
167167
)));
168168
let _dtor_inner = ScopedDestructor::new(&inner, |__p| __p.with(|__f| __f.destroy()));
169169
let outer: Value<FnPtr<fn()>> = Rc::new(RefCell::new(lambda!(
170170
{
171171
let inner: Value<FnPtr<fn()>> =
172172
Rc::new(RefCell::new((*inner.borrow()).copy_from()));
173173
},
174-
|| {}
174+
|| {},
175175
)));
176176
let _dtor_outer = ScopedDestructor::new(&outer, |__p| __p.with(|__f| __f.destroy()));
177177
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn main_0() -> i32 {
2222
},
2323
|x: i32| -> i32 {
2424
return ((((*a.borrow()) + (*b.borrow())) + (*c.borrow())) + x);
25-
}
25+
},
2626
)));
2727
assert!((({ (*by_value.borrow()).call(10,) }) == 16));
2828
(*a.borrow_mut()) = 100;
@@ -35,7 +35,7 @@ fn main_0() -> i32 {
3535
},
3636
|x: i32| -> i32 {
3737
return ((((a.read()) + (b.read())) + (c.read())) + x);
38-
}
38+
},
3939
)));
4040
assert!((({ (*by_ref.borrow()).call(10,) }) == 115));
4141
(*b.borrow_mut()) = 200;
@@ -52,7 +52,7 @@ fn main_0() -> i32 {
5252
c.with_mut(|__v| *__v = *__v + __rhs)
5353
};
5454
return (((*a.borrow()) + (*b.borrow())) + (c.read()));
55-
}
55+
},
5656
)));
5757
assert!((({ (*mixed.borrow()).call(1,) }) == ((100 + 200) + 4)));
5858
assert!(((*c.borrow()) == 4));

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main_0() -> i32 {
1818
},
1919
|| -> i32 {
2020
return ((*v.borrow()).len() as i32);
21-
}
21+
},
2222
)));
2323
let g: Value<FnPtr<fn() -> i32>> = Rc::new(RefCell::new((*f.borrow()).copy_from()));
2424
let h: Value<FnPtr<fn() -> i32>> = Rc::new(RefCell::new((*f.borrow()).move_from()));
@@ -36,7 +36,7 @@ fn main_0() -> i32 {
3636
} else {
3737
0
3838
};
39-
}
39+
},
4040
)));
4141
let q: Value<FnPtr<fn() -> i32>> = Rc::new(RefCell::new((*p.borrow()).move_from()));
4242
assert!((({ (*q.borrow()).call() }) == 5));
@@ -48,7 +48,7 @@ fn main_0() -> i32 {
4848
},
4949
|| -> i32 {
5050
return (*n.borrow_mut()).prefix_inc();
51-
}
51+
},
5252
)));
5353
let outer: Value<FnPtr<fn() -> i32>> = Rc::new(RefCell::new(lambda!(
5454
{
@@ -57,7 +57,7 @@ fn main_0() -> i32 {
5757
},
5858
|| -> i32 {
5959
return ({ (*inner.borrow()).call() }).clone();
60-
}
60+
},
6161
)));
6262
assert!((({ (*outer.borrow()).call() }) == 1));
6363
let outer2: Value<FnPtr<fn() -> i32>> = Rc::new(RefCell::new((*outer.borrow()).copy_from()));

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main_0() -> i32 {
2626
},
2727
|x: i32| -> i32 {
2828
return (x + (base.read()));
29-
}
29+
},
3030
)));
3131
assert!((({ (*add_base.borrow()).call(5,) }) == 15));
3232
(*base.borrow_mut()) = 100;
@@ -38,7 +38,7 @@ fn main_0() -> i32 {
3838
},
3939
|| -> i32 {
4040
return (s.with(|__s| __s.x) + s.with(|__s| __s.y));
41-
}
41+
},
4242
)));
4343
assert!((({ (*sum.borrow()).call() }) == 3));
4444
(*s.borrow_mut()).x = 50;
@@ -50,7 +50,7 @@ fn main_0() -> i32 {
5050
},
5151
|| {
5252
counter.with_mut(|__v| __v.postfix_inc());
53-
}
53+
},
5454
)));
5555
({ (*bump.borrow()).call() });
5656
({ (*bump.borrow()).call() });
@@ -64,7 +64,7 @@ fn main_0() -> i32 {
6464
let mut t: u16 = (elem!((arr), j).read());
6565
elem!((arr), j).write({ (elem!((arr), i).read()) });
6666
elem!((arr), i).write(t);
67-
}
67+
},
6868
)));
6969
({ (*swap.borrow()).call(0_usize, 3_usize) });
7070
assert!((((*arr.borrow())[(0) as usize] as i32) == 0));
@@ -79,7 +79,7 @@ fn main_0() -> i32 {
7979
let __rhs = { x };
8080
t.with_mut(|__v| *__v = *__v + __rhs)
8181
};
82-
}
82+
},
8383
)));
8484
({ (*add.borrow()).call(2) });
8585
({ (*add.borrow()).call(3) });
@@ -90,7 +90,7 @@ fn main_0() -> i32 {
9090
},
9191
|v: i32| {
9292
y.write({ v });
93-
}
93+
},
9494
)));
9595
({ (*set_y.borrow()).call(9) });
9696
assert!(({ (*s.borrow()).y } == 9));

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl SImpl for Ptr<S> {
4646
field!(this_.as_pointer(), n).with_mut(|__v| *__v = *__v + 10)
4747
};
4848
return this_.as_pointer().with(|__s| __s.n);
49-
}
49+
},
5050
)));
5151
let mut r: i32 = ({ (*f.borrow()).call() }).clone();
5252
return ((r * 100) + (*self).with(|__s| __s.n));
@@ -58,7 +58,7 @@ impl SImpl for Ptr<S> {
5858
},
5959
|| -> i32 {
6060
return ({ SImpl::twice(&this_.as_pointer()) });
61-
}
61+
},
6262
)));
6363
field!((*self), n).write(99);
6464
return ({ (*f.borrow()).call() }).clone();
@@ -72,7 +72,7 @@ impl SImpl for Ptr<S> {
7272
},
7373
|| -> i32 {
7474
return (this_.as_pointer().with(|__s| __s.n) + (*k.borrow()));
75-
}
75+
},
7676
)));
7777
field!((*self), n).write(0);
7878
return ({ (*f.borrow()).call() }).clone();

0 commit comments

Comments
 (0)