Skip to content

Commit bab7cd4

Browse files
committed
Add some tests for internal methods
1 parent f40fe2a commit bab7cd4

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/sage/modules/numpy_util.pyx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ cpdef int set_mzd_from_numpy(uintptr_t entries_addr, Py_ssize_t degree, x) excep
6969
cpdef int _set_matrix_mod2_from_numpy_helper(Matrix_mod2_dense a, np.ndarray[numpy_integral, ndim=2] b) except -1:
7070
"""
7171
Internal function, helper for :func:`set_matrix_mod2_from_numpy`.
72+
73+
TESTS::
74+
75+
sage: from sage.modules.numpy_util import _set_matrix_mod2_from_numpy_helper
76+
sage: import numpy as np
77+
sage: a = matrix(GF(2), 2, 3)
78+
sage: b = np.array([[1, 0, 1], [0, 1, 0]], dtype=np.int8)
79+
sage: _set_matrix_mod2_from_numpy_helper(a, b)
80+
1
81+
sage: a
82+
[1 0 1]
83+
[0 1 0]
84+
sage: _set_matrix_mod2_from_numpy_helper(a, np.array([[1, 0], [0, 1]], dtype=np.int8))
85+
Traceback (most recent call last):
86+
...
87+
ValueError: shape mismatch
7288
"""
7389
if not (a.nrows() == b.shape[0] and a.ncols() == b.shape[1]):
7490
raise ValueError("shape mismatch")
@@ -87,7 +103,31 @@ cpdef int set_matrix_mod2_from_numpy(Matrix_mod2_dense a, b) except -1:
87103
- ``a`` -- the destination matrix
88104
- ``b`` -- a numpy array, must have dimension 2 and the same shape as ``a``
89105
90-
OUTPUT: ``True`` if successful, ``False`` otherwise. May throw ``ValueError``.
106+
OUTPUT: ``True`` (when used as bool) if successful, ``False`` otherwise. May throw ``ValueError``.
107+
108+
The exact type of the return value is not guaranteed, in the actual current implementation
109+
it is ``1`` for success and ``0`` for failure.
110+
111+
TESTS::
112+
113+
sage: from sage.modules.numpy_util import set_matrix_mod2_from_numpy
114+
sage: import numpy as np
115+
sage: a = matrix(GF(2), 2, 3)
116+
sage: b = np.array([[1, 0, 1], [0, 1, 0]], dtype=np.int8)
117+
sage: set_matrix_mod2_from_numpy(a, b)
118+
1
119+
sage: a
120+
[1 0 1]
121+
[0 1 0]
122+
sage: set_matrix_mod2_from_numpy(a, np.array([[1, 0], [0, 1]], dtype=np.int8))
123+
Traceback (most recent call last):
124+
...
125+
ValueError: shape mismatch
126+
sage: # unsupported type (may be supported in the future)
127+
sage: set_matrix_mod2_from_numpy(a, np.array([[1, 1, 0], [0, 1, 0]], dtype=np.float64))
128+
0
129+
sage: set_matrix_mod2_from_numpy(a, np.array([1, 0, 0], dtype=np.int8)) # wrong number of dimensions
130+
0
91131
"""
92132
try:
93133
return (<object>_set_matrix_mod2_from_numpy_helper)(a, b) # https://github.com/cython/cython/issues/6588

0 commit comments

Comments
 (0)