Skip to content

Commit 28b17ef

Browse files
committed
Update tests
1 parent 3fc0369 commit 28b17ef

12 files changed

Lines changed: 1196 additions & 0 deletions

‎tests/unit/lambda_generic.cpp‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <assert.h>
2+
3+
int main() {
4+
auto twice = [](auto x) { return x + x; };
5+
assert(twice(4) == 8);
6+
assert(twice(1.5) == 3.0);
7+
8+
int base = 10;
9+
auto add_base = [base](auto x) { return x + base; };
10+
assert(add_base(5) == 15);
11+
assert(add_base(2.5) == 12.5);
12+
13+
int total = 0;
14+
auto accumulate = [&total](auto x, auto y) { total += x * y; };
15+
accumulate(2, 3);
16+
accumulate(4u, 5u);
17+
assert(total == 26);
18+
19+
auto sub = []<typename T>(T x, T y) { return x - y; };
20+
assert(sub(9, 4) == 5);
21+
assert(sub(2.5, 1.0) == 1.5);
22+
23+
auto mixed = [base]<typename T, typename U>(T x, U y) {
24+
return x * y + base;
25+
};
26+
assert(mixed(2, 3) == 16);
27+
assert(mixed(2, 0.5) == 11.0);
28+
29+
auto cast_to = []<typename T>(int x) { return static_cast<T>(x) / 2; };
30+
assert(cast_to.operator()<int>(5) == 2);
31+
assert(cast_to.operator()<double>(5) == 2.5);
32+
33+
return 0;
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <assert.h>
2+
3+
template <typename F> int apply_int(F fn, int x) { return fn(x); }
4+
5+
template <typename F> double apply_double(F fn, double x) { return fn(x); }
6+
7+
int main() {
8+
int factor = 3;
9+
auto scale = [factor](auto x) { return x * factor; };
10+
assert(apply_int(scale, 4) == 12);
11+
assert(apply_double(scale, 1.5) == 4.5);
12+
13+
assert(apply_int([](auto x) { return -x; }, 9) == -9);
14+
15+
auto offset = [factor]<typename T>(T x) { return x + factor; };
16+
assert(apply_int(offset, 4) == 7);
17+
assert(apply_double(offset, 1.5) == 4.5);
18+
19+
return 0;
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <assert.h>
2+
3+
struct Val {
4+
int x;
5+
};
6+
7+
static int sum(Val a, Val b) { return a.x + b.x; }
8+
9+
int main() {
10+
int total = 0;
11+
auto tally = [&total]<typename T, typename U> { total += sizeof(T) + sizeof(U); };
12+
tally.operator()<char, char>();
13+
tally.operator()<int, char>();
14+
assert(total == 7);
15+
16+
Val v{5};
17+
int acc = 0;
18+
auto pick = [&v, &acc]<typename Q> { acc += sum(static_cast<Q>(v), v); };
19+
pick.operator()<Val &>();
20+
pick.operator()<const Val &>();
21+
pick.operator()<Val &&>();
22+
assert(acc == 30);
23+
24+
return 0;
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <assert.h>
2+
3+
int main() {
4+
auto negate = [](auto x) { return -x; };
5+
int (*fi)(int) = negate;
6+
double (*fd)(double) = negate;
7+
assert(fi(3) == -3);
8+
assert(fd(1.5) == -1.5);
9+
auto square = []<typename T>(T x) { return x * x; };
10+
int (*si)(int) = square;
11+
double (*sd)(double) = square;
12+
assert(si(3) == 9);
13+
assert(sd(1.5) == 2.25);
14+
return 0;
15+
}
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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 main() {
10+
__cpp2rust_init_globals();
11+
std::process::exit(main_0());
12+
}
13+
fn main_0() -> i32 {
14+
let twice: Value<lambda_0> = Rc::new(RefCell::new((lambda_0 {})));
15+
assert!((({ lambda_0::operator_call_i32__int_const(4,) }) == 8));
16+
assert!((({ lambda_0::operator_call_f64__double_const(1.5E+0,) }) == 3.0E+0));
17+
let base: Value<i32> = Rc::new(RefCell::new(10));
18+
let add_base: Value<lambda_1> = Rc::new(RefCell::new(
19+
(lambda_1 {
20+
base: Rc::new(RefCell::new((*base.borrow()))),
21+
}),
22+
));
23+
assert!((({ lambda_1::operator_call_i32__int_const(&(*add_base.borrow_mut()), 5,) }) == 15));
24+
assert!(
25+
(({ lambda_1::operator_call_f64__double_const(&(*add_base.borrow_mut()), 2.5E+0,) })
26+
== 1.25E+1)
27+
);
28+
let total: Value<i32> = Rc::new(RefCell::new(0));
29+
let accumulate: Value<lambda_2> = Rc::new(RefCell::new(
30+
(lambda_2 {
31+
total: total.as_pointer(),
32+
}),
33+
));
34+
({ lambda_2::operator_call_i32_i32__int_int_const(&(*accumulate.borrow_mut()), 2, 3) });
35+
({
36+
lambda_2::operator_call_u32_u32__unsigned_int_unsigned_int_const(
37+
&(*accumulate.borrow_mut()),
38+
4_u32,
39+
5_u32,
40+
)
41+
});
42+
assert!(((*total.borrow()) == 26));
43+
let sub: Value<lambda_3> = Rc::new(RefCell::new((lambda_3 {})));
44+
assert!((({ lambda_3::operator_call_i32_i32__int_const(9, 4,) }) == 5));
45+
assert!((({ lambda_3::operator_call_f64_f64__double_const(2.5E+0, 1.0E+0,) }) == 1.5E+0));
46+
let mixed: Value<lambda_4> = Rc::new(RefCell::new(
47+
(lambda_4 {
48+
base: Rc::new(RefCell::new((*base.borrow()))),
49+
}),
50+
));
51+
assert!(
52+
(({ lambda_4::operator_call_i32_i32__int_int_const(&(*mixed.borrow_mut()), 2, 3,) }) == 16)
53+
);
54+
assert!(
55+
(({
56+
lambda_4::operator_call_i32_f64__int_double_const(&(*mixed.borrow_mut()), 2, 5.0E-1)
57+
}) == 1.1E+1)
58+
);
59+
let cast_to: Value<lambda_5> = Rc::new(RefCell::new((lambda_5 {})));
60+
assert!((({ lambda_5::operator_call_i32__int_const(5,) }) == 2));
61+
assert!((({ lambda_5::operator_call_i32__double_const(5,) }) == 2.5E+0));
62+
return 0;
63+
}
64+
#[derive(Clone, ByteRepr, Default)]
65+
pub struct lambda_0 {}
66+
impl lambda_0 {
67+
pub fn operator_call_i32__int_const(x: i32) -> i32 {
68+
let x: Value<i32> = Rc::new(RefCell::new(x));
69+
return ((*x.borrow()) + (*x.borrow()));
70+
}
71+
pub fn operator_call_f64__double_const(x: f64) -> f64 {
72+
let x: Value<f64> = Rc::new(RefCell::new(x));
73+
return ((*x.borrow()) + (*x.borrow()));
74+
}
75+
}
76+
impl Callable1<i32, i32> for lambda_0 {
77+
fn call(&self, a1: i32) -> i32 {
78+
{ lambda_0::operator_call_i32__int_const(a1) }
79+
}
80+
}
81+
impl Callable1<f64, f64> for lambda_0 {
82+
fn call(&self, a1: f64) -> f64 {
83+
{ lambda_0::operator_call_f64__double_const(a1) }
84+
}
85+
}
86+
#[derive(Default)]
87+
pub struct lambda_1 {
88+
base: Value<i32>,
89+
}
90+
impl lambda_1 {
91+
pub fn operator_call_i32__int_const(&self, x: i32) -> i32 {
92+
let x: Value<i32> = Rc::new(RefCell::new(x));
93+
return ((*x.borrow()) + (*self.base.borrow()));
94+
}
95+
pub fn operator_call_f64__double_const(&self, x: f64) -> f64 {
96+
let x: Value<f64> = Rc::new(RefCell::new(x));
97+
return ((*x.borrow()) + ((*self.base.borrow()) as f64));
98+
}
99+
}
100+
impl Clone for lambda_1 {
101+
fn clone(&self) -> Self {
102+
Self {
103+
base: Rc::new(RefCell::new((*self.base.borrow()).clone())),
104+
}
105+
}
106+
}
107+
impl ByteRepr for lambda_1 {
108+
fn byte_size() -> usize {
109+
4
110+
}
111+
fn to_bytes(&self, buf: &mut [u8]) {
112+
(*self.base.borrow()).to_bytes(&mut buf[0..4]);
113+
}
114+
fn from_bytes(buf: &[u8]) -> Self {
115+
Self {
116+
base: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
117+
}
118+
}
119+
}
120+
impl Callable1<i32, i32> for lambda_1 {
121+
fn call(&self, a1: i32) -> i32 {
122+
{ lambda_1::operator_call_i32__int_const(self, a1) }
123+
}
124+
}
125+
impl Callable1<f64, f64> for lambda_1 {
126+
fn call(&self, a1: f64) -> f64 {
127+
{ lambda_1::operator_call_f64__double_const(self, a1) }
128+
}
129+
}
130+
#[derive(Clone, Default)]
131+
pub struct lambda_2 {
132+
total: Ptr<i32>,
133+
}
134+
impl lambda_2 {
135+
pub fn operator_call_i32_i32__int_int_const(&self, x: i32, y: i32) {
136+
let x: Value<i32> = Rc::new(RefCell::new(x));
137+
let y: Value<i32> = Rc::new(RefCell::new(y));
138+
{
139+
let _ptr = self.total.clone();
140+
_ptr.write(_ptr.read() + ((*x.borrow()) * (*y.borrow())))
141+
};
142+
}
143+
pub fn operator_call_u32_u32__unsigned_int_unsigned_int_const(&self, x: u32, y: u32) {
144+
let x: Value<u32> = Rc::new(RefCell::new(x));
145+
let y: Value<u32> = Rc::new(RefCell::new(y));
146+
{
147+
let rhs_0 = (((self.total.read()) as u32)
148+
.wrapping_add((*x.borrow()).wrapping_mul((*y.borrow()))))
149+
as i32;
150+
self.total.write(rhs_0)
151+
};
152+
}
153+
}
154+
impl ByteRepr for lambda_2 {}
155+
impl Callable2<i32, i32, ()> for lambda_2 {
156+
fn call(&self, a1: i32, a2: i32) -> () {
157+
{ lambda_2::operator_call_i32_i32__int_int_const(self, a1, a2) }
158+
}
159+
}
160+
impl Callable2<u32, u32, ()> for lambda_2 {
161+
fn call(&self, a1: u32, a2: u32) -> () {
162+
{ lambda_2::operator_call_u32_u32__unsigned_int_unsigned_int_const(self, a1, a2) }
163+
}
164+
}
165+
#[derive(Clone, ByteRepr, Default)]
166+
pub struct lambda_3 {}
167+
impl lambda_3 {
168+
pub fn operator_call_i32_i32__int_const(x: i32, y: i32) -> i32 {
169+
let x: Value<i32> = Rc::new(RefCell::new(x));
170+
let y: Value<i32> = Rc::new(RefCell::new(y));
171+
return ((*x.borrow()) - (*y.borrow()));
172+
}
173+
pub fn operator_call_f64_f64__double_const(x: f64, y: f64) -> f64 {
174+
let x: Value<f64> = Rc::new(RefCell::new(x));
175+
let y: Value<f64> = Rc::new(RefCell::new(y));
176+
return ((*x.borrow()) - (*y.borrow()));
177+
}
178+
}
179+
impl Callable2<i32, i32, i32> for lambda_3 {
180+
fn call(&self, a1: i32, a2: i32) -> i32 {
181+
{ lambda_3::operator_call_i32_i32__int_const(a1, a2) }
182+
}
183+
}
184+
impl Callable2<f64, f64, f64> for lambda_3 {
185+
fn call(&self, a1: f64, a2: f64) -> f64 {
186+
{ lambda_3::operator_call_f64_f64__double_const(a1, a2) }
187+
}
188+
}
189+
#[derive(Default)]
190+
pub struct lambda_4 {
191+
base: Value<i32>,
192+
}
193+
impl lambda_4 {
194+
pub fn operator_call_i32_i32__int_int_const(&self, x: i32, y: i32) -> i32 {
195+
let x: Value<i32> = Rc::new(RefCell::new(x));
196+
let y: Value<i32> = Rc::new(RefCell::new(y));
197+
return (((*x.borrow()) * (*y.borrow())) + (*self.base.borrow()));
198+
}
199+
pub fn operator_call_i32_f64__int_double_const(&self, x: i32, y: f64) -> f64 {
200+
let x: Value<i32> = Rc::new(RefCell::new(x));
201+
let y: Value<f64> = Rc::new(RefCell::new(y));
202+
return ((((*x.borrow()) as f64) * (*y.borrow())) + ((*self.base.borrow()) as f64));
203+
}
204+
}
205+
impl Clone for lambda_4 {
206+
fn clone(&self) -> Self {
207+
Self {
208+
base: Rc::new(RefCell::new((*self.base.borrow()).clone())),
209+
}
210+
}
211+
}
212+
impl ByteRepr for lambda_4 {
213+
fn byte_size() -> usize {
214+
4
215+
}
216+
fn to_bytes(&self, buf: &mut [u8]) {
217+
(*self.base.borrow()).to_bytes(&mut buf[0..4]);
218+
}
219+
fn from_bytes(buf: &[u8]) -> Self {
220+
Self {
221+
base: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
222+
}
223+
}
224+
}
225+
impl Callable2<i32, i32, i32> for lambda_4 {
226+
fn call(&self, a1: i32, a2: i32) -> i32 {
227+
{ lambda_4::operator_call_i32_i32__int_int_const(self, a1, a2) }
228+
}
229+
}
230+
impl Callable2<i32, f64, f64> for lambda_4 {
231+
fn call(&self, a1: i32, a2: f64) -> f64 {
232+
{ lambda_4::operator_call_i32_f64__int_double_const(self, a1, a2) }
233+
}
234+
}
235+
#[derive(Clone, ByteRepr, Default)]
236+
pub struct lambda_5 {}
237+
impl lambda_5 {
238+
pub fn operator_call_i32__int_const(x: i32) -> i32 {
239+
let x: Value<i32> = Rc::new(RefCell::new(x));
240+
return (((*x.borrow()) as i32) / 2);
241+
}
242+
pub fn operator_call_i32__double_const(x: i32) -> f64 {
243+
let x: Value<i32> = Rc::new(RefCell::new(x));
244+
return (((*x.borrow()) as f64) / 2_f64);
245+
}
246+
}
247+
pub fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)