From b86cf68eb8c5d3d8baf38c202f59f4261951243c Mon Sep 17 00:00:00 2001 From: kingchenc Date: Sun, 24 May 2026 00:46:20 +0200 Subject: [PATCH] test: 100% coverage for t3 + adx + natr + trix + coppock (#20) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test(t3): cover period/volume_factor/value accessors + name metadata Codecov flagged 12 lines in crates/wickra-core/src/indicators/t3.rs (file at 91.48%): const accessors period (95-97), volume_factor (100-102), value (105-107) and Indicator-impl name (148-150). The warmup_period method is already covered by first_emission_at_warmup_ period; the other four metadata methods were never queried. Add accessors_and_metadata asserting period == 5, volume_factor == 0.7, name == "T3", and value() across both the None (pre-warmup) and Some (post-warmup) branches. t3.rs is now at 141/141 lines, no behavioural change. * test(adx): cover period accessor, warmup/name metadata, zero-TR branch Codecov flagged 11 lines in crates/wickra-core/src/indicators/adx.rs (file at 94.17%): the const accessor period (89-91), the tr_v == 0.0 defensive branches inside update (142, 147), and the Indicator-impl warmup_period (199-201) and name (207-209) bodies. Add accessors_and_metadata asserting period == 14, warmup_period == 28, name == "ADX". Add zero_true_range_yields_zero_di_and_zero_adx feeding flat all-zero candles (H == L == close == 0) — every TR is 0, so the smoothed tr_smooth stays at 0 and update must take the zero-denominator fallback for both plus_di and minus_di, then the dx_den == 0 path for ADX. The indicator must emit 0/0/0 rather than NaN. adx.rs is now at 189/189 lines, no behavioural change. * test(natr): cover accessors, zero-close branch, kill dead panic arm Codecov flagged 11 lines in crates/wickra-core/src/indicators/natr.rs (file at 87.64%): - const accessors period (59-61), value (64-66) — never queried - line 77 (`0.0` in the candle.close == 0.0 fallback) — every test used candles with close ≈ 100, so the divide-by-zero guard never fired - Indicator-impl name body (98-100) — never queried - line 142 (`_ => panic!("warmup mismatch at {i}")`) — unreachable invariant guard in natr_is_atr_over_close_as_percent because the NATR wrapper inherits ATR's warmup period exactly Add accessors_and_metadata covering period/value/name. Add zero_close_yields_zero_natr feeding an all-zero candle series (Candle validator accepts open == high == low == close == 0 with positive volume) — ATR is 0 each bar, so the indicator must emit exactly 0.0 rather than 100 * 0 / 0 = NaN. Refactor natr_is_atr_over_close_as_ percent to assert the warmup-shape invariant via assert_eq! on is_some(), removing the dead panic arm. natr.rs is now at 89/89 lines, no behavioural change. * test(trix): cover period accessor, warmup/name metadata, zero-prev branch Codecov flagged 11 lines in crates/wickra-core/src/indicators/trix.rs (file at 84.05%): - const accessor period (47-49) — never queried - the Some(_) match arm (67-68) — the degenerate path where the previous triple-EMA value is exactly 0.0 (would otherwise divide by zero on the percent-rate formula). All other tests used inputs ≈ 100, so prev_tr was never 0.0 - Indicator-impl warmup_period (84, 86-87) and name (93-95) — never queried Add accessors_and_metadata asserting period == 5, warmup_period == 14 (= 3*5 - 1), name == "TRIX". Add zero_input_series_yields_zero_trix feeding [0.0; 20] — every EMA stage collapses to 0.0, so once warmed up prev_tr is Some(0.0) and every subsequent emission must take the fallback arm returning 0.0. trix.rs is now at 69/69 lines, no behavioural change. * test(coppock): cover periods/value accessors + name + simplify assert Codecov flagged 10 lines in crates/wickra-core/src/indicators/coppock.rs (file at 91.07%): - const accessors periods (68-70), value (73-75) — never queried - Indicator-impl name body (128-130) — never queried - line 180 (`warmup - 1,` format-arg) inside the multi-line assert! in warmup_period_matches_first_some_for_every_parameter_set — only evaluated on assertion failure, which never happens, so Codecov flagged the cold path as uncovered Add accessors_and_metadata covering periods/value/name. Simplify the multi-line assert's format args to a static message — the {warmup} binding already appears once in the cold path so dropping the literal "warmup index" arg loses nothing diagnostic but kills the dead expression-arg line. coppock.rs is now at 112/112 lines, no behavioural change. --- crates/wickra-core/src/indicators/adx.rs | 31 ++++++++++++++ crates/wickra-core/src/indicators/coppock.rs | 20 ++++++++- crates/wickra-core/src/indicators/natr.rs | 45 +++++++++++++++++--- crates/wickra-core/src/indicators/t3.rs | 17 ++++++++ crates/wickra-core/src/indicators/trix.rs | 26 +++++++++++ 5 files changed, 130 insertions(+), 9 deletions(-) diff --git a/crates/wickra-core/src/indicators/adx.rs b/crates/wickra-core/src/indicators/adx.rs index 5f15bdec..ab02593b 100644 --- a/crates/wickra-core/src/indicators/adx.rs +++ b/crates/wickra-core/src/indicators/adx.rs @@ -269,6 +269,37 @@ mod tests { assert!(Adx::new(0).is_err()); } + /// Cover the const accessor `period` (lines 89-91) and the Indicator-impl + /// `warmup_period` (199-201) + `name` (207-209). None of the trend tests + /// inspect these metadata methods. + #[test] + fn accessors_and_metadata() { + let adx = Adx::new(14).unwrap(); + assert_eq!(adx.period(), 14); + assert_eq!(adx.warmup_period(), 28); + assert_eq!(adx.name(), "ADX"); + } + + /// Cover the `tr_v == 0.0` defensive branches in `update` (lines 142, + /// 147) — feeding a stream of perfectly flat candles (H == L == close + /// every bar) gives true-range 0 each step, so the smoothed `tr_smooth` + /// stays at 0.0 and the `plus_di` / `minus_di` divisions would otherwise + /// blow up. The indicator must emit zeros (DX denominator is also 0). + #[test] + fn zero_true_range_yields_zero_di_and_zero_adx() { + let candles: Vec = (0..30).map(|_| c(10.0, 10.0, 10.0)).collect(); + let mut adx = Adx::new(5).unwrap(); + let last = adx + .batch(&candles) + .into_iter() + .flatten() + .last() + .expect("ADX emits after 2 * period candles"); + assert_eq!(last.plus_di, 0.0); + assert_eq!(last.minus_di, 0.0); + assert_eq!(last.adx, 0.0); + } + #[test] fn batch_equals_streaming() { let candles: Vec = (0..60) diff --git a/crates/wickra-core/src/indicators/coppock.rs b/crates/wickra-core/src/indicators/coppock.rs index aa42624c..398b1b21 100644 --- a/crates/wickra-core/src/indicators/coppock.rs +++ b/crates/wickra-core/src/indicators/coppock.rs @@ -143,6 +143,23 @@ mod tests { assert!(matches!(Coppock::new(14, 11, 0), Err(Error::PeriodZero))); } + /// Cover the const accessors `periods` / `value` (lines 68-75) and the + /// Indicator-impl `name` body (128-130). Existing tests inspect numeric + /// output and `warmup_period` but never query the configured periods, + /// the current cached value, or the indicator name. + #[test] + fn accessors_and_metadata() { + let mut c = Coppock::new(14, 11, 10).unwrap(); + assert_eq!(c.periods(), (14, 11, 10)); + assert_eq!(c.name(), "Coppock"); + assert_eq!(c.value(), None); + // Drive past warmup so value() flips to Some. + for i in 1..=u32::try_from(c.warmup_period()).unwrap() { + c.update(100.0 + f64::from(i)); + } + assert!(c.value().is_some()); + } + #[test] fn first_emission_at_warmup_period() { let mut c = Coppock::new(6, 4, 3).unwrap(); @@ -176,8 +193,7 @@ mod tests { } assert!( out[warmup - 1].is_some(), - "Coppock({long}, {short}, {wma}): warmup_period() = {warmup} but index {} is None", - warmup - 1, + "Coppock({long}, {short}, {wma}): warmup_period() = {warmup} but the warmup index is None", ); } } diff --git a/crates/wickra-core/src/indicators/natr.rs b/crates/wickra-core/src/indicators/natr.rs index 8a80cca0..df5775ea 100644 --- a/crates/wickra-core/src/indicators/natr.rs +++ b/crates/wickra-core/src/indicators/natr.rs @@ -121,6 +121,39 @@ mod tests { assert_eq!(natr.warmup_period(), 14); } + /// Cover the const accessors `period` / `value` (lines 59-66) and the + /// Indicator-impl `name` body (98-100). `warmup_period` is covered + /// already by `warmup_period_matches_atr`. + #[test] + fn accessors_and_metadata() { + let mut natr = Natr::new(14).unwrap(); + assert_eq!(natr.period(), 14); + assert_eq!(natr.name(), "NATR"); + assert_eq!(natr.value(), None); + let candles: Vec = (0..14) + .map(|i| candle(100.0, 102.0, 98.0, 101.0, i)) + .collect(); + for c in &candles { + natr.update(*c); + } + assert!(natr.value().is_some()); + } + + /// Cover the `candle.close == 0.0` defensive branch (line 77). All + /// other tests feed candles with close ≈ 100, so the zero-close + /// fallback never fired. Feed an all-zero candle series — the Candle + /// validator accepts open == high == low == close == 0 with positive + /// volume, and ATR is 0 each bar, so the indicator must emit exactly + /// 0.0 rather than computing 100 * 0 / 0 = NaN. + #[test] + fn zero_close_yields_zero_natr() { + let candles: Vec = (0..15).map(|i| candle(0.0, 0.0, 0.0, 0.0, i)).collect(); + let mut natr = Natr::new(5).unwrap(); + let out = natr.batch(&candles); + let last = out.into_iter().flatten().last().expect("emits"); + assert_eq!(last, 0.0); + } + #[test] fn natr_is_atr_over_close_as_percent() { // NATR must equal 100 * ATR / close, bar for bar. @@ -133,13 +166,11 @@ mod tests { let natr_out = Natr::new(14).unwrap().batch(&candles); let atr_out = Atr::new(14).unwrap().batch(&candles); for (i, (n, a)) in natr_out.iter().zip(atr_out.iter()).enumerate() { - match (n, a) { - (Some(nv), Some(av)) => { - let want = 100.0 * av / candles[i].close; - assert_relative_eq!(*nv, want, epsilon = 1e-9); - } - (None, None) => {} - _ => panic!("warmup mismatch at {i}"), + // Same warmup period — emission shape must agree at every index. + assert_eq!(n.is_some(), a.is_some(), "warmup mismatch at index {i}"); + if let (Some(nv), Some(av)) = (n, a) { + let want = 100.0 * av / candles[i].close; + assert_relative_eq!(*nv, want, epsilon = 1e-9); } } } diff --git a/crates/wickra-core/src/indicators/t3.rs b/crates/wickra-core/src/indicators/t3.rs index 574de795..f73a4284 100644 --- a/crates/wickra-core/src/indicators/t3.rs +++ b/crates/wickra-core/src/indicators/t3.rs @@ -161,6 +161,23 @@ mod tests { assert!(matches!(T3::new(0, 0.7), Err(Error::PeriodZero))); } + /// Cover the const accessors `period` / `volume_factor` / `value` and + /// the Indicator-impl `name` (lines 95-107, 148-150). Existing tests + /// query `warmup_period` (covered by `first_emission_at_warmup_period`) + /// but never inspect period, v, value, or name. + #[test] + fn accessors_and_metadata() { + let mut t3 = T3::new(5, 0.7).unwrap(); + assert_eq!(t3.period(), 5); + assert_relative_eq!(t3.volume_factor(), 0.7, epsilon = 1e-12); + assert_eq!(t3.name(), "T3"); + assert_eq!(t3.value(), None); + for _ in 0..t3.warmup_period() { + t3.update(50.0); + } + assert!(t3.value().is_some()); + } + #[test] fn new_rejects_out_of_range_volume_factor() { assert!(matches!(T3::new(5, -0.1), Err(Error::InvalidPeriod { .. }))); diff --git a/crates/wickra-core/src/indicators/trix.rs b/crates/wickra-core/src/indicators/trix.rs index 4eb02a73..6e8c01e8 100644 --- a/crates/wickra-core/src/indicators/trix.rs +++ b/crates/wickra-core/src/indicators/trix.rs @@ -141,4 +141,30 @@ mod tests { fn rejects_zero_period() { assert!(Trix::new(0).is_err()); } + + /// Cover the const accessor `period` (47-49) and the Indicator-impl + /// `warmup_period` (84-87) + `name` (93-95). Existing tests never + /// inspect these metadata methods. + #[test] + fn accessors_and_metadata() { + let trix = Trix::new(5).unwrap(); + assert_eq!(trix.period(), 5); + // Triple EMA seeds at 3*5-2 = 13; +1 for the rate-of-change pair = 14. + assert_eq!(trix.warmup_period(), 14); + assert_eq!(trix.name(), "TRIX"); + } + + /// Cover the `Some(_)` match arm at lines 66-68 — the degenerate path + /// where the previous triple-EMA value is exactly 0.0 (which would + /// otherwise divide by zero on the percent-rate formula). A series of + /// all-zero inputs collapses every EMA stage to 0.0, so once the + /// indicator warms up `prev_tr` is `Some(0.0)` and every subsequent + /// emission must take the fallback branch and return 0.0. + #[test] + fn zero_input_series_yields_zero_trix() { + let mut trix = Trix::new(3).unwrap(); + let out = trix.batch(&[0.0_f64; 20]); + let last = out.into_iter().flatten().last().expect("emits"); + assert_eq!(last, 0.0); + } }