Skip to content

Commit 7d26cc2

Browse files
committed
Added failing test for tuple BnB metric ordering propagation
1 parent 89f5568 commit 7d26cc2

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

tests/bnb.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ impl BnbMetric for MinExcessThenWeight {
5252
}
5353
}
5454

55+
#[derive(Clone, Copy)]
56+
struct OrderSensitive;
57+
58+
impl BnbMetric for OrderSensitive {
59+
fn score(&mut self, _cs: &CoinSelector<'_>) -> Option<Ordf32> {
60+
Some(Ordf32(0.0))
61+
}
62+
63+
fn bound(&mut self, _cs: &CoinSelector<'_>) -> Option<Ordf32> {
64+
Some(Ordf32(0.0))
65+
}
66+
67+
fn requires_ordering_by_descending_value_pwu(&self) -> bool {
68+
true
69+
}
70+
}
71+
72+
#[derive(Clone, Copy)]
73+
struct NoOrderMetric;
74+
75+
impl BnbMetric for NoOrderMetric {
76+
fn score(&mut self, _cs: &CoinSelector<'_>) -> Option<Ordf32> {
77+
Some(Ordf32(0.0))
78+
}
79+
80+
fn bound(&mut self, _cs: &CoinSelector<'_>) -> Option<Ordf32> {
81+
Some(Ordf32(0.0))
82+
}
83+
}
84+
5585
#[test]
5686
/// Detect regressions/improvements by making sure it always finds the solution in the same
5787
/// number of iterations.
@@ -139,6 +169,24 @@ fn bnb_finds_solution_if_possible_in_n_iter() {
139169
assert_eq!(excess, 0);
140170
}
141171

172+
#[test]
173+
fn bnb_tuple_metric_respects_ordering_requirement() {
174+
assert!(
175+
((OrderSensitive, 1.0), (OrderSensitive, 1.0)).requires_ordering_by_descending_value_pwu(),
176+
"both require ordering, so ordering should be required"
177+
);
178+
179+
assert!(
180+
((OrderSensitive, 1.0), (NoOrderMetric, 1.0)).requires_ordering_by_descending_value_pwu(),
181+
"one requires ordering, so ordering should be required"
182+
);
183+
184+
assert!(
185+
!((NoOrderMetric, 1.0), (NoOrderMetric, 1.0)).requires_ordering_by_descending_value_pwu(),
186+
"none require ordering, so ordering should not be required"
187+
);
188+
}
189+
142190
proptest! {
143191
#[test]
144192
fn bnb_always_finds_solution_if_possible(num_inputs in 1usize..18, target_value in 0u64..10_000) {

0 commit comments

Comments
 (0)