Skip to content

Commit 3350370

Browse files
committed
Update tests
1 parent 6b10a29 commit 3350370

10 files changed

Lines changed: 138 additions & 22 deletions

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

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn foo_0(mut a: i32, b: Option<i32>) -> i32 {
10-
let mut b: i32 = b.unwrap_or(10);
10+
let mut b: i32 = b.unwrap_or_else(|| 10);
1111
return (a + b);
1212
}
1313
pub fn baz_1(mut a: Ptr<i32>, b: Option<Ptr<i32>>) -> bool {
14-
let mut b: Ptr<i32> = b.unwrap_or(Ptr::<i32>::null());
14+
let mut b: Ptr<i32> = b.unwrap_or_else(|| Ptr::<i32>::null());
1515
return ({ a } == { b });
1616
}
1717
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)]
@@ -22,7 +22,7 @@ pub struct Bar {
2222
}
2323
impl Bar {
2424
pub fn new(v: Option<i32>) -> Self {
25-
let mut v: i32 = v.unwrap_or(1);
25+
let mut v: i32 = v.unwrap_or_else(|| 1);
2626
Self { v: v }
2727
}
2828
}
@@ -31,6 +31,43 @@ impl Default for Bar {
3131
{ Bar::new(None) }
3232
}
3333
}
34+
thread_local!(
35+
pub static counter_2: Value<i32> = Rc::new(RefCell::new(0));
36+
);
37+
pub fn next_3() -> i32 {
38+
return (*counter_2.with(Value::clone).borrow_mut()).prefix_inc();
39+
}
40+
pub fn lazy_4(x: Option<i32>) -> i32 {
41+
let mut x: i32 = x.unwrap_or_else(|| ({ next_3() }));
42+
return x;
43+
}
44+
pub fn by_ref_5(b: Option<Ptr<Bar>>) -> i32 {
45+
let mut __b_default: Option<Value<Bar>> = None;
46+
let b: Ptr<Bar> = b.unwrap_or_else(|| {
47+
__b_default
48+
.insert(Rc::new(RefCell::new(Bar::new({ Some(7) }))))
49+
.as_pointer()
50+
});
51+
return b.with(|__s| __s.v);
52+
}
53+
thread_local!(
54+
pub static global_bar_6: Value<Bar> = Rc::new(RefCell::new(Bar::new({ Some(9) })));
55+
);
56+
pub fn by_global_ref_7(b: Option<Ptr<Bar>>) -> i32 {
57+
let b: Ptr<Bar> = b.unwrap_or_else(|| global_bar_6.with(|v| v.as_pointer()));
58+
return b.with(|__s| __s.v);
59+
}
60+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
61+
#[byte_size(4)]
62+
pub struct Holder_int_ {
63+
#[offset(0)]
64+
pub v: i32,
65+
}
66+
impl Holder_int_ {
67+
pub fn new(mut v: i32) -> Self {
68+
Self { v: v }
69+
}
70+
}
3471
pub fn main() {
3572
__cpp2rust_init_globals();
3673
std::process::exit(main_0());
@@ -54,6 +91,29 @@ fn main_0() -> i32 {
5491
let mut arr: [Bar; 3] = [Bar::new(None), Bar::new(None), Bar::new(None)];
5592
assert!(({ arr[(0) as usize].v } == 1));
5693
assert!(({ arr[(2) as usize].v } == 1));
94+
assert!((({ lazy_4(Some(5),) }) == 5));
95+
assert!((counter_2.with(|rc| *rc.borrow()) == 0));
96+
assert!((({ lazy_4(None,) }) == 1));
97+
assert!((counter_2.with(|rc| *rc.borrow()) == 1));
98+
assert!((({ by_ref_5(None,) }) == 7));
99+
assert!(
100+
(({
101+
let _b: Value<Bar> = Rc::new(RefCell::new(Bar::new({ Some(3) })));
102+
by_ref_5(Some(_b.as_pointer()))
103+
}) == 3)
104+
);
105+
assert!((({ by_global_ref_7(None,) }) == 9));
106+
assert!(
107+
(({
108+
let _b: Value<Bar> = Rc::new(RefCell::new(Bar::new({ Some(4) })));
109+
by_global_ref_7(Some(_b.as_pointer()))
110+
}) == 4)
111+
);
112+
let mut h: Holder_int_ = Holder_int_::new({ 4 });
113+
assert!((h.v == 4));
57114
return 0;
58115
}
59-
pub fn __cpp2rust_init_globals() {}
116+
pub fn __cpp2rust_init_globals() {
117+
let _ = counter_2.with(|_| ());
118+
let _ = global_bar_6.with(|_| ());
119+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn identity_0(mut x: i32) -> i32 {
1010
return x;
1111
}
1212
pub fn apply_1(mut x: i32, fn_: Option<FnPtr<fn(i32) -> i32>>) -> i32 {
13-
let mut fn_: FnPtr<fn(i32) -> i32> = fn_.unwrap_or(FnPtr::<fn(i32) -> i32>::null());
13+
let mut fn_: FnPtr<fn(i32) -> i32> = fn_.unwrap_or_else(|| FnPtr::<fn(i32) -> i32>::null());
1414
if !(fn_).is_null() {
1515
return ({ fn_.call(x) });
1616
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn out_param_0(out: Option<Ptr<usize>>) {
10-
let mut out: Ptr<usize> = out.unwrap_or(Ptr::<usize>::null());
10+
let mut out: Ptr<usize> = out.unwrap_or_else(|| Ptr::<usize>::null());
1111
if !(out).is_null() {
1212
out.write(4_usize);
1313
}
1414
}
1515
pub fn parse_1(mut v: i32, idx: Option<Ptr<usize>>) -> i32 {
16-
let mut idx: Ptr<usize> = idx.unwrap_or(Ptr::<usize>::null());
16+
let mut idx: Ptr<usize> = idx.unwrap_or_else(|| Ptr::<usize>::null());
1717
if !(idx).is_null() {
1818
idx.write(3_usize);
1919
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ impl NoDefault {
5353
}
5454
}
5555
pub fn used_0(x: Option<A>) -> i32 {
56-
let mut x: A = x.unwrap_or(A::new_1());
56+
let mut x: A = x.unwrap_or_else(|| A::new_1());
5757
return x.v;
5858
}
5959
pub fn used_1(x: Option<B>) -> i32 {
60-
let mut x: B = x.unwrap_or(B::new());
60+
let mut x: B = x.unwrap_or_else(|| B::new());
6161
return x.v;
6262
}
6363
pub fn scaled_2(mut x: A, n: Option<i32>) -> i32 {
64-
let mut n: i32 = n.unwrap_or((4usize as i32));
64+
let mut n: i32 = n.unwrap_or_else(|| (4usize as i32));
6565
return (x.v * n);
6666
}
6767
pub fn always_given_3(mut x: NoDefault) -> i32 {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn sum_3(mut arr: Ptr<i32>, mut n: i32) -> i32 {
3838
return s;
3939
}
4040
pub fn countdown_4(mut n: i32, step: Option<i32>) -> i32 {
41-
let mut step: i32 = step.unwrap_or(1);
41+
let mut step: i32 = step.unwrap_or_else(|| 1);
4242
let mut steps: i32 = 0;
4343
'loop_: while (n > 0) {
4444
n -= step;

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

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
pub unsafe fn foo_0(mut a: i32, mut b: Option<i32>) -> i32 {
10-
let mut b: i32 = b.unwrap_or(10);
10+
let mut b: i32 = b.unwrap_or_else(|| unsafe { 10 });
1111
return ((a) + (b));
1212
}
1313
pub unsafe fn baz_1(mut a: *mut i32, mut b: Option<*mut i32>) -> bool {
14-
let mut b: *mut i32 = b.unwrap_or(std::ptr::null_mut());
14+
let mut b: *mut i32 = b.unwrap_or_else(|| unsafe { std::ptr::null_mut() });
1515
return ((a) == (b));
1616
}
1717
#[repr(C)]
@@ -21,7 +21,7 @@ pub struct Bar {
2121
}
2222
impl Bar {
2323
pub unsafe fn new(mut v: Option<i32>) -> Self {
24-
let mut v: i32 = v.unwrap_or(1);
24+
let mut v: i32 = v.unwrap_or_else(|| unsafe { 1 });
2525
let mut this = Self { v: v };
2626
this
2727
}
@@ -31,6 +31,39 @@ impl Default for Bar {
3131
unsafe { Bar::new(None) }
3232
}
3333
}
34+
pub static mut counter_2: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { 0 });
35+
pub unsafe fn next_3() -> i32 {
36+
return (*std::cell::LazyCell::force_mut(&mut *&raw mut counter_2)).prefix_inc();
37+
}
38+
pub unsafe fn lazy_4(mut x: Option<i32>) -> i32 {
39+
let mut x: i32 = x.unwrap_or_else(|| unsafe { (unsafe { next_3() }) });
40+
return x;
41+
}
42+
pub unsafe fn by_ref_5(b: Option<*const Bar>) -> i32 {
43+
let mut __b_default: Option<Bar> = None;
44+
let mut b: *const Bar =
45+
b.unwrap_or_else(|| unsafe { __b_default.insert(Bar::new({ Some(7) })) as *const Bar });
46+
return (*b).v;
47+
}
48+
pub static mut global_bar_6: std::cell::LazyCell<Bar> =
49+
std::cell::LazyCell::new(|| unsafe { Bar::new({ Some(9) }) });
50+
pub unsafe fn by_global_ref_7(b: Option<*const Bar>) -> i32 {
51+
let mut b: *const Bar = b.unwrap_or_else(|| unsafe {
52+
&(*std::cell::LazyCell::force_mut(&mut *&raw mut global_bar_6))
53+
});
54+
return (*b).v;
55+
}
56+
#[repr(C)]
57+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
58+
pub struct Holder_int_ {
59+
pub v: i32,
60+
}
61+
impl Holder_int_ {
62+
pub unsafe fn new(mut v: i32) -> Self {
63+
let mut this = Self { v: v };
64+
this
65+
}
66+
}
3467
pub fn main() {
3568
unsafe {
3669
__cpp2rust_init_globals();
@@ -56,6 +89,29 @@ unsafe fn main_0() -> i32 {
5689
let mut arr: [Bar; 3] = [Bar::new(None), Bar::new(None), Bar::new(None)];
5790
assert!(((arr[(0) as usize].v) == (1)));
5891
assert!(((arr[(2) as usize].v) == (1)));
92+
assert!(((unsafe { lazy_4(Some(5),) }) == (5)));
93+
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut counter_2)) == (0)));
94+
assert!(((unsafe { lazy_4(None,) }) == (1)));
95+
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut counter_2)) == (1)));
96+
assert!(((unsafe { by_ref_5(None,) }) == (7)));
97+
assert!(
98+
((unsafe {
99+
let mut _b: Bar = Bar::new({ Some(3) });
100+
by_ref_5(Some(&mut _b))
101+
}) == (3))
102+
);
103+
assert!(((unsafe { by_global_ref_7(None,) }) == (9)));
104+
assert!(
105+
((unsafe {
106+
let mut _b: Bar = Bar::new({ Some(4) });
107+
by_global_ref_7(Some(&mut _b))
108+
}) == (4))
109+
);
110+
let mut h: Holder_int_ = Holder_int_::new({ 4 });
111+
assert!(((h.v) == (4)));
59112
return 0;
60113
}
61-
pub unsafe fn __cpp2rust_init_globals() {}
114+
pub unsafe fn __cpp2rust_init_globals() {
115+
std::cell::LazyCell::force(&*&raw const counter_2);
116+
std::cell::LazyCell::force(&*&raw const global_bar_6);
117+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub unsafe fn identity_0(mut x: i32) -> i32 {
1010
return x;
1111
}
1212
pub unsafe fn apply_1(mut x: i32, mut fn_: Option<Option<unsafe fn(i32) -> i32>>) -> i32 {
13-
let mut fn_: Option<unsafe fn(i32) -> i32> = fn_.unwrap_or(None);
13+
let mut fn_: Option<unsafe fn(i32) -> i32> = fn_.unwrap_or_else(|| unsafe { None });
1414
if !(fn_).is_none() {
1515
return (unsafe { (fn_).unwrap()(x) });
1616
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
pub unsafe fn out_param_0(mut out: Option<*mut usize>) {
10-
let mut out: *mut usize = out.unwrap_or(std::ptr::null_mut());
10+
let mut out: *mut usize = out.unwrap_or_else(|| unsafe { std::ptr::null_mut() });
1111
if !(out).is_null() {
1212
(*out) = 4_usize;
1313
}
1414
}
1515
pub unsafe fn parse_1(mut v: i32, mut idx: Option<*mut usize>) -> i32 {
16-
let mut idx: *mut usize = idx.unwrap_or(std::ptr::null_mut());
16+
let mut idx: *mut usize = idx.unwrap_or_else(|| unsafe { std::ptr::null_mut() });
1717
if !(idx).is_null() {
1818
(*idx) = 3_usize;
1919
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ impl NoDefault {
5454
}
5555
}
5656
pub unsafe fn used_0(mut x: Option<A>) -> i32 {
57-
let mut x: A = x.unwrap_or(A::new_1());
57+
let mut x: A = x.unwrap_or_else(|| unsafe { A::new_1() });
5858
return x.v;
5959
}
6060
pub unsafe fn used_1(mut x: Option<B>) -> i32 {
61-
let mut x: B = x.unwrap_or(B::new());
61+
let mut x: B = x.unwrap_or_else(|| unsafe { B::new() });
6262
return x.v;
6363
}
6464
pub unsafe fn scaled_2(mut x: A, mut n: Option<i32>) -> i32 {
65-
let mut n: i32 = n.unwrap_or((::std::mem::size_of::<A>() as i32));
65+
let mut n: i32 = n.unwrap_or_else(|| unsafe { (::std::mem::size_of::<A>() as i32) });
6666
return ((x.v) * (n));
6767
}
6868
pub unsafe fn always_given_3(mut x: NoDefault) -> i32 {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub unsafe fn sum_3(mut arr: *const i32, mut n: i32) -> i32 {
3636
return s;
3737
}
3838
pub unsafe fn countdown_4(mut n: i32, mut step: Option<i32>) -> i32 {
39-
let mut step: i32 = step.unwrap_or(1);
39+
let mut step: i32 = step.unwrap_or_else(|| unsafe { 1 });
4040
let mut steps: i32 = 0;
4141
'loop_: while ((n) > (0)) {
4242
n -= step;

0 commit comments

Comments
 (0)