Skip to content

Commit 3b55efb

Browse files
committed
tests : add more tensor tests (python)
1 parent 1bc9af7 commit 3b55efb

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

tests/test_eigen_tensor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ NB_MODULE(test_eigen_tensor_ext, m) {
2828
return a + b;
2929
}, "a"_a, "b"_a);
3030
m.def("add3dTensor_nc", [](const Tensor3d &a, const Tensor3d &b) -> Tensor3d {
31-
return (a + b).eval();
31+
return a + b;
3232
}, "a"_a.noconvert(), "b"_a.noconvert());
3333
m.def("square3dTensorR", [](const RowTensor3d &a) -> RowTensor3d {
34-
return a.square().eval();
34+
return a.square();
3535
}, "a"_a.noconvert());
3636

3737
m.def("mul3dTensor", [](double a, const Tensor3d &b) -> Tensor3d {

tests/test_eigen_tensor.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,26 @@ def test01_tensor3d():
1919
a_colmaj = np.asfortranarray(a)
2020
with pytest.raises(TypeError, match='incompatible function arguments'):
2121
t.square3dTensorR(a_colmaj)
22+
23+
# add3dTensor: col-major inputs (with implicit conversion from row-major)
24+
b = np.arange(12, 24, dtype=float).reshape(2, 3, 2)
25+
assert_array_equal(t.add3dTensor(a, b), a + b)
26+
# row-major numpy arrays are implicitly converted
27+
assert_array_equal(t.add3dTensor(a, a), a + a)
28+
29+
# add3dTensor_nc: noconvert — rejects row-major (C-order) arrays
30+
with pytest.raises(TypeError, match='incompatible function arguments'):
31+
t.add3dTensor_nc(a, b)
32+
# Fortran-order (col-major) arrays are accepted without conversion
33+
assert_array_equal(t.add3dTensor_nc(a_colmaj, np.asfortranarray(b)), a + b)
34+
35+
# mul3dTensor: scalar * col-major tensor
36+
assert_array_equal(t.mul3dTensor(2.0, a), 2.0 * a)
37+
38+
# mul3dTensorMap: read-only TensorMap, returns a new tensor
39+
assert_array_equal(t.mul3dTensorMap(3.0, a_colmaj), 3.0 * a)
40+
41+
# mul3dTensorMapInPlace: mutates the array in place via TensorMap
42+
arr = np.asfortranarray(a.copy())
43+
t.mul3dTensorMapInPlace(2.0, arr)
44+
assert_array_equal(arr, 2.0 * a)

0 commit comments

Comments
 (0)