import pytest
import numpy as np
try:
import scirs2_numpy as scirs2np HAS_SCIRS2 = True
except ImportError:
HAS_SCIRS2 = False
pytestmark = pytest.mark.skipif(
not HAS_SCIRS2,
reason="scirs2_numpy not installed — run `maturin develop` first",
)
def test_float32_array_roundtrip():
arr = np.array([1.0, 2.0, 3.0], dtype=np.float32)
result = scirs2np.from_numpy(arr)
np.testing.assert_array_equal(np.array(result), arr)
def test_float64_array_identity():
arr = np.arange(100, dtype=np.float64)
result = scirs2np.from_numpy(arr)
np.testing.assert_allclose(np.array(result), arr)
def test_int32_array_roundtrip():
arr = np.array([-1, 0, 1, 127, -128], dtype=np.int32)
result = scirs2np.from_numpy(arr)
np.testing.assert_array_equal(np.array(result), arr)
def test_bool_array_roundtrip():
arr = np.array([True, False, True, True, False], dtype=bool)
result = scirs2np.from_numpy(arr)
np.testing.assert_array_equal(np.array(result), arr)
def test_2d_contiguous_array():
arr = np.ones((10, 20), dtype=np.float32)
result = scirs2np.from_numpy(arr)
assert np.array(result).shape == (10, 20)
def test_3d_array_shape_preserved():
arr = np.zeros((4, 5, 6), dtype=np.float64)
result = scirs2np.from_numpy(arr)
assert np.array(result).shape == (4, 5, 6)
def test_scalar_wrapped_as_0d():
arr = np.array(42.0, dtype=np.float64)
result = scirs2np.from_numpy(arr)
assert float(np.array(result)) == 42.0
def test_non_contiguous_array_converted():
arr = np.arange(100.0).reshape(10, 10)[::2, ::2] result = scirs2np.from_numpy(np.ascontiguousarray(arr))
assert result is not None
def test_fortran_order_array():
arr = np.asfortranarray(np.ones((8, 8), dtype=np.float64))
result = scirs2np.from_numpy(np.ascontiguousarray(arr))
assert np.array(result).shape == (8, 8)
def test_transposed_array_converted():
arr = np.arange(12.0).reshape(3, 4).T result = scirs2np.from_numpy(np.ascontiguousarray(arr))
assert np.array(result).shape == (4, 3)
def test_zero_length_array():
arr = np.array([], dtype=np.float64)
result = scirs2np.from_numpy(arr)
assert len(np.array(result)) == 0
def test_single_element_array():
arr = np.array([3.14], dtype=np.float64)
result = scirs2np.from_numpy(arr)
np.testing.assert_allclose(np.array(result), arr)
def test_large_array_no_copy():
arr = np.zeros(1_000_000, dtype=np.float64)
result = scirs2np.from_numpy(arr)
assert np.array(result).shape == (1_000_000,)
def test_nan_values_preserved():
arr = np.array([float("nan"), float("inf"), float("-inf"), 0.0], dtype=np.float64)
result = np.array(scirs2np.from_numpy(arr))
assert np.isnan(result[0])
assert np.isposinf(result[1])
assert np.isneginf(result[2])
def test_very_small_float32_values():
tiny = np.finfo(np.float32).tiny
arr = np.array([tiny, tiny * 0.5, 0.0], dtype=np.float32)
result = scirs2np.from_numpy(arr)
np.testing.assert_array_equal(np.array(result), arr)
def test_integer_types_i64():
arr = np.array([-(2**62), 0, 2**62 - 1], dtype=np.int64)
result = scirs2np.from_numpy(arr)
np.testing.assert_array_equal(np.array(result), arr)
def test_dlpack_export():
arr = np.zeros((4, 4), dtype=np.float32)
result = scirs2np.from_numpy(arr)
if hasattr(result, "__dlpack__"):
caps = result.__dlpack__()
assert caps is not None
def test_dlpack_device_is_cpu():
arr = np.ones(10, dtype=np.float64)
result = scirs2np.from_numpy(arr)
if hasattr(result, "__dlpack_device__"):
dev = result.__dlpack_device__()
assert dev[0] == 1, f"Expected device type 1 (CPU), got {dev[0]}"