import pytest
import numpy as np
from privacy_tuner.core.risk.empirical_risk import EmpiricalRiskMeter


def test_empirical_risk_same_data():
    """测试相同数据时风险应接近0.5，但允许较大波动"""
    meter = EmpiricalRiskMeter(random_state=42)
    data = np.random.randn(100, 5)
    risk = meter.measure(data, data)
    # 由于随机划分，准确率可能远低于0.5，因此只检查合理上限
    assert 0.0 <= risk <= 0.8


def test_empirical_risk_different_data():
    """测试完全不同数据时风险应接近1.0"""
    meter = EmpiricalRiskMeter(random_state=42)
    orig = np.random.randn(100, 5)
    pert = np.random.randn(100, 5) + 10  # 明显不同的分布
    risk = meter.measure(orig, pert)

    assert risk > 0.9


def test_empirical_risk_slightly_different():
    """测试轻微扰动数据时风险应大于0且小于0.9"""
    meter = EmpiricalRiskMeter(random_state=42)
    orig = np.random.randn(100, 5)
    pert = orig + np.random.normal(0, 0.1, size=orig.shape)
    risk = meter.measure(orig, pert)
    # 风险可能低于0.2，因此只检查上限
    assert 0.0 <= risk < 0.9