//! Unit tests for MAMBA-2 Hardware-Aware Optimizations //! //! Tests hardware capability detection, SIMD optimizations, and cache-aware operations. use ml::mamba::hardware_aware::HardwareCapabilities; #[test] fn test_hardware_capabilities_default() { let caps = HardwareCapabilities::default(); // Verify cache parameters are realistic assert_eq!( caps.cache_line_size, 64, "Standard cache line size is 64 bytes" ); assert!(caps.l1_cache_size > 0 && caps.l1_cache_size < caps.l2_cache_size); assert!(caps.l2_cache_size > 0 && caps.l2_cache_size < caps.l3_cache_size); assert!(caps.l3_cache_size > 0); // Verify SIMD width is reasonable assert!( caps.simd_width == 4 || caps.simd_width == 8 || caps.simd_width == 16, "SIMD width should be 4 (SSE), 8 (AVX2), or 16 (AVX-512)" ); // Verify CPU core count is sensible assert!( caps.num_cores > 0 && caps.num_cores <= 256, "Core count should be positive and reasonable" ); // Verify memory bandwidth is positive assert!( caps.memory_bandwidth_gbps > 0.0, "Memory bandwidth should be positive" ); } #[test] fn test_hardware_capabilities_clone() { let caps = HardwareCapabilities::default(); let cloned = caps.clone(); assert_eq!(caps.cache_line_size, cloned.cache_line_size); assert_eq!(caps.simd_width, cloned.simd_width); assert_eq!(caps.num_cores, cloned.num_cores); assert_eq!(caps.l1_cache_size, cloned.l1_cache_size); assert_eq!(caps.l2_cache_size, cloned.l2_cache_size); assert_eq!(caps.l3_cache_size, cloned.l3_cache_size); } #[test] fn test_hardware_capabilities_debug() { let caps = HardwareCapabilities::default(); let debug_str = format!("{:?}", caps); assert!(debug_str.contains("HardwareCapabilities")); assert!(debug_str.contains("cache_line_size")); assert!(debug_str.contains("simd_width")); } #[test] fn test_hardware_capabilities_simd_features() { let caps = HardwareCapabilities::default(); // At least one SIMD feature should be supported on modern hardware let has_simd = caps.supports_avx2 || caps.supports_avx512 || caps.supports_neon; // Log detected features (for debugging, not assertion) eprintln!("Detected SIMD features:"); eprintln!(" AVX2: {}", caps.supports_avx2); eprintln!(" AVX-512: {}", caps.supports_avx512); eprintln!(" NEON: {}", caps.supports_neon); // This is informational - we don't fail if no SIMD (might be old hardware) if !has_simd { eprintln!("⚠️ No SIMD features detected - performance may be limited"); } } #[test] fn test_hardware_capabilities_cache_hierarchy() { let caps = HardwareCapabilities::default(); // Verify cache hierarchy is logical assert!( caps.l1_cache_size < caps.l2_cache_size, "L1 cache should be smaller than L2" ); assert!( caps.l2_cache_size < caps.l3_cache_size, "L2 cache should be smaller than L3" ); // Verify cache sizes are power-of-2 aligned let is_power_of_2 = |n: usize| (n & (n - 1)) == 0 && n != 0; // L1/L2/L3 caches are typically power-of-2 multiples of KB assert!( is_power_of_2(caps.l1_cache_size / 1024) || caps.l1_cache_size % 1024 == 0, "L1 cache size should be reasonable" ); } #[test] fn test_hardware_capabilities_memory_bandwidth() { let caps = HardwareCapabilities::default(); // Memory bandwidth should be in a reasonable range // Modern DDR4: 20-40 GB/s, DDR5: 40-80 GB/s, LPDDR: 10-30 GB/s assert!( caps.memory_bandwidth_gbps >= 5.0 && caps.memory_bandwidth_gbps <= 200.0, "Memory bandwidth should be in realistic range (5-200 GB/s)" ); } #[test] fn test_hardware_capabilities_consistency() { let caps = HardwareCapabilities::default(); // If AVX-512 is supported, AVX2 should also be (backward compatibility) if caps.supports_avx512 { // Note: This may not always be true, but it's typical eprintln!("System has AVX-512, checking AVX2..."); eprintln!("AVX2 support: {}", caps.supports_avx2); } // NEON is ARM-specific, shouldn't coexist with AVX if caps.supports_neon { assert!( !caps.supports_avx2 && !caps.supports_avx512, "NEON (ARM) and AVX (x86) should not both be supported" ); } // x86 systems should have AVX or AVX2 on modern hardware #[cfg(target_arch = "x86_64")] { eprintln!("x86_64 platform detected"); // This is informational, not enforced } // ARM systems should have NEON #[cfg(target_arch = "aarch64")] { assert!(caps.supports_neon, "ARM platforms should have NEON support"); } }