Skip to content

Commit

Permalink
Add tests to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Oct 4, 2024
1 parent bc281e7 commit 2e657df
Showing 1 changed file with 87 additions and 44 deletions.
131 changes: 87 additions & 44 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ mod tests {
use super::*;
use bitcoin::{Amount, ScriptBuf, TxOut, Weight};
use bitcoin::transaction::effective_value;
use std::iter::zip;

use arbitrary::{Arbitrary, Result, Unstructured};
use arbtest::arbtest;
Expand All @@ -123,6 +124,43 @@ mod tests {
Utxo { output, satisfaction_weight }
}

pub fn build_pool() -> Vec<Utxo> {
let amts = [
27336,
238,
9225,
20540,
35590,
49463,
6331,
35548,
50363,
28009,
];

let weights = [
25350,
106,
3311,
2633,
21081,
35260,
3896,
6377,
6851,
20236
];

let utxos: Vec<_> = zip(amts, weights)
.map(|(a, w)| {
let amt = Amount::from_sat(a);
let weight = Weight::from_wu(w);
build_utxo(amt, weight)
}).collect();

utxos
}

#[derive(Debug, Clone, PartialEq, Ord, Eq, PartialOrd, Arbitrary)]
pub struct Utxo {
output: TxOut,
Expand Down Expand Up @@ -164,69 +202,74 @@ mod tests {
}
}

use std::iter::zip;
#[test]
fn select_coins_no_solution() {
let target = Amount::from_sat(255432);
let cost_of_change = Amount::ZERO;
let fee_rate = FeeRate::ZERO;
let lt_fee_rate = FeeRate::ZERO;
let pool = build_pool();

let amts = [
27336,
238,
9225,
20540,
35590,
49463,
6331,
35548,
50363,
28009,
];
let result = select_coins(
target,
cost_of_change,
fee_rate,
lt_fee_rate,
&pool,
);

let weights = [
25350,
106,
3311,
2633,
21081,
35260,
3896,
6377,
6851,
20236
];
// This yields no solution because:
// * BnB fails because the sum overage is greater than ost_of_change
// * SRD fails because the sum is greater the utxo sum + CHANGE_LOWER
assert!(result.is_none());
}

let utxos: Vec<_> = zip(amts, weights)
.map(|(a, w)| {
let amt = Amount::from_sat(a);
let tx_out = TxOut {
value: amt,
script_pubkey: vec![].into()
};
let weight = Weight::from_wu(w);
#[test]
fn select_coins_srd_solution() {
let target = Amount::from_sat(255432) - CHANGE_LOWER;
let cost_of_change = Amount::ZERO;
let fee_rate = FeeRate::ZERO;
let lt_fee_rate = FeeRate::ZERO;
let pool = build_pool();

let result = select_coins(
target,
cost_of_change,
fee_rate,
lt_fee_rate,
&pool,
);

let u = Utxo {
output: tx_out,
satisfaction_weight: weight
};
assert!(result.is_some());
let result: Amount = result.unwrap().map(|u| u.value()).sum();
assert!(result > target);
}

u
}).collect();
#[test]
fn select_coins_bnb_solution() {
let target = Amount::from_sat(255432);
let fee_rate = FeeRate::ZERO;
let lt_fee_rate = FeeRate::ZERO;
let pool = build_pool();

// set cost_of_change to be the differene
// between the total pool sum and the target amount
// plus 1. This creates an upper bound that the sum
// of all utxos will fall bellow resulting in a BnB match.
let cost_of_change = Amount::from_sat(7211);

let result = select_coins(
target,
cost_of_change,
fee_rate,
lt_fee_rate,
&utxos,
&pool,
);

// This yields no solution because:
// * BnB fails because the sum overage is greater than ost_of_change
// * SRD fails because the sum is greater the utxo sum + CHANGE_LOWER
assert!(result.is_none());
assert!(result.is_some());
let result: Amount = result.unwrap().map(|u| u.value()).sum();
assert!(result > target);
assert!(result <= target + cost_of_change);
}

#[test]
Expand Down

0 comments on commit 2e657df

Please sign in to comment.