Skip to content

Commit 5b42b0a

Browse files
committed
Add anonymous struct tests
1 parent 89b6646 commit 5b42b0a

6 files changed

Lines changed: 615 additions & 0 deletions

File tree

tests/unit/anonymous-struct.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <cassert>
2+
3+
struct Outer {
4+
struct Named {
5+
int a;
6+
int b;
7+
} named;
8+
9+
struct {
10+
int c;
11+
int d;
12+
} anonymous_named_0;
13+
14+
struct {
15+
int g;
16+
int h;
17+
} anonymous_named_1;
18+
19+
struct {
20+
int e;
21+
int f;
22+
};
23+
24+
struct {
25+
int i;
26+
struct {
27+
int j;
28+
} inner_named;
29+
struct {
30+
int k;
31+
};
32+
};
33+
};
34+
35+
int main() {
36+
Outer o = {};
37+
38+
o.named.a = 1;
39+
o.named.b = 2;
40+
o.anonymous_named_0.c = 3;
41+
o.anonymous_named_0.d = 4;
42+
o.anonymous_named_1.g = 5;
43+
o.anonymous_named_1.h = 6;
44+
o.e = 7;
45+
o.f = 8;
46+
o.i = 9;
47+
o.inner_named.j = 10;
48+
o.k = 11;
49+
50+
assert(o.named.a == 1);
51+
assert(o.named.b == 2);
52+
assert(o.anonymous_named_0.c == 3);
53+
assert(o.anonymous_named_0.d == 4);
54+
assert(o.anonymous_named_1.g == 5);
55+
assert(o.anonymous_named_1.h == 6);
56+
assert(o.e == 7);
57+
assert(o.f == 8);
58+
assert(o.i == 9);
59+
assert(o.inner_named.j == 10);
60+
assert(o.k == 11);
61+
62+
return 0;
63+
}

tests/unit/anonymous-struct_c.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <assert.h>
2+
3+
struct Outer {
4+
struct Named {
5+
int a;
6+
int b;
7+
} named;
8+
9+
struct {
10+
int c;
11+
int d;
12+
} anon0;
13+
14+
struct {
15+
int g;
16+
int h;
17+
} anon1;
18+
19+
struct {
20+
int e;
21+
int f;
22+
};
23+
24+
struct {
25+
int i;
26+
struct {
27+
int j;
28+
} inner_named;
29+
struct {
30+
int k;
31+
};
32+
};
33+
};
34+
35+
int main(void) {
36+
struct Outer o = {0};
37+
38+
o.named.a = 1;
39+
o.named.b = 2;
40+
o.anon0.c = 3;
41+
o.anon0.d = 4;
42+
o.anon1.g = 5;
43+
o.anon1.h = 6;
44+
o.e = 7;
45+
o.f = 8;
46+
o.i = 9;
47+
o.inner_named.j = 10;
48+
o.k = 11;
49+
50+
assert(o.named.a == 1);
51+
assert(o.named.b == 2);
52+
assert(o.anon0.c == 3);
53+
assert(o.anon0.d == 4);
54+
assert(o.anon1.g == 5);
55+
assert(o.anon1.h == 6);
56+
assert(o.e == 7);
57+
assert(o.f == 8);
58+
assert(o.i == 9);
59+
assert(o.inner_named.j == 10);
60+
assert(o.k == 11);
61+
62+
return 0;
63+
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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(Default)]
10+
pub struct Outer_Named {
11+
pub a: Value<i32>,
12+
pub b: Value<i32>,
13+
}
14+
impl Clone for Outer_Named {
15+
fn clone(&self) -> Self {
16+
let mut this = Self {
17+
a: Rc::new(RefCell::new((*self.a.borrow()))),
18+
b: Rc::new(RefCell::new((*self.b.borrow()))),
19+
};
20+
this
21+
}
22+
}
23+
impl ByteRepr for Outer_Named {}
24+
#[derive(Default)]
25+
pub struct Outer_anon_0 {
26+
pub c: Value<i32>,
27+
pub d: Value<i32>,
28+
}
29+
impl Clone for Outer_anon_0 {
30+
fn clone(&self) -> Self {
31+
let mut this = Self {
32+
c: Rc::new(RefCell::new((*self.c.borrow()))),
33+
d: Rc::new(RefCell::new((*self.d.borrow()))),
34+
};
35+
this
36+
}
37+
}
38+
impl ByteRepr for Outer_anon_0 {}
39+
#[derive(Default)]
40+
pub struct Outer_anon_1 {
41+
pub g: Value<i32>,
42+
pub h: Value<i32>,
43+
}
44+
impl Clone for Outer_anon_1 {
45+
fn clone(&self) -> Self {
46+
let mut this = Self {
47+
g: Rc::new(RefCell::new((*self.g.borrow()))),
48+
h: Rc::new(RefCell::new((*self.h.borrow()))),
49+
};
50+
this
51+
}
52+
}
53+
impl ByteRepr for Outer_anon_1 {}
54+
#[derive(Default)]
55+
pub struct Outer_anon_2 {
56+
pub e: Value<i32>,
57+
pub f: Value<i32>,
58+
}
59+
impl Clone for Outer_anon_2 {
60+
fn clone(&self) -> Self {
61+
let mut this = Self {
62+
e: Rc::new(RefCell::new((*self.e.borrow()))),
63+
f: Rc::new(RefCell::new((*self.f.borrow()))),
64+
};
65+
this
66+
}
67+
}
68+
impl ByteRepr for Outer_anon_2 {}
69+
#[derive(Default)]
70+
pub struct Outer_anon_3_anon_0 {
71+
pub j: Value<i32>,
72+
}
73+
impl Clone for Outer_anon_3_anon_0 {
74+
fn clone(&self) -> Self {
75+
let mut this = Self {
76+
j: Rc::new(RefCell::new((*self.j.borrow()))),
77+
};
78+
this
79+
}
80+
}
81+
impl ByteRepr for Outer_anon_3_anon_0 {}
82+
#[derive(Default)]
83+
pub struct Outer_anon_3_anon_1 {
84+
pub k: Value<i32>,
85+
}
86+
impl Clone for Outer_anon_3_anon_1 {
87+
fn clone(&self) -> Self {
88+
let mut this = Self {
89+
k: Rc::new(RefCell::new((*self.k.borrow()))),
90+
};
91+
this
92+
}
93+
}
94+
impl ByteRepr for Outer_anon_3_anon_1 {}
95+
#[derive(Default)]
96+
pub struct Outer_anon_3 {
97+
pub i: Value<i32>,
98+
pub inner_named: Value<Outer_anon_3_anon_0>,
99+
pub anon_1: Value<Outer_anon_3_anon_1>,
100+
}
101+
impl Clone for Outer_anon_3 {
102+
fn clone(&self) -> Self {
103+
let mut this = Self {
104+
i: Rc::new(RefCell::new((*self.i.borrow()))),
105+
inner_named: Rc::new(RefCell::new((*self.inner_named.borrow()).clone())),
106+
anon_1: Rc::new(RefCell::new((*self.anon_1.borrow()).clone())),
107+
};
108+
this
109+
}
110+
}
111+
impl ByteRepr for Outer_anon_3 {}
112+
#[derive(Default)]
113+
pub struct Outer {
114+
pub named: Value<Outer_Named>,
115+
pub anonymous_named_0: Value<Outer_anon_0>,
116+
pub anonymous_named_1: Value<Outer_anon_1>,
117+
pub anon_2: Value<Outer_anon_2>,
118+
pub anon_3: Value<Outer_anon_3>,
119+
}
120+
impl Clone for Outer {
121+
fn clone(&self) -> Self {
122+
let mut this = Self {
123+
named: Rc::new(RefCell::new((*self.named.borrow()).clone())),
124+
anonymous_named_0: Rc::new(RefCell::new((*self.anonymous_named_0.borrow()).clone())),
125+
anonymous_named_1: Rc::new(RefCell::new((*self.anonymous_named_1.borrow()).clone())),
126+
anon_2: Rc::new(RefCell::new((*self.anon_2.borrow()).clone())),
127+
anon_3: Rc::new(RefCell::new((*self.anon_3.borrow()).clone())),
128+
};
129+
this
130+
}
131+
}
132+
impl ByteRepr for Outer {}
133+
pub fn main() {
134+
std::process::exit(main_0());
135+
}
136+
fn main_0() -> i32 {
137+
let o: Value<Outer> = Rc::new(RefCell::new(Outer {
138+
named: Rc::new(RefCell::new(Outer_Named {
139+
a: Rc::new(RefCell::new(<i32>::default())),
140+
b: Rc::new(RefCell::new(<i32>::default())),
141+
})),
142+
anonymous_named_0: Rc::new(RefCell::new(Outer_anon_0 {
143+
c: Rc::new(RefCell::new(<i32>::default())),
144+
d: Rc::new(RefCell::new(<i32>::default())),
145+
})),
146+
anonymous_named_1: Rc::new(RefCell::new(Outer_anon_1 {
147+
g: Rc::new(RefCell::new(<i32>::default())),
148+
h: Rc::new(RefCell::new(<i32>::default())),
149+
})),
150+
anon_2: Rc::new(RefCell::new(Outer_anon_2 {
151+
e: Rc::new(RefCell::new(<i32>::default())),
152+
f: Rc::new(RefCell::new(<i32>::default())),
153+
})),
154+
anon_3: Rc::new(RefCell::new(Outer_anon_3 {
155+
i: Rc::new(RefCell::new(<i32>::default())),
156+
inner_named: Rc::new(RefCell::new(Outer_anon_3_anon_0 {
157+
j: Rc::new(RefCell::new(<i32>::default())),
158+
})),
159+
anon_1: Rc::new(RefCell::new(Outer_anon_3_anon_1 {
160+
k: Rc::new(RefCell::new(<i32>::default())),
161+
})),
162+
})),
163+
}));
164+
(*(*(*o.borrow()).named.borrow()).a.borrow_mut()) = 1;
165+
(*(*(*o.borrow()).named.borrow()).b.borrow_mut()) = 2;
166+
(*(*(*o.borrow()).anonymous_named_0.borrow()).c.borrow_mut()) = 3;
167+
(*(*(*o.borrow()).anonymous_named_0.borrow()).d.borrow_mut()) = 4;
168+
(*(*(*o.borrow()).anonymous_named_1.borrow()).g.borrow_mut()) = 5;
169+
(*(*(*o.borrow()).anonymous_named_1.borrow()).h.borrow_mut()) = 6;
170+
(*(*(*o.borrow()).anon_2.borrow()).e.borrow_mut()) = 7;
171+
(*(*(*o.borrow()).anon_2.borrow()).f.borrow_mut()) = 8;
172+
(*(*(*o.borrow()).anon_3.borrow()).i.borrow_mut()) = 9;
173+
(*(*(*(*o.borrow()).anon_3.borrow()).inner_named.borrow())
174+
.j
175+
.borrow_mut()) = 10;
176+
(*(*(*(*o.borrow()).anon_3.borrow()).anon_1.borrow())
177+
.k
178+
.borrow_mut()) = 11;
179+
assert!(((*(*(*o.borrow()).named.borrow()).a.borrow()) == 1));
180+
assert!(((*(*(*o.borrow()).named.borrow()).b.borrow()) == 2));
181+
assert!(((*(*(*o.borrow()).anonymous_named_0.borrow()).c.borrow()) == 3));
182+
assert!(((*(*(*o.borrow()).anonymous_named_0.borrow()).d.borrow()) == 4));
183+
assert!(((*(*(*o.borrow()).anonymous_named_1.borrow()).g.borrow()) == 5));
184+
assert!(((*(*(*o.borrow()).anonymous_named_1.borrow()).h.borrow()) == 6));
185+
assert!(((*(*(*o.borrow()).anon_2.borrow()).e.borrow()) == 7));
186+
assert!(((*(*(*o.borrow()).anon_2.borrow()).f.borrow()) == 8));
187+
assert!(((*(*(*o.borrow()).anon_3.borrow()).i.borrow()) == 9));
188+
assert!(
189+
((*(*(*(*o.borrow()).anon_3.borrow()).inner_named.borrow())
190+
.j
191+
.borrow())
192+
== 10)
193+
);
194+
assert!(
195+
((*(*(*(*o.borrow()).anon_3.borrow()).anon_1.borrow())
196+
.k
197+
.borrow())
198+
== 11)
199+
);
200+
return 0;
201+
}

0 commit comments

Comments
 (0)