379 lines
13 KiB
Rust
379 lines
13 KiB
Rust
//! QA contract tests for the public `curate_etl` split API (T-47 / M1.2a).
|
|
//!
|
|
//! These only use the crate's public surface so they guard the contract that M1.2b/M2 callers
|
|
//! (Mongo cursor, Parquet/ILP writers) will rely on.
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use curate_etl::bench::{run_split_benchmark, BENCH_KEY_COUNT};
|
|
use curate_etl::{
|
|
unadjust_ohlcv, unadjust_price, unadjust_volume, ymd_to_epoch_days, OhlcvBar, SplitError,
|
|
SplitEvent, SplitTable,
|
|
};
|
|
|
|
fn day(y: i32, m: u32, d: u32) -> i32 {
|
|
ymd_to_epoch_days(y, m, d).expect("valid fixture date")
|
|
}
|
|
|
|
fn ev(ex_date: i32, ratio: f64) -> SplitEvent {
|
|
SplitEvent { ex_date, ratio }
|
|
}
|
|
|
|
fn assert_rel_eq(actual: f64, expected: f64, rel_tol: f64) {
|
|
let diff = (actual - expected).abs();
|
|
let scale = expected.abs().max(f64::MIN_POSITIVE);
|
|
assert!(
|
|
diff <= rel_tol * scale,
|
|
"actual={actual} expected={expected} diff={diff} rel_tol={rel_tol}"
|
|
);
|
|
}
|
|
|
|
/// Deterministic SplitMix64 so the randomized oracle test is reproducible.
|
|
struct Rng(u64);
|
|
|
|
impl Rng {
|
|
fn next_u64(&mut self) -> u64 {
|
|
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
|
|
let mut z = self.0;
|
|
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
|
|
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
|
|
z ^ (z >> 31)
|
|
}
|
|
}
|
|
|
|
/// Brute-force oracle: product of every non-no-op ratio for `key` with `ex_date > bar_date`.
|
|
fn naive_factor(events: &[(u32, SplitEvent)], key: u32, bar_date: i32) -> f64 {
|
|
events
|
|
.iter()
|
|
.filter(|(k, e)| *k == key && e.ex_date > bar_date && e.ratio != 1.0)
|
|
.map(|(_, e)| e.ratio)
|
|
.product()
|
|
}
|
|
|
|
#[test]
|
|
fn nvda_daily_bar_volume_matches_acceptance_fixture() {
|
|
let table =
|
|
SplitTable::from_events([("NVDA".to_string(), ev(day(2024, 6, 10), 10.0))]).unwrap();
|
|
|
|
let factor = table.cum_factor_after("NVDA", day(2024, 6, 7));
|
|
assert_rel_eq(unadjust_volume(412_390_000.0, factor), 41_239_000.0, 1e-9);
|
|
|
|
// eodPrices rule: OHLC is already raw, so only volume moves; ex-date bar is post-split.
|
|
assert_eq!(table.cum_factor_after("NVDA", day(2024, 6, 10)), 1.0);
|
|
assert_eq!(unadjust_volume(412_390_000.0, 1.0), 412_390_000.0);
|
|
}
|
|
|
|
#[test]
|
|
fn nexm_reverse_split_unadjusts_close_and_volume() {
|
|
let table =
|
|
SplitTable::from_events([("NEXM.V".to_string(), ev(day(2025, 6, 18), 0.05))]).unwrap();
|
|
|
|
let factor = table.cum_factor_after("NEXM.V", day(2025, 6, 17));
|
|
assert_rel_eq(unadjust_price(24.29, factor), 1.2145, 1e-9);
|
|
assert_rel_eq(unadjust_volume(3_000.0, factor), 60_000.0, 1e-9);
|
|
assert_eq!(table.cum_factor_after("NEXM.V", day(2025, 6, 18)), 1.0);
|
|
assert_eq!(table.cum_factor_after("NEXM.V", day(2025, 6, 19)), 1.0);
|
|
}
|
|
|
|
#[test]
|
|
fn readme_example_nvda_2021_ex_date_still_includes_2024_split() {
|
|
let table = SplitTable::from_events([
|
|
("NVDA".to_string(), ev(day(2024, 6, 10), 10.0)),
|
|
("NVDA".to_string(), ev(day(2021, 7, 20), 4.0)),
|
|
])
|
|
.unwrap();
|
|
|
|
assert_eq!(table.cum_factor_after("NVDA", day(2021, 7, 19)), 40.0);
|
|
assert_eq!(table.cum_factor_after("NVDA", day(2021, 7, 20)), 10.0);
|
|
assert_eq!(table.cum_factor_after("NVDA", day(2024, 6, 9)), 10.0);
|
|
assert_eq!(table.cum_factor_after("NVDA", day(2024, 6, 10)), 1.0);
|
|
}
|
|
|
|
#[test]
|
|
fn splits_for_exposes_sorted_entries_with_suffix_products() {
|
|
let table = SplitTable::from_events([
|
|
("X".to_string(), ev(30, 0.5)),
|
|
("X".to_string(), ev(10, 2.0)),
|
|
("X".to_string(), ev(20, 3.0)),
|
|
])
|
|
.unwrap();
|
|
|
|
let entries = table.splits_for("X");
|
|
let dates: Vec<i32> = entries.iter().map(|e| e.ex_date).collect();
|
|
let factors: Vec<f64> = entries.iter().map(|e| e.suffix_factor).collect();
|
|
assert_eq!(dates, vec![10, 20, 30]);
|
|
assert_eq!(factors, vec![3.0, 1.5, 0.5]);
|
|
assert!(table.splits_for("missing").is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn keys_are_isolated_from_each_other() {
|
|
let table = SplitTable::from_events([
|
|
("AAA".to_string(), ev(100, 2.0)),
|
|
("BBB".to_string(), ev(200, 5.0)),
|
|
("AAA".to_string(), ev(300, 3.0)),
|
|
])
|
|
.unwrap();
|
|
|
|
assert_eq!(table.len(), 2);
|
|
assert_eq!(table.event_count(), 3);
|
|
assert_eq!(table.cum_factor_after("AAA", 0), 6.0);
|
|
assert_eq!(table.cum_factor_after("BBB", 0), 5.0);
|
|
assert_eq!(table.cum_factor_after("AAA", 200), 3.0);
|
|
assert_eq!(table.cum_factor_after("BBB", 200), 1.0);
|
|
// Key matching is exact and case-sensitive (no symbol normalization in this layer).
|
|
assert_eq!(table.cum_factor_after("aaa", 0), 1.0);
|
|
assert_eq!(table.cum_factor_after("AAA.US", 0), 1.0);
|
|
}
|
|
|
|
#[test]
|
|
fn empty_and_all_noop_inputs_build_empty_tables() {
|
|
let empty: SplitTable<String> = SplitTable::from_events(Vec::new()).unwrap();
|
|
assert!(empty.is_empty());
|
|
assert_eq!(empty.len(), 0);
|
|
assert_eq!(empty.event_count(), 0);
|
|
assert_eq!(empty.cum_factor_after("ANY", 0), 1.0);
|
|
|
|
let noops = SplitTable::from_events((0..258).map(|i| (format!("K{i}"), ev(i, 1.0)))).unwrap();
|
|
assert!(noops.is_empty());
|
|
assert_eq!(noops.event_count(), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_ratio_anywhere_in_stream_fails_the_whole_build() {
|
|
let err = SplitTable::from_events([
|
|
("OK".to_string(), ev(10, 2.0)),
|
|
("OK".to_string(), ev(20, 1.0)),
|
|
("BAD".to_string(), ev(30, -0.5)),
|
|
("OK".to_string(), ev(40, 4.0)),
|
|
])
|
|
.unwrap_err();
|
|
|
|
assert_eq!(
|
|
err,
|
|
SplitError::InvalidRatio {
|
|
ex_date: 30,
|
|
ratio: -0.5
|
|
}
|
|
);
|
|
assert!(err.to_string().contains("must be finite and > 0"));
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_ratio_is_rejected_even_when_later_cancelled_or_duplicated() {
|
|
// A NaN must not be "multiplied away" by same-date coalescing.
|
|
let err = SplitTable::from_events([
|
|
("K".to_string(), ev(5, 2.0)),
|
|
("K".to_string(), ev(5, f64::NAN)),
|
|
])
|
|
.unwrap_err();
|
|
assert!(matches!(err, SplitError::InvalidRatio { ex_date: 5, ratio } if ratio.is_nan()));
|
|
}
|
|
|
|
#[test]
|
|
fn split_error_is_std_error_with_readable_messages() {
|
|
let errors: [Box<dyn std::error::Error>; 3] = [
|
|
Box::new(SplitError::InvalidRatio {
|
|
ex_date: 1,
|
|
ratio: 0.0,
|
|
}),
|
|
Box::new(SplitError::FactorOverflow {
|
|
ex_date: 2,
|
|
factor: f64::INFINITY,
|
|
}),
|
|
Box::new(SplitError::InvalidDate {
|
|
year: 2023,
|
|
month: 2,
|
|
day: 29,
|
|
}),
|
|
];
|
|
let messages: Vec<String> = errors.iter().map(|e| e.to_string()).collect();
|
|
assert!(messages[0].contains("invalid split ratio"));
|
|
assert!(messages[1].contains("overflow"));
|
|
assert_eq!(messages[2], "invalid Gregorian date 2023-02-29");
|
|
}
|
|
|
|
#[test]
|
|
fn same_date_underflow_returns_factor_overflow_error() {
|
|
let err = SplitTable::from_events([
|
|
("U".to_string(), ev(7, 1e-200)),
|
|
("U".to_string(), ev(7, 1e-200)),
|
|
])
|
|
.unwrap_err();
|
|
assert!(matches!(err, SplitError::FactorOverflow { ex_date: 7, factor } if factor == 0.0));
|
|
}
|
|
|
|
#[test]
|
|
fn string_table_accepts_borrowed_and_owned_lookups() {
|
|
let table = SplitTable::from_events([("MSFT".to_string(), ev(50, 2.0))]).unwrap();
|
|
let owned = String::from("MSFT");
|
|
assert_eq!(table.cum_factor_after(owned.as_str(), 49), 2.0);
|
|
assert_eq!(table.cum_factor_after(&owned, 49), 2.0);
|
|
}
|
|
|
|
#[test]
|
|
fn randomized_tables_match_brute_force_oracle() {
|
|
let mut rng = Rng(0xC0FF_EE00_D15C_0001);
|
|
const RATIOS: [f64; 8] = [1.0, 2.0, 3.0, 10.0, 0.5, 0.2, 0.05, 1.5];
|
|
|
|
for _case in 0..50 {
|
|
let n_events = (rng.next_u64() % 40) as usize;
|
|
let events: Vec<(u32, SplitEvent)> = (0..n_events)
|
|
.map(|_| {
|
|
let key = (rng.next_u64() % 6) as u32;
|
|
// Narrow date window forces many same-date duplicates.
|
|
let ex_date = (rng.next_u64() % 25) as i32 - 5;
|
|
let ratio = RATIOS[(rng.next_u64() % RATIOS.len() as u64) as usize];
|
|
(key, ev(ex_date, ratio))
|
|
})
|
|
.collect();
|
|
|
|
let table = SplitTable::from_events(events.iter().copied()).unwrap();
|
|
|
|
let mut effective: BTreeMap<u32, usize> = BTreeMap::new();
|
|
for key in 0..7u32 {
|
|
for bar_date in -8..24 {
|
|
let expected = naive_factor(&events, key, bar_date);
|
|
let actual = table.cum_factor_after(&key, bar_date);
|
|
assert_rel_eq(actual, expected, 1e-12);
|
|
let later = events
|
|
.iter()
|
|
.any(|(k, e)| *k == key && e.ex_date > bar_date && e.ratio != 1.0);
|
|
if !later {
|
|
// No later split: factor must be exactly 1.0 so bars pass through untouched.
|
|
assert_eq!(actual, 1.0, "key={key} bar_date={bar_date}");
|
|
}
|
|
}
|
|
let entries = table.splits_for(&key);
|
|
assert!(entries.windows(2).all(|w| w[0].ex_date < w[1].ex_date));
|
|
assert!(entries
|
|
.iter()
|
|
.all(|e| e.suffix_factor.is_finite() && e.suffix_factor > 0.0));
|
|
if !entries.is_empty() {
|
|
effective.insert(key, entries.len());
|
|
}
|
|
}
|
|
assert_eq!(table.len(), effective.len());
|
|
assert_eq!(table.event_count(), effective.values().sum::<usize>());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn unadjust_helpers_pass_neutral_factor_through_bit_exact() {
|
|
let bar = OhlcvBar {
|
|
open: 0.1 + 0.2,
|
|
high: 1e-300,
|
|
low: 0.0,
|
|
close: 123.456_789,
|
|
volume: 9_007_199_254_740_993.0,
|
|
};
|
|
let out = unadjust_ohlcv(bar, 1.0);
|
|
assert_eq!(out.open.to_bits(), bar.open.to_bits());
|
|
assert_eq!(out.high.to_bits(), bar.high.to_bits());
|
|
assert_eq!(out.close.to_bits(), bar.close.to_bits());
|
|
assert_eq!(out.volume.to_bits(), bar.volume.to_bits());
|
|
assert_eq!(unadjust_price(bar.open, 1.0).to_bits(), bar.open.to_bits());
|
|
assert_eq!(unadjust_volume(bar.open, 1.0).to_bits(), bar.open.to_bits());
|
|
}
|
|
|
|
#[test]
|
|
fn unadjust_ohlcv_scales_every_price_field_and_divides_volume() {
|
|
let adj = OhlcvBar {
|
|
open: 12.0,
|
|
high: 13.0,
|
|
low: 11.0,
|
|
close: 12.5,
|
|
volume: 4_000.0,
|
|
};
|
|
let raw = unadjust_ohlcv(adj, 4.0);
|
|
assert_eq!(
|
|
raw,
|
|
OhlcvBar {
|
|
open: 48.0,
|
|
high: 52.0,
|
|
low: 44.0,
|
|
close: 50.0,
|
|
volume: 1_000.0,
|
|
}
|
|
);
|
|
// Ordering of OHLC is preserved for positive factors.
|
|
assert!(raw.low <= raw.open && raw.open <= raw.high && raw.low <= raw.close);
|
|
// Dollar volume is invariant for every price field, not just close.
|
|
for (a, r) in [
|
|
(adj.open, raw.open),
|
|
(adj.high, raw.high),
|
|
(adj.low, raw.low),
|
|
] {
|
|
assert_rel_eq(r * raw.volume, a * adj.volume, 1e-12);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn zero_volume_bar_stays_zero_after_unadjust() {
|
|
assert_eq!(unadjust_volume(0.0, 0.05), 0.0);
|
|
assert_eq!(unadjust_volume(0.0, 10.0), 0.0);
|
|
}
|
|
|
|
#[test]
|
|
fn ymd_to_epoch_days_is_contiguous_across_leap_and_century_years() {
|
|
let mut expected = day(1899, 12, 31);
|
|
for year in 1900..=2100 {
|
|
for month in 1..=12u32 {
|
|
for d in 1..=31u32 {
|
|
if let Ok(days) = ymd_to_epoch_days(year, month, d) {
|
|
expected += 1;
|
|
assert_eq!(days, expected, "{year}-{month:02}-{d:02}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
assert_eq!(expected, day(2100, 12, 31));
|
|
// 201 years: 49 leap years in 1900..=2100 (1900 and 2100 are not leap, 2000 is).
|
|
assert_eq!(day(2100, 12, 31) - day(1899, 12, 31), 201 * 365 + 49);
|
|
}
|
|
|
|
#[test]
|
|
fn ymd_to_epoch_days_rejects_out_of_range_years_without_panicking() {
|
|
assert!(matches!(
|
|
ymd_to_epoch_days(i32::MAX, 12, 31),
|
|
Err(SplitError::InvalidDate { .. })
|
|
));
|
|
assert!(matches!(
|
|
ymd_to_epoch_days(i32::MIN, 1, 1),
|
|
Err(SplitError::InvalidDate { .. })
|
|
));
|
|
assert!(matches!(
|
|
ymd_to_epoch_days(2024, 6, 0),
|
|
Err(SplitError::InvalidDate { .. })
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn benchmark_fixture_and_small_run_checksum_are_pinned() {
|
|
let a = run_split_benchmark(1_000).unwrap();
|
|
let b = run_split_benchmark(1_000).unwrap();
|
|
|
|
assert_eq!(a.key_count, BENCH_KEY_COUNT);
|
|
assert_eq!(a.table_keys_with_splits, 12_153);
|
|
assert_eq!(a.table_event_count, 18_999);
|
|
assert_eq!(a.checksum.to_bits(), b.checksum.to_bits());
|
|
// Pinned so a change to the generator or lookup semantics is caught.
|
|
assert_eq!(format!("{:.3}", a.checksum), "20285840610.475");
|
|
}
|
|
|
|
#[test]
|
|
fn same_date_reciprocal_pair_leaves_no_entry_behind() {
|
|
let table = SplitTable::from_events([
|
|
("REV".to_string(), ev(10, 4.0)),
|
|
("REV".to_string(), ev(10, 0.25)),
|
|
("KEEP".to_string(), ev(10, 2.0)),
|
|
("KEEP".to_string(), ev(20, 2.0)),
|
|
("KEEP".to_string(), ev(20, 0.5)),
|
|
])
|
|
.unwrap();
|
|
|
|
assert!(table.splits_for("REV").is_empty());
|
|
assert_eq!(table.splits_for("KEEP").len(), 1);
|
|
assert_eq!(table.len(), 1);
|
|
assert_eq!(table.event_count(), 1);
|
|
}
|