Skip to content

Commit f01c29a

Browse files
committed
Update tests
1 parent 5b15fbb commit f01c29a

6 files changed

Lines changed: 251 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <cassert>
2+
3+
template <int N> struct S {
4+
int v;
5+
int get() const { return v * N; }
6+
};
7+
8+
template <> struct S<2> {
9+
int v;
10+
int w;
11+
int get() const { return v - w; }
12+
};
13+
14+
template <int N> int scale(int x) { return x * N; }
15+
16+
template <> int scale<2>(int x) { return x - 2; }
17+
18+
int main() {
19+
S<1> s1{5};
20+
S<2> s2{5, 1};
21+
S<3> s3{5};
22+
assert(s1.get() == 5);
23+
assert(s2.get() == 4);
24+
assert(s3.get() == 15);
25+
26+
assert(scale<1>(7) == 7);
27+
assert(scale<2>(7) == 5);
28+
assert(scale<3>(7) == 21);
29+
return 0;
30+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
10+
#[byte_size(8)]
11+
pub struct S_2_ {
12+
#[offset(0)]
13+
pub v: i32,
14+
#[offset(4)]
15+
pub w: i32,
16+
}
17+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
18+
#[byte_size(4)]
19+
pub struct S_1_ {
20+
#[offset(0)]
21+
pub v: i32,
22+
}
23+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
24+
#[byte_size(4)]
25+
pub struct S_3_ {
26+
#[offset(0)]
27+
pub v: i32,
28+
}
29+
pub fn scale_0(mut x: i32) -> i32 {
30+
return (x * 1);
31+
}
32+
pub fn scale_1(mut x: i32) -> i32 {
33+
return (x * 3);
34+
}
35+
pub fn scale_2(mut x: i32) -> i32 {
36+
return (x - 2);
37+
}
38+
pub fn main() {
39+
__cpp2rust_init_globals();
40+
std::process::exit(main_0());
41+
}
42+
fn main_0() -> i32 {
43+
let s1: Value<S_1_> = Rc::new(RefCell::new(S_1_ { v: 5 }));
44+
let s2: Value<S_2_> = Rc::new(RefCell::new(S_2_ { v: 5, w: 1 }));
45+
let s3: Value<S_3_> = Rc::new(RefCell::new(S_3_ { v: 5 }));
46+
assert!((({ S_1_Impl::get(&s1.as_pointer(),) }) == 5));
47+
assert!((({ S_2_Impl::get(&s2.as_pointer(),) }) == 4));
48+
assert!((({ S_3_Impl::get(&s3.as_pointer(),) }) == 15));
49+
assert!((({ scale_0(7,) }) == 7));
50+
assert!((({ scale_2(7,) }) == 5));
51+
assert!((({ scale_1(7,) }) == 21));
52+
return 0;
53+
}
54+
pub trait S_1_Impl {
55+
fn get(&self) -> i32;
56+
}
57+
impl S_1_Impl for Ptr<S_1_> {
58+
fn get(&self) -> i32 {
59+
return ((*self).with(|__s| __s.v) * 1);
60+
}
61+
}
62+
pub trait S_2_Impl {
63+
fn get(&self) -> i32;
64+
}
65+
impl S_2_Impl for Ptr<S_2_> {
66+
fn get(&self) -> i32 {
67+
return ((*self).with(|__s| __s.v) - (*self).with(|__s| __s.w));
68+
}
69+
}
70+
pub trait S_3_Impl {
71+
fn get(&self) -> i32;
72+
}
73+
impl S_3_Impl for Ptr<S_3_> {
74+
fn get(&self) -> i32 {
75+
return ((*self).with(|__s| __s.v) * 3);
76+
}
77+
}
78+
pub fn __cpp2rust_init_globals() {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
pub fn second_of_0(p: Ptr<(Value<f64>, Value<i32>)>) -> i32 {
10+
return ((*p.upgrade().deref()).1.read());
11+
}
12+
pub fn first_of_1(p: Ptr<(Value<f64>, Value<i32>)>) -> f64 {
13+
return ((*p.upgrade().deref()).0.read());
14+
}
15+
pub fn main() {
16+
__cpp2rust_init_globals();
17+
std::process::exit(main_0());
18+
}
19+
fn main_0() -> i32 {
20+
let p: Value<(Value<f64>, Value<i32>)> = Rc::new(RefCell::new((
21+
Rc::new(RefCell::new(1.5_f64.try_into().expect("failed conversion"))),
22+
Rc::new(RefCell::new(2.try_into().expect("failed conversion"))),
23+
)));
24+
assert!((((*p.borrow()).0.read()) == 1.5_f64));
25+
assert!((((*p.borrow()).1.read()) == 2));
26+
assert!((({ first_of_1(p.as_pointer(),) }) == 1.5_f64));
27+
assert!((({ second_of_0(p.as_pointer(),) }) == 2));
28+
return 0;
29+
}
30+
pub fn __cpp2rust_init_globals() {}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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, VaArg, FnPtrArg, Default)]
11+
pub struct S_2_ {
12+
pub v: i32,
13+
pub w: i32,
14+
}
15+
impl S_2_ {
16+
pub unsafe fn get(&self) -> i32 {
17+
return ((self.v) - (self.w));
18+
}
19+
}
20+
#[repr(C)]
21+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
22+
pub struct S_1_ {
23+
pub v: i32,
24+
}
25+
impl S_1_ {
26+
pub unsafe fn get(&self) -> i32 {
27+
return ((self.v) * (1));
28+
}
29+
}
30+
#[repr(C)]
31+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
32+
pub struct S_3_ {
33+
pub v: i32,
34+
}
35+
impl S_3_ {
36+
pub unsafe fn get(&self) -> i32 {
37+
return ((self.v) * (3));
38+
}
39+
}
40+
pub unsafe fn scale_0(mut x: i32) -> i32 {
41+
return ((x) * (1));
42+
}
43+
pub unsafe fn scale_1(mut x: i32) -> i32 {
44+
return ((x) * (3));
45+
}
46+
pub unsafe fn scale_2(mut x: i32) -> i32 {
47+
return ((x) - (2));
48+
}
49+
pub fn main() {
50+
unsafe {
51+
__cpp2rust_init_globals();
52+
std::process::exit(main_0() as i32);
53+
}
54+
}
55+
unsafe fn main_0() -> i32 {
56+
let mut s1: S_1_ = S_1_ { v: 5 };
57+
let mut s2: S_2_ = S_2_ { v: 5, w: 1 };
58+
let mut s3: S_3_ = S_3_ { v: 5 };
59+
assert!(((unsafe { S_1_::get(&s1,) }) == (5)));
60+
assert!(((unsafe { S_2_::get(&s2,) }) == (4)));
61+
assert!(((unsafe { S_3_::get(&s3,) }) == (15)));
62+
assert!(((unsafe { scale_0(7,) }) == (7)));
63+
assert!(((unsafe { scale_2(7,) }) == (5)));
64+
assert!(((unsafe { scale_1(7,) }) == (21)));
65+
return 0;
66+
}
67+
pub unsafe fn __cpp2rust_init_globals() {}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 unsafe fn second_of_0(p: *const (f64, i32)) -> i32 {
10+
return (*(*p).1);
11+
}
12+
pub unsafe fn first_of_1(p: *const (f64, i32)) -> f64 {
13+
return (*(*p).0);
14+
}
15+
pub fn main() {
16+
unsafe {
17+
__cpp2rust_init_globals();
18+
std::process::exit(main_0() as i32);
19+
}
20+
}
21+
unsafe fn main_0() -> i32 {
22+
let mut p: (f64, i32) = (1.5_f64.into(), 2.into());
23+
assert!(((*p.0) == (1.5_f64)));
24+
assert!(((*p.1) == (2)));
25+
assert!(((unsafe { first_of_1(&p,) }) == (1.5_f64)));
26+
assert!(((unsafe { second_of_0(&p,) }) == (2)));
27+
return 0;
28+
}
29+
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/pair_get.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <cassert>
2+
#include <utility>
3+
4+
static int second_of(const std::pair<double, int> &p) { return std::get<1>(p); }
5+
6+
static double first_of(const std::pair<double, int> &p) {
7+
return std::get<0>(p);
8+
}
9+
10+
int main() {
11+
std::pair<double, int> p(1.5, 2);
12+
assert(std::get<0>(p) == 1.5);
13+
assert(std::get<1>(p) == 2);
14+
assert(first_of(p) == 1.5);
15+
assert(second_of(p) == 2);
16+
return 0;
17+
}

0 commit comments

Comments
 (0)