""" Test: Algorithm Comparison Verify that gradient routing outperforms baseline algorithms. """ import pytest from sim.experiments.runner import run_single_experiment @pytest.fixture def seed(): return 42 def test_gradient_outperforms_random(seed): """Test that gradient routing has better or equal PDR than random.""" gradient = run_single_experiment( routing="gradient", node_count=12, area_size=800, sim_time=100, seed=seed, ) random = run_single_experiment( routing="random", node_count=12, area_size=800, sim_time=100, seed=seed, ) print(f"\nGradient PDR: {gradient['pdr']:.2f}%") print(f"Random PDR: {random['pdr']:.2f}%") # Gradient should be at least as good as random (with small tolerance) assert gradient["pdr"] >= random["pdr"] - 5.0, ( f"Gradient ({gradient['pdr']}%) should outperform Random ({random['pdr']}%)" ) def test_all_algorithms_run(seed): """Test that all routing algorithms can run without errors.""" for routing in ["gradient", "flooding", "random"]: result = run_single_experiment( routing=routing, node_count=10, area_size=600, sim_time=100, seed=seed, ) assert result["pdr"] >= 0, f"{routing} should produce valid PDR" assert result["max_hop"] >= 0, f"{routing} should produce valid max_hop" def test_flooding_produces_more_hops(seed): """Test that flooding produces more hops due to broadcast nature.""" gradient = run_single_experiment( routing="gradient", node_count=12, area_size=800, sim_time=100, seed=seed, ) flooding = run_single_experiment( routing="flooding", node_count=12, area_size=800, sim_time=100, seed=seed, ) print(f"\nGradient max_hop: {gradient['max_hop']}") print(f"Flooding max_hop: {flooding['max_hop']}") # Flooding should have higher max hop due to multi-path forwarding assert flooding["max_hop"] >= gradient["max_hop"], ( "Flooding should produce more hops than gradient routing" ) if __name__ == "__main__": pytest.main([__file__, "-v", "-s"])