Skip to content

Commit 7f093c5

Browse files
committed
migrated tests
1 parent c68d2b1 commit 7f093c5

File tree

3 files changed

+78
-72
lines changed

3 files changed

+78
-72
lines changed

src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use numpy::PyReadonlyArray1;
22
use pyo3::prelude::*;
33
use wide::*;
4+
use pyo3::types::PyBool;
5+
use pyo3::types::PyAny;
46

57
#[pyfunction]
68
fn first_true_1d_a(array: PyReadonlyArray1<bool>) -> isize {
@@ -237,14 +239,17 @@ fn first_true_1d_f(py: Python, array: PyReadonlyArray1<bool>) -> isize {
237239

238240
#[pyfunction]
239241
#[pyo3(signature = (array, forward=true))]
240-
fn first_true_1d(py: Python, array: PyReadonlyArray1<bool>, forward: bool) -> isize {
242+
fn first_true_1d(py: Python,
243+
array: PyReadonlyArray1<bool>,
244+
forward: &PyBool
245+
) -> isize {
241246
if let Ok(slice) = array.as_slice() {
242247
py.allow_threads(|| {
243248
let len = slice.len();
244249
let ptr = slice.as_ptr() as *const u8;
245250
let ones = u8x32::splat(1);
246251

247-
if forward {
252+
if forward.is_true()? {
248253
let mut i = 0;
249254
unsafe {
250255
// Process 32 bytes at a time with SIMD
@@ -299,7 +304,7 @@ fn first_true_1d(py: Python, array: PyReadonlyArray1<bool>, forward: bool) -> is
299304
} else {
300305
let array_view = array.as_array();
301306
py.allow_threads(|| {
302-
if forward {
307+
if forward.is_true()? {
303308
array_view
304309
.iter()
305310
.position(|&v| v)

tests/test_basic.py

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -9,72 +9,3 @@ def test_first_true_1d():
99
b = np.array([False, False, False])
1010
assert first_true_1d(b) == -1
1111

12-
13-
#--------------------------------------------------------------------------
14-
def test_first_true_1d_a() -> None:
15-
a1 = np.arange(100) == 50
16-
post = first_true_1d(a1, forward=True)
17-
assert post == 50
18-
19-
# def test_first_true_1d_b() -> None:
20-
# with self.assertRaises(TypeError):
21-
# a1 = [2, 4, 5,]
22-
# first_true_1d(a1, forward=True)
23-
24-
# def test_first_true_1d_c() -> None:
25-
# with self.assertRaises(ValueError):
26-
# a1 = np.arange(100) == 50
27-
# first_true_1d(a1, forward=a1)
28-
29-
def test_first_true_1d_d() -> None:
30-
a1 = np.arange(100) < 0
31-
post = first_true_1d(a1, forward=True)
32-
assert post == -1
33-
34-
# def test_first_true_1d_e() -> None:
35-
# a1 = np.arange(100)
36-
# # only a Boolean array
37-
# with self.assertRaises(ValueError):
38-
# post = first_true_1d(a1, forward=True)
39-
40-
# def test_first_true_1d_f() -> None:
41-
# a1 = (np.arange(100) == 0)[:50:2]
42-
# # only a contiguous array
43-
# with self.assertRaises(ValueError):
44-
# post = first_true_1d(a1, forward=True)
45-
46-
# def test_first_true_1d_g() -> None:
47-
# a1 = (np.arange(100) == 0).reshape(10, 10)
48-
# # only a contiguous array
49-
# with self.assertRaises(ValueError):
50-
# post = first_true_1d(a1, forward=True)
51-
52-
def test_first_true_1d_reverse_a() -> None:
53-
a1 = np.arange(100) == 50
54-
post = first_true_1d(a1, forward=False)
55-
assert post == 50
56-
57-
def test_first_true_1d_reverse_b() -> None:
58-
a1 = np.arange(100) == 0
59-
post = first_true_1d(a1, forward=False)
60-
assert post == 0
61-
62-
def test_first_true_1d_reverse_c() -> None:
63-
a1 = np.arange(100) == -1
64-
post = first_true_1d(a1, forward=False)
65-
assert post == -1
66-
67-
def test_first_true_1d_reverse_d() -> None:
68-
a1 = np.arange(100) == 99
69-
post = first_true_1d(a1, forward=False)
70-
assert post == 99
71-
72-
def test_first_true_1d_multi_a() -> None:
73-
a1 = np.isin(np.arange(100), (50, 70, 90))
74-
assert first_true_1d(a1, forward=True) == 50
75-
assert first_true_1d(a1, forward=False) == 90
76-
77-
def test_first_true_1d_multi_b() -> None:
78-
a1 = np.isin(np.arange(100), (10, 30, 50))
79-
assert first_true_1d(a1, forward=True) == 10
80-
assert first_true_1d(a1, forward=False) == 50

tests/test_first_true_1d.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from arrayredox import first_true_1d
2+
import numpy as np
3+
4+
import pytest
5+
6+
#--------------------------------------------------------------------------
7+
def test_first_true_1d_a() -> None:
8+
a1 = np.arange(100) == 50
9+
post = first_true_1d(a1, forward=True)
10+
assert post == 50
11+
12+
def test_first_true_1d_b() -> None:
13+
with pytest.raises(TypeError):
14+
a1 = [2, 4, 5,]
15+
first_true_1d(a1, forward=True)
16+
17+
def test_first_true_1d_c() -> None:
18+
with pytest.raises(TypeError):
19+
a1 = np.arange(100) == 50
20+
first_true_1d(a1, forward=0) # bad Boolean
21+
22+
def test_first_true_1d_d() -> None:
23+
a1 = np.arange(100) < 0
24+
post = first_true_1d(a1, forward=True)
25+
assert post == -1
26+
27+
def test_first_true_1d_e() -> None:
28+
a1 = np.arange(100)
29+
# only a Boolean array
30+
with pytest.raises(TypeError):
31+
post = first_true_1d(a1, forward=True)
32+
33+
def test_first_true_1d_f() -> None:
34+
a1 = (np.arange(100) == 3)[:50:2]
35+
assert first_true_1d(a1), 4
36+
37+
def test_first_true_1d_g() -> None:
38+
a1 = (np.arange(100) == 0).reshape(10, 10)
39+
with pytest.raises(TypeError):
40+
post = first_true_1d(a1, forward=True)
41+
42+
def test_first_true_1d_reverse_a() -> None:
43+
a1 = np.arange(100) == 50
44+
post = first_true_1d(a1, forward=False)
45+
assert post == 50
46+
47+
def test_first_true_1d_reverse_b() -> None:
48+
a1 = np.arange(100) == 0
49+
post = first_true_1d(a1, forward=False)
50+
assert post == 0
51+
52+
def test_first_true_1d_reverse_c() -> None:
53+
a1 = np.arange(100) == -1
54+
post = first_true_1d(a1, forward=False)
55+
assert post == -1
56+
57+
def test_first_true_1d_reverse_d() -> None:
58+
a1 = np.arange(100) == 99
59+
post = first_true_1d(a1, forward=False)
60+
assert post == 99
61+
62+
def test_first_true_1d_multi_a() -> None:
63+
a1 = np.isin(np.arange(100), (50, 70, 90))
64+
assert first_true_1d(a1, forward=True) == 50
65+
assert first_true_1d(a1, forward=False) == 90
66+
67+
def test_first_true_1d_multi_b() -> None:
68+
a1 = np.isin(np.arange(100), (10, 30, 50))
69+
assert first_true_1d(a1, forward=True) == 10
70+
assert first_true_1d(a1, forward=False) == 50

0 commit comments

Comments
 (0)