Skip to content

Commit b665569

Browse files
committed
Add tests for Sub/SubAssign in Decimal/Decimal256
1 parent 88ce08d commit b665569

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

packages/std/src/math/decimal.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,26 @@ mod tests {
914914
}
915915

916916
#[test]
917-
fn decimal_sub() {
917+
fn decimal_sub_works() {
918918
let value = Decimal::one() - Decimal::percent(50); // 0.5
919919
assert_eq!(value.0, Decimal::DECIMAL_FRACTIONAL / Uint128::from(2u8));
920+
921+
assert_eq!(
922+
Decimal::percent(9) - Decimal::percent(4),
923+
Decimal::percent(5)
924+
);
925+
assert_eq!(Decimal::percent(16) - Decimal::zero(), Decimal::percent(16));
926+
assert_eq!(Decimal::percent(16) - Decimal::percent(16), Decimal::zero());
927+
assert_eq!(Decimal::zero() - Decimal::zero(), Decimal::zero());
928+
929+
// works for refs
930+
let a = Decimal::percent(13);
931+
let b = Decimal::percent(6);
932+
let expected = Decimal::percent(7);
933+
assert_eq!(a - b, expected);
934+
assert_eq!(&a - b, expected);
935+
assert_eq!(a - &b, expected);
936+
assert_eq!(&a - &b, expected);
920937
}
921938

922939
#[test]
@@ -925,6 +942,20 @@ mod tests {
925942
let _value = Decimal::zero() - Decimal::percent(50);
926943
}
927944

945+
#[test]
946+
fn decimal_sub_assign_works() {
947+
let mut a = Decimal::percent(20);
948+
a -= Decimal::percent(2);
949+
assert_eq!(a, Decimal::percent(18));
950+
951+
// works for refs
952+
let mut a = Decimal::percent(33);
953+
let b = Decimal::percent(13);
954+
let expected = Decimal::percent(20);
955+
a -= &b;
956+
assert_eq!(a, expected);
957+
}
958+
928959
#[test]
929960
fn decimal_implements_mul() {
930961
let one = Decimal::one();

packages/std/src/math/decimal256.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,9 +995,26 @@ mod tests {
995995
}
996996

997997
#[test]
998-
fn decimal256_sub() {
998+
fn decimal256_sub_works() {
999999
let value = Decimal256::one() - Decimal256::percent(50); // 0.5
10001000
assert_eq!(value.0, Decimal256::DECIMAL_FRACTIONAL / Uint256::from(2u8));
1001+
1002+
assert_eq!(
1003+
Decimal256::percent(9) - Decimal256::percent(4),
1004+
Decimal256::percent(5)
1005+
);
1006+
assert_eq!(Decimal256::percent(16) - Decimal256::zero(), Decimal256::percent(16));
1007+
assert_eq!(Decimal256::percent(16) - Decimal256::percent(16), Decimal256::zero());
1008+
assert_eq!(Decimal256::zero() - Decimal256::zero(), Decimal256::zero());
1009+
1010+
// works for refs
1011+
let a = Decimal256::percent(13);
1012+
let b = Decimal256::percent(6);
1013+
let expected = Decimal256::percent(7);
1014+
assert_eq!(a - b, expected);
1015+
assert_eq!(&a - b, expected);
1016+
assert_eq!(a - &b, expected);
1017+
assert_eq!(&a - &b, expected);
10011018
}
10021019

10031020
#[test]
@@ -1006,6 +1023,20 @@ mod tests {
10061023
let _value = Decimal256::zero() - Decimal256::percent(50);
10071024
}
10081025

1026+
#[test]
1027+
fn decimal256_sub_assign_works() {
1028+
let mut a = Decimal256::percent(20);
1029+
a -= Decimal256::percent(2);
1030+
assert_eq!(a, Decimal256::percent(18));
1031+
1032+
// works for refs
1033+
let mut a = Decimal256::percent(33);
1034+
let b = Decimal256::percent(13);
1035+
let expected = Decimal256::percent(20);
1036+
a -= &b;
1037+
assert_eq!(a, expected);
1038+
}
1039+
10091040
#[test]
10101041
fn decimal256_implements_mul() {
10111042
let one = Decimal256::one();

0 commit comments

Comments
 (0)