Skip to content

Commit 064a5ca

Browse files
committed
accel: add tests for failure branches in scatter/argsort
1 parent b3f0c68 commit 064a5ca

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/lenskit/_accel/data.pyi

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@ def dense_cooc(
3737
*,
3838
diagonal: bool = True,
3939
) -> AccelTask[np.ndarray[tuple[int, int], np.dtype[np.float32]]]: ...
40-
def scatter_array(dst: _A, idx: pa.Array, src: _A) -> _A: ...
41-
def scatter_array_empty(dst_size: int, idx: pa.Array, src: _A) -> _A: ...
40+
def scatter_array(dst: _A, idx: pa.Array, src: _A) -> _A:
41+
"""
42+
Create a new array merging a base array with the scattered content of a second array.
43+
"""
44+
45+
def scatter_array_empty(dst_size: int, idx: pa.Array, src: _A) -> _A:
46+
"""
47+
Scatter array elements into a new, otherwise-empty array.
48+
"""
49+
4250
def sample_negatives(
4351
coords: CoordinateTable,
4452
rows: np.ndarray[tuple[int], np.dtype[np.int32]],

tests/accel/test_argsort.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import hypothesis.extra.numpy as nph
1111
import hypothesis.strategies as st
1212
from hypothesis import given
13-
from pytest import mark
13+
from pytest import mark, raises
1414

1515
from lenskit._accel import data
1616

@@ -209,3 +209,9 @@ def test_topn_any_float(arr, n):
209209
mask[np.isnan(arr)] = False
210210
nopes = arr[mask]
211211
assert np.all(nopes <= np.min(items))
212+
213+
214+
def test_topn_rejects_strings():
215+
strings = pa.array(["a", "b", "c", "x", "9", "0", "3", "@"])
216+
with raises(TypeError):
217+
data.argtopn(strings, 5)

tests/accel/test_scatter.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import hypothesis.extra.numpy as nph
1313
import hypothesis.strategies as st
1414
from hypothesis import given
15+
from pytest import raises
1516

1617
from lenskit._accel import data
1718

@@ -82,3 +83,15 @@ def test_scatter_dst_size(hd: st.DataObject, size, idx_t: np.dtype):
8283
arr = arr_a.to_numpy(zero_copy_only=False)
8384

8485
assert np.array_equal(arr[idx], src, equal_nan=True)
86+
87+
88+
def test_scatter_rejects_strings():
89+
strings = pa.array(["a", "b", "c", "x", "9", "0", "3", "@"])
90+
with raises(TypeError):
91+
data.scatter_array(strings, pa.array([2, 7, 0]), strings)
92+
93+
94+
def test_scatter_empty_rejects_strings():
95+
strings = pa.array(["a", "b", "c", "x", "9", "0", "3", "@"])
96+
with raises(TypeError):
97+
data.scatter_array_empty(100, pa.array([2, 7, 0]), strings)

0 commit comments

Comments
 (0)