Skip to content

Commit 7691542

Browse files
committed
Update tests
1 parent a863d43 commit 7691542

3 files changed

Lines changed: 272 additions & 0 deletions

File tree

‎tests/unit/class_templates.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,25 @@ template <typename T> T Boxed<T>::twice(T v) { return v + v; }
2828

2929
template <typename T> T Boxed<T>::plus(T other) const { return value + other; }
3030

31+
template <typename T> struct Outer {
32+
template <typename U> struct Inner {
33+
T t;
34+
U u;
35+
int sum() const { return (int)t + (int)u; }
36+
};
37+
38+
T v;
39+
Inner<int> with(int n) const { return Inner<int>{v, n}; }
40+
};
41+
3142
int main() {
43+
Outer<int> oi{3};
44+
assert(oi.with(4).sum() == 7);
45+
Outer<long> ol{5};
46+
Outer<long>::Inner<char> ic{6, 'a'};
47+
assert(ol.with(2).sum() == 7);
48+
assert(ic.sum() == 6 + 'a');
49+
3250
assert(Boxed<int>::twice(3) == 6);
3351
Boxed<int> bi{4};
3452
assert(bi.plus(5) == 9);

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

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,156 @@ impl ByteRepr for Boxed_long_ {
148148
}
149149
}
150150
}
151+
#[derive(Default)]
152+
pub struct Outer_int__Inner_int_ {
153+
pub t: Value<i32>,
154+
pub u: Value<i32>,
155+
}
156+
impl Clone for Outer_int__Inner_int_ {
157+
fn clone(&self) -> Self {
158+
let __this: Value<Outer_int__Inner_int_> = Rc::new(RefCell::new(Self {
159+
t: Rc::new(RefCell::new((*self.t.borrow()))),
160+
u: Rc::new(RefCell::new((*self.u.borrow()))),
161+
}));
162+
let this: Ptr<Outer_int__Inner_int_> = __this.as_pointer();
163+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
164+
}
165+
}
166+
impl ByteRepr for Outer_int__Inner_int_ {
167+
fn byte_size() -> usize {
168+
8
169+
}
170+
fn to_bytes(&self, buf: &mut [u8]) {
171+
(*self.t.borrow()).to_bytes(&mut buf[0..4]);
172+
(*self.u.borrow()).to_bytes(&mut buf[4..8]);
173+
}
174+
fn from_bytes(buf: &[u8]) -> Self {
175+
Self {
176+
t: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
177+
u: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
178+
}
179+
}
180+
}
181+
#[derive(Default)]
182+
pub struct Outer_int_ {
183+
pub v: Value<i32>,
184+
}
185+
impl Clone for Outer_int_ {
186+
fn clone(&self) -> Self {
187+
let __this: Value<Outer_int_> = Rc::new(RefCell::new(Self {
188+
v: Rc::new(RefCell::new((*self.v.borrow()))),
189+
}));
190+
let this: Ptr<Outer_int_> = __this.as_pointer();
191+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
192+
}
193+
}
194+
impl ByteRepr for Outer_int_ {
195+
fn byte_size() -> usize {
196+
4
197+
}
198+
fn to_bytes(&self, buf: &mut [u8]) {
199+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
200+
}
201+
fn from_bytes(buf: &[u8]) -> Self {
202+
Self {
203+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
204+
}
205+
}
206+
}
207+
#[derive(Default)]
208+
pub struct Outer_long__Inner_int_ {
209+
pub t: Value<i64>,
210+
pub u: Value<i32>,
211+
}
212+
impl Clone for Outer_long__Inner_int_ {
213+
fn clone(&self) -> Self {
214+
let __this: Value<Outer_long__Inner_int_> = Rc::new(RefCell::new(Self {
215+
t: Rc::new(RefCell::new((*self.t.borrow()))),
216+
u: Rc::new(RefCell::new((*self.u.borrow()))),
217+
}));
218+
let this: Ptr<Outer_long__Inner_int_> = __this.as_pointer();
219+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
220+
}
221+
}
222+
impl ByteRepr for Outer_long__Inner_int_ {
223+
fn byte_size() -> usize {
224+
16
225+
}
226+
fn to_bytes(&self, buf: &mut [u8]) {
227+
(*self.t.borrow()).to_bytes(&mut buf[0..8]);
228+
(*self.u.borrow()).to_bytes(&mut buf[8..12]);
229+
}
230+
fn from_bytes(buf: &[u8]) -> Self {
231+
Self {
232+
t: Rc::new(RefCell::new(<i64>::from_bytes(&buf[0..8]))),
233+
u: Rc::new(RefCell::new(<i32>::from_bytes(&buf[8..12]))),
234+
}
235+
}
236+
}
237+
#[derive(Default)]
238+
pub struct Outer_long_ {
239+
pub v: Value<i64>,
240+
}
241+
impl Clone for Outer_long_ {
242+
fn clone(&self) -> Self {
243+
let __this: Value<Outer_long_> = Rc::new(RefCell::new(Self {
244+
v: Rc::new(RefCell::new((*self.v.borrow()))),
245+
}));
246+
let this: Ptr<Outer_long_> = __this.as_pointer();
247+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
248+
}
249+
}
250+
impl ByteRepr for Outer_long_ {
251+
fn byte_size() -> usize {
252+
8
253+
}
254+
fn to_bytes(&self, buf: &mut [u8]) {
255+
(*self.v.borrow()).to_bytes(&mut buf[0..8]);
256+
}
257+
fn from_bytes(buf: &[u8]) -> Self {
258+
Self {
259+
v: Rc::new(RefCell::new(<i64>::from_bytes(&buf[0..8]))),
260+
}
261+
}
262+
}
151263
pub fn main() {
152264
__cpp2rust_init_globals();
153265
std::process::exit(main_0());
154266
}
155267
fn main_0() -> i32 {
268+
let oi: Value<Outer_int_> = Rc::new(RefCell::new(Outer_int_ {
269+
v: Rc::new(RefCell::new(3)),
270+
}));
271+
assert!(
272+
(({
273+
Outer_int__Inner_int_Impl::sum(
274+
&Rc::new(RefCell::new(
275+
({ Outer_int_Impl::with(&oi.as_pointer(), 4) }),
276+
))
277+
.as_pointer(),
278+
)
279+
}) == 7)
280+
);
281+
let ol: Value<Outer_long_> = Rc::new(RefCell::new(Outer_long_ {
282+
v: Rc::new(RefCell::new(5_i64)),
283+
}));
284+
let ic: Value<Outer_long__Inner_char_> = Rc::new(RefCell::new(Outer_long__Inner_char_ {
285+
t: Rc::new(RefCell::new(6_i64)),
286+
u: Rc::new(RefCell::new(('a' as u8))),
287+
}));
288+
assert!(
289+
(({
290+
Outer_long__Inner_int_Impl::sum(
291+
&Rc::new(RefCell::new(
292+
({ Outer_long_Impl::with(&ol.as_pointer(), 2) }),
293+
))
294+
.as_pointer(),
295+
)
296+
}) == 7)
297+
);
298+
assert!(
299+
(({ Outer_long__Inner_int_Impl::sum(&ic.as_pointer(),) }) == (6 + (('a' as u8) as i32)))
300+
);
156301
assert!((({ Boxed_int_::twice(3,) }) == 6));
157302
let bi: Value<Boxed_int_> = Rc::new(RefCell::new(Boxed_int_ {
158303
value: Rc::new(RefCell::new(4)),
@@ -313,4 +458,50 @@ impl MyContainer_int_Impl for Ptr<MyContainer_int_> {
313458
};
314459
}
315460
}
461+
pub trait Outer_int_Impl {
462+
fn with(&self, n: i32) -> Outer_int__Inner_int_;
463+
}
464+
impl Outer_int_Impl for Ptr<Outer_int_> {
465+
fn with(&self, n: i32) -> Outer_int__Inner_int_ {
466+
let n: Value<i32> = Rc::new(RefCell::new(n));
467+
return Outer_int__Inner_int_ {
468+
t: Rc::new(RefCell::new((*(*(*self).upgrade().deref()).v.borrow()))),
469+
u: Rc::new(RefCell::new((*n.borrow()))),
470+
};
471+
}
472+
}
473+
pub trait Outer_int__Inner_int_Impl {
474+
fn sum(&self) -> i32;
475+
}
476+
impl Outer_int__Inner_int_Impl for Ptr<Outer_int__Inner_int_> {
477+
fn sum(&self) -> i32 {
478+
return (((*(*(*self).upgrade().deref()).t.borrow()) as i32)
479+
+ ((*(*(*self).upgrade().deref()).u.borrow()) as i32));
480+
}
481+
}
482+
pub trait Outer_long_Impl {
483+
fn with(&self, n: i32) -> Outer_long__Inner_int_;
484+
}
485+
impl Outer_long_Impl for Ptr<Outer_long_> {
486+
fn with(&self, n: i32) -> Outer_long__Inner_int_ {
487+
let n: Value<i32> = Rc::new(RefCell::new(n));
488+
return Outer_long__Inner_int_ {
489+
t: Rc::new(RefCell::new((*(*(*self).upgrade().deref()).v.borrow()))),
490+
u: Rc::new(RefCell::new((*n.borrow()))),
491+
};
492+
}
493+
}
494+
pub trait Outer_long__Inner_int_Impl {
495+
fn sum(&self) -> i32;
496+
}
497+
impl Outer_long__Inner_int_Impl for Ptr<Outer_long__Inner_int_> {
498+
fn sum(&self) -> i32 {
499+
return (((*(*(*self).upgrade().deref()).t.borrow()) as i32)
500+
+ ((*(*(*self).upgrade().deref()).u.borrow()) as i32));
501+
}
502+
fn sum(&self) -> i32 {
503+
return (((*(*(*self).upgrade().deref()).t.borrow()) as i32)
504+
+ ((*(*(*self).upgrade().deref()).u.borrow()) as i32));
505+
}
506+
}
316507
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,76 @@ impl Boxed_long_ {
110110
return ((self.value) + (other));
111111
}
112112
}
113+
#[repr(C)]
114+
#[derive(Copy, Clone, Default)]
115+
pub struct Outer_int__Inner_int_ {
116+
pub t: i32,
117+
pub u: i32,
118+
}
119+
impl Outer_int__Inner_int_ {
120+
pub unsafe fn sum(&self) -> i32 {
121+
return ((self.t as i32) + (self.u as i32));
122+
}
123+
}
124+
#[repr(C)]
125+
#[derive(Copy, Clone, Default)]
126+
pub struct Outer_int_ {
127+
pub v: i32,
128+
}
129+
impl Outer_int_ {
130+
pub unsafe fn with(&self, mut n: i32) -> Outer_int__Inner_int_ {
131+
return Outer_int__Inner_int_ { t: self.v, u: n };
132+
}
133+
}
134+
#[repr(C)]
135+
#[derive(Copy, Clone, Default)]
136+
pub struct Outer_long__Inner_int_ {
137+
pub t: i64,
138+
pub u: i32,
139+
}
140+
impl Outer_long__Inner_int_ {
141+
pub unsafe fn sum(&self) -> i32 {
142+
return ((self.t as i32) + (self.u as i32));
143+
}
144+
}
145+
impl Outer_long__Inner_int_ {
146+
pub unsafe fn sum(&self) -> i32 {
147+
return ((self.t as i32) + (self.u as i32));
148+
}
149+
}
150+
#[repr(C)]
151+
#[derive(Copy, Clone, Default)]
152+
pub struct Outer_long_ {
153+
pub v: i64,
154+
}
155+
impl Outer_long_ {
156+
pub unsafe fn with(&self, mut n: i32) -> Outer_long__Inner_int_ {
157+
return Outer_long__Inner_int_ { t: self.v, u: n };
158+
}
159+
}
113160
pub fn main() {
114161
unsafe {
115162
__cpp2rust_init_globals();
116163
std::process::exit(main_0() as i32);
117164
}
118165
}
119166
unsafe fn main_0() -> i32 {
167+
let mut oi: Outer_int_ = Outer_int_ { v: 3 };
168+
assert!(
169+
((unsafe { Outer_int__Inner_int_::sum(&(unsafe { Outer_int_::with(&oi, 4,) }),) }) == (7))
170+
);
171+
let mut ol: Outer_long_ = Outer_long_ { v: 5_i64 };
172+
let mut ic: Outer_long__Inner_char_ = Outer_long__Inner_char_ {
173+
t: 6_i64,
174+
u: ('a' as libc::c_char),
175+
};
176+
assert!(
177+
((unsafe { Outer_long__Inner_int_::sum(&(unsafe { Outer_long_::with(&ol, 2,) }),) })
178+
== (7))
179+
);
180+
assert!(
181+
((unsafe { Outer_long__Inner_int_::sum(&ic,) }) == ((6) + (('a' as libc::c_char) as i32)))
182+
);
120183
assert!(((unsafe { Boxed_int_::twice(3,) }) == (6)));
121184
let mut bi: Boxed_int_ = Boxed_int_ { value: 4 };
122185
assert!(((unsafe { Boxed_int_::plus(&bi, 5,) }) == (9)));

0 commit comments

Comments
 (0)