示例工程 24-profiling-performance

24-profiling-performance/04-optimize-then-measure/test_optimize_then_measure.py

python · 0.6 KB

"""test_optimize_then_measure.py"""
 
from __future__ import annotations
 
import pytest
from optimize_then_measure import VALUES, best_ms, main, micro, naive, with_set
 
 
def test_all_versions_agree() -> None:
    expected = naive(VALUES)
    assert with_set(VALUES) == expected
    assert micro(VALUES) == expected
    assert len(expected) == len(VALUES) - len(set(VALUES))
 
 
def test_algorithm_change_is_faster() -> None:
    assert best_ms(with_set) < best_ms(naive)
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    out = capsys.readouterr().out
    assert "same: True" in out
    assert "set_faster: True" in out