#[cfg(test)]
mod tests {
    use std::process::Command;

    #[test]
    fn test_pi_10_decimals() {
        let output = Command::new("./print_hej")
            .arg("10")
            .output()
            .expect("Kunde inte köra program");
        
        let result = String::from_utf8_lossy(&output.stdout).trim().to_string();
        let expected = "3.1415926535";
        
        assert_eq!(result, expected, "Fel resultat för 10 decimaler");
    }

    #[test]
    fn test_pi_5_decimals() {
        let output = Command::new("./print_hej")
            .arg("5")
            .output()
            .expect("Kunde inte köra program");
        
        let result = String::from_utf8_lossy(&output.stdout).trim().to_string();
        let expected = "3.14159";
        
        assert_eq!(result, expected, "Fel resultat för 5 decimaler");
    }

    #[test]
    fn test_pi_1_decimal() {
        let output = Command::new("./print_hej")
            .arg("1")
            .output()
            .expect("Kunde inte köra program");
        
        let result = String::from_utf8_lossy(&output.stdout).trim().to_string();
        let expected = "3.1";
        
        assert_eq!(result, expected, "Fel resultat för 1 decimal");
    }

    #[test]
    fn test_pi_default() {
        let output = Command::new("./print_hej")
            .output()
            .expect("Kunde inte köra program");
        
        let result = String::from_utf8_lossy(&output.stdout).trim().to_string();
        
        // Läs facit
        let facit = std::fs::read_to_string("../facit.txt")
            .expect("Kunde inte läsa facit");
        let expected: String = facit.chars().take(103).collect();
        
        assert_eq!(result, expected, "Fel resultat för default (100 decimaler)");
    }

    #[test]
    fn test_invalid_input() {
        let output = Command::new("./print_hej")
            .arg("abc")
            .output()
            .expect("Kunde inte köra program");
        
        let result = String::from_utf8_lossy(&output.stdout).trim().to_string();
        
        // Ska använda default (100 decimaler)
        assert!(result.starts_with("3.1415926535"), "Hanterar inte ogiltig input korrekt");
    }

    #[test]
    fn test_calculate_pi_exact() {
        let pi = calculate_pi_exact(10);
        let result = format_pi(&pi, 10);
        
        let expected = "3.1415926535";
        assert_eq!(result, expected, "calculate_pi_exact gav fel resultat");
    }

    #[test]
    fn test_arctan_exact() {
        let scale = pow10(20);
        let atan1_5 = arctan_exact(5, &scale);
        
        // Kontrollera att resultatet inte är tomt
        assert!(!is_zero(&atan1_5), "arctan(1/5) ska inte vara noll");
    }

    #[test]
    fn test_format_pi() {
        // Skapa ett pi-värde: 31415926535... (skalat med 10^10)
        let mut pi = vec![0u32; 2];
        pi[0] = 3;
        pi[1] = 141592653;
        
        let result = format_pi(&pi, 10);
        let expected = "3.141592653";
        
        assert_eq!(result, expected, "format_pi gav fel resultat");
    }

    #[test]
    fn test_add_big() {
        let a = vec![1, 500_000_000];
        let b = vec![2, 600_000_000];
        
        let result = add_big(&a, &b);
        
        // 1*10^9 + 500000000 + 2*10^9 + 600000000 = 4100000000
        // = 4 * 10^9 + 100000000
        assert_eq!(result[0], 4);
        assert_eq!(result[1], 100_000_000);
    }

    #[test]
    fn test_subtract_big() {
        let a = vec![5, 700_000_000];
        let b = vec![2, 300_000_000];
        
        let result = subtract_big(&a, &b);
        
        // 5*10^9 + 700000000 - 2*10^9 - 300000000 = 3400000000
        // = 3 * 10^9 + 400000000
        assert_eq!(result[0], 3);
        assert_eq!(result[1], 400_000_000);
    }

    #[test]
    fn test_multiply() {
        let a = vec![1, 500_000_000];
        
        let result = multiply(&a, 2);
        
        // 1*10^9 + 500000000 * 2 = 3000000000
        // = 3 * 10^9
        assert_eq!(result[0], 3);
        assert_eq!(result[1], 0);
    }

    #[test]
    fn test_divide_big() {
        let a = vec![1, 0]; // 10^9
        
        let result = divide_big(&a, 2);
        
        // 10^9 / 2 = 500000000
        assert_eq!(result[0], 0);
        assert_eq!(result[1], 500_000_000);
    }
}