Skip to content

Commit 3c16769

Browse files
committed
Update tests
1 parent e3ce76a commit 3c16769

3 files changed

Lines changed: 187 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <cassert>
2+
3+
// This tests that cpp2rust respects the lazy instantiation of templates. On
4+
// Box<int> and Box<Point> declarations, only the class template and data
5+
// memebers are instantiated.
6+
//
7+
// Then, each used method is lazyly instantiated when it's used, i.e. twice()
8+
// is not instantiated for Box<Point>.
9+
10+
struct Point {
11+
int x;
12+
};
13+
14+
template <typename T> struct Box {
15+
T val;
16+
T get() { return val; }
17+
18+
// This only works for T's that have operator+.
19+
// int is fine, Point is not.
20+
T twice() { return val + val; }
21+
};
22+
23+
int main() {
24+
Box<int> i = {3};
25+
assert(i.twice() == 6);
26+
27+
Box<Point> p = {{4}};
28+
assert(p.get().x == 4);
29+
30+
return 0;
31+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 Point {
11+
pub x: Value<i32>,
12+
}
13+
impl Clone for Point {
14+
fn clone(&self) -> Self {
15+
let mut this = Self {
16+
x: Rc::new(RefCell::new((*self.x.borrow()))),
17+
};
18+
this
19+
}
20+
}
21+
impl ByteRepr for Point {
22+
fn byte_size() -> usize {
23+
4
24+
}
25+
fn to_bytes(&self, buf: &mut [u8]) {
26+
(*self.x.borrow()).to_bytes(&mut buf[0..4]);
27+
}
28+
fn from_bytes(buf: &[u8]) -> Self {
29+
Self {
30+
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
31+
}
32+
}
33+
}
34+
#[derive(Default)]
35+
pub struct Box_int_ {
36+
pub val: Value<i32>,
37+
}
38+
impl Box_int_ {
39+
pub fn twice(&self) -> i32 {
40+
return ((*self.val.borrow()) + (*self.val.borrow()));
41+
}
42+
}
43+
impl Clone for Box_int_ {
44+
fn clone(&self) -> Self {
45+
let mut this = Self {
46+
val: Rc::new(RefCell::new((*self.val.borrow()))),
47+
};
48+
this
49+
}
50+
}
51+
impl ByteRepr for Box_int_ {
52+
fn byte_size() -> usize {
53+
4
54+
}
55+
fn to_bytes(&self, buf: &mut [u8]) {
56+
(*self.val.borrow()).to_bytes(&mut buf[0..4]);
57+
}
58+
fn from_bytes(buf: &[u8]) -> Self {
59+
Self {
60+
val: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
61+
}
62+
}
63+
}
64+
#[derive(Default)]
65+
pub struct Box_Point_ {
66+
pub val: Value<Point>,
67+
}
68+
impl Box_Point_ {
69+
pub fn get(&self) -> Point {
70+
return (*self.val.borrow()).clone();
71+
}
72+
}
73+
impl Clone for Box_Point_ {
74+
fn clone(&self) -> Self {
75+
let mut this = Self {
76+
val: Rc::new(RefCell::new((*self.val.borrow()).clone())),
77+
};
78+
this
79+
}
80+
}
81+
impl ByteRepr for Box_Point_ {
82+
fn byte_size() -> usize {
83+
4
84+
}
85+
fn to_bytes(&self, buf: &mut [u8]) {
86+
(*self.val.borrow()).to_bytes(&mut buf[0..4]);
87+
}
88+
fn from_bytes(buf: &[u8]) -> Self {
89+
Self {
90+
val: Rc::new(RefCell::new(<Point>::from_bytes(&buf[0..4]))),
91+
}
92+
}
93+
}
94+
pub fn main() {
95+
std::process::exit(main_0());
96+
}
97+
fn main_0() -> i32 {
98+
let i: Value<Box_int_> = Rc::new(RefCell::new(Box_int_ {
99+
val: Rc::new(RefCell::new(3)),
100+
}));
101+
assert!((({ (*i.borrow()).twice() }) == 6));
102+
let p: Value<Box_Point_> = Rc::new(RefCell::new(Box_Point_ {
103+
val: Rc::new(RefCell::new(Point {
104+
x: Rc::new(RefCell::new(4)),
105+
})),
106+
}));
107+
assert!(((*({ (*p.borrow()).get() }).x.borrow()) == 4));
108+
return 0;
109+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 Point {
12+
pub x: i32,
13+
}
14+
#[repr(C)]
15+
#[derive(Copy, Clone, Default)]
16+
pub struct Box_int_ {
17+
pub val: i32,
18+
}
19+
impl Box_int_ {
20+
pub unsafe fn twice(&mut self) -> i32 {
21+
return ((self.val) + (self.val));
22+
}
23+
}
24+
#[repr(C)]
25+
#[derive(Copy, Clone, Default)]
26+
pub struct Box_Point_ {
27+
pub val: Point,
28+
}
29+
impl Box_Point_ {
30+
pub unsafe fn get(&mut self) -> Point {
31+
return self.val.clone();
32+
}
33+
}
34+
pub fn main() {
35+
unsafe {
36+
std::process::exit(main_0() as i32);
37+
}
38+
}
39+
unsafe fn main_0() -> i32 {
40+
let mut i: Box_int_ = Box_int_ { val: 3 };
41+
assert!(((unsafe { i.twice() }) == (6)));
42+
let mut p: Box_Point_ = Box_Point_ {
43+
val: Point { x: 4 },
44+
};
45+
assert!((((unsafe { p.get() }).x) == (4)));
46+
return 0;
47+
}

0 commit comments

Comments
 (0)