Skip to content

Commit 99a4474

Browse files
committed
Add operator overloads test
1 parent e8e1ed2 commit 99a4474

3 files changed

Lines changed: 314 additions & 0 deletions

File tree

‎tests/unit/operator_overloads.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <cassert>
2+
3+
struct S {
4+
int v;
5+
int operator==(int o) const { return v == o ? 1 : 0; }
6+
int operator==(long o) const { return v == o ? 2 : 0; }
7+
int operator==(double o) const { return v == o ? 3 : 0; }
8+
int operator+(const S &o) const { return v + o.v; }
9+
int operator-(S o) const { return v - o.v; }
10+
int operator*(const S &o) const { return v * o.v; }
11+
int operator*(int o) const { return v * o + 1; }
12+
};
13+
14+
int operator/(const S &a, const S &b) { return a.v / b.v; }
15+
int operator/(S a, int b) { return a.v / b + 1; }
16+
int operator%(S a, S b) { return a.v % b.v; }
17+
int operator%(const S &a, int b) { return a.v % b + 1; }
18+
int operator==(int a, S b) { return a == b.v ? 4 : 0; }
19+
int operator==(long a, const S &b) { return a == b.v ? 5 : 0; }
20+
21+
int main() {
22+
S s{6}, t{4};
23+
assert((s == 6) == 1);
24+
assert((s == 6L) == 2);
25+
assert((s == 6.0) == 3);
26+
assert((s == 7) == 0);
27+
assert(s + t == 10);
28+
assert(s - t == 2);
29+
assert(s * t == 24);
30+
assert(s * 2 == 13);
31+
assert(s / t == 1);
32+
assert(s / 4 == 2);
33+
assert(s % t == 2);
34+
assert(s % 4 == 3);
35+
assert((6 == s) == 4);
36+
assert((6L == s) == 5);
37+
return 0;
38+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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 S {
11+
pub v: Value<i32>,
12+
}
13+
impl Clone for S {
14+
fn clone(&self) -> Self {
15+
let __this: Value<S> = Rc::new(RefCell::new(Self {
16+
v: Rc::new(RefCell::new((*self.v.borrow()))),
17+
}));
18+
let this: Ptr<S> = __this.as_pointer();
19+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
20+
}
21+
}
22+
impl ByteRepr for S {
23+
fn byte_size() -> usize {
24+
4
25+
}
26+
fn to_bytes(&self, buf: &mut [u8]) {
27+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
28+
}
29+
fn from_bytes(buf: &[u8]) -> Self {
30+
Self {
31+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
32+
}
33+
}
34+
}
35+
pub fn operator_div_0(a: Ptr<S>, b: Ptr<S>) -> i32 {
36+
return {
37+
let _lhs = (*(*a.upgrade().deref()).v.borrow());
38+
_lhs / (*(*b.upgrade().deref()).v.borrow())
39+
};
40+
}
41+
pub fn operator_div_1(a: S, b: i32) -> i32 {
42+
let a: Value<S> = Rc::new(RefCell::new(a));
43+
let b: Value<i32> = Rc::new(RefCell::new(b));
44+
return (((*(*a.borrow()).v.borrow()) / (*b.borrow())) + 1);
45+
}
46+
pub fn operator_rem_2(a: S, b: S) -> i32 {
47+
let a: Value<S> = Rc::new(RefCell::new(a));
48+
let b: Value<S> = Rc::new(RefCell::new(b));
49+
return ((*(*a.borrow()).v.borrow()) % (*(*b.borrow()).v.borrow()));
50+
}
51+
pub fn operator_rem_3(a: Ptr<S>, b: i32) -> i32 {
52+
let b: Value<i32> = Rc::new(RefCell::new(b));
53+
return ({
54+
let _lhs = (*(*a.upgrade().deref()).v.borrow());
55+
_lhs % (*b.borrow())
56+
} + 1);
57+
}
58+
pub fn operator_eq_4(a: i32, b: S) -> i32 {
59+
let a: Value<i32> = Rc::new(RefCell::new(a));
60+
let b: Value<S> = Rc::new(RefCell::new(b));
61+
return if ((*a.borrow()) == (*(*b.borrow()).v.borrow())) {
62+
4
63+
} else {
64+
0
65+
};
66+
}
67+
pub fn operator_eq_5(a: i64, b: Ptr<S>) -> i32 {
68+
let a: Value<i64> = Rc::new(RefCell::new(a));
69+
return if {
70+
let _lhs = (*a.borrow());
71+
_lhs == ((*(*b.upgrade().deref()).v.borrow()) as i64)
72+
} {
73+
5
74+
} else {
75+
0
76+
};
77+
}
78+
pub fn main() {
79+
std::process::exit(main_0());
80+
}
81+
fn main_0() -> i32 {
82+
let s: Value<S> = Rc::new(RefCell::new(S {
83+
v: Rc::new(RefCell::new(6)),
84+
}));
85+
let t: Value<S> = Rc::new(RefCell::new(S {
86+
v: Rc::new(RefCell::new(4)),
87+
}));
88+
assert!((({ SImpl::operator_eq_i32_const(&s.as_pointer(), 6,) }) == 1));
89+
assert!((({ SImpl::operator_eq_i64_const(&s.as_pointer(), 6_i64,) }) == 2));
90+
assert!((({ SImpl::operator_eq_f64_const(&s.as_pointer(), 6.0E+0,) }) == 3));
91+
assert!((({ SImpl::operator_eq_i32_const(&s.as_pointer(), 7,) }) == 0));
92+
assert!((({ SImpl::operator_add(&s.as_pointer(), t.as_pointer(),) }) == 10));
93+
assert!((({ SImpl::operator_sub(&s.as_pointer(), (*t.borrow()).clone(),) }) == 2));
94+
assert!((({ SImpl::operator_mul_pconstS_const(&s.as_pointer(), t.as_pointer(),) }) == 24));
95+
assert!((({ SImpl::operator_mul_i32_const(&s.as_pointer(), 2,) }) == 13));
96+
assert!(
97+
(({
98+
let _a: Ptr<S> = s.as_pointer();
99+
operator_div_0(_a, t.as_pointer())
100+
}) == 1)
101+
);
102+
assert!(
103+
(({
104+
let _a: S = (*s.borrow()).clone();
105+
operator_div_1(_a, 4)
106+
}) == 2)
107+
);
108+
assert!(
109+
(({
110+
let _a: S = (*s.borrow()).clone();
111+
operator_rem_2(_a, (*t.borrow()).clone())
112+
}) == 2)
113+
);
114+
assert!(
115+
(({
116+
let _a: Ptr<S> = s.as_pointer();
117+
operator_rem_3(_a, 4)
118+
}) == 3)
119+
);
120+
assert!((({ operator_eq_4(6, (*s.borrow()).clone(),) }) == 4));
121+
assert!((({ operator_eq_5(6_i64, s.as_pointer(),) }) == 5));
122+
return 0;
123+
}
124+
pub trait SImpl {
125+
fn operator_eq_i32_const(&self, o: i32) -> i32;
126+
fn operator_eq_i64_const(&self, o: i64) -> i32;
127+
fn operator_eq_f64_const(&self, o: f64) -> i32;
128+
fn operator_add(&self, o: Ptr<S>) -> i32;
129+
fn operator_sub(&self, o: S) -> i32;
130+
fn operator_mul_pconstS_const(&self, o: Ptr<S>) -> i32;
131+
fn operator_mul_i32_const(&self, o: i32) -> i32;
132+
}
133+
impl SImpl for Ptr<S> {
134+
fn operator_eq_i32_const(&self, o: i32) -> i32 {
135+
let o: Value<i32> = Rc::new(RefCell::new(o));
136+
return if ((*(*(*self).upgrade().deref()).v.borrow()) == (*o.borrow())) {
137+
1
138+
} else {
139+
0
140+
};
141+
}
142+
fn operator_eq_i64_const(&self, o: i64) -> i32 {
143+
let o: Value<i64> = Rc::new(RefCell::new(o));
144+
return if (((*(*(*self).upgrade().deref()).v.borrow()) as i64) == (*o.borrow())) {
145+
2
146+
} else {
147+
0
148+
};
149+
}
150+
fn operator_eq_f64_const(&self, o: f64) -> i32 {
151+
let o: Value<f64> = Rc::new(RefCell::new(o));
152+
return if (((*(*(*self).upgrade().deref()).v.borrow()) as f64) == (*o.borrow())) {
153+
3
154+
} else {
155+
0
156+
};
157+
}
158+
fn operator_add(&self, o: Ptr<S>) -> i32 {
159+
return {
160+
let _lhs = (*(*(*self).upgrade().deref()).v.borrow());
161+
_lhs + (*(*o.upgrade().deref()).v.borrow())
162+
};
163+
}
164+
fn operator_sub(&self, o: S) -> i32 {
165+
let o: Value<S> = Rc::new(RefCell::new(o));
166+
return ((*(*(*self).upgrade().deref()).v.borrow()) - (*(*o.borrow()).v.borrow()));
167+
}
168+
fn operator_mul_pconstS_const(&self, o: Ptr<S>) -> i32 {
169+
return {
170+
let _lhs = (*(*(*self).upgrade().deref()).v.borrow());
171+
_lhs * (*(*o.upgrade().deref()).v.borrow())
172+
};
173+
}
174+
fn operator_mul_i32_const(&self, o: i32) -> i32 {
175+
let o: Value<i32> = Rc::new(RefCell::new(o));
176+
return (((*(*(*self).upgrade().deref()).v.borrow()) * (*o.borrow())) + 1);
177+
}
178+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 S {
12+
pub v: i32,
13+
}
14+
impl S {
15+
pub unsafe fn operator_eq_i32_const(&self, mut o: i32) -> i32 {
16+
return if ((self.v) == (o)) { 1 } else { 0 };
17+
}
18+
pub unsafe fn operator_eq_i64_const(&self, mut o: i64) -> i32 {
19+
return if ((self.v as i64) == (o)) { 2 } else { 0 };
20+
}
21+
pub unsafe fn operator_eq_f64_const(&self, mut o: f64) -> i32 {
22+
return if ((self.v as f64) == (o)) { 3 } else { 0 };
23+
}
24+
pub unsafe fn operator_add(&self, o: *const S) -> i32 {
25+
return ((self.v) + ((*o).v));
26+
}
27+
pub unsafe fn operator_sub(&self, mut o: S) -> i32 {
28+
return ((self.v) - (o.v));
29+
}
30+
pub unsafe fn operator_mul_pconstS_const(&self, o: *const S) -> i32 {
31+
return ((self.v) * ((*o).v));
32+
}
33+
pub unsafe fn operator_mul_i32_const(&self, mut o: i32) -> i32 {
34+
return (((self.v) * (o)) + (1));
35+
}
36+
}
37+
pub unsafe fn operator_div_0(a: *const S, b: *const S) -> i32 {
38+
return (((*a).v) / ((*b).v));
39+
}
40+
pub unsafe fn operator_div_1(mut a: S, mut b: i32) -> i32 {
41+
return (((a.v) / (b)) + (1));
42+
}
43+
pub unsafe fn operator_rem_2(mut a: S, mut b: S) -> i32 {
44+
return ((a.v) % (b.v));
45+
}
46+
pub unsafe fn operator_rem_3(a: *const S, mut b: i32) -> i32 {
47+
return ((((*a).v) % (b)) + (1));
48+
}
49+
pub unsafe fn operator_eq_4(mut a: i32, mut b: S) -> i32 {
50+
return if ((a) == (b.v)) { 4 } else { 0 };
51+
}
52+
pub unsafe fn operator_eq_5(mut a: i64, b: *const S) -> i32 {
53+
return if ((a) == ((*b).v as i64)) { 5 } else { 0 };
54+
}
55+
pub fn main() {
56+
unsafe {
57+
std::process::exit(main_0() as i32);
58+
}
59+
}
60+
unsafe fn main_0() -> i32 {
61+
let mut s: S = S { v: 6 };
62+
let mut t: S = S { v: 4 };
63+
assert!(((unsafe { S::operator_eq_i32_const(&s, 6,) }) == (1)));
64+
assert!(((unsafe { S::operator_eq_i64_const(&s, 6_i64,) }) == (2)));
65+
assert!(((unsafe { S::operator_eq_f64_const(&s, 6.0E+0,) }) == (3)));
66+
assert!(((unsafe { S::operator_eq_i32_const(&s, 7,) }) == (0)));
67+
assert!(((unsafe { S::operator_add(&s, &t as *const S,) }) == (10)));
68+
assert!(((unsafe { S::operator_sub(&s, t,) }) == (2)));
69+
assert!(((unsafe { S::operator_mul_pconstS_const(&s, &t as *const S,) }) == (24)));
70+
assert!(((unsafe { S::operator_mul_i32_const(&s, 2,) }) == (13)));
71+
assert!(
72+
((unsafe {
73+
let _a: *const S = &s as *const S;
74+
operator_div_0(_a, &t as *const S)
75+
}) == (1))
76+
);
77+
assert!(
78+
((unsafe {
79+
let _a: S = s;
80+
operator_div_1(_a, 4)
81+
}) == (2))
82+
);
83+
assert!(
84+
((unsafe {
85+
let _a: S = s;
86+
operator_rem_2(_a, t)
87+
}) == (2))
88+
);
89+
assert!(
90+
((unsafe {
91+
let _a: *const S = &s as *const S;
92+
operator_rem_3(_a, 4)
93+
}) == (3))
94+
);
95+
assert!(((unsafe { operator_eq_4(6, s,) }) == (4)));
96+
assert!(((unsafe { operator_eq_5(6_i64, &s as *const S,) }) == (5)));
97+
return 0;
98+
}

0 commit comments

Comments
 (0)