Skip to content

Commit a35b550

Browse files
committed
Update tests
1 parent bcab56f commit a35b550

3 files changed

Lines changed: 87 additions & 12 deletions

File tree

‎tests/unit/class.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ struct Counter {
5151
}
5252
};
5353

54+
struct Tally {
55+
mutable int calls;
56+
57+
int SameNameBetweenConstAndNonConst() {
58+
calls += 1;
59+
return 1;
60+
}
61+
62+
int SameNameBetweenConstAndNonConst() const {
63+
calls += 10;
64+
return 2;
65+
}
66+
};
67+
5468
int RandomRoute(Route &route) {
5569
if (route.path.first % 2) {
5670
return route.path.SetFirst(route.path.SetSecond(10));
@@ -74,5 +88,13 @@ int main() {
7488
assert(c2 == c1);
7589
assert(c1.calls == 3);
7690
assert(c2.calls == 2);
91+
Tally t{0};
92+
const Tally ct{0};
93+
const Tally &rt = t;
94+
assert(t.SameNameBetweenConstAndNonConst() == 1);
95+
assert(ct.SameNameBetweenConstAndNonConst() == 2);
96+
assert(rt.SameNameBetweenConstAndNonConst() == 2);
97+
assert(t.calls == 11);
98+
assert(ct.calls == 10);
7799
return 0;
78100
}

‎tests/unit/out/refcount/class.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ impl std::cmp::PartialEq for Counter {
5050
}
5151
}
5252
impl std::cmp::Eq for Counter {}
53+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
54+
#[byte_size(4)]
55+
pub struct Tally {
56+
#[offset(0)]
57+
pub calls: i32,
58+
}
5359
pub fn RandomRoute_0(route: Ptr<Route>) -> i32 {
5460
if ((route.with(|__s| __s.path.first) % 2) != 0) {
5561
return ({
@@ -105,6 +111,14 @@ fn main_0() -> i32 {
105111
assert!(({ CounterImpl::operator_eq(&c2.as_pointer(), c1.as_pointer(),) }));
106112
assert!(({ (*c1.borrow()).calls } == 3));
107113
assert!(({ (*c2.borrow()).calls } == 2));
114+
let t: Value<Tally> = Rc::new(RefCell::new(Tally { calls: 0 }));
115+
let ct: Value<Tally> = Rc::new(RefCell::new(Tally { calls: 0 }));
116+
let rt: Ptr<Tally> = t.as_pointer();
117+
assert!((({ TallyImpl::SameNameBetweenConstAndNonConst_1(&t.as_pointer(),) }) == 1));
118+
assert!((({ TallyImpl::SameNameBetweenConstAndNonConst_2(&ct.as_pointer(),) }) == 2));
119+
assert!((({ TallyImpl::SameNameBetweenConstAndNonConst_2(&rt,) }) == 2));
120+
assert!(({ (*t.borrow()).calls } == 11));
121+
assert!(({ (*ct.borrow()).calls } == 10));
108122
return 0;
109123
}
110124
pub trait CounterImpl {
@@ -168,4 +182,22 @@ impl RouteImpl for Ptr<Route> {
168182
return old_cost;
169183
}
170184
}
185+
pub trait TallyImpl {
186+
fn SameNameBetweenConstAndNonConst_1(&self) -> i32;
187+
fn SameNameBetweenConstAndNonConst_2(&self) -> i32;
188+
}
189+
impl TallyImpl for Ptr<Tally> {
190+
fn SameNameBetweenConstAndNonConst_1(&self) -> i32 {
191+
{
192+
field!((*self), calls).with_mut(|__v| *__v = *__v + 1)
193+
};
194+
return 1;
195+
}
196+
fn SameNameBetweenConstAndNonConst_2(&self) -> i32 {
197+
{
198+
field!((*self), calls).with_mut(|__v| *__v = *__v + 10)
199+
};
200+
return 2;
201+
}
202+
}
171203
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/class.rs‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,36 @@ pub struct Counter {
6161
pub calls: i32,
6262
}
6363
impl Counter {
64-
pub unsafe fn Get(&mut self) -> i32 {
65-
self.calls.prefix_inc();
64+
pub unsafe fn Get(&self) -> i32 {
65+
(*(&raw const self.calls).cast_mut()).prefix_inc();
6666
return self.v;
6767
}
68-
pub unsafe fn operator_eq(&mut self, o: *const Counter) -> bool {
69-
self.calls.prefix_inc();
68+
pub unsafe fn operator_eq(&self, o: *const Counter) -> bool {
69+
(*(&raw const self.calls).cast_mut()).prefix_inc();
7070
return ((self.v) == ((*o).v));
7171
}
7272
}
7373
impl std::cmp::PartialEq for Counter {
7474
fn eq(&self, other: &Self) -> bool {
75-
unsafe {
76-
Counter::operator_eq(&mut *(&raw const *self).cast_mut(), other as *const Counter)
77-
}
75+
unsafe { Counter::operator_eq(self, other as *const Counter) }
7876
}
7977
}
8078
impl std::cmp::Eq for Counter {}
79+
#[repr(C)]
80+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
81+
pub struct Tally {
82+
pub calls: i32,
83+
}
84+
impl Tally {
85+
pub unsafe fn SameNameBetweenConstAndNonConst_1(&mut self) -> i32 {
86+
self.calls += 1;
87+
return 1;
88+
}
89+
pub unsafe fn SameNameBetweenConstAndNonConst_2(&self) -> i32 {
90+
(*(&raw const self.calls).cast_mut()) += 10;
91+
return 2;
92+
}
93+
}
8194
pub unsafe fn RandomRoute_0(route: *mut Route) -> i32 {
8295
if ((((*route).path.first) % (2)) != 0) {
8396
return (unsafe {
@@ -128,13 +141,21 @@ unsafe fn main_0() -> i32 {
128141
let mut c1: Counter = Counter { v: 3, calls: 0 };
129142
let c2: Counter = Counter { v: 3, calls: 0 };
130143
let mut pc: *const Counter = (&mut c1 as *mut Counter).cast_const();
131-
assert!(((unsafe { Counter::Get(&mut *(&raw const c1).cast_mut(),) }) == (3)));
132-
assert!(((unsafe { Counter::Get(&mut *(&raw const c2).cast_mut(),) }) == (3)));
133-
assert!(((unsafe { Counter::Get(&mut *(&raw const (*pc)).cast_mut(),) }) == (3)));
134-
assert!((unsafe { Counter::operator_eq(&mut *(&raw const c1).cast_mut(), &c2,) }));
135-
assert!((unsafe { Counter::operator_eq(&mut *(&raw const c2).cast_mut(), &c1,) }));
144+
assert!(((unsafe { Counter::Get(&c1,) }) == (3)));
145+
assert!(((unsafe { Counter::Get(&c2,) }) == (3)));
146+
assert!(((unsafe { Counter::Get(&(*pc),) }) == (3)));
147+
assert!((unsafe { Counter::operator_eq(&c1, &c2,) }));
148+
assert!((unsafe { Counter::operator_eq(&c2, &c1,) }));
136149
assert!(((c1.calls) == (3)));
137150
assert!(((c2.calls) == (2)));
151+
let mut t: Tally = Tally { calls: 0 };
152+
let ct: Tally = Tally { calls: 0 };
153+
let rt: *const Tally = &t;
154+
assert!(((unsafe { Tally::SameNameBetweenConstAndNonConst_1(&mut t,) }) == (1)));
155+
assert!(((unsafe { Tally::SameNameBetweenConstAndNonConst_2(&ct,) }) == (2)));
156+
assert!(((unsafe { Tally::SameNameBetweenConstAndNonConst_2(&(*rt),) }) == (2)));
157+
assert!(((t.calls) == (11)));
158+
assert!(((ct.calls) == (10)));
138159
return 0;
139160
}
140161
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)