ppflib 0.1.0

Advanced computational library for Physics-Prime Factorization (PPF): quantum mechanics through number theory, featuring Sign Prime (-1), state space collapse, topological analysis, and IOT geometric realizations
Documentation
//! Integration tests for PPF library
//!
//! These tests verify that the core PPF framework components work together
//! correctly and demonstrate the key mathematical properties.

use ppflib::core::*;

#[test]
fn test_basic_ppf_framework() -> Result<(), Box<dyn std::error::Error>> {
    // Test Sign Prime
    let sign_prime = SignPrime::new();
    assert_eq!(sign_prime.value(), -1);
    assert!(sign_prime.is_sign_prime());

    // Test P-prime detection
    assert!(is_p_prime(-1));  // Sign Prime
    assert!(is_p_prime(2));   // Magnitude Prime
    assert!(is_p_prime(7));   // Magnitude Prime
    assert!(!is_p_prime(4));  // Not prime
    assert!(!is_p_prime(-2)); // Not a P-prime

    Ok(())
}

#[test]
fn test_factorization_state_spaces() -> Result<(), Box<dyn std::error::Error>> {
    // Test positive integer state space
    let s6 = FactorizationStateSpace::new(6)?;
    assert_eq!(s6.value(), 6);
    assert_eq!(s6.size(), 2); // {2×3, (-2)×(-3)}
    assert!(s6.is_classical());
    assert!(!s6.is_quantum());

    // Test negative integer state space  
    let s_neg6 = FactorizationStateSpace::new(-6)?;
    assert_eq!(s_neg6.value(), -6);
    assert_eq!(s_neg6.size(), 3); // {(-1)×2×3, (-2)×3, 2×(-3)}
    assert!(!s_neg6.is_classical());
    assert!(s_neg6.is_quantum());

    // Verify theoretical size formula: |S(n)| = 2^(k-1)
    assert!(s6.verify_size()?);
    assert!(s_neg6.verify_size()?);

    Ok(())
}

#[test]
fn test_quantum_collapse() -> Result<(), Box<dyn std::error::Error>> {
    // Test the fundamental quantum collapse: (-a) × (-b) = +ab
    let s_neg2 = FactorizationStateSpace::new(-2)?;
    let s_neg3 = FactorizationStateSpace::new(-3)?;
    
    // Both inputs are quantum (negative)
    assert!(s_neg2.is_quantum());
    assert!(s_neg3.is_quantum());
    
    // Multiply: (-2) × (-3) = 6
    let result = s_neg2.multiply(&s_neg3)?;
    
    // Result is classical (positive)
    assert_eq!(result.result_space.value(), 6);
    assert!(result.result_space.is_classical());
    
    // Collapse should have occurred
    assert!(result.collapse_info.collapsed);
    assert_eq!(result.collapse_info.operation_type, OperationType::QuantumQuantum);
    
    // State space size should be reduced (collapse)
    let max_input_size = s_neg2.size().max(s_neg3.size());
    assert!(result.result_space.size() <= max_input_size);
    assert!(result.collapse_info.collapse_ratio <= 1.0);

    Ok(())
}

#[test]
fn test_classical_operations() -> Result<(), Box<dyn std::error::Error>> {
    // Test classical × classical = classical (no collapse)
    let s2 = FactorizationStateSpace::new(2)?;
    let s3 = FactorizationStateSpace::new(3)?;
    
    let result = s2.multiply(&s3)?;
    
    assert_eq!(result.result_space.value(), 6);
    assert!(!result.collapse_info.collapsed);
    assert_eq!(result.collapse_info.operation_type, OperationType::ClassicalClassical);

    Ok(())
}

#[test]
fn test_mixed_operations() -> Result<(), Box<dyn std::error::Error>> {
    // Test classical × quantum = quantum (no collapse)
    let s2 = FactorizationStateSpace::new(2)?;
    let s_neg3 = FactorizationStateSpace::new(-3)?;
    
    let result = s2.multiply(&s_neg3)?;
    
    assert_eq!(result.result_space.value(), -6);
    assert!(result.result_space.is_quantum());
    assert!(!result.collapse_info.collapsed);
    assert_eq!(result.collapse_info.operation_type, OperationType::ClassicalQuantum);

    Ok(())
}

#[test]
fn test_sign_operations() -> Result<(), Box<dyn std::error::Error>> {
    // Test sign flip operations
    let s6 = FactorizationStateSpace::new(6)?;
    let s_neg6 = s6.sign_flip()?;
    
    assert_eq!(s_neg6.value(), -6);
    assert!(s_neg6.is_quantum());
    
    // Flip back
    let s6_again = s_neg6.sign_flip()?;
    assert_eq!(s6_again.value(), 6);
    assert!(s6_again.is_classical());

    Ok(())
}

#[test]
fn test_power_operations() -> Result<(), Box<dyn std::error::Error>> {
    let s_neg2 = FactorizationStateSpace::new(-2)?;
    
    // Even power causes collapse: (-2)^2 = 4
    let even_power = s_neg2.power(2)?;
    assert_eq!(even_power.result_space.value(), 4);
    assert!(even_power.collapse_info.collapsed);
    assert!(even_power.result_space.is_classical());
    
    // Odd power preserves quantum: (-2)^3 = -8
    let odd_power = s_neg2.power(3)?;
    assert_eq!(odd_power.result_space.value(), -8);
    assert!(!odd_power.collapse_info.collapsed);
    assert!(odd_power.result_space.is_quantum());
    
    // Zero power gives unity
    let zero_power = s_neg2.power(0)?;
    assert_eq!(zero_power.result_space.value(), 1);

    Ok(())
}

#[test]
fn test_superposition_analysis() -> Result<(), Box<dyn std::error::Error>> {
    // Analyze classical state
    let s6 = FactorizationStateSpace::new(6)?;
    let classical_analysis = s6.analyze_superposition();
    
    assert!(classical_analysis.is_classical);
    assert!(!classical_analysis.is_quantum);
    assert_eq!(classical_analysis.sign_prime_factorizations, 0);
    
    // Analyze quantum state
    let s_neg6 = FactorizationStateSpace::new(-6)?;
    let quantum_analysis = s_neg6.analyze_superposition();
    
    assert!(!quantum_analysis.is_classical);
    assert!(quantum_analysis.is_quantum);
    assert!(quantum_analysis.sign_prime_factorizations > 0);
    assert!(quantum_analysis.state_space_size > classical_analysis.state_space_size);
    assert!(quantum_analysis.entropy() > classical_analysis.entropy());
    assert!(quantum_analysis.superposition_measure > 0.0);

    Ok(())
}

#[test]
fn test_p_prime_iteration() -> Result<(), Box<dyn std::error::Error>> {
    // Test P-prime iteration
    let first_p_primes: Vec<i64> = PPrimeIterator::all_p_primes()
        .take(6)
        .map(|p| p.value())
        .collect();
    
    assert_eq!(first_p_primes, vec![-1, 2, 3, 5, 7, 11]);
    
    // Test magnitude primes only
    let magnitude_primes: Vec<i64> = PPrimeIterator::magnitude_primes()
        .take(5)
        .map(|p| p.value())
        .collect();
    
    assert_eq!(magnitude_primes, vec![2, 3, 5, 7, 11]);

    Ok(())
}

#[test]
fn test_factorization_details() -> Result<(), Box<dyn std::error::Error>> {
    // Test individual factorizations
    let f1 = PFactorization::new(vec![2, 3])?;
    assert_eq!(f1.value(), 6);
    assert!(f1.is_canonical());
    assert!(!f1.has_sign_prime());
    assert_eq!(f1.complexity(), 2);
    
    let f2 = PFactorization::new(vec![-1, 2, 3])?;
    assert_eq!(f2.value(), -6);
    assert!(f2.is_canonical());
    assert!(f2.has_sign_prime());
    assert_eq!(f2.complexity(), 2); // Sign prime doesn't contribute to complexity
    
    let f3 = PFactorization::new(vec![-2, 3])?;
    assert_eq!(f3.value(), -6);
    assert!(f3.is_canonical());
    assert!(!f3.has_sign_prime());
    assert_eq!(f3.complexity(), 2);

    Ok(())
}

#[test]
fn test_edge_cases() -> Result<(), Box<dyn std::error::Error>> {
    // Test unity
    let s1 = FactorizationStateSpace::new(1)?;
    assert_eq!(s1.size(), 1);
    assert!(s1.is_classical());
    
    let s_neg1 = FactorizationStateSpace::new(-1)?;
    assert_eq!(s_neg1.size(), 1);
    assert!(s_neg1.is_quantum());
    
    // Test primes
    let s7 = FactorizationStateSpace::new(7)?;
    assert_eq!(s7.size(), 1);
    
    let s_neg7 = FactorizationStateSpace::new(-7)?;
    assert_eq!(s_neg7.size(), 2); // {-1×7, -7}
    
    // Test zero (should fail)
    assert!(FactorizationStateSpace::new(0).is_err());

    Ok(())
}

#[test]
fn test_mathematical_properties() -> Result<(), Box<dyn std::error::Error>> {
    // Test that positive integers always have smaller or equal state spaces
    // compared to their negative counterparts
    for n in [2, 3, 5, 6, 7, 10, 12, 15, 30] {
        let s_pos = FactorizationStateSpace::new(n)?;
        let s_neg = FactorizationStateSpace::new(-n)?;
        
        // Negative integers should have larger or equal state spaces
        assert!(s_neg.size() >= s_pos.size(), 
               "S(-{}) size {} should be >= S({}) size {}", 
               n, s_neg.size(), n, s_pos.size());
    }

    Ok(())
}

#[test]
fn test_ppf_fundamental_equation() -> Result<(), Box<dyn std::error::Error>> {
    // Test the fundamental PPF equation: Past × Future = Present
    // In mathematical terms: (-a) × (-b) = +ab
    
    for (a, b) in [(2, 3), (3, 5), (5, 7), (2, 7)] {
        let past = FactorizationStateSpace::new(-a)?;      // Unobserved past
        let future = FactorizationStateSpace::new(-b)?;    // Unobserved future
        
        let present = past.multiply(&future)?;             // Observed present
        
        assert_eq!(present.result_space.value(), a * b);
        assert!(present.result_space.is_classical());
        assert!(present.collapse_info.collapsed);
        
        println!("Past({}) × Future({}) = Present({})", -a, -b, a * b);
    }

    Ok(())
}