69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
"""core 包测试。
|
|||
|
|
|
||
|
|
TODO(L0):实现后去掉 skip 标记。
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import numpy as np
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L0")
|
||
|
|
class TestRandom:
|
||
|
|
def test_get_rng_is_reproducible(self):
|
||
|
|
from mcm_toolkit.core import get_rng
|
||
|
|
|
||
|
|
assert get_rng(42).random() == get_rng(42).random()
|
||
|
|
|
||
|
|
def test_get_rng_passes_through_generator(self):
|
||
|
|
from mcm_toolkit.core import get_rng
|
||
|
|
|
||
|
|
g = np.random.default_rng(0)
|
||
|
|
assert get_rng(g) is g
|
||
|
|
|
||
|
|
def test_set_seed_makes_global_reproducible(self):
|
||
|
|
from mcm_toolkit.core import set_seed
|
||
|
|
|
||
|
|
set_seed(7)
|
||
|
|
a = np.random.default_rng(7).random()
|
||
|
|
set_seed(7)
|
||
|
|
b = np.random.default_rng(7).random()
|
||
|
|
assert a == b
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L0")
|
||
|
|
class TestValidate:
|
||
|
|
def test_check_same_length_raises_with_detail(self):
|
||
|
|
from mcm_toolkit.core import check_same_length
|
||
|
|
|
||
|
|
with pytest.raises(ValueError, match=r"2.*3|长度"):
|
||
|
|
check_same_length([1, 2], [1, 2, 3])
|
||
|
|
|
||
|
|
def test_check_array_rejects_nan(self):
|
||
|
|
from mcm_toolkit.core import check_array
|
||
|
|
|
||
|
|
with pytest.raises(ValueError):
|
||
|
|
check_array([1.0, np.nan, 2.0], finite=True)
|
||
|
|
|
||
|
|
def test_check_probability_bounds(self):
|
||
|
|
from mcm_toolkit.core import check_probability
|
||
|
|
|
||
|
|
assert check_probability(0.5) == 0.5
|
||
|
|
with pytest.raises(ValueError):
|
||
|
|
check_probability(1.5)
|
||
|
|
|
||
|
|
def test_check_square_matrix(self):
|
||
|
|
from mcm_toolkit.core import check_square_matrix
|
||
|
|
|
||
|
|
check_square_matrix([[1, 2], [3, 4]])
|
||
|
|
with pytest.raises(ValueError):
|
||
|
|
check_square_matrix([[1, 2, 3], [4, 5, 6]])
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L0")
|
||
|
|
def test_result_repr_is_readable():
|
||
|
|
from mcm_toolkit.core import Result
|
||
|
|
|
||
|
|
text = repr(Result("demo", params={"a": 1}, metrics={"r2": 0.9}))
|
||
|
|
assert "demo" in text and "r2" in text
|