Skip to content

Commit

Permalink
Add proptest for arbitrary target
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Jul 30, 2024
1 parent 41e4aa0 commit e4b0ab0
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions prop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use proptest::prelude::*;
use bitcoin_coin_selection::select_coins_bnb;
use bitcoin_coin_selection::WeightedUtxo;

use std::str::FromStr;

#[derive(Clone, Debug)]
pub struct Utxo {
output: TxOut,
Expand Down Expand Up @@ -43,6 +45,26 @@ fn build_pool(vec: &mut Vec<(u8, bool)>) -> Vec<Utxo> {
.collect()
}

fn build_linear_pool() -> Vec<Utxo> {
(1..10)
.map(|i| format!("{} cBTC", i))
.map(|s| Amount::from_str(&s).unwrap())
.map(|a| {
let output = TxOut {
value: a,
script_pubkey: ScriptBuf::new()
};

let satisfaction_weight = Weight::ZERO;

Utxo {
output,
satisfaction_weight
}
})
.collect()
}

fn create_target_amt_from(pool: &Vec<(u8, bool)>) -> Amount {
pool.iter().fold(Amount::ZERO, |acc, (u, b)|
{
Expand All @@ -61,6 +83,7 @@ proptest! {
// later, that it does not overflow.
fn test_bnb_arbitrary_pool(ref mut vec in any::<Vec<(u8, bool)>>()) {
vec.truncate(10);

let pool: Vec<_> = build_pool(vec);
let target = create_target_amt_from(&vec);

Expand All @@ -77,8 +100,28 @@ proptest! {
let sum: Amount = selection.into_iter().sum();
assert_eq!(target, sum);
}

#[test]
fn test_bnb_arbitrary_target(t in any::<u64>()) {
let target_str = format!("{} cBTC", t % 10);
let target = Amount::from_str(&target_str).unwrap();
let pool = build_linear_pool();

let selection_sum: Amount =
select_coins_bnb(
target,
Amount::ZERO,
FeeRate::ZERO,
FeeRate::ZERO,
&pool)
.unwrap()
.map(|i| i.value()).sum();

assert_eq!(selection_sum, target);
}
}

fn main() {
test_bnb_arbitrary_pool();
test_bnb_arbitrary_target();
}

0 comments on commit e4b0ab0

Please sign in to comment.