Skip to content

Commit a37075b

Browse files
committed
Update tests
1 parent ad2f126 commit a37075b

4 files changed

Lines changed: 508 additions & 0 deletions

File tree

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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 shape_a {
11+
pub code: Value<u16>,
12+
pub pad: Value<Box<[u8]>>,
13+
}
14+
impl Default for shape_a {
15+
fn default() -> Self {
16+
shape_a {
17+
code: <Value<u16>>::default(),
18+
pad: Rc::new(RefCell::new(
19+
(0..14).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
20+
)),
21+
}
22+
}
23+
}
24+
impl ByteRepr for shape_a {
25+
fn byte_size() -> usize {
26+
16
27+
}
28+
fn to_bytes(&self, buf: &mut [u8]) {
29+
(*self.code.borrow()).to_bytes(&mut buf[0..2]);
30+
(*self.pad.borrow()).to_bytes(&mut buf[2..16]);
31+
}
32+
fn from_bytes(buf: &[u8]) -> Self {
33+
Self {
34+
code: Rc::new(RefCell::new(<u16>::from_bytes(&buf[0..2]))),
35+
pad: Rc::new(RefCell::new(<Box<[u8]>>::from_bytes(&buf[2..16]))),
36+
}
37+
}
38+
}
39+
#[derive()]
40+
pub struct shape_b {
41+
pub code: Value<u16>,
42+
pub lo: Value<u16>,
43+
pub mid: Value<u32>,
44+
pub fill: Value<Box<[u8]>>,
45+
pub tail: Value<u32>,
46+
}
47+
impl Default for shape_b {
48+
fn default() -> Self {
49+
shape_b {
50+
code: <Value<u16>>::default(),
51+
lo: <Value<u16>>::default(),
52+
mid: <Value<u32>>::default(),
53+
fill: Rc::new(RefCell::new(
54+
(0..16).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
55+
)),
56+
tail: <Value<u32>>::default(),
57+
}
58+
}
59+
}
60+
impl ByteRepr for shape_b {
61+
fn byte_size() -> usize {
62+
28
63+
}
64+
fn to_bytes(&self, buf: &mut [u8]) {
65+
(*self.code.borrow()).to_bytes(&mut buf[0..2]);
66+
(*self.lo.borrow()).to_bytes(&mut buf[2..4]);
67+
(*self.mid.borrow()).to_bytes(&mut buf[4..8]);
68+
(*self.fill.borrow()).to_bytes(&mut buf[8..24]);
69+
(*self.tail.borrow()).to_bytes(&mut buf[24..28]);
70+
}
71+
fn from_bytes(buf: &[u8]) -> Self {
72+
Self {
73+
code: Rc::new(RefCell::new(<u16>::from_bytes(&buf[0..2]))),
74+
lo: Rc::new(RefCell::new(<u16>::from_bytes(&buf[2..4]))),
75+
mid: Rc::new(RefCell::new(<u32>::from_bytes(&buf[4..8]))),
76+
fill: Rc::new(RefCell::new(<Box<[u8]>>::from_bytes(&buf[8..24]))),
77+
tail: Rc::new(RefCell::new(<u32>::from_bytes(&buf[24..28]))),
78+
}
79+
}
80+
}
81+
pub struct anon_0 {
82+
__bytes: Value<Box<[u8]>>,
83+
}
84+
impl anon_0 {
85+
pub fn a(&self) -> Ptr<shape_a> {
86+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
87+
}
88+
pub fn b(&self) -> Ptr<shape_b> {
89+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
90+
}
91+
pub fn raw_(&self) -> Ptr<Box<[u8]>> {
92+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
93+
}
94+
}
95+
impl Clone for anon_0 {
96+
fn clone(&self) -> Self {
97+
anon_0 {
98+
__bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())),
99+
}
100+
}
101+
}
102+
impl Default for anon_0 {
103+
fn default() -> Self {
104+
anon_0 {
105+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 64]))),
106+
}
107+
}
108+
}
109+
impl ByteRepr for anon_0 {}
110+
#[derive(Default)]
111+
pub struct Container {
112+
pub len: Value<u32>,
113+
pub u: Value<anon_0>,
114+
}
115+
impl ByteRepr for Container {}
116+
pub fn main() {
117+
std::process::exit(main_0());
118+
}
119+
fn main_0() -> i32 {
120+
let c: Value<Container> = <Value<Container>>::default();
121+
{
122+
((c.as_pointer()) as Ptr<Container>)
123+
.to_any()
124+
.memset((0) as u8, 68usize as usize);
125+
((c.as_pointer()) as Ptr<Container>).to_any().clone()
126+
};
127+
(*(*(*(*c.borrow()).u.borrow()).a().upgrade().deref())
128+
.code
129+
.borrow_mut()) = 10_u16;
130+
(*(*c.borrow()).len.borrow_mut()) = (28usize as u32);
131+
(*(*(((*(*c.borrow()).u.borrow()).a())
132+
.clone()
133+
.to_any()
134+
.cast::<shape_b>()
135+
.expect("ub:wrong type"))
136+
.upgrade()
137+
.deref())
138+
.tail
139+
.borrow_mut()) = 3735928559_u32;
140+
assert!(
141+
((((*(*(*(*c.borrow()).u.borrow()).b().upgrade().deref())
142+
.tail
143+
.borrow())
144+
== 3735928559_u32) as i32)
145+
!= 0)
146+
);
147+
assert!(
148+
(((((*(*(*(*c.borrow()).u.borrow()).b().upgrade().deref())
149+
.code
150+
.borrow()) as i32)
151+
== 10) as i32)
152+
!= 0)
153+
);
154+
(*(*(*(*c.borrow()).u.borrow()).b().upgrade().deref())
155+
.lo
156+
.borrow_mut()) = 8080_u16;
157+
assert!(
158+
((((((((*(*c.borrow()).u.borrow()).raw_().reinterpret_cast::<u8>())
159+
.to_strong()
160+
.as_pointer() as Ptr::<u8>)
161+
.offset((2) as isize)
162+
.read()) as i32)
163+
== 144) as i32)
164+
!= 0)
165+
);
166+
assert!(
167+
((((((((*(*c.borrow()).u.borrow()).raw_().reinterpret_cast::<u8>())
168+
.to_strong()
169+
.as_pointer() as Ptr::<u8>)
170+
.offset((3) as isize)
171+
.read()) as i32)
172+
== 31) as i32)
173+
!= 0)
174+
);
175+
return 0;
176+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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 record {
11+
pub code: Value<u16>,
12+
pub pad: Value<Box<[u8]>>,
13+
}
14+
impl Default for record {
15+
fn default() -> Self {
16+
record {
17+
code: <Value<u16>>::default(),
18+
pad: Rc::new(RefCell::new(
19+
(0..14).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
20+
)),
21+
}
22+
}
23+
}
24+
impl ByteRepr for record {
25+
fn byte_size() -> usize {
26+
16
27+
}
28+
fn to_bytes(&self, buf: &mut [u8]) {
29+
(*self.code.borrow()).to_bytes(&mut buf[0..2]);
30+
(*self.pad.borrow()).to_bytes(&mut buf[2..16]);
31+
}
32+
fn from_bytes(buf: &[u8]) -> Self {
33+
Self {
34+
code: Rc::new(RefCell::new(<u16>::from_bytes(&buf[0..2]))),
35+
pad: Rc::new(RefCell::new(<Box<[u8]>>::from_bytes(&buf[2..16]))),
36+
}
37+
}
38+
}
39+
pub struct anon_0 {
40+
__bytes: Value<Box<[u8]>>,
41+
}
42+
impl anon_0 {
43+
pub fn h(&self) -> Ptr<record> {
44+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
45+
}
46+
pub fn raw_(&self) -> Ptr<Box<[u8]>> {
47+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
48+
}
49+
}
50+
impl Clone for anon_0 {
51+
fn clone(&self) -> Self {
52+
anon_0 {
53+
__bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())),
54+
}
55+
}
56+
}
57+
impl Default for anon_0 {
58+
fn default() -> Self {
59+
anon_0 {
60+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 128]))),
61+
}
62+
}
63+
}
64+
impl ByteRepr for anon_0 {}
65+
#[derive(Default)]
66+
pub struct inner {
67+
pub view: Value<anon_0>,
68+
}
69+
impl ByteRepr for inner {}
70+
pub struct anon_1 {
71+
__bytes: Value<Box<[u8]>>,
72+
}
73+
impl anon_1 {
74+
pub fn h(&self) -> Ptr<record> {
75+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
76+
}
77+
pub fn nested(&self) -> Ptr<inner> {
78+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
79+
}
80+
}
81+
impl Clone for anon_1 {
82+
fn clone(&self) -> Self {
83+
anon_1 {
84+
__bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())),
85+
}
86+
}
87+
}
88+
impl Default for anon_1 {
89+
fn default() -> Self {
90+
anon_1 {
91+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 128]))),
92+
}
93+
}
94+
}
95+
impl ByteRepr for anon_1 {}
96+
#[derive(Default)]
97+
pub struct Outer {
98+
pub kind: Value<i32>,
99+
pub level: Value<i32>,
100+
pub variant: Value<i32>,
101+
pub len: Value<u32>,
102+
pub body: Value<anon_1>,
103+
}
104+
impl ByteRepr for Outer {}
105+
pub fn main() {
106+
std::process::exit(main_0());
107+
}
108+
fn main_0() -> i32 {
109+
let ex: Value<Outer> = <Value<Outer>>::default();
110+
{
111+
((ex.as_pointer()) as Ptr<Outer>)
112+
.to_any()
113+
.memset((0) as u8, 144usize as usize);
114+
((ex.as_pointer()) as Ptr<Outer>).to_any().clone()
115+
};
116+
(*(*ex.borrow()).kind.borrow_mut()) = 2;
117+
(*(*ex.borrow()).level.borrow_mut()) = 1;
118+
(*(*ex.borrow()).variant.borrow_mut()) = 6;
119+
(*(*ex.borrow()).len.borrow_mut()) = (16usize as u32);
120+
(*(*(*(*ex.borrow()).body.borrow()).h().upgrade().deref())
121+
.code
122+
.borrow_mut()) = 2_u16;
123+
(*(*(*(*ex.borrow()).body.borrow()).h().upgrade().deref())
124+
.pad
125+
.borrow_mut())[(0) as usize] = (('X' as i32) as u8);
126+
assert!(
127+
(((((*(*(*(*ex.borrow()).body.borrow()).h().upgrade().deref())
128+
.code
129+
.borrow()) as i32)
130+
== 2) as i32)
131+
!= 0)
132+
);
133+
assert!(
134+
(((((*(*(*(*ex.borrow()).body.borrow()).h().upgrade().deref())
135+
.pad
136+
.borrow())[(0) as usize] as i32)
137+
== ('X' as i32)) as i32)
138+
!= 0)
139+
);
140+
assert!(
141+
(((((*(*(*(*(*(*ex.borrow()).body.borrow()).nested().upgrade().deref())
142+
.view
143+
.borrow())
144+
.h()
145+
.upgrade()
146+
.deref())
147+
.code
148+
.borrow()) as i32)
149+
== 2) as i32)
150+
!= 0)
151+
);
152+
return 0;
153+
}

0 commit comments

Comments
 (0)