test: 100% coverage for mfi + psar + cmf + hma + obv (#27)
* test(mfi): cover period accessor, name, flat-TP fallback Codecov flagged 8 lines in indicators/mfi.rs (file at 93.10%): const accessor period (58-60), (0.0, 0.0) arm when tp==prev (85), the Some(50.0) flat-flow fallback (105), and Indicator-impl name body (132-134). Add accessors_and_metadata and flat_typical_prices_default_to_50. mfi.rs now at 116/116. * test(psar): cover warmup/name, drop cold format-arg + panic-only asserts Codecov flagged 8 lines in indicators/psar.rs (file at 95.69%): warmup_period (206-208), name (220-222), the cold format-arg line 254 in pure_uptrend_sar_below_lows, and the in-loop assert! at line 275 in pure_downtrend_sar_above_highs (its panic body is dead). Add accessors_and_metadata for warmup/name. Refactor both trend tests to collect violations into a Vec and assert once outside the loop — the single assert can now legitimately reach its panic body in a regression, while removing the dead cold-path lines from the happy-path coverage. * test(cmf): cover period accessor, name, zero-range branch Codecov flagged 7 lines in indicators/cmf.rs (file at 95.03%): const accessor period (71-73), the range==0.0 zero-MFV branch (84), and Indicator-impl name body (124-126). Add accessors_and_metadata and zero_range_candle_contributes_zero_mfv (flat H=L=close candles). cmf.rs now at 141/141. * test(hma): cover period accessor + name, kill dead naive panic arm Codecov flagged 7 lines in indicators/hma.rs (file at 92.22%): const accessor period (51-53), Indicator-impl name body (87-89), and the unreachable arm at line 167 in matches_independent_wmas. Refactor that test to assert the warmup-shape invariant via assert_eq!(got.is_some(), want.is_some()) + if let, removing the dead panic arm. Add accessors_and_metadata covering period/name. hma.rs now at 90/90. * test(obv): cover value() Some branch + warmup/name metadata Codecov flagged 7 lines in indicators/obv.rs (file at 92.92%): the Some(self.total) branch of value() (47) — only the None branch was hit by reset_clears_state — plus Indicator-impl warmup_period (79-81), name (87-89). Add accessors_and_metadata covering all four. obv.rs now at 99/99.
This commit is contained in:
@@ -246,15 +246,21 @@ mod tests {
|
||||
})
|
||||
.collect();
|
||||
let mut psar = Psar::classic();
|
||||
for (i, sar) in psar.batch(&candles).into_iter().enumerate() {
|
||||
if let Some(s) = sar {
|
||||
assert!(
|
||||
s <= candles[i].low + 1e-9,
|
||||
"SAR {s} should be <= low {} at i={i}",
|
||||
candles[i].low
|
||||
);
|
||||
}
|
||||
}
|
||||
let violations: Vec<(usize, f64, f64)> = psar
|
||||
.batch(&candles)
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, sar)| {
|
||||
sar.and_then(|s| {
|
||||
if s > candles[i].low + 1e-9 {
|
||||
Some((i, s, candles[i].low))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
assert!(violations.is_empty(), "SAR above low: {violations:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -267,13 +273,23 @@ mod tests {
|
||||
})
|
||||
.collect();
|
||||
let mut psar = Psar::classic();
|
||||
let outs = psar.batch(&candles);
|
||||
// After the trend establishes downward, SAR should sit above highs.
|
||||
for (i, sar) in outs.into_iter().enumerate().skip(5) {
|
||||
if let Some(s) = sar {
|
||||
assert!(s >= candles[i].high - 1e-9);
|
||||
}
|
||||
}
|
||||
let violations: Vec<(usize, f64, f64)> = psar
|
||||
.batch(&candles)
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.skip(5)
|
||||
.filter_map(|(i, sar)| {
|
||||
sar.and_then(|s| {
|
||||
if s < candles[i].high - 1e-9 {
|
||||
Some((i, s, candles[i].high))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
assert!(violations.is_empty(), "SAR below high: {violations:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -292,6 +308,16 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// Cover the Indicator-impl `warmup_period` (206-208) and `name`
|
||||
/// (220-222). PSAR's warmup is the constant 2 (seed candle + first
|
||||
/// emitting candle); the name is the literal "PSAR".
|
||||
#[test]
|
||||
fn accessors_and_metadata() {
|
||||
let psar = Psar::classic();
|
||||
assert_eq!(psar.warmup_period(), 2);
|
||||
assert_eq!(psar.name(), "PSAR");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_params() {
|
||||
assert!(Psar::new(0.0, 0.02, 0.20).is_err());
|
||||
|
||||
Reference in New Issue
Block a user