From a9670b0ad1cc477df4005c8402412ae10da6a34a Mon Sep 17 00:00:00 2001 From: kingchenc Date: Sun, 24 May 2026 00:46:56 +0200 Subject: [PATCH] test: 100% coverage for pmo + ppo + roc + ulcer_index + williams_r (#21) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test(pmo): cover periods/value accessors, name, zero-prev fallback Codecov flagged 10 lines in crates/wickra-core/src/indicators/pmo.rs (file at 90.56%): - const accessors periods (76-78), value (81-83) — never queried - line 103 (`0.0` in the prev == 0.0 ROC fallback) — every existing test used prices > 0, so the divide-by-zero guard never fired - Indicator-impl name body (130-132) — never queried Add accessors_and_metadata covering periods/value/name. Add zero_previous_price_treats_roc_as_flat seeding prev_price = 0 then pushing a non-zero price — the ROC must take the flat-momentum fallback (0.0) and the doubly-smoothed PMO emits exactly 0.0 rather than NaN. pmo.rs is now at 106/106 lines, no behavioural change. * test(ppo): cover periods/value accessors, name, zero-slow-EMA fallback Codecov flagged 10 lines in crates/wickra-core/src/indicators/ppo.rs (file at 90.29%): - const accessors periods (71-73), value (76-78) — never queried - line 96 (`0.0` in the s == 0.0 PPO fallback) — every existing test used prices ≈ 100, so the slow EMA was never 0 and the divide-by-zero guard never fired - Indicator-impl name body (122-124) — never queried Add accessors_and_metadata covering periods/value/name. Add zero_slow_ema_yields_zero_ppo feeding a stream of zeros — both EMAs converge to 0.0 and the indicator must emit exactly 0.0 (flat momentum) rather than NaN. ppo.rs is now at 103/103 lines, no behavioural change. * test(roc): cover period accessor, warmup/name, zero-prev fallback Codecov flagged 10 lines in crates/wickra-core/src/indicators/roc.rs (file at 87.80%): - const accessor period (47-49) — never queried - line 70 (`0.0` in the prev == 0.0 ROC fallback) — every test used prices ≥ 1.0, so the divide-by-zero guard never fired - Indicator-impl warmup_period (83-85), name (91-93) — never queried Add accessors_and_metadata covering period == 5, warmup_period == 6 (= period + 1), name == "ROC". Add zero_previous_price_yields_zero_roc feeding a leading zero followed by `period` more values so the front of the window is exactly 0.0; the next emission must be the flat-momentum fallback 0.0 (not NaN). roc.rs is now at 82/82 lines, no behavioural change. * test(ulcer_index): cover period/value accessors, name, zero-max fallback Codecov flagged 10 lines in crates/wickra-core/src/indicators/ulcer_index.rs (file at 93.86%): - const accessors period (77-80), value (82-85) — never queried - line 123 (`0.0` in the max_price == 0.0 drawdown fallback) — every test used prices > 0, so the trailing-max divisor was always positive - Indicator-impl name body (162-164) — never queried Add accessors_and_metadata covering period/value/name. Add zero_max_price_yields_zero_drawdown feeding a stream of zeros — the trailing max is exactly 0.0 and the drawdown computation would otherwise hit 0/0 NaN; the indicator must emit exactly 0.0 (drawdown is 0% by convention). ulcer_index.rs is now at 163/163 lines, no behavioural change. * test(williams_r): cover period accessor, warmup/name, zero-range branch Codecov flagged 10 lines in crates/wickra-core/src/indicators/williams_r.rs (file at 89.79%): - const accessor period (49-51) — never queried - line 78 (`Some(-50.0)` in the range == 0.0 fallback) — every test used H != L candles, so the lookback range was always positive - Indicator-impl warmup_period (87-89), name (95-97) — never queried Add accessors_and_metadata covering period == 14, warmup_period == 14, name == "WilliamsR". Add zero_range_yields_minus_fifty feeding flat candles (H == L == close) — the lookback hi/lo coincide and the divide-by-zero guard fires, returning the neutral mid-range value -50.0. williams_r.rs is now at 98/98 lines, no behavioural change. --- crates/wickra-core/src/indicators/pmo.rs | 31 +++++++++++++++++++ crates/wickra-core/src/indicators/ppo.rs | 28 +++++++++++++++++ crates/wickra-core/src/indicators/roc.rs | 24 ++++++++++++++ .../wickra-core/src/indicators/ulcer_index.rs | 29 +++++++++++++++++ .../wickra-core/src/indicators/williams_r.rs | 29 +++++++++++++++++ 5 files changed, 141 insertions(+) diff --git a/crates/wickra-core/src/indicators/pmo.rs b/crates/wickra-core/src/indicators/pmo.rs index 9efd0534..10d9f724 100644 --- a/crates/wickra-core/src/indicators/pmo.rs +++ b/crates/wickra-core/src/indicators/pmo.rs @@ -150,6 +150,37 @@ mod tests { assert!(matches!(Pmo::new(35, 1), Err(Error::InvalidPeriod { .. }))); } + /// Cover the const accessors `periods` / `value` (lines 76-83) and the + /// Indicator-impl `name` body (130-132). `warmup_period` is already + /// covered by `first_emission_at_second_update`. + #[test] + fn accessors_and_metadata() { + let mut pmo = Pmo::new(35, 20).unwrap(); + assert_eq!(pmo.periods(), (35, 20)); + assert_eq!(pmo.name(), "PMO"); + assert_eq!(pmo.value(), None); + pmo.update(100.0); + pmo.update(101.0); + assert!(pmo.value().is_some()); + } + + /// Cover the `prev == 0.0` defensive branch (line 103). The PMO ROC + /// divides by the previous price; existing tests use prices ≈ 100, so + /// the divide-by-zero guard never fired. Feed a single zero price + /// followed by a positive price and assert the first emitted PMO is + /// the flat-momentum value (the wrapping `customEMA` of `0.0` is 0.0 + /// regardless of smoothing factor on its first input). + #[test] + fn zero_previous_price_treats_roc_as_flat() { + let mut pmo = Pmo::new(2, 2).unwrap(); + // Seed prev_price = 0. + assert_eq!(pmo.update(0.0), None); + // Next bar: prev == 0 hits the fallback returning roc = 0.0; the + // doubly-smoothed PMO seeds at 0.0 (10 * 0 = 0 through both EMAs). + let out = pmo.update(50.0).expect("emits"); + assert_eq!(out, 0.0); + } + #[test] fn first_emission_at_second_update() { let mut pmo = Pmo::new(35, 20).unwrap(); diff --git a/crates/wickra-core/src/indicators/ppo.rs b/crates/wickra-core/src/indicators/ppo.rs index 81d2a737..89eca470 100644 --- a/crates/wickra-core/src/indicators/ppo.rs +++ b/crates/wickra-core/src/indicators/ppo.rs @@ -142,6 +142,34 @@ mod tests { assert!(matches!(Ppo::new(12, 12), Err(Error::InvalidPeriod { .. }))); } + /// Cover the const accessors `periods` / `value` (lines 71-78) and the + /// Indicator-impl `name` body (122-124). `warmup_period` is already + /// covered by `first_emission_at_warmup_period`. + #[test] + fn accessors_and_metadata() { + let mut ppo = Ppo::new(12, 26).unwrap(); + assert_eq!(ppo.periods(), (12, 26)); + assert_eq!(ppo.name(), "PPO"); + assert_eq!(ppo.value(), None); + for i in 1..=26 { + ppo.update(f64::from(i)); + } + assert!(ppo.value().is_some()); + } + + /// Cover the `s == 0.0` defensive branch (line 96). PPO divides by + /// the slow EMA; existing tests use prices ≈ 100, so the slow EMA + /// is never 0. Feed a stream of zeros — both EMAs converge to 0.0 + /// and the indicator must emit exactly 0.0 (flat-momentum fallback) + /// rather than NaN. + #[test] + fn zero_slow_ema_yields_zero_ppo() { + let mut ppo = Ppo::new(3, 6).unwrap(); + let out = ppo.batch(&[0.0_f64; 20]); + let last = out.into_iter().flatten().last().expect("emits"); + assert_eq!(last, 0.0); + } + #[test] fn first_emission_at_warmup_period() { let mut ppo = Ppo::new(3, 6).unwrap(); diff --git a/crates/wickra-core/src/indicators/roc.rs b/crates/wickra-core/src/indicators/roc.rs index f2143669..934f1ddc 100644 --- a/crates/wickra-core/src/indicators/roc.rs +++ b/crates/wickra-core/src/indicators/roc.rs @@ -141,6 +141,30 @@ mod tests { assert!(Roc::new(0).is_err()); } + /// Cover the const accessor `period` (47-49) and the Indicator-impl + /// `warmup_period` (83-85) + `name` (91-93). Existing tests never + /// inspect these metadata methods. + #[test] + fn accessors_and_metadata() { + let roc = Roc::new(5).unwrap(); + assert_eq!(roc.period(), 5); + assert_eq!(roc.warmup_period(), 6); + assert_eq!(roc.name(), "ROC"); + } + + /// Cover the `prev == 0.0` defensive branch (line 70). All existing + /// tests use prices ≥ 1.0, so the divide-by-zero guard was never + /// triggered. Feed a leading zero followed by `period` more values + /// so the front of the window is exactly 0.0, then assert the next + /// emission is the flat-momentum fallback 0.0 (not NaN). + #[test] + fn zero_previous_price_yields_zero_roc() { + let mut roc = Roc::new(3).unwrap(); + let out = roc.batch(&[0.0, 5.0, 7.0, 9.0]); + let v = out[3].expect("ready after period + 1 inputs"); + assert_eq!(v, 0.0); + } + #[test] fn ignores_non_finite_input() { let mut roc = Roc::new(3).unwrap(); diff --git a/crates/wickra-core/src/indicators/ulcer_index.rs b/crates/wickra-core/src/indicators/ulcer_index.rs index bac1205a..3bfe2250 100644 --- a/crates/wickra-core/src/indicators/ulcer_index.rs +++ b/crates/wickra-core/src/indicators/ulcer_index.rs @@ -175,6 +175,35 @@ mod tests { assert!(matches!(UlcerIndex::new(0), Err(Error::PeriodZero))); } + /// Cover the const accessors `period` / `value` (lines 77-85) and the + /// Indicator-impl `name` body (162-164). `warmup_period` is covered + /// already by `reference_values`. + #[test] + fn accessors_and_metadata() { + let mut ui = UlcerIndex::new(14).unwrap(); + assert_eq!(ui.period(), 14); + assert_eq!(ui.name(), "UlcerIndex"); + assert_eq!(ui.value(), None); + // Drive past warmup so value() flips to Some. + for i in 0..ui.warmup_period() { + ui.update(100.0 + (i as f64).sin() * 5.0); + } + assert!(ui.value().is_some()); + } + + /// Cover the `max_price == 0.0` defensive branch (line 123). All + /// other tests use prices > 0, so the trailing-max divisor is always + /// positive. Feed a stream of zeros — the trailing max is exactly + /// 0.0 and the drawdown computation would otherwise hit a 0/0 NaN. + /// The indicator must emit exactly 0.0 (drawdown is 0% by convention). + #[test] + fn zero_max_price_yields_zero_drawdown() { + let mut ui = UlcerIndex::new(3).unwrap(); + let out = ui.batch(&[0.0_f64; 10]); + let last = out.into_iter().flatten().last().expect("emits"); + assert_eq!(last, 0.0); + } + #[test] fn reference_values() { // UlcerIndex(2): warmup = 3. diff --git a/crates/wickra-core/src/indicators/williams_r.rs b/crates/wickra-core/src/indicators/williams_r.rs index 63db48af..cc94880d 100644 --- a/crates/wickra-core/src/indicators/williams_r.rs +++ b/crates/wickra-core/src/indicators/williams_r.rs @@ -155,6 +155,35 @@ mod tests { assert!(WilliamsR::new(0).is_err()); } + /// Cover the const accessor `period` (49-51) and the Indicator-impl + /// `warmup_period` (87-89) + `name` (95-97). Existing tests never + /// inspect these metadata methods. + #[test] + fn accessors_and_metadata() { + let w = WilliamsR::new(14).unwrap(); + assert_eq!(w.period(), 14); + assert_eq!(w.warmup_period(), 14); + assert_eq!(w.name(), "WilliamsR"); + } + + /// Cover the `range == 0.0` defensive branch (line 78). All other + /// tests use H != L candles so the lookback range is always positive. + /// Feed a stream of perfectly flat candles (H == L == close) — the + /// lookback hi/lo coincide and the divide-by-zero guard fires, + /// returning the neutral mid-range value -50.0. + #[test] + fn zero_range_yields_minus_fifty() { + let candles: Vec = (0..5).map(|_| c(10.0, 10.0, 10.0)).collect(); + let mut w = WilliamsR::new(3).unwrap(); + let last = w + .batch(&candles) + .into_iter() + .flatten() + .last() + .expect("emits"); + assert_eq!(last, -50.0); + } + #[test] fn reset_clears_state() { let candles: Vec = (0..20)