Files
foxhunt/crates/ml/tests/ppo_reward_normalizer_tests.rs
jgrusewski 9c3d741a08 refactor: restructure repo — crates/, bin/, testing/ layout
Move 17 library crates into crates/, CLI binary into bin/fxt,
consolidate 10 test crates into testing/, split config crate
from deployment config files.

Root directory reduced from 38+ to ~17 directories.
All Cargo.toml paths and build.rs proto refs updated.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 11:56:00 +01:00

88 lines
2.6 KiB
Rust

//! Test suite for PPO RewardNormalizer
//!
//! TDD Red Phase: Tests written FIRST before implementation
//! These tests define the expected behavior of the RewardNormalizer module
// Import from PPO module (will fail until implementation exists)
use ml::ppo::reward_normalizer::RewardNormalizer;
#[test]
fn test_reward_normalizer_welford() {
// Test Welford's algorithm correctness with known statistics
let mut normalizer = RewardNormalizer::new();
// Add values: [10, 20, 30, 40, 50]
let values = vec![10.0, 20.0, 30.0, 40.0, 50.0];
for &value in &values {
normalizer.update(value);
}
// Expected mean: 30.0
// Expected variance: 200.0 (population variance)
// Expected std: 14.142
let (mean, std) = normalizer.get_stats();
assert!((mean - 30.0).abs() < 1e-6, "Mean should be 30.0, got {}", mean);
assert!(
(std - 14.142135623730951).abs() < 1e-6,
"Std should be ~14.142, got {}",
std
);
// Test normalization: (40 - 30) / 14.142 ≈ 0.707
let normalized = normalizer.normalize(40.0);
assert!(
(normalized - 0.7071067811865475).abs() < 1e-6,
"Normalized value should be ~0.707, got {}",
normalized
);
// Verify count
assert_eq!(normalizer.count(), 5, "Count should be 5");
}
#[test]
fn test_reward_normalizer_numerical_stability() {
// Test with extreme values to ensure numerical stability
let mut normalizer = RewardNormalizer::new();
// Add extreme positive and negative values
let extreme_values = vec![-1000.0, -500.0, 0.0, 500.0, 1000.0];
for &value in &extreme_values {
normalizer.update(value);
}
let (mean, std) = normalizer.get_stats();
// Mean should be 0.0 (symmetric distribution)
assert!(
mean.abs() < 1e-6,
"Mean should be ~0.0 for symmetric values, got {}",
mean
);
// Std should be ~707.1 (for uniform extreme distribution)
assert!(
std > 700.0 && std < 710.0,
"Std should be ~707.1, got {}",
std
);
// Normalize a new extreme value
let normalized_extreme = normalizer.normalize(1000.0);
// Should be bounded: 1000 is 1 std above mean
assert!(
normalized_extreme > 1.0 && normalized_extreme < 2.0,
"Normalized extreme should be ~1.58, got {}",
normalized_extreme
);
// Test with very small epsilon (no division by zero)
normalizer.update(0.0);
let normalized_zero = normalizer.normalize(0.0);
assert!(
!normalized_zero.is_nan() && !normalized_zero.is_infinite(),
"Normalized value should not be NaN or Inf"
);
}