Files
foxhunt/tests/fixtures/helpers.rs
jgrusewski 030a15ee05 🔧 Emergency Fix: Resolve catastrophic _i32 suffix corruption (463→0 errors)
- Fixed systematic array indexing corruption: [0_i32] → [0]
- Fixed numeric literal suffixes across 835 files
- Fixed iterator patterns on RwLockReadGuard (.iter() required)
- Fixed float type annotations (365.25_f64 for sqrt)
- Fixed missing semicolons in position manager
- Fixed reference dereferencing in data loader

Root cause: Mass refactoring incorrectly added _i32 suffixes to array indices
Impact: Complete compilation failure (463 errors)
Resolution: Automated regex + targeted fixes
Result: 100% compilation success (0 errors)

Validated: cargo check --workspace passes
Ready for: Production deployment
2025-10-10 23:05:26 +02:00

95 lines
2.1 KiB
Rust

/// Decimal Conversion Helpers for Tests
///
/// Provides utilities to safely convert between f64 and Decimal types in test code.
///
/// Production code should use Decimal throughout, but tests often need to work with
/// floating point values for convenience.
use rust_decimal::Decimal;
pub use rust_decimal_macros::dec;
/// Trait to convert f64 to Decimal safely
pub trait ToDecimal {
fn to_decimal(self) -> Decimal;
}
impl ToDecimal for f64 {
/// Convert f64 to Decimal, returning ZERO on failure
fn to_decimal(self) -> Decimal {
Decimal::from_f64_retain(self).unwrap_or(Decimal::ZERO)
}
}
impl ToDecimal for i32 {
/// Convert i32 to Decimal safely
fn to_decimal(self) -> Decimal {
Decimal::from(self)
}
}
impl ToDecimal for i64 {
/// Convert i64 to Decimal safely
fn to_decimal(self) -> Decimal {
Decimal::from(self)
}
}
impl ToDecimal for u32 {
/// Convert u32 to Decimal safely
fn to_decimal(self) -> Decimal {
Decimal::from(self)
}
}
impl ToDecimal for u64 {
/// Convert u64 to Decimal safely
fn to_decimal(self) -> Decimal {
Decimal::from(self)
}
}
/// Convert f64 to Decimal (convenience function)
pub fn decimal(f: f64) -> Decimal {
f.to_decimal()
}
/// Convert f64 to Decimal with explicit error message
pub fn decimal_or_panic(f: f64, msg: &str) -> Decimal {
Decimal::from_f64_retain(f).expect(msg)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_f64_to_decimal() {
let d = 100.5.to_decimal();
assert_eq!(d.to_string(), "100.5");
let d2 = decimal(200.75);
assert_eq!(d2.to_string(), "200.75");
}
#[test]
fn test_integer_to_decimal() {
let d1 = 100i32.to_decimal();
assert_eq!(d1, Decimal::from(100));
let d2 = 1000u64.to_decimal();
assert_eq!(d2, Decimal::from(1000));
}
#[test]
fn test_nan_returns_zero() {
let d = f64::NAN.to_decimal();
assert_eq!(d, Decimal::ZERO);
}
#[test]
fn test_infinity_returns_zero() {
let d = f64::INFINITY.to_decimal();
assert_eq!(d, Decimal::ZERO);
}
}