Skip to content

Implement PartialEq for references on all math types - LHS and tests #1382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,12 @@ impl PartialEq<&Decimal> for Decimal {
}
}

impl PartialEq<Decimal> for &Decimal {
fn eq(&self, rhs: &Decimal) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1936,4 +1942,24 @@ mod tests {
Err(RoundUpOverflowError { .. })
));
}

#[test]
fn decimal_partial_eq() {
let test_cases = [
("1", "1", true),
("0.5", "0.5", true),
("0.5", "0.51", false),
("0", "0.00000", true),
]
.into_iter()
.map(|(lhs, rhs, expected)| (dec(lhs), dec(rhs), expected));

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}
26 changes: 26 additions & 0 deletions packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,12 @@ impl PartialEq<&Decimal256> for Decimal256 {
}
}

impl PartialEq<Decimal256> for &Decimal256 {
fn eq(&self, rhs: &Decimal256) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -2083,4 +2089,24 @@ mod tests {
);
assert_eq!(Decimal256::MAX.checked_ceil(), Err(RoundUpOverflowError));
}

#[test]
fn decimal256_partial_eq() {
let test_cases = [
("1", "1", true),
("0.5", "0.5", true),
("0.5", "0.51", false),
("0", "0.00000", true),
]
.into_iter()
.map(|(lhs, rhs, expected)| (dec(lhs), dec(rhs), expected));

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}
21 changes: 21 additions & 0 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ impl PartialEq<&Uint128> for Uint128 {
}
}

impl PartialEq<Uint128> for &Uint128 {
fn eq(&self, rhs: &Uint128) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -988,4 +994,19 @@ mod tests {
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}

#[test]
fn uint128_partial_eq() {
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
.into_iter()
.map(|(lhs, rhs, expected)| (Uint128::new(lhs), Uint128::new(rhs), expected));

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}
27 changes: 27 additions & 0 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,12 @@ impl PartialEq<&Uint256> for Uint256 {
}
}

impl PartialEq<Uint256> for &Uint256 {
fn eq(&self, rhs: &Uint256) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1539,4 +1545,25 @@ mod tests {
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}

#[test]
fn uint256_partial_eq() {
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
.into_iter()
.map(|(lhs, rhs, expected)| {
(
Uint256::from(lhs as u64),
Uint256::from(rhs as u64),
expected,
)
});

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}
27 changes: 27 additions & 0 deletions packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,12 @@ impl PartialEq<&Uint512> for Uint512 {
}
}

impl PartialEq<Uint512> for &Uint512 {
fn eq(&self, rhs: &Uint512) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1174,4 +1180,25 @@ mod tests {
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}

#[test]
fn uint512_partial_eq() {
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
.into_iter()
.map(|(lhs, rhs, expected)| {
(
Uint512::from(lhs as u64),
Uint512::from(rhs as u64),
Comment on lines +1190 to +1191
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates signed ints and then unsafely casts them to u64, right? Can you use 1u64 literals instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, can do, just thought this was less verbose

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got scared of unsafe casts after seeing a some bug in a TypeScript app that popped up on the user's machine but could have been cought at compile time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something like Uint512::from<u64>(lhs) for multiple versions of a trait implementation, even if from is not generic?

expected,
)
});

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}
21 changes: 21 additions & 0 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,12 @@ impl PartialEq<&Uint64> for Uint64 {
}
}

impl PartialEq<Uint64> for &Uint64 {
fn eq(&self, rhs: &Uint64) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -901,4 +907,19 @@ mod tests {
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}

#[test]
fn uint64_partial_eq() {
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
.into_iter()
.map(|(lhs, rhs, expected)| (Uint64::new(lhs), Uint64::new(rhs), expected));

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}