Skip to content

Commit 6639c39

Browse files
committed
Add null in statics test
1 parent 84face0 commit 6639c39

3 files changed

Lines changed: 131 additions & 0 deletions

File tree

tests/unit/null_in_statics.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <cassert>
2+
#include <cstddef>
3+
4+
static int *p_mut;
5+
static const int *p_const;
6+
static const char *cp;
7+
static int *arr_of_ptr[4];
8+
static int **pp;
9+
static const int *const_arr_of_ptr[3];
10+
static const char *cp_explicit_null = nullptr;
11+
static int *p_zero = 0;
12+
13+
int main() {
14+
assert(p_mut == nullptr);
15+
assert(p_const == nullptr);
16+
assert(cp == nullptr);
17+
for (int i = 0; i < 4; ++i) {
18+
assert(arr_of_ptr[i] == nullptr);
19+
}
20+
assert(pp == nullptr);
21+
for (int i = 0; i < 3; ++i) {
22+
assert(const_arr_of_ptr[i] == nullptr);
23+
}
24+
assert(cp_explicit_null == nullptr);
25+
assert(p_zero == nullptr);
26+
return 0;
27+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
thread_local!(
10+
pub static p_mut: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));
11+
);
12+
thread_local!(
13+
pub static p_const: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));
14+
);
15+
thread_local!(
16+
pub static cp: Value<Ptr<u8>> = Rc::new(RefCell::new(Ptr::<u8>::null()));
17+
);
18+
thread_local!(
19+
pub static arr_of_ptr: Value<Box<[Ptr<i32>]>> = Rc::new(RefCell::new(
20+
(0..4)
21+
.map(|_| Ptr::<i32>::null())
22+
.collect::<Box<[Ptr<i32>]>>(),
23+
));
24+
);
25+
thread_local!(
26+
pub static pp: Value<Ptr<Ptr<i32>>> = Rc::new(RefCell::new(Ptr::<Ptr<i32>>::null()));
27+
);
28+
thread_local!(
29+
pub static const_arr_of_ptr: Value<Box<[Ptr<i32>]>> = Rc::new(RefCell::new(
30+
(0..3)
31+
.map(|_| Ptr::<i32>::null())
32+
.collect::<Box<[Ptr<i32>]>>(),
33+
));
34+
);
35+
thread_local!(
36+
pub static cp_explicit_null: Value<Ptr<u8>> = Rc::new(RefCell::new(Default::default()));
37+
);
38+
thread_local!(
39+
pub static p_zero: Value<Ptr<i32>> = Rc::new(RefCell::new(Default::default()));
40+
);
41+
pub fn main() {
42+
std::process::exit(main_0());
43+
}
44+
fn main_0() -> i32 {
45+
assert!((*p_mut.with(Value::clone).borrow()).is_null());
46+
assert!((*p_const.with(Value::clone).borrow()).is_null());
47+
assert!((*cp.with(Value::clone).borrow()).is_null());
48+
let i: Value<i32> = Rc::new(RefCell::new(0));
49+
'loop_: while ((*i.borrow()) < 4) {
50+
assert!(((*arr_of_ptr.with(Value::clone).borrow())[(*i.borrow()) as usize]).is_null());
51+
(*i.borrow_mut()).prefix_inc();
52+
}
53+
assert!((*pp.with(Value::clone).borrow()).is_null());
54+
let i: Value<i32> = Rc::new(RefCell::new(0));
55+
'loop_: while ((*i.borrow()) < 3) {
56+
assert!(
57+
((*const_arr_of_ptr.with(Value::clone).borrow())[(*i.borrow()) as usize]).is_null()
58+
);
59+
(*i.borrow_mut()).prefix_inc();
60+
}
61+
assert!((*cp_explicit_null.with(Value::clone).borrow()).is_null());
62+
assert!((*p_zero.with(Value::clone).borrow()).is_null());
63+
return 0;
64+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
pub static mut p_mut: *mut i32 = std::ptr::null_mut();
10+
pub static mut p_const: *const i32 = std::ptr::null();
11+
pub static mut cp: *const u8 = std::ptr::null();
12+
pub static mut arr_of_ptr: [*mut i32; 4] = [std::ptr::null_mut(); 4];
13+
pub static mut pp: *mut *mut i32 = std::ptr::null_mut();
14+
pub static mut const_arr_of_ptr: [*const i32; 3] = [std::ptr::null(); 3];
15+
pub static mut cp_explicit_null: *const u8 = std::ptr::null();
16+
pub static mut p_zero: *mut i32 = std::ptr::null_mut();
17+
pub fn main() {
18+
unsafe {
19+
std::process::exit(main_0() as i32);
20+
}
21+
}
22+
unsafe fn main_0() -> i32 {
23+
assert!((p_mut).is_null());
24+
assert!((p_const).is_null());
25+
assert!((cp).is_null());
26+
let mut i: i32 = 0;
27+
'loop_: while ((i) < (4)) {
28+
assert!((arr_of_ptr[(i) as usize]).is_null());
29+
i.prefix_inc();
30+
}
31+
assert!((pp).is_null());
32+
let mut i: i32 = 0;
33+
'loop_: while ((i) < (3)) {
34+
assert!((const_arr_of_ptr[(i) as usize]).is_null());
35+
i.prefix_inc();
36+
}
37+
assert!((cp_explicit_null).is_null());
38+
assert!((p_zero).is_null());
39+
return 0;
40+
}

0 commit comments

Comments
 (0)