units: Test that SignedAmount float conversion overflows
We should not be able to roundtrip a `SignedAmount` value greater than `MAX`, add a test to prove so. While we are at it document the assertion above that proves we can parse a float representing an `Amount` greater than `SignedAmount::MAX`.
This commit is contained in:
parent
d768f25da8
commit
baadcf4c0a
|
@ -1710,6 +1710,8 @@ mod tests {
|
||||||
assert_eq!(f(42.123456781, D::Bitcoin), Err(ParseAmountError::TooPrecise));
|
assert_eq!(f(42.123456781, D::Bitcoin), Err(ParseAmountError::TooPrecise));
|
||||||
assert_eq!(sf(-184467440738.0, D::Bitcoin), Err(ParseAmountError::TooBig));
|
assert_eq!(sf(-184467440738.0, D::Bitcoin), Err(ParseAmountError::TooBig));
|
||||||
assert_eq!(f(18446744073709551617.0, D::Satoshi), Err(ParseAmountError::TooBig));
|
assert_eq!(f(18446744073709551617.0, D::Satoshi), Err(ParseAmountError::TooBig));
|
||||||
|
|
||||||
|
// Amount can be grater than the max SignedAmount.
|
||||||
assert!(f(SignedAmount::MAX.to_float_in(D::Satoshi) + 1.0, D::Satoshi).is_ok());
|
assert!(f(SignedAmount::MAX.to_float_in(D::Satoshi) + 1.0, D::Satoshi).is_ok());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -1717,6 +1719,11 @@ mod tests {
|
||||||
Err(ParseAmountError::TooBig)
|
Err(ParseAmountError::TooBig)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
sf(SignedAmount::MAX.to_float_in(D::Satoshi) + 1.0, D::Satoshi),
|
||||||
|
Err(ParseAmountError::TooBig)
|
||||||
|
);
|
||||||
|
|
||||||
let btc = move |f| SignedAmount::from_btc(f).unwrap();
|
let btc = move |f| SignedAmount::from_btc(f).unwrap();
|
||||||
assert_eq!(btc(2.5).to_float_in(D::Bitcoin), 2.5);
|
assert_eq!(btc(2.5).to_float_in(D::Bitcoin), 2.5);
|
||||||
assert_eq!(btc(-2.5).to_float_in(D::MilliBitcoin), -2500.0);
|
assert_eq!(btc(-2.5).to_float_in(D::MilliBitcoin), -2500.0);
|
||||||
|
|
Loading…
Reference in New Issue