Add affinity_parallelism_model/tests/test_core.py

This commit is contained in:
Mika 2026-03-21 17:02:40 +00:00
parent ef722eebd6
commit ea30ddb9bf

View file

@ -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)