Skip to content

Commit 8dcd152

Browse files
committed
Add implicit autoref tests
1 parent 5b60ce6 commit 8dcd152

3 files changed

Lines changed: 196 additions & 0 deletions

File tree

tests/unit/implicit_autoref.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <cstdio>
2+
3+
struct Counter {
4+
int value = 0;
5+
void bump() { ++value; }
6+
int get() const { return value; }
7+
};
8+
9+
struct Holder {
10+
Counter c;
11+
Counter &ref;
12+
Holder(Counter &c) : ref(c) {}
13+
};
14+
15+
void via_ref(Counter &r) { r.bump(); }
16+
17+
int main() {
18+
Counter c;
19+
Counter *p = &c;
20+
(*p).bump();
21+
p->bump();
22+
23+
Counter arr[2];
24+
arr[0].bump();
25+
arr[1].bump();
26+
27+
Holder h(c);
28+
h.c.bump();
29+
h.ref.bump();
30+
31+
via_ref(c);
32+
33+
int sum = (*p).get() + h.c.get() + h.ref.get() + arr[0].get() + arr[1].get();
34+
printf("%d\n", sum);
35+
return 0;
36+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 Counter {
11+
pub value: Value<i32>,
12+
}
13+
impl Counter {
14+
pub fn bump(&self) {
15+
(*self.value.borrow_mut()).prefix_inc();
16+
}
17+
pub fn get(&self) -> i32 {
18+
return (*self.value.borrow());
19+
}
20+
}
21+
impl Clone for Counter {
22+
fn clone(&self) -> Self {
23+
let mut this = Self {
24+
value: Rc::new(RefCell::new((*self.value.borrow()))),
25+
};
26+
this
27+
}
28+
}
29+
impl ByteRepr for Counter {}
30+
#[derive(Default)]
31+
pub struct Holder {
32+
pub c: Value<Counter>,
33+
pub ref_: Ptr<Counter>,
34+
}
35+
impl Holder {
36+
pub fn Holder(c: Ptr<Counter>) -> Self {
37+
let mut this = Self {
38+
c: Rc::new(RefCell::new(<Counter>::default())),
39+
ref_: (c).clone(),
40+
};
41+
this
42+
}
43+
}
44+
impl Clone for Holder {
45+
fn clone(&self) -> Self {
46+
let mut this = Self {
47+
c: Rc::new(RefCell::new((*self.c.borrow()).clone())),
48+
ref_: (self.ref_).clone(),
49+
};
50+
this
51+
}
52+
}
53+
impl ByteRepr for Holder {}
54+
pub fn via_ref_0(r: Ptr<Counter>) {
55+
({ (*r.upgrade().deref()).bump() });
56+
}
57+
pub fn main() {
58+
std::process::exit(main_0());
59+
}
60+
fn main_0() -> i32 {
61+
let c: Value<Counter> = Rc::new(RefCell::new(<Counter>::default()));
62+
let p: Value<Ptr<Counter>> = Rc::new(RefCell::new((c.as_pointer())));
63+
({ (*(*p.borrow()).upgrade().deref()).bump() });
64+
({ (*(*p.borrow()).upgrade().deref()).bump() });
65+
let arr: Value<Box<[Counter]>> = Rc::new(RefCell::new(
66+
(0..2)
67+
.map(|_| <Counter>::default())
68+
.collect::<Box<[Counter]>>(),
69+
));
70+
({ (*arr.borrow())[(0) as usize].bump() });
71+
({ (*arr.borrow())[(1) as usize].bump() });
72+
let h: Value<Holder> = Rc::new(RefCell::new(Holder::Holder(c.as_pointer())));
73+
({ (*(*h.borrow()).c.borrow()).bump() });
74+
({ (*(*h.borrow()).ref_.upgrade().deref()).bump() });
75+
({
76+
let _r: Ptr<Counter> = c.as_pointer();
77+
via_ref_0(_r)
78+
});
79+
let sum: Value<i32> = Rc::new(RefCell::new({
80+
let _lhs = {
81+
let _lhs = {
82+
let _lhs = {
83+
let _lhs = ({ (*(*p.borrow()).upgrade().deref()).get() });
84+
_lhs + ({ (*(*h.borrow()).c.borrow()).get() })
85+
};
86+
_lhs + ({ (*(*h.borrow()).ref_.upgrade().deref()).get() })
87+
};
88+
_lhs + ({ (*arr.borrow())[(0) as usize].get() })
89+
};
90+
_lhs + ({ (*arr.borrow())[(1) as usize].get() })
91+
}));
92+
println!("{}", (*sum.borrow()));
93+
return 0;
94+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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, Default)]
11+
pub struct Counter {
12+
pub value: i32,
13+
}
14+
impl Counter {
15+
pub unsafe fn bump(&mut self) {
16+
self.value.prefix_inc();
17+
}
18+
pub unsafe fn get(&self) -> i32 {
19+
return self.value;
20+
}
21+
}
22+
#[repr(C)]
23+
#[derive(Copy, Clone, Default)]
24+
pub struct Holder {
25+
pub c: Counter,
26+
pub ref_: *mut Counter,
27+
}
28+
impl Holder {
29+
pub unsafe fn Holder(c: *mut Counter) -> Self {
30+
let mut this = Self {
31+
c: <Counter>::default(),
32+
ref_: c,
33+
};
34+
this
35+
}
36+
}
37+
pub unsafe fn via_ref_0(r: *mut Counter) {
38+
(unsafe { (*r).bump() });
39+
}
40+
pub fn main() {
41+
unsafe {
42+
std::process::exit(main_0() as i32);
43+
}
44+
}
45+
unsafe fn main_0() -> i32 {
46+
let mut c: Counter = <Counter>::default();
47+
let mut p: *mut Counter = (&mut c as *mut Counter);
48+
(unsafe { (*p).bump() });
49+
(unsafe { (*p).bump() });
50+
let mut arr: [Counter; 2] = [<Counter>::default(); 2];
51+
(unsafe { arr[(0) as usize].bump() });
52+
(unsafe { arr[(1) as usize].bump() });
53+
let mut h: Holder = Holder::Holder(&mut c as *mut Counter);
54+
(unsafe { h.c.bump() });
55+
(unsafe { (*h.ref_).bump() });
56+
(unsafe {
57+
let _r: *mut Counter = &mut c as *mut Counter;
58+
via_ref_0(_r)
59+
});
60+
let mut sum: i32 = (((((unsafe { (*p).get() }) + (unsafe { h.c.get() }))
61+
+ (unsafe { (*h.ref_).get() }))
62+
+ (unsafe { arr[(0) as usize].get() }))
63+
+ (unsafe { arr[(1) as usize].get() }));
64+
printf(b"%d\n\0".as_ptr() as *const i8, sum);
65+
return 0;
66+
}

0 commit comments

Comments
 (0)