Skip to content

Commit d0984e8

Browse files
author
peng.li24
committed
test: update test_astype.py
1 parent 5cff6c1 commit d0984e8

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

tests/test_astype.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
"""
44
import numpy as np, pytest, numpycpp as cpp
55

6-
# all source × target combinations
76
_SRC = {"f64": lambda: np.array([1.5,-2.7,3.1], dtype=np.float64),
87
"f32": lambda: np.array([1.5,-2.7,3.1], dtype=np.float32),
98
"i32": lambda: np.array([1,-2,3], dtype=np.int32),
109
"i64": lambda: np.array([1,-2,3], dtype=np.int64),
1110
"bool": lambda: np.array([True,False,True])}
12-
# numpy 约定: ndarray.astype(float) → float64, astype(int) → int32
11+
# 目标必须显式指定精度; "float" 已移除
1312
_EXPECT = {
1413
"float64": np.float64, "double": np.float64,
15-
"float32": np.float32, "float": np.float64,
14+
"float32": np.float32,
1615
"int": np.int32, "int32": np.int32,
1716
"int64": np.int64, "bool": np.bool_,
1817
}
@@ -25,17 +24,22 @@ def test_astype(src, dst, exp_dt):
2524
assert r.dtype == exp_dt, f"{src}{dst}: got {r.dtype}, expected {exp_dt}"
2625
assert np.array_equal(r, a.astype(exp_dt)), f"{src}{dst}: value mismatch"
2726

27+
def test_float_rejected():
28+
"""'float' 作为目标必须被拒绝(必须显式指定 float32/float64)"""
29+
a = np.array([1.5], dtype=np.float64)
30+
with pytest.raises(RuntimeError, match="unsupported.*target"):
31+
cpp.astype(a, "float")
32+
2833
@pytest.mark.parametrize("label,arr,expect_fallback", [
2934
("f32 LE", np.array([1.5],dtype=np.float32), True),
3035
("f32 BE", np.array([1.5],dtype='>f4'), True),
3136
("f64 LE", np.array([1.5],dtype=np.float64), True),
32-
("i32", np.array([1],dtype=np.int32), False), # float fallback 不适用
33-
("bool", np.array([True]), False), # float fallback 不适用
37+
("i32", np.array([1],dtype=np.int32), False),
38+
("bool", np.array([True]), False),
3439
])
3540
def test_dtype_diag(label, arr, expect_fallback):
3641
info = cpp._diag_astype_dtype(arr)
37-
assert info["fallback_works"] == expect_fallback, \
38-
f"{label}: fallback_works={info['fallback_works']} expect={expect_fallback}"
42+
assert info["fallback_works"] == expect_fallback, f"{label}: {info['fallback_works']}"
3943

4044
def test_no_recursion_32():
4145
for i in range(32):

0 commit comments

Comments
 (0)