Skip to content

Commit 70a4749

Browse files
committed
Update tests
1 parent 02cdd79 commit 70a4749

3 files changed

Lines changed: 361 additions & 0 deletions

File tree

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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+
#[derive()]
10+
pub struct A {
11+
pub v: Value<i32>,
12+
}
13+
impl A {
14+
pub fn A1() -> Self {
15+
let __this: Value<A> = Rc::new(RefCell::new(Self {
16+
v: Rc::new(RefCell::new(1)),
17+
}));
18+
let this: Ptr<A> = __this.as_pointer();
19+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
20+
}
21+
pub fn A2(v: i32) -> Self {
22+
let v: Value<i32> = Rc::new(RefCell::new(v));
23+
let __this: Value<A> = Rc::new(RefCell::new(Self {
24+
v: Rc::new(RefCell::new((*v.borrow()))),
25+
}));
26+
let this: Ptr<A> = __this.as_pointer();
27+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
28+
}
29+
}
30+
impl Clone for A {
31+
fn clone(&self) -> Self {
32+
let __this: Value<A> = Rc::new(RefCell::new(Self {
33+
v: Rc::new(RefCell::new((*self.v.borrow()))),
34+
}));
35+
let this: Ptr<A> = __this.as_pointer();
36+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
37+
}
38+
}
39+
impl Default for A {
40+
fn default() -> Self {
41+
{ A::A1() }
42+
}
43+
}
44+
impl ByteRepr for A {
45+
fn byte_size() -> usize {
46+
4
47+
}
48+
fn to_bytes(&self, buf: &mut [u8]) {
49+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
50+
}
51+
fn from_bytes(buf: &[u8]) -> Self {
52+
Self {
53+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
54+
}
55+
}
56+
}
57+
#[derive()]
58+
pub struct B {
59+
pub v: Value<i32>,
60+
}
61+
impl B {
62+
pub fn B() -> Self {
63+
let __this: Value<B> = Rc::new(RefCell::new(Self {
64+
v: Rc::new(RefCell::new(2)),
65+
}));
66+
let this: Ptr<B> = __this.as_pointer();
67+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
68+
}
69+
}
70+
impl Clone for B {
71+
fn clone(&self) -> Self {
72+
let __this: Value<B> = Rc::new(RefCell::new(Self {
73+
v: Rc::new(RefCell::new((*self.v.borrow()))),
74+
}));
75+
let this: Ptr<B> = __this.as_pointer();
76+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
77+
}
78+
}
79+
impl Default for B {
80+
fn default() -> Self {
81+
{ B::B() }
82+
}
83+
}
84+
impl ByteRepr for B {
85+
fn byte_size() -> usize {
86+
4
87+
}
88+
fn to_bytes(&self, buf: &mut [u8]) {
89+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
90+
}
91+
fn from_bytes(buf: &[u8]) -> Self {
92+
Self {
93+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
94+
}
95+
}
96+
}
97+
#[derive(Default)]
98+
pub struct NoDefault {
99+
pub v: Value<i32>,
100+
}
101+
impl NoDefault {
102+
pub fn NoDefault(v: i32) -> Self {
103+
let v: Value<i32> = Rc::new(RefCell::new(v));
104+
let __this: Value<NoDefault> = Rc::new(RefCell::new(Self {
105+
v: Rc::new(RefCell::new((*v.borrow()))),
106+
}));
107+
let this: Ptr<NoDefault> = __this.as_pointer();
108+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
109+
}
110+
}
111+
impl Clone for NoDefault {
112+
fn clone(&self) -> Self {
113+
let __this: Value<NoDefault> = Rc::new(RefCell::new(Self {
114+
v: Rc::new(RefCell::new((*self.v.borrow()))),
115+
}));
116+
let this: Ptr<NoDefault> = __this.as_pointer();
117+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
118+
}
119+
}
120+
impl ByteRepr for NoDefault {
121+
fn byte_size() -> usize {
122+
4
123+
}
124+
fn to_bytes(&self, buf: &mut [u8]) {
125+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
126+
}
127+
fn from_bytes(buf: &[u8]) -> Self {
128+
Self {
129+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
130+
}
131+
}
132+
}
133+
pub fn used_0(x: Option<A>) -> i32 {
134+
let x: Value<A> = Rc::new(RefCell::new(x.unwrap_or(A::A1())));
135+
return (*(*x.borrow()).v.borrow());
136+
}
137+
pub fn used_1(x: Option<B>) -> i32 {
138+
let x: Value<B> = Rc::new(RefCell::new(x.unwrap_or(B::B())));
139+
return (*(*x.borrow()).v.borrow());
140+
}
141+
pub fn scaled_2(x: A, n: Option<i32>) -> i32 {
142+
let x: Value<A> = Rc::new(RefCell::new(x));
143+
let n: Value<i32> = Rc::new(RefCell::new(n.unwrap_or((4usize as i32))));
144+
return ((*(*x.borrow()).v.borrow()) * (*n.borrow()));
145+
}
146+
pub fn always_given_3(x: Option<NoDefault>) -> i32 {
147+
let x: Value<NoDefault> = Rc::new(RefCell::new(x.unwrap_or()));
148+
return (*(*x.borrow()).v.borrow());
149+
}
150+
#[derive(Default)]
151+
pub struct S_NoDefault_ {
152+
pub v: Value<i32>,
153+
}
154+
impl S_NoDefault_ {
155+
pub fn S_NoDefault_(v: i32) -> Self {
156+
let v: Value<i32> = Rc::new(RefCell::new(v));
157+
let __this: Value<S_NoDefault_> = Rc::new(RefCell::new(Self {
158+
v: Rc::new(RefCell::new((*v.borrow()))),
159+
}));
160+
let this: Ptr<S_NoDefault_> = __this.as_pointer();
161+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
162+
}
163+
}
164+
impl Clone for S_NoDefault_ {
165+
fn clone(&self) -> Self {
166+
let __this: Value<S_NoDefault_> = Rc::new(RefCell::new(Self {
167+
v: Rc::new(RefCell::new((*self.v.borrow()))),
168+
}));
169+
let this: Ptr<S_NoDefault_> = __this.as_pointer();
170+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
171+
}
172+
}
173+
impl ByteRepr for S_NoDefault_ {
174+
fn byte_size() -> usize {
175+
4
176+
}
177+
fn to_bytes(&self, buf: &mut [u8]) {
178+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
179+
}
180+
fn from_bytes(buf: &[u8]) -> Self {
181+
Self {
182+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
183+
}
184+
}
185+
}
186+
pub fn main() {
187+
__cpp2rust_init_globals();
188+
std::process::exit(main_0());
189+
}
190+
fn main_0() -> i32 {
191+
assert!((({ used_0(None,) }) == 1));
192+
assert!((({ used_0(Some(A::A2({ 5 },)),) }) == 5));
193+
assert!((({ used_1(None,) }) == 2));
194+
assert!((({ scaled_2(A::A2({ 3 },), None,) }) == (3 * (4usize as i32))));
195+
assert!((({ scaled_2(A::A2({ 3 },), Some(2),) }) == 6));
196+
assert!((({ always_given_3(Some(NoDefault::NoDefault({ 3 },)),) }) == 3));
197+
let s: Value<S_NoDefault_> = Rc::new(RefCell::new(S_NoDefault_::S_NoDefault_({ 1 })));
198+
assert!(
199+
(({ S_NoDefault_Impl::get(&s.as_pointer(), Some(NoDefault::NoDefault({ 4 },)),) }) == 5)
200+
);
201+
return 0;
202+
}
203+
pub trait S_NoDefault_Impl {
204+
fn get(&self, t: Option<NoDefault>) -> i32;
205+
}
206+
impl S_NoDefault_Impl for Ptr<S_NoDefault_> {
207+
fn get(&self, t: Option<NoDefault>) -> i32 {
208+
let t: Value<NoDefault> = Rc::new(RefCell::new(t.unwrap_or()));
209+
return ((*(*(*self).upgrade().deref()).v.borrow()) + (*(*t.borrow()).v.borrow()));
210+
}
211+
}
212+
pub fn __cpp2rust_init_globals() {}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
#[repr(C)]
10+
#[derive(Copy, Clone)]
11+
pub struct A {
12+
pub v: i32,
13+
}
14+
impl A {
15+
pub unsafe fn A1() -> Self {
16+
let mut this = Self { v: 1 };
17+
this
18+
}
19+
pub unsafe fn A2(mut v: i32) -> Self {
20+
let mut this = Self { v: v };
21+
this
22+
}
23+
}
24+
impl Default for A {
25+
fn default() -> Self {
26+
unsafe { A::A1() }
27+
}
28+
}
29+
#[repr(C)]
30+
#[derive(Copy, Clone)]
31+
pub struct B {
32+
pub v: i32,
33+
}
34+
impl B {
35+
pub unsafe fn B() -> Self {
36+
let mut this = Self { v: 2 };
37+
this
38+
}
39+
}
40+
impl Default for B {
41+
fn default() -> Self {
42+
unsafe { B::B() }
43+
}
44+
}
45+
#[repr(C)]
46+
#[derive(Copy, Clone, Default)]
47+
pub struct NoDefault {
48+
pub v: i32,
49+
}
50+
impl NoDefault {
51+
pub unsafe fn NoDefault(mut v: i32) -> Self {
52+
let mut this = Self { v: v };
53+
this
54+
}
55+
}
56+
pub unsafe fn used_0(mut x: Option<A>) -> i32 {
57+
let mut x: A = x.unwrap_or(A::A1());
58+
return x.v;
59+
}
60+
pub unsafe fn used_1(mut x: Option<B>) -> i32 {
61+
let mut x: B = x.unwrap_or(B::B());
62+
return x.v;
63+
}
64+
pub unsafe fn scaled_2(mut x: A, mut n: Option<i32>) -> i32 {
65+
let mut n: i32 = n.unwrap_or((::std::mem::size_of::<A>() as i32));
66+
return ((x.v) * (n));
67+
}
68+
pub unsafe fn always_given_3(mut x: Option<NoDefault>) -> i32 {
69+
let mut x: NoDefault = x.unwrap_or();
70+
return x.v;
71+
}
72+
#[repr(C)]
73+
#[derive(Copy, Clone, Default)]
74+
pub struct S_NoDefault_ {
75+
pub v: i32,
76+
}
77+
impl S_NoDefault_ {
78+
pub unsafe fn S_NoDefault_(mut v: i32) -> Self {
79+
let mut this = Self { v: v };
80+
this
81+
}
82+
pub unsafe fn get(&self, mut t: Option<NoDefault>) -> i32 {
83+
let mut t: NoDefault = t.unwrap_or();
84+
return ((self.v) + (t.v));
85+
}
86+
}
87+
pub fn main() {
88+
unsafe {
89+
__cpp2rust_init_globals();
90+
std::process::exit(main_0() as i32);
91+
}
92+
}
93+
unsafe fn main_0() -> i32 {
94+
assert!(((unsafe { used_0(None,) }) == (1)));
95+
assert!(((unsafe { used_0(Some(A::A2({ 5 },)),) }) == (5)));
96+
assert!(((unsafe { used_1(None,) }) == (2)));
97+
assert!(
98+
((unsafe { scaled_2(A::A2({ 3 },), None,) })
99+
== ((3) * (::std::mem::size_of::<A>() as i32)))
100+
);
101+
assert!(((unsafe { scaled_2(A::A2({ 3 },), Some(2),) }) == (6)));
102+
assert!(((unsafe { always_given_3(Some(NoDefault::NoDefault({ 3 },)),) }) == (3)));
103+
let mut s: S_NoDefault_ = S_NoDefault_::S_NoDefault_({ 1 });
104+
assert!(((unsafe { S_NoDefault_::get(&s, Some(NoDefault::NoDefault({ 4 },)),) }) == (5)));
105+
return 0;
106+
}
107+
pub unsafe fn __cpp2rust_init_globals() {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <cassert>
2+
3+
struct A {
4+
int v;
5+
A() : v(1) {}
6+
A(int v) : v(v) {}
7+
};
8+
9+
struct B {
10+
int v;
11+
B() : v(2) {}
12+
};
13+
14+
struct NoDefault {
15+
int v;
16+
NoDefault(int v) : v(v) {}
17+
};
18+
19+
template <typename T> int used(T x = T()) { return x.v; }
20+
21+
template <typename T> int scaled(T x, int n = sizeof(T)) { return x.v * n; }
22+
23+
template <typename T> int always_given(T x = T()) { return x.v; }
24+
25+
template <typename T> struct S {
26+
int v;
27+
S(int v) : v(v) {}
28+
int get(T t = T()) const { return v + t.v; }
29+
};
30+
31+
int main() {
32+
assert(used<A>() == 1);
33+
assert(used(A(5)) == 5);
34+
assert(used<B>() == 2);
35+
assert(scaled(A(3)) == 3 * (int)sizeof(A));
36+
assert(scaled(A(3), 2) == 6);
37+
38+
assert(always_given(NoDefault(3)) == 3);
39+
S<NoDefault> s(1);
40+
assert(s.get(NoDefault(4)) == 5);
41+
return 0;
42+
}

0 commit comments

Comments
 (0)