Skip to content

Commit fc89a44

Browse files
authored
Merge pull request #1382 from CosmWasm/1349-implement-parialeq-lhs
Implement PartialEq for references on all math types - LHS and tests
2 parents 9b4d5be + b4846f4 commit fc89a44

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

packages/std/src/math/decimal.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,12 @@ impl PartialEq<&Decimal> for Decimal {
624624
}
625625
}
626626

627+
impl PartialEq<Decimal> for &Decimal {
628+
fn eq(&self, rhs: &Decimal) -> bool {
629+
*self == rhs
630+
}
631+
}
632+
627633
#[cfg(test)]
628634
mod tests {
629635
use super::*;
@@ -1936,4 +1942,24 @@ mod tests {
19361942
Err(RoundUpOverflowError { .. })
19371943
));
19381944
}
1945+
1946+
#[test]
1947+
fn decimal_partial_eq() {
1948+
let test_cases = [
1949+
("1", "1", true),
1950+
("0.5", "0.5", true),
1951+
("0.5", "0.51", false),
1952+
("0", "0.00000", true),
1953+
]
1954+
.into_iter()
1955+
.map(|(lhs, rhs, expected)| (dec(lhs), dec(rhs), expected));
1956+
1957+
#[allow(clippy::op_ref)]
1958+
for (lhs, rhs, expected) in test_cases {
1959+
assert_eq!(lhs == rhs, expected);
1960+
assert_eq!(&lhs == rhs, expected);
1961+
assert_eq!(lhs == &rhs, expected);
1962+
assert_eq!(&lhs == &rhs, expected);
1963+
}
1964+
}
19391965
}

packages/std/src/math/decimal256.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,12 @@ impl PartialEq<&Decimal256> for Decimal256 {
649649
}
650650
}
651651

652+
impl PartialEq<Decimal256> for &Decimal256 {
653+
fn eq(&self, rhs: &Decimal256) -> bool {
654+
*self == rhs
655+
}
656+
}
657+
652658
#[cfg(test)]
653659
mod tests {
654660
use super::*;
@@ -2083,4 +2089,24 @@ mod tests {
20832089
);
20842090
assert_eq!(Decimal256::MAX.checked_ceil(), Err(RoundUpOverflowError));
20852091
}
2092+
2093+
#[test]
2094+
fn decimal256_partial_eq() {
2095+
let test_cases = [
2096+
("1", "1", true),
2097+
("0.5", "0.5", true),
2098+
("0.5", "0.51", false),
2099+
("0", "0.00000", true),
2100+
]
2101+
.into_iter()
2102+
.map(|(lhs, rhs, expected)| (dec(lhs), dec(rhs), expected));
2103+
2104+
#[allow(clippy::op_ref)]
2105+
for (lhs, rhs, expected) in test_cases {
2106+
assert_eq!(lhs == rhs, expected);
2107+
assert_eq!(&lhs == rhs, expected);
2108+
assert_eq!(lhs == &rhs, expected);
2109+
assert_eq!(&lhs == &rhs, expected);
2110+
}
2111+
}
20862112
}

packages/std/src/math/uint128.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,12 @@ impl PartialEq<&Uint128> for Uint128 {
517517
}
518518
}
519519

520+
impl PartialEq<Uint128> for &Uint128 {
521+
fn eq(&self, rhs: &Uint128) -> bool {
522+
*self == rhs
523+
}
524+
}
525+
520526
#[cfg(test)]
521527
mod tests {
522528
use super::*;
@@ -988,4 +994,19 @@ mod tests {
988994
assert_eq!(a.abs_diff(b), expected);
989995
assert_eq!(b.abs_diff(a), expected);
990996
}
997+
998+
#[test]
999+
fn uint128_partial_eq() {
1000+
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
1001+
.into_iter()
1002+
.map(|(lhs, rhs, expected)| (Uint128::new(lhs), Uint128::new(rhs), expected));
1003+
1004+
#[allow(clippy::op_ref)]
1005+
for (lhs, rhs, expected) in test_cases {
1006+
assert_eq!(lhs == rhs, expected);
1007+
assert_eq!(&lhs == rhs, expected);
1008+
assert_eq!(lhs == &rhs, expected);
1009+
assert_eq!(&lhs == &rhs, expected);
1010+
}
1011+
}
9911012
}

packages/std/src/math/uint256.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,12 @@ impl PartialEq<&Uint256> for Uint256 {
614614
}
615615
}
616616

617+
impl PartialEq<Uint256> for &Uint256 {
618+
fn eq(&self, rhs: &Uint256) -> bool {
619+
*self == rhs
620+
}
621+
}
622+
617623
#[cfg(test)]
618624
mod tests {
619625
use super::*;
@@ -1539,4 +1545,25 @@ mod tests {
15391545
assert_eq!(a.abs_diff(b), expected);
15401546
assert_eq!(b.abs_diff(a), expected);
15411547
}
1548+
1549+
#[test]
1550+
fn uint256_partial_eq() {
1551+
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
1552+
.into_iter()
1553+
.map(|(lhs, rhs, expected)| {
1554+
(
1555+
Uint256::from(lhs as u64),
1556+
Uint256::from(rhs as u64),
1557+
expected,
1558+
)
1559+
});
1560+
1561+
#[allow(clippy::op_ref)]
1562+
for (lhs, rhs, expected) in test_cases {
1563+
assert_eq!(lhs == rhs, expected);
1564+
assert_eq!(&lhs == rhs, expected);
1565+
assert_eq!(lhs == &rhs, expected);
1566+
assert_eq!(&lhs == &rhs, expected);
1567+
}
1568+
}
15421569
}

packages/std/src/math/uint512.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,12 @@ impl PartialEq<&Uint512> for Uint512 {
575575
}
576576
}
577577

578+
impl PartialEq<Uint512> for &Uint512 {
579+
fn eq(&self, rhs: &Uint512) -> bool {
580+
*self == rhs
581+
}
582+
}
583+
578584
#[cfg(test)]
579585
mod tests {
580586
use super::*;
@@ -1174,4 +1180,25 @@ mod tests {
11741180
assert_eq!(a.abs_diff(b), expected);
11751181
assert_eq!(b.abs_diff(a), expected);
11761182
}
1183+
1184+
#[test]
1185+
fn uint512_partial_eq() {
1186+
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
1187+
.into_iter()
1188+
.map(|(lhs, rhs, expected)| {
1189+
(
1190+
Uint512::from(lhs as u64),
1191+
Uint512::from(rhs as u64),
1192+
expected,
1193+
)
1194+
});
1195+
1196+
#[allow(clippy::op_ref)]
1197+
for (lhs, rhs, expected) in test_cases {
1198+
assert_eq!(lhs == rhs, expected);
1199+
assert_eq!(&lhs == rhs, expected);
1200+
assert_eq!(lhs == &rhs, expected);
1201+
assert_eq!(&lhs == &rhs, expected);
1202+
}
1203+
}
11771204
}

packages/std/src/math/uint64.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,12 @@ impl PartialEq<&Uint64> for Uint64 {
471471
}
472472
}
473473

474+
impl PartialEq<Uint64> for &Uint64 {
475+
fn eq(&self, rhs: &Uint64) -> bool {
476+
*self == rhs
477+
}
478+
}
479+
474480
#[cfg(test)]
475481
mod tests {
476482
use super::*;
@@ -901,4 +907,19 @@ mod tests {
901907
assert_eq!(a.abs_diff(b), expected);
902908
assert_eq!(b.abs_diff(a), expected);
903909
}
910+
911+
#[test]
912+
fn uint64_partial_eq() {
913+
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
914+
.into_iter()
915+
.map(|(lhs, rhs, expected)| (Uint64::new(lhs), Uint64::new(rhs), expected));
916+
917+
#[allow(clippy::op_ref)]
918+
for (lhs, rhs, expected) in test_cases {
919+
assert_eq!(lhs == rhs, expected);
920+
assert_eq!(&lhs == rhs, expected);
921+
assert_eq!(lhs == &rhs, expected);
922+
assert_eq!(&lhs == &rhs, expected);
923+
}
924+
}
904925
}

0 commit comments

Comments
 (0)