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
2 changes: 1 addition & 1 deletion src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ macro_rules! impl_for_tuple {
}
#[allow(unused)]
fn requires_ordering_by_descending_value_pwu(&self) -> bool {
[$(self.$b.0.requires_ordering_by_descending_value_pwu()),*].iter().all(|x| *x)
[$(self.$b.0.requires_ordering_by_descending_value_pwu()),*].iter().any(|x| *x)
}
}
};
Expand Down
48 changes: 48 additions & 0 deletions tests/bnb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ impl BnbMetric for MinExcessThenWeight {
}
}

#[derive(Clone, Copy)]
struct OrderSensitive;

impl BnbMetric for OrderSensitive {
fn score(&mut self, _cs: &CoinSelector<'_>) -> Option<Ordf32> {
Some(Ordf32(0.0))
}

fn bound(&mut self, _cs: &CoinSelector<'_>) -> Option<Ordf32> {
Some(Ordf32(0.0))
}

fn requires_ordering_by_descending_value_pwu(&self) -> bool {
true
}
}

#[derive(Clone, Copy)]
struct NoOrderMetric;

impl BnbMetric for NoOrderMetric {
fn score(&mut self, _cs: &CoinSelector<'_>) -> Option<Ordf32> {
Some(Ordf32(0.0))
}

fn bound(&mut self, _cs: &CoinSelector<'_>) -> Option<Ordf32> {
Some(Ordf32(0.0))
}
}

#[test]
/// Detect regressions/improvements by making sure it always finds the solution in the same
/// number of iterations.
Expand Down Expand Up @@ -139,6 +169,24 @@ fn bnb_finds_solution_if_possible_in_n_iter() {
assert_eq!(excess, 0);
}

#[test]
fn bnb_tuple_metric_respects_ordering_requirement() {
assert!(
((OrderSensitive, 1.0), (OrderSensitive, 1.0)).requires_ordering_by_descending_value_pwu(),
"both require ordering, so ordering should be required"
);

assert!(
((OrderSensitive, 1.0), (NoOrderMetric, 1.0)).requires_ordering_by_descending_value_pwu(),
"one requires ordering, so ordering should be required"
);

assert!(
!((NoOrderMetric, 1.0), (NoOrderMetric, 1.0)).requires_ordering_by_descending_value_pwu(),
"none require ordering, so ordering should not be required"
);
}

proptest! {
#[test]
fn bnb_always_finds_solution_if_possible(num_inputs in 1usize..18, target_value in 0u64..10_000) {
Expand Down