Skip to content

Commit

Permalink
wip: Add exhaust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Mar 5, 2024
1 parent 80bb80c commit 45ad1b1
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ mod tests {
use bitcoin::TxOut;
use bitcoin::Weight;
use core::str::FromStr;
use std::iter::once;
use std::iter::zip;

fn create_weighted_utxos(fee: Amount) -> Vec<WeightedUtxo> {
let amts = [
Expand Down Expand Up @@ -719,4 +721,63 @@ mod tests {
assert_eq!(list[1].utxo.value, Amount::from_str("6 cBTC").unwrap());
assert_eq!(list[2].utxo.value, Amount::from_str("2 cBTC").unwrap());
}

#[test]
fn select_coins_bnb_exhaust() {
// Recreate make_hard from bitcoin core test suit.
// Takes 327,661 to find a solution.
let base: usize = 2;
let alpha = (0..17).enumerate().map(|(i, _)| base.pow(17 + i as u32));
let target = Amount::from_sat(alpha.clone().sum::<usize>() as u64);

let beta = (0..17).enumerate().map(|(i, _)| {
let a = base.pow(17 + i as u32);
let b = base.pow(16 - i as u32);
a + b
});

let vals: Vec<_> = zip(alpha, beta)
// flatten requires iterable types.
// use once() to make tuple iterable.
.flat_map(|tup| once(tup.0).chain(once(tup.1)))
.map(|a| Amount::from_sat(a as u64))
.collect();

let weighted_utxos = create_weighted_utxos_from_values(vals);
let list = select_coins_bnb(
target,
Amount::ONE_SAT,
FeeRate::ZERO,
FeeRate::ZERO,
&weighted_utxos,
);

assert!(list.is_none());
}

#[test]
fn select_coins_bnb_exhaust_v2() {
// Takes 163,819 iterations to find a solution.
let base: usize = 2;
let mut target = 0;
let vals = (0..15)
.enumerate()
.flat_map(|(i, _)| {
let a = base.pow(15 + i as u32) as u64;
target += a;
vec![a, a + 2]
});

let vals: Vec<_> = vals.map(Amount::from_sat).collect();
let weighted_utxos = create_weighted_utxos_from_values(vals);
let list = select_coins_bnb(
Amount::from_sat(target),
Amount::ONE_SAT,
FeeRate::ZERO,
FeeRate::ZERO,
&weighted_utxos,
);

assert!(list.is_none());
}
}

0 comments on commit 45ad1b1

Please sign in to comment.