//! Unit tests for CPU profiling module

#[cfg(test)]
mod tests {
    use super::super::*;
    use std::time::Duration;

    #[test]
    fn test_cpu_profiler_creation() {
        let profiler = CPUProfiler::new();
        // CPUProfiler is a Zero-Sized Type, verify it exists (can be referenced)
        let _ = &profiler;
    }

    #[test]
    fn test_cpu_benchmark_basic() {
        let profiler = CPUProfiler::new();
        let result = profiler.run_benchmark(1, Duration::from_secs(1));

        assert!(result.is_ok());
        let benchmark = result.unwrap();

        // Verify single thread results exist
        assert!(benchmark.single_thread.combined_score > 0.0);
        assert!(benchmark.single_thread.integer_score > 0.0);
        assert!(benchmark.single_thread.float_score > 0.0);
    }

    #[test]
    fn test_cpu_benchmark_multi_thread() {
        let profiler = CPUProfiler::new();
        let result = profiler.run_benchmark(2, Duration::from_secs(1));

        if let Ok(benchmark) = result {
            // Multi-thread results may not exist on single-core systems
            if let Some(multi) = benchmark.multi_thread {
                assert!(multi.threads >= 1);
                assert!(multi.total_score > 0.0);
            }
        }
    }

    #[test]
    fn test_single_thread_performance() {
        let result = measure_single_thread_performance();
        assert!(result.is_ok());

        let perf = result.unwrap();
        assert!(perf.combined_score > 0.0);
        // Use as_millis() instead of as_secs() - benchmark may complete in sub-second time
        assert!(perf.duration.as_millis() > 0);
    }

    #[test]
    fn test_cache_misses_measurement() {
        let result = measure_cache_misses();
        assert!(result.is_ok());

        let cache = result.unwrap();
        // Cache miss rates should be between 0 and 1
        assert!(cache.l1_miss_rate >= 0.0 && cache.l1_miss_rate <= 1.0);
        assert!(cache.l2_miss_rate >= 0.0 && cache.l2_miss_rate <= 1.0);
    }

    #[test]
    fn test_cpu_topology_detection() {
        let result = topology::detect_cpu_topology();
        assert!(result.is_ok());

        let topo = result.unwrap();
        assert!(topo.cores > 0);
        assert!(topo.threads >= topo.cores);
    }

    #[test]
    fn test_run_cpu_benchmark_convenience() {
        let result = run_cpu_benchmark(1, Duration::from_secs(1));
        assert!(result.is_ok());
    }
}
