Skip to content

Commit 45555a7

Browse files
committed
Update tests
1 parent 5e214e7 commit 45555a7

3 files changed

Lines changed: 210 additions & 0 deletions

File tree

‎tests/unit/operator_comparison_free.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,23 @@ bool operator>=(const S &a, const S &b) { return a.v >= b.v; }
1313
bool operator<(const S &a, int b) { return a.v < b; }
1414
bool operator<(int a, const S &b) { return a < b.v; }
1515

16+
struct V {
17+
int v;
18+
};
19+
20+
bool operator==(V a, V b) { return a.v == b.v; }
21+
bool operator!=(V a, V b) { return a.v != b.v; }
22+
bool operator<(V a, V b) { return a.v < b.v; }
23+
bool operator>(V a, V b) { return a.v > b.v; }
24+
1625
int main() {
26+
V x{1}, y{2}, z{1};
27+
assert(x == z);
28+
assert(x != y);
29+
assert(x < y);
30+
assert(y > x);
31+
assert(!(y < x));
32+
1733
S a{1}, b{2}, c{1};
1834
assert(a == c);
1935
assert(a != b);

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

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,131 @@ pub fn operator_lt_7(a: i32, b: Ptr<S>) -> bool {
117117
_lhs < (*(*b.upgrade().deref()).v.borrow())
118118
};
119119
}
120+
#[derive(Default)]
121+
pub struct V {
122+
pub v: Value<i32>,
123+
}
124+
impl std::cmp::Ord for V {
125+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
126+
{
127+
if operator_lt_8(
128+
Rc::new(RefCell::new(V { v: self.v.clone() })).as_pointer(),
129+
Rc::new(RefCell::new(V { v: other.v.clone() })).as_pointer(),
130+
) {
131+
std::cmp::Ordering::Less
132+
} else if operator_lt_8(
133+
Rc::new(RefCell::new(V { v: other.v.clone() })).as_pointer(),
134+
Rc::new(RefCell::new(V { v: self.v.clone() })).as_pointer(),
135+
) {
136+
std::cmp::Ordering::Greater
137+
} else {
138+
std::cmp::Ordering::Equal
139+
}
140+
}
141+
}
142+
}
143+
impl std::cmp::PartialOrd for V {
144+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
145+
Some(self.cmp(other))
146+
}
147+
}
148+
impl std::cmp::PartialEq for V {
149+
fn eq(&self, other: &Self) -> bool {
150+
{
151+
operator_eq_9(
152+
Rc::new(RefCell::new(V { v: self.v.clone() })).as_pointer(),
153+
Rc::new(RefCell::new(V { v: other.v.clone() })).as_pointer(),
154+
)
155+
}
156+
}
157+
}
158+
impl std::cmp::Eq for V {}
159+
impl Clone for V {
160+
fn clone(&self) -> Self {
161+
let __this: Value<V> = Rc::new(RefCell::new(Self {
162+
v: Rc::new(RefCell::new((*self.v.borrow()))),
163+
}));
164+
let this: Ptr<V> = __this.as_pointer();
165+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
166+
}
167+
}
168+
impl ByteRepr for V {
169+
fn byte_size() -> usize {
170+
4
171+
}
172+
fn to_bytes(&self, buf: &mut [u8]) {
173+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
174+
}
175+
fn from_bytes(buf: &[u8]) -> Self {
176+
Self {
177+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
178+
}
179+
}
180+
}
181+
pub fn operator_eq_9(a: V, b: V) -> bool {
182+
let a: Value<V> = Rc::new(RefCell::new(a));
183+
let b: Value<V> = Rc::new(RefCell::new(b));
184+
return ((*(*a.borrow()).v.borrow()) == (*(*b.borrow()).v.borrow()));
185+
}
186+
pub fn operator_ne_10(a: V, b: V) -> bool {
187+
let a: Value<V> = Rc::new(RefCell::new(a));
188+
let b: Value<V> = Rc::new(RefCell::new(b));
189+
return ((*(*a.borrow()).v.borrow()) != (*(*b.borrow()).v.borrow()));
190+
}
191+
pub fn operator_lt_8(a: V, b: V) -> bool {
192+
let a: Value<V> = Rc::new(RefCell::new(a));
193+
let b: Value<V> = Rc::new(RefCell::new(b));
194+
return ((*(*a.borrow()).v.borrow()) < (*(*b.borrow()).v.borrow()));
195+
}
196+
pub fn operator_gt_11(a: V, b: V) -> bool {
197+
let a: Value<V> = Rc::new(RefCell::new(a));
198+
let b: Value<V> = Rc::new(RefCell::new(b));
199+
return ((*(*a.borrow()).v.borrow()) > (*(*b.borrow()).v.borrow()));
200+
}
120201
pub fn main() {
121202
__cpp2rust_init_globals();
122203
std::process::exit(main_0());
123204
}
124205
fn main_0() -> i32 {
206+
let x: Value<V> = Rc::new(RefCell::new(V {
207+
v: Rc::new(RefCell::new(1)),
208+
}));
209+
let y: Value<V> = Rc::new(RefCell::new(V {
210+
v: Rc::new(RefCell::new(2)),
211+
}));
212+
let z: Value<V> = Rc::new(RefCell::new(V {
213+
v: Rc::new(RefCell::new(1)),
214+
}));
215+
assert!(
216+
({
217+
let _a: V = (*x.borrow()).clone();
218+
operator_eq_9(_a, (*z.borrow()).clone())
219+
})
220+
);
221+
assert!(
222+
({
223+
let _a: V = (*x.borrow()).clone();
224+
operator_ne_10(_a, (*y.borrow()).clone())
225+
})
226+
);
227+
assert!(
228+
({
229+
let _a: V = (*x.borrow()).clone();
230+
operator_lt_8(_a, (*y.borrow()).clone())
231+
})
232+
);
233+
assert!(
234+
({
235+
let _a: V = (*y.borrow()).clone();
236+
operator_gt_11(_a, (*x.borrow()).clone())
237+
})
238+
);
239+
assert!(
240+
!({
241+
let _a: V = (*y.borrow()).clone();
242+
operator_lt_8(_a, (*x.borrow()).clone())
243+
})
244+
);
125245
let a: Value<S> = Rc::new(RefCell::new(S {
126246
v: Rc::new(RefCell::new(1)),
127247
}));

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,87 @@ pub unsafe fn operator_lt_6(a: *const S, mut b: i32) -> bool {
5959
pub unsafe fn operator_lt_7(mut a: i32, b: *const S) -> bool {
6060
return ((a) < ((*b).v));
6161
}
62+
#[repr(C)]
63+
#[derive(Copy, Clone, Default)]
64+
pub struct V {
65+
pub v: i32,
66+
}
67+
impl std::cmp::Ord for V {
68+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
69+
unsafe {
70+
if operator_lt_8(self as *const V, other as *const V) {
71+
std::cmp::Ordering::Less
72+
} else if operator_lt_8(other as *const V, self as *const V) {
73+
std::cmp::Ordering::Greater
74+
} else {
75+
std::cmp::Ordering::Equal
76+
}
77+
}
78+
}
79+
}
80+
impl std::cmp::PartialOrd for V {
81+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
82+
Some(self.cmp(other))
83+
}
84+
}
85+
impl std::cmp::PartialEq for V {
86+
fn eq(&self, other: &Self) -> bool {
87+
unsafe { operator_eq_9(self as *const V, other as *const V) }
88+
}
89+
}
90+
impl std::cmp::Eq for V {}
91+
pub unsafe fn operator_eq_9(mut a: V, mut b: V) -> bool {
92+
return ((a.v) == (b.v));
93+
}
94+
pub unsafe fn operator_ne_10(mut a: V, mut b: V) -> bool {
95+
return ((a.v) != (b.v));
96+
}
97+
pub unsafe fn operator_lt_8(mut a: V, mut b: V) -> bool {
98+
return ((a.v) < (b.v));
99+
}
100+
pub unsafe fn operator_gt_11(mut a: V, mut b: V) -> bool {
101+
return ((a.v) > (b.v));
102+
}
62103
pub fn main() {
63104
unsafe {
64105
__cpp2rust_init_globals();
65106
std::process::exit(main_0() as i32);
66107
}
67108
}
68109
unsafe fn main_0() -> i32 {
110+
let mut x: V = V { v: 1 };
111+
let mut y: V = V { v: 2 };
112+
let mut z: V = V { v: 1 };
113+
assert!(
114+
(unsafe {
115+
let _a: V = x;
116+
operator_eq_9(_a, z)
117+
})
118+
);
119+
assert!(
120+
(unsafe {
121+
let _a: V = x;
122+
operator_ne_10(_a, y)
123+
})
124+
);
125+
assert!(
126+
(unsafe {
127+
let _a: V = x;
128+
operator_lt_8(_a, y)
129+
})
130+
);
131+
assert!(
132+
(unsafe {
133+
let _a: V = y;
134+
operator_gt_11(_a, x)
135+
})
136+
);
137+
assert!(
138+
!(unsafe {
139+
let _a: V = y;
140+
operator_lt_8(_a, x)
141+
})
142+
);
69143
let mut a: S = S { v: 1 };
70144
let mut b: S = S { v: 2 };
71145
let mut c: S = S { v: 1 };

0 commit comments

Comments
 (0)