Skip to content

Commit c2336d9

Browse files
committed
Add default_in_statics test
1 parent 86495dc commit c2336d9

3 files changed

Lines changed: 498 additions & 0 deletions

File tree

tests/unit/default_in_statics.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <cassert>
2+
#include <cstddef>
3+
4+
typedef int (*FnPtr)(int);
5+
6+
struct Inner {
7+
int v;
8+
const char *name;
9+
};
10+
11+
struct Outer {
12+
int *p1;
13+
const int *p2;
14+
int *arr[3];
15+
const char *cp;
16+
int **pp;
17+
Inner inner;
18+
int x;
19+
FnPtr fn;
20+
};
21+
22+
struct Foo {
23+
const char *s1;
24+
const char *s2;
25+
FnPtr fn1;
26+
FnPtr fn2;
27+
int n;
28+
};
29+
30+
static FnPtr static_fn;
31+
static Outer static_outer;
32+
static Inner static_inner_array[2];
33+
34+
static Foo static_foo = {"hello", 0, 0, 0, 42};
35+
36+
static Foo static_foo_array[2] = {
37+
{"first", 0, 0, 0, 1},
38+
{"second", 0, 0, 0, 2},
39+
};
40+
41+
void check_local_static() {
42+
static Outer local_outer;
43+
static FnPtr local_fn;
44+
static int *local_p;
45+
assert(local_outer.p1 == nullptr);
46+
assert(local_outer.fn == nullptr);
47+
assert(local_fn == nullptr);
48+
assert(local_p == nullptr);
49+
}
50+
51+
int main() {
52+
assert(static_fn == nullptr);
53+
54+
assert(static_outer.p1 == nullptr);
55+
assert(static_outer.p2 == nullptr);
56+
assert(static_outer.cp == nullptr);
57+
assert(static_outer.pp == nullptr);
58+
assert(static_outer.fn == nullptr);
59+
for (int i = 0; i < 3; ++i) {
60+
assert(static_outer.arr[i] == nullptr);
61+
}
62+
assert(static_outer.inner.name == nullptr);
63+
64+
for (int i = 0; i < 2; ++i) {
65+
assert(static_inner_array[i].name == nullptr);
66+
}
67+
68+
assert(static_foo.s2 == nullptr);
69+
assert(static_foo.fn1 == nullptr);
70+
assert(static_foo.fn2 == nullptr);
71+
assert(static_foo.n == 42);
72+
73+
for (int i = 0; i < 2; ++i) {
74+
assert(static_foo_array[i].s2 == nullptr);
75+
assert(static_foo_array[i].fn1 == nullptr);
76+
assert(static_foo_array[i].fn2 == nullptr);
77+
}
78+
79+
check_local_static();
80+
81+
return 0;
82+
}
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
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 Inner {
11+
pub v: Value<i32>,
12+
pub name: Value<Ptr<u8>>,
13+
}
14+
impl Clone for Inner {
15+
fn clone(&self) -> Self {
16+
let mut this = Self {
17+
v: Rc::new(RefCell::new((*self.v.borrow()))),
18+
name: Rc::new(RefCell::new((*self.name.borrow()).clone())),
19+
};
20+
this
21+
}
22+
}
23+
impl ByteRepr for Inner {}
24+
#[derive()]
25+
pub struct Outer {
26+
pub p1: Value<Ptr<i32>>,
27+
pub p2: Value<Ptr<i32>>,
28+
pub arr: Value<Box<[Ptr<i32>]>>,
29+
pub cp: Value<Ptr<u8>>,
30+
pub pp: Value<Ptr<Ptr<i32>>>,
31+
pub inner: Value<Inner>,
32+
pub x: Value<i32>,
33+
pub fn_: Value<FnPtr<fn(i32) -> i32>>,
34+
}
35+
impl Clone for Outer {
36+
fn clone(&self) -> Self {
37+
let mut this = Self {
38+
p1: Rc::new(RefCell::new((*self.p1.borrow()).clone())),
39+
p2: Rc::new(RefCell::new((*self.p2.borrow()).clone())),
40+
arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())),
41+
cp: Rc::new(RefCell::new((*self.cp.borrow()).clone())),
42+
pp: Rc::new(RefCell::new((*self.pp.borrow()).clone())),
43+
inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())),
44+
x: Rc::new(RefCell::new((*self.x.borrow()))),
45+
fn_: Rc::new(RefCell::new((*self.fn_.borrow()).clone())),
46+
};
47+
this
48+
}
49+
}
50+
impl Default for Outer {
51+
fn default() -> Self {
52+
Outer {
53+
p1: Rc::new(RefCell::new(Ptr::<i32>::null())),
54+
p2: Rc::new(RefCell::new(Ptr::<i32>::null())),
55+
arr: Rc::new(RefCell::new(
56+
(0..3)
57+
.map(|_| Ptr::<i32>::null())
58+
.collect::<Box<[Ptr<i32>]>>(),
59+
)),
60+
cp: Rc::new(RefCell::new(Ptr::<u8>::null())),
61+
pp: Rc::new(RefCell::new(Ptr::<Ptr<i32>>::null())),
62+
inner: <Value<Inner>>::default(),
63+
x: <Value<i32>>::default(),
64+
fn_: Rc::new(RefCell::new(FnPtr::null())),
65+
}
66+
}
67+
}
68+
impl ByteRepr for Outer {}
69+
#[derive()]
70+
pub struct Foo {
71+
pub s1: Value<Ptr<u8>>,
72+
pub s2: Value<Ptr<u8>>,
73+
pub fn1: Value<FnPtr<fn(i32) -> i32>>,
74+
pub fn2: Value<FnPtr<fn(i32) -> i32>>,
75+
pub n: Value<i32>,
76+
}
77+
impl Clone for Foo {
78+
fn clone(&self) -> Self {
79+
let mut this = Self {
80+
s1: Rc::new(RefCell::new((*self.s1.borrow()).clone())),
81+
s2: Rc::new(RefCell::new((*self.s2.borrow()).clone())),
82+
fn1: Rc::new(RefCell::new((*self.fn1.borrow()).clone())),
83+
fn2: Rc::new(RefCell::new((*self.fn2.borrow()).clone())),
84+
n: Rc::new(RefCell::new((*self.n.borrow()))),
85+
};
86+
this
87+
}
88+
}
89+
impl Default for Foo {
90+
fn default() -> Self {
91+
Foo {
92+
s1: Rc::new(RefCell::new(Ptr::<u8>::null())),
93+
s2: Rc::new(RefCell::new(Ptr::<u8>::null())),
94+
fn1: Rc::new(RefCell::new(FnPtr::null())),
95+
fn2: Rc::new(RefCell::new(FnPtr::null())),
96+
n: <Value<i32>>::default(),
97+
}
98+
}
99+
}
100+
impl ByteRepr for Foo {}
101+
thread_local!(
102+
pub static static_p1: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));
103+
);
104+
thread_local!(
105+
pub static static_p2: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));
106+
);
107+
thread_local!(
108+
pub static static_cp: Value<Ptr<u8>> = Rc::new(RefCell::new(Ptr::<u8>::null()));
109+
);
110+
thread_local!(
111+
pub static static_arr: Value<Box<[Ptr<i32>]>> = Rc::new(RefCell::new(
112+
(0..4)
113+
.map(|_| Ptr::<i32>::null())
114+
.collect::<Box<[Ptr<i32>]>>(),
115+
));
116+
);
117+
thread_local!(
118+
pub static static_pp: Value<Ptr<Ptr<i32>>> = Rc::new(RefCell::new(Ptr::<Ptr<i32>>::null()));
119+
);
120+
thread_local!(
121+
pub static static_fn: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::null()));
122+
);
123+
thread_local!(
124+
pub static static_outer: Value<Outer> = Rc::new(RefCell::new(<Outer>::default()));
125+
);
126+
thread_local!(
127+
pub static static_inner_array: Value<Box<[Inner]>> = Rc::new(RefCell::new(
128+
(0..2).map(|_| <Inner>::default()).collect::<Box<[Inner]>>(),
129+
));
130+
);
131+
thread_local!(
132+
pub static static_foo: Value<Foo> = Rc::new(RefCell::new(Foo {
133+
s1: Rc::new(RefCell::new(Ptr::from_string_literal("hello"))),
134+
s2: Rc::new(RefCell::new(Default::default())),
135+
fn1: Rc::new(RefCell::new(FnPtr::null())),
136+
fn2: Rc::new(RefCell::new(FnPtr::null())),
137+
n: Rc::new(RefCell::new(42)),
138+
}));
139+
);
140+
thread_local!(
141+
pub static static_foo_array: Value<Box<[Foo]>> = Rc::new(RefCell::new(Box::new([
142+
Foo {
143+
s1: Rc::new(RefCell::new(Ptr::from_string_literal("first"))),
144+
s2: Rc::new(RefCell::new(Default::default())),
145+
fn1: Rc::new(RefCell::new(FnPtr::null())),
146+
fn2: Rc::new(RefCell::new(FnPtr::null())),
147+
n: Rc::new(RefCell::new(1)),
148+
},
149+
Foo {
150+
s1: Rc::new(RefCell::new(Ptr::from_string_literal("second"))),
151+
s2: Rc::new(RefCell::new(Default::default())),
152+
fn1: Rc::new(RefCell::new(FnPtr::null())),
153+
fn2: Rc::new(RefCell::new(FnPtr::null())),
154+
n: Rc::new(RefCell::new(2)),
155+
},
156+
])));
157+
);
158+
pub fn check_local_static_0() {
159+
thread_local!(
160+
static local_outer: Value<Outer> = Rc::new(RefCell::new(<Outer>::default()));
161+
);
162+
thread_local!(
163+
static local_fn: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::null()));
164+
);
165+
thread_local!(
166+
static local_p: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));
167+
);
168+
assert!((*(*local_outer.with(Value::clone).borrow()).p1.borrow()).is_null());
169+
assert!((*(*local_outer.with(Value::clone).borrow()).fn_.borrow()).is_null());
170+
assert!((*local_fn.with(Value::clone).borrow()).is_null());
171+
assert!((*local_p.with(Value::clone).borrow()).is_null());
172+
}
173+
pub fn main() {
174+
std::process::exit(main_0());
175+
}
176+
fn main_0() -> i32 {
177+
assert!((*static_p1.with(Value::clone).borrow()).is_null());
178+
assert!((*static_p2.with(Value::clone).borrow()).is_null());
179+
assert!((*static_cp.with(Value::clone).borrow()).is_null());
180+
let i: Value<i32> = Rc::new(RefCell::new(0));
181+
'loop_: while ((*i.borrow()) < 4) {
182+
assert!(((*static_arr.with(Value::clone).borrow())[(*i.borrow()) as usize]).is_null());
183+
(*i.borrow_mut()).prefix_inc();
184+
}
185+
assert!((*static_pp.with(Value::clone).borrow()).is_null());
186+
assert!((*static_fn.with(Value::clone).borrow()).is_null());
187+
assert!((*(*static_outer.with(Value::clone).borrow()).p1.borrow()).is_null());
188+
assert!((*(*static_outer.with(Value::clone).borrow()).p2.borrow()).is_null());
189+
assert!((*(*static_outer.with(Value::clone).borrow()).cp.borrow()).is_null());
190+
assert!((*(*static_outer.with(Value::clone).borrow()).pp.borrow()).is_null());
191+
assert!((*(*static_outer.with(Value::clone).borrow()).fn_.borrow()).is_null());
192+
let i: Value<i32> = Rc::new(RefCell::new(0));
193+
'loop_: while ((*i.borrow()) < 3) {
194+
assert!(((*(*static_outer.with(Value::clone).borrow()).arr.borrow())
195+
[(*i.borrow()) as usize])
196+
.is_null());
197+
(*i.borrow_mut()).prefix_inc();
198+
}
199+
assert!(
200+
(*(*(*static_outer.with(Value::clone).borrow()).inner.borrow())
201+
.name
202+
.borrow())
203+
.is_null()
204+
);
205+
let i: Value<i32> = Rc::new(RefCell::new(0));
206+
'loop_: while ((*i.borrow()) < 2) {
207+
assert!(
208+
(*(*static_inner_array.with(Value::clone).borrow())[(*i.borrow()) as usize]
209+
.name
210+
.borrow())
211+
.is_null()
212+
);
213+
(*i.borrow_mut()).prefix_inc();
214+
}
215+
assert!((*(*static_foo.with(Value::clone).borrow()).s2.borrow()).is_null());
216+
assert!((*(*static_foo.with(Value::clone).borrow()).fn1.borrow()).is_null());
217+
assert!((*(*static_foo.with(Value::clone).borrow()).fn2.borrow()).is_null());
218+
assert!(((*(*static_foo.with(Value::clone).borrow()).n.borrow()) == 42));
219+
let i: Value<i32> = Rc::new(RefCell::new(0));
220+
'loop_: while ((*i.borrow()) < 2) {
221+
assert!(
222+
(*(*static_foo_array.with(Value::clone).borrow())[(*i.borrow()) as usize]
223+
.s2
224+
.borrow())
225+
.is_null()
226+
);
227+
assert!(
228+
(*(*static_foo_array.with(Value::clone).borrow())[(*i.borrow()) as usize]
229+
.fn1
230+
.borrow())
231+
.is_null()
232+
);
233+
assert!(
234+
(*(*static_foo_array.with(Value::clone).borrow())[(*i.borrow()) as usize]
235+
.fn2
236+
.borrow())
237+
.is_null()
238+
);
239+
(*i.borrow_mut()).prefix_inc();
240+
}
241+
({ check_local_static_0() });
242+
return 0;
243+
}

0 commit comments

Comments
 (0)