|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// © James Ross Ω FLYING•ROBOTS <https://github.com/flyingrobots> |
| 3 | + |
| 4 | +//! Comprehensive tests for NaN canonicalization and determinism. |
| 5 | +//! |
| 6 | +//! This test suite validates that `F32Scalar` correctly implements the strict |
| 7 | +//! determinism policy by: |
| 8 | +//! 1. Canonicalizing all NaN bit patterns (signaling, quiet, payload) to a |
| 9 | +//! single standard Positive Quiet NaN (`0x7fc00000`). |
| 10 | +//! 2. Preserving Infinities (distinct from NaNs). |
| 11 | +//! 3. Ensuring reflexivity (`x == x`) holds for the canonicalized NaNs. |
| 12 | +
|
| 13 | +use rmg_core::math::scalar::F32Scalar; |
| 14 | +use rmg_core::math::Scalar; |
| 15 | + |
| 16 | +/// Verifies that various classes of NaN values are correctly canonicalized. |
| 17 | +/// |
| 18 | +/// This includes: |
| 19 | +/// - Specific edge cases (smallest/largest mantissas). |
| 20 | +/// - Signaling vs Quiet NaNs. |
| 21 | +/// - Positive vs Negative NaNs. |
| 22 | +/// - NaNs with arbitrary payloads. |
| 23 | +/// |
| 24 | +/// It also performs a sweep of low and high mantissa bits to catch potential |
| 25 | +/// off-by-one errors in bitmask logic. |
| 26 | +#[test] |
| 27 | +fn test_comprehensive_nan_coverage() { |
| 28 | + // IEEE 754 float32: |
| 29 | + // Sign: 1 bit (31) |
| 30 | + // Exponent: 8 bits (23-30) -> All 1s for NaN/Inf (0xFF) |
| 31 | + // Mantissa: 23 bits (0-22) -> Non-zero for NaN (Zero for Inf) |
| 32 | + |
| 33 | + let exponent_mask = 0x7F800000; |
| 34 | + let mantissa_mask = 0x007FFFFF; |
| 35 | + let sign_mask = 0x80000000u32; |
| 36 | + |
| 37 | + // 1. Verify specific edge case NaNs |
| 38 | + let patterns = vec![ |
| 39 | + 0x7F800001, // Smallest mantissa, positive |
| 40 | + 0x7FFFFFFF, // Largest mantissa, positive |
| 41 | + 0xFF800001, // Smallest mantissa, negative |
| 42 | + 0xFFFFFFFF, // Largest mantissa, negative |
| 43 | + 0x7FC00000, // Canonical qNaN positive |
| 44 | + 0xFFC00000, // Canonical qNaN negative |
| 45 | + 0x7FA00000, // Some payload |
| 46 | + 0x7F80DEAD, // Dead beef payload |
| 47 | + ]; |
| 48 | + |
| 49 | + for bits in patterns { |
| 50 | + let f = f32::from_bits(bits); |
| 51 | + // Pre-condition: verify our assumption that these ARE NaNs according to Rust |
| 52 | + assert!(f.is_nan(), "Rust did not identify {:#x} as NaN", bits); |
| 53 | + |
| 54 | + let scalar = F32Scalar::new(f); |
| 55 | + let out_bits = scalar.to_f32().to_bits(); |
| 56 | + |
| 57 | + assert_eq!( |
| 58 | + out_bits, 0x7fc00000, |
| 59 | + "Input NaN {:#x} was not canonicalized to 0x7fc00000, got {:#x}", |
| 60 | + bits, out_bits |
| 61 | + ); |
| 62 | + |
| 63 | + // Explicitly test reflexivity for the canonicalized NaN |
| 64 | + assert_eq!( |
| 65 | + scalar, scalar, |
| 66 | + "Reflexivity failed for canonicalized NaN from input {:#x}", |
| 67 | + bits |
| 68 | + ); |
| 69 | + assert_eq!( |
| 70 | + scalar.cmp(&scalar), |
| 71 | + std::cmp::Ordering::Equal, |
| 72 | + "Ordering reflexivity failed for canonicalized NaN from input {:#x}", |
| 73 | + bits |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + // 2. Fuzz / Sweep a range of mantissas |
| 78 | + // We can't check all 2^23 * 2 NaNs, but we can check a lot. |
| 79 | + // Let's check the first 1000 and last 1000 mantissas for both signs. |
| 80 | + |
| 81 | + let signs = [0u32, sign_mask]; |
| 82 | + |
| 83 | + for sign in signs { |
| 84 | + // Mantissa cannot be 0 (that's Infinity) |
| 85 | + // Loop 1..1000 |
| 86 | + for m in 1..1000 { |
| 87 | + let bits = sign | exponent_mask | m; |
| 88 | + let f = f32::from_bits(bits); |
| 89 | + let s = F32Scalar::new(f); |
| 90 | + assert_eq!( |
| 91 | + s.to_f32().to_bits(), |
| 92 | + 0x7fc00000, |
| 93 | + "Failed low mantissa {:#x}", |
| 94 | + bits |
| 95 | + ); |
| 96 | + } |
| 97 | + // Loop max-1000..max |
| 98 | + for m in (mantissa_mask - 1000)..=mantissa_mask { |
| 99 | + let bits = sign | exponent_mask | m; |
| 100 | + let f = f32::from_bits(bits); |
| 101 | + let s = F32Scalar::new(f); |
| 102 | + assert_eq!( |
| 103 | + s.to_f32().to_bits(), |
| 104 | + 0x7fc00000, |
| 105 | + "Failed high mantissa {:#x}", |
| 106 | + bits |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/// Verifies that Infinity values are preserved and NOT canonicalized. |
| 113 | +/// |
| 114 | +/// The determinism policy requires finite numbers and infinities to be preserved |
| 115 | +/// (modulo -0.0 normalization), while only NaNs are collapsed. |
| 116 | +#[test] |
| 117 | +fn test_infinity_preservation() { |
| 118 | + // Ensure we didn't accidentally canonicalize Infinity |
| 119 | + let pos_inf = f32::from_bits(0x7F800000); |
| 120 | + let neg_inf = f32::from_bits(0xFF800000); |
| 121 | + |
| 122 | + assert!(!pos_inf.is_nan()); |
| 123 | + assert!(!neg_inf.is_nan()); |
| 124 | + |
| 125 | + let s_pos = F32Scalar::new(pos_inf); |
| 126 | + let s_neg = F32Scalar::new(neg_inf); |
| 127 | + |
| 128 | + assert_eq!(s_pos.to_f32().to_bits(), 0x7F800000); |
| 129 | + assert_eq!(s_neg.to_f32().to_bits(), 0xFF800000); |
| 130 | +} |
0 commit comments