Skip to content

Commit 27117f7

Browse files
committed
Update tests
1 parent 5952f49 commit 27117f7

3 files changed

Lines changed: 145 additions & 4 deletions

File tree

‎tests/unit/as_const.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ struct S {
1010
int v;
1111
Overload f() { return kMutableOverload; }
1212
Overload f() const { return kConstOverload; }
13-
int &ref() { return v; }
14-
const int &ref() const { return v; }
13+
int &value_ref() { return v; }
14+
const int &value_ref() const { return v; }
1515
};
1616

1717
static Overload g(S &) { return kMutableOverload; }
@@ -26,9 +26,9 @@ int main() {
2626
assert(g(s) == kMutableOverload);
2727
assert(g(std::as_const(s)) == kConstOverload);
2828

29-
s.ref() = 9;
29+
s.value_ref() = 9;
3030
assert(s.v == 9);
31-
assert(std::as_const(s).ref() == 9);
31+
assert(std::as_const(s).value_ref() == 9);
3232

3333
const S &cs = std::as_const(s);
3434
assert(cs.f() == kConstOverload);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 type Overload = u32;
10+
pub const Overload_kMutableOverload: Overload = 1;
11+
pub const Overload_kConstOverload: Overload = 2;
12+
#[derive(Default)]
13+
pub struct S {
14+
pub v: Value<i32>,
15+
}
16+
impl Clone for S {
17+
fn clone(&self) -> Self {
18+
let __this: Value<S> = Rc::new(RefCell::new(Self {
19+
v: Rc::new(RefCell::new((*self.v.borrow()))),
20+
}));
21+
let this: Ptr<S> = __this.as_pointer();
22+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
23+
}
24+
}
25+
impl ByteRepr for S {
26+
fn byte_size() -> usize {
27+
4
28+
}
29+
fn to_bytes(&self, buf: &mut [u8]) {
30+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
31+
}
32+
fn from_bytes(buf: &[u8]) -> Self {
33+
Self {
34+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
35+
}
36+
}
37+
}
38+
pub fn g_0(_a0: Ptr<S>) -> Overload {
39+
return Overload_kMutableOverload;
40+
}
41+
pub fn g_1(_a0: Ptr<S>) -> Overload {
42+
return Overload_kConstOverload;
43+
}
44+
pub fn main() {
45+
__cpp2rust_init_globals();
46+
std::process::exit(main_0());
47+
}
48+
fn main_0() -> i32 {
49+
let s: Value<S> = Rc::new(RefCell::new(S {
50+
v: Rc::new(RefCell::new(7)),
51+
}));
52+
assert!(((({ SImpl::f(&s.as_pointer(),) }) as i32) == (Overload_kMutableOverload as i32)));
53+
assert!(((({ SImpl::f_const(&s.as_pointer(),) }) as i32) == (Overload_kConstOverload as i32)));
54+
assert!(((({ g_0(s.as_pointer(),) }) as i32) == (Overload_kMutableOverload as i32)));
55+
assert!(((({ g_1(s.as_pointer(),) }) as i32) == (Overload_kConstOverload as i32)));
56+
({ SImpl::value_ref(&s.as_pointer()) }).write(9);
57+
assert!(((*(*s.borrow()).v.borrow()) == 9));
58+
assert!(((({ SImpl::value_ref_const(&s.as_pointer(),) }).read()) == 9));
59+
let cs: Ptr<S> = s.as_pointer();
60+
assert!(((({ SImpl::f_const(&cs,) }) as i32) == (Overload_kConstOverload as i32)));
61+
assert!(((*(*cs.upgrade().deref()).v.borrow()) == 9));
62+
return 0;
63+
}
64+
pub trait SImpl {
65+
fn f(&self) -> Overload;
66+
fn f_const(&self) -> Overload;
67+
fn value_ref(&self) -> Ptr<i32>;
68+
fn value_ref_const(&self) -> Ptr<i32>;
69+
}
70+
impl SImpl for Ptr<S> {
71+
fn f(&self) -> Overload {
72+
return Overload_kMutableOverload;
73+
}
74+
fn f_const(&self) -> Overload {
75+
return Overload_kConstOverload;
76+
}
77+
fn value_ref(&self) -> Ptr<i32> {
78+
return (*(*self).upgrade().deref()).v.as_pointer();
79+
}
80+
fn value_ref_const(&self) -> Ptr<i32> {
81+
return (*(*self).upgrade().deref()).v.as_pointer();
82+
}
83+
}
84+
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 type Overload = u32;
10+
pub const Overload_kMutableOverload: Overload = 1;
11+
pub const Overload_kConstOverload: Overload = 2;
12+
#[repr(C)]
13+
#[derive(Copy, Clone, Default)]
14+
pub struct S {
15+
pub v: i32,
16+
}
17+
impl S {
18+
pub unsafe fn f(&mut self) -> Overload {
19+
return Overload_kMutableOverload;
20+
}
21+
pub unsafe fn f_const(&self) -> Overload {
22+
return Overload_kConstOverload;
23+
}
24+
pub unsafe fn value_ref(&mut self) -> *mut i32 {
25+
return &mut self.v;
26+
}
27+
pub unsafe fn value_ref_const(&self) -> *const i32 {
28+
return &self.v;
29+
}
30+
}
31+
pub unsafe fn g_0(_a0: *mut S) -> Overload {
32+
return Overload_kMutableOverload;
33+
}
34+
pub unsafe fn g_1(_a0: *const S) -> Overload {
35+
return Overload_kConstOverload;
36+
}
37+
pub fn main() {
38+
unsafe {
39+
__cpp2rust_init_globals();
40+
std::process::exit(main_0() as i32);
41+
}
42+
}
43+
unsafe fn main_0() -> i32 {
44+
let mut s: S = S { v: 7 };
45+
assert!((((unsafe { S::f(&mut s,) }) as i32) == (Overload_kMutableOverload as i32)));
46+
assert!((((unsafe { S::f_const(&s,) }) as i32) == (Overload_kConstOverload as i32)));
47+
assert!((((unsafe { g_0(&mut s,) }) as i32) == (Overload_kMutableOverload as i32)));
48+
assert!((((unsafe { g_1(&s,) }) as i32) == (Overload_kConstOverload as i32)));
49+
(*(unsafe { S::value_ref(&mut s) })) = 9;
50+
assert!(((s.v) == (9)));
51+
assert!(((*(unsafe { S::value_ref_const(&s,) })) == (9)));
52+
let cs: *const S = &s;
53+
assert!((((unsafe { S::f_const(&(*cs),) }) as i32) == (Overload_kConstOverload as i32)));
54+
assert!((((*cs).v) == (9)));
55+
return 0;
56+
}
57+
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)