Skip to content
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
131 changes: 131 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ typetag = "0.2"

[dev-dependencies]
assert_cmd = "2.1.2"
proptest = "1.11.0"
71 changes: 70 additions & 1 deletion src/currency/btc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use strum_macros::{Display, EnumString};

use crate::currency::Currency;

#[derive(Serialize, Deserialize, Debug, PartialEq, EnumString, Display)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, EnumString, Display)]
#[strum(ascii_case_insensitive, serialize_all = "UPPERCASE")]
pub enum BitcoinUnit {
BTC, // bitcoin
Expand Down Expand Up @@ -35,3 +35,72 @@ impl Currency for BitcoinUnit {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;

fn arb_btc_unit() -> impl Strategy<Value = BitcoinUnit> {
prop_oneof![
Just(BitcoinUnit::BTC),
Just(BitcoinUnit::MBTC),
Just(BitcoinUnit::BITS),
Just(BitcoinUnit::SAT),
Just(BitcoinUnit::MSAT),
]
}

proptest! {
#[test]
fn roundtrip_btc_conversion(
amount in 1.0e-8_f64..1.0e12,
from in arb_btc_unit(),
to in arb_btc_unit(),
) {
// Convert from -> BTC -> to -> BTC -> from
let in_btc = amount * from.btc_value();
let in_target = in_btc / to.btc_value();
let back_in_btc = in_target * to.btc_value();
let back_in_from = back_in_btc / from.btc_value();

let relative_error = ((back_in_from - amount) / amount).abs();
prop_assert!(
relative_error < 1.0e-10,
"Roundtrip error too large: {amount} -> {back_in_from} (error: {relative_error})"
);
}

#[test]
fn btc_value_is_positive(unit in arb_btc_unit()) {
prop_assert!(unit.btc_value() > 0.0);
}

#[test]
fn conversion_preserves_order(
a in 1.0_f64..1.0e12,
b in 1.0_f64..1.0e12,
unit in arb_btc_unit(),
) {
// If a > b in one unit, a > b in any other unit
let a_btc = a * unit.btc_value();
let b_btc = b * unit.btc_value();
prop_assert_eq!(a > b, a_btc > b_btc);
}

#[test]
fn round_value_has_correct_decimal_places(
amount in 0.0_f64..1.0e8,
unit in arb_btc_unit(),
) {
let rounded = unit.round_value(amount);
let factor = 10_f64.powi(unit.decimal_places().into());
let check = (rounded * factor).round() / factor;
let diff = (rounded - check).abs();
prop_assert!(
diff < 1.0e-10,
"round_value produced too many decimal places: {rounded} (expected {check})"
);
}
}
}
Loading