From ea30ddb9bf28c4579ec7692c25d7d21f72035af5 Mon Sep 17 00:00:00 2001 From: Mika Date: Sat, 21 Mar 2026 17:02:40 +0000 Subject: [PATCH] Add affinity_parallelism_model/tests/test_core.py --- affinity_parallelism_model/tests/test_core.py | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 affinity_parallelism_model/tests/test_core.py diff --git a/affinity_parallelism_model/tests/test_core.py b/affinity_parallelism_model/tests/test_core.py new file mode 100644 index 0000000..0c40549 --- /dev/null +++ b/affinity_parallelism_model/tests/test_core.py @@ -0,0 +1,59 @@ +import pytest +import math +import random + +from affinity_parallelism_model import core + +@pytest.fixture(autouse=True) +def set_seed(): + random.seed(1234) + + +def test_simulate_effects_keys_and_types(): + result = core.simulate_effects(4, 'randomized') + # Check result type + assert isinstance(result, dict) + # Required keys + assert set(result.keys()) == {'bandwidth', 'retry_tail'} + # Type validation for fields + assert isinstance(result['bandwidth'], float) + assert isinstance(result['retry_tail'], float) + + +def test_simulate_effects_value_ranges(): + # Parallelism levels and affinity types to test + for parallelism, affinity in [(1, 'randomized'), (8, 'enforced')]: + result = core.simulate_effects(parallelism, affinity) + bandwidth = result['bandwidth'] + retry_tail = result['retry_tail'] + + # Bandwidth should be positive + assert bandwidth > 0.0 + # Retry tail should be finite and within reasonable bounds + assert -100.0 < retry_tail < 100.0 + + +def test_simulate_effects_invalid_inputs(): + # Negative parallelism level + with pytest.raises(ValueError): + core.simulate_effects(-5, 'randomized') + + # Invalid affinity value type + with pytest.raises(ValueError): + core.simulate_effects(4, 'unsupported_affinity') + + +def test_deterministic_behavior_with_seed(): + random.seed(999) + r1 = core.simulate_effects(4, 'randomized') + random.seed(999) + r2 = core.simulate_effects(4, 'randomized') + # Deterministic output when seed is fixed + assert r1 == r2 + + +def test_parallelism_scaling_trend(): + # Higher parallelism generally increases bandwidth + low = core.simulate_effects(2, 'enforced')['bandwidth'] + high = core.simulate_effects(16, 'enforced')['bandwidth'] + assert high >= low or math.isclose(high, low, rel_tol=1e-3)