Skip to content

Commit 55ec18d

Browse files
Евгений БлиновЕвгений Блинов
authored andcommitted
Add tests for percentile edge cases, JSON serialization, and Scenario
addition behavior
1 parent 0b3d4ab commit 55ec18d

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

tests/units/test_benchmark_result.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ def test_percentile_inf_raises(self) -> None:
200200
with pytest.raises(ValueError, match='percentile'):
201201
result.percentile(float('inf'))
202202

203+
def test_percentile_neg_inf_raises(self) -> None:
204+
result = make_result((1.0, 2.0, 3.0))
205+
with pytest.raises(ValueError, match='percentile'):
206+
result.percentile(float('-inf'))
207+
208+
def test_percentile_zero_float_raises(self) -> None:
209+
result = make_result((1.0, 2.0, 3.0))
210+
with pytest.raises(ValueError, match='percentile'):
211+
result.percentile(0.0)
212+
203213
def test_percentile_preserves_fsum_mean(self) -> None:
204214
durations = tuple(0.1 * i for i in range(1, 11))
205215
result = make_result(durations)
@@ -409,3 +419,23 @@ def test_from_json_not_dict_raises(self) -> None:
409419
payload = json.dumps([1, 2, 3])
410420
with pytest.raises(ValueError, match='JSON must be an object'):
411421
BenchmarkResult.from_json(payload)
422+
423+
def test_from_json_inf_round_trip(self) -> None:
424+
# Python's json module handles Infinity via allow_nan=True (default)
425+
result = make_result((float('inf'), 1.0))
426+
restored = BenchmarkResult.from_json(result.to_json())
427+
assert math.isinf(restored.durations[0])
428+
assert restored.durations[1] == 1.0
429+
430+
def test_from_json_bool_duration_converts_to_float(self) -> None:
431+
# JSON true/false → Python True/False → float(True)=1.0, float(False)=0.0
432+
payload = json.dumps({'durations': [True, False], 'is_primary': True})
433+
restored = BenchmarkResult.from_json(payload)
434+
assert restored.durations == (1.0, 0.0)
435+
436+
def test_from_json_negative_durations_accepted(self) -> None:
437+
# negative durations can occur with non-monotonic timers; no validation
438+
payload = json.dumps({'durations': [-0.1, 0.2], 'is_primary': True})
439+
restored = BenchmarkResult.from_json(payload)
440+
assert restored.mean == pytest.approx(0.05)
441+
assert restored.best == -0.1

tests/units/test_scenario.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ def fake_timer() -> float:
193193
assert tick[0] == 10
194194
# only the 3 measured durations should be stored
195195
assert len(result.durations) == 3
196+
# each measured interval: end - start = 1 (timer increments by 1 each call)
197+
assert result.durations == pytest.approx((1.0, 1.0, 1.0))
196198

197199
def test_run_result_scenario_is_self(self) -> None:
198200
s = Scenario(lambda: None, name='s', number=5)
@@ -296,6 +298,11 @@ def test_add_unknown_type_returns_not_implemented(self) -> None:
296298
result = s.__add__(42) # type: ignore[arg-type]
297299
assert result is NotImplemented
298300

301+
def test_radd_unknown_type_returns_not_implemented(self) -> None:
302+
s = Scenario(lambda: None, name='s')
303+
result = s.__radd__(42) # type: ignore[arg-type]
304+
assert result is NotImplemented
305+
299306
def test_radd_scenario_scenario(self) -> None:
300307
s1 = Scenario(lambda: None, name='s1')
301308
s2 = Scenario(lambda: None, name='s2')

0 commit comments

Comments
 (0)