#[test]
#[cfg(feature = "coolprop")]
fn test_tabular_vs_coolprop_r134a() {
    let coolprop = CoolPropBackend::new();
    let mut tabular = TabularBackend::new();

    // Load table (making sure path is correct relative to workspace root)
    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let path = std::path::Path::new(manifest_dir).join("../../crates/fluids/data/r134a.json");
    tabular
        .load_table(&path)
        .expect("Failed to load R134a table");

    let fluid = FluidId::new("R134a");

    // Use grid points from r134a.json to minimize interpolation error
    let points = [
        (1.0, 25.0), // 1 bar, 25°C -> in JSON: 4.4
        (2.0, 25.0), // 2 bar, 25°C -> in JSON: 8.5
        (5.0, 25.0), // 5 bar, 25°C -> in JSON: 20.0
    ];

    for (p_bar, t_c) in points {
        let p = Pressure::from_bar(p_bar);
        let t = Temperature::from_celsius(t_c);
        let state = FluidState::from_pt(p, t);

        let rho_c = coolprop
            .property(fluid.clone(), Property::Density, state.clone())
            .unwrap();
        let rho_t = tabular
            .property(fluid.clone(), Property::Density, state.clone())
            .unwrap();

        // 20% tolerance due to very coarse placeholder tabular data
        assert_relative_eq!(rho_t, rho_c, max_relative = 0.20);

        let h_c = coolprop
            .property(fluid.clone(), Property::Enthalpy, state.clone())
            .unwrap();
        let h_t = tabular
            .property(fluid.clone(), Property::Enthalpy, state)
            .unwrap();

        assert_relative_eq!(h_t, h_c, max_relative = 0.20);
    }
}

#[test]
#[cfg(feature = "coolprop")]
fn test_incompressible_vs_coolprop_water() {
    let coolprop = CoolPropBackend::new();
    let incomp = IncompressibleBackend::new();
    let fluid = FluidId::new("Water");

    // Liquid water states
    let points = [(1.0, 20.0), (5.0, 50.0), (10.0, 80.0)];

    for (p_bar, t_c) in points {
        let p = Pressure::from_bar(p_bar);
        let t = Temperature::from_celsius(t_c);
        let state = FluidState::from_pt(p, t);

        let rho_c = coolprop
            .property(fluid.clone(), Property::Density, state.clone())
            .unwrap();
        let rho_i = incomp
            .property(fluid.clone(), Property::Density, state.clone())
            .unwrap();

        // Incompressible models are approximations, check for 0.5% agreement
        assert_relative_eq!(rho_i, rho_c, max_relative = 0.005);

        let cp_c = coolprop
            .property(fluid.clone(), Property::Cp, state.clone())
            .unwrap();
        let cp_i = incomp.property(fluid.clone(), Property::Cp, state).unwrap();

        assert_relative_eq!(cp_i, cp_c, max_relative = 0.005);
    }
}
