Skip to content

Commit

Permalink
TST: Add test code for less frequently encountered paths
Browse files Browse the repository at this point in the history
  • Loading branch information
bashtage committed Oct 1, 2024
1 parent 366347b commit 16ad4e4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 28 deletions.
7 changes: 5 additions & 2 deletions randomgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
]


def test(extra_args: str | list[str] | None = None) -> None:
def test(extra_args: str | list[str] | None = None, exit=True) -> None:
try:
import pytest
except ImportError as err:
Expand All @@ -90,4 +90,7 @@ def test(extra_args: str | list[str] | None = None) -> None:
cmd += [PKG]
joined = " ".join(cmd)
print(f"running: pytest {joined}")
sys.exit(pytest.main(cmd))
result = pytest.main(cmd)
if exit:
sys.exit(result)

Check warning on line 95 in randomgen/__init__.py

View check run for this annotation

Codecov / codecov/patch

randomgen/__init__.py#L95

Added line #L95 was not covered by tests
return result
2 changes: 0 additions & 2 deletions randomgen/common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ ctypedef uint32_t (*random_uint_1_i_32)(bitgen_t *state, uint32_t a) noexcept no
ctypedef int32_t (*random_int_2_i_32)(bitgen_t *state, int32_t a, int32_t b) noexcept nogil
ctypedef int64_t (*random_int_2_i)(bitgen_t *state, int64_t a, int64_t b) noexcept nogil

cdef double kahan_sum(double *darr, np.npy_intp n)

cdef inline double uint64_to_double(uint64_t rnd) noexcept nogil:
return (rnd >> 11) * (1.0 / 9007199254740992.0)

Expand Down
25 changes: 1 addition & 24 deletions randomgen/common.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,7 @@ from randomgen cimport api
from randomgen._deprecated_value import _DeprecatedValue
from randomgen.seed_sequence import ISeedSequence

ISEED_SEQUENCES = (ISeedSequence,)
# NumPy 1.17
try:
ISEED_SEQUENCES += (np.random.bit_generator.ISeedSequence,)
except AttributeError:
pass
# NumPy 1.18
try:
ISEED_SEQUENCES += (np.random._bit_generator.ISeedSequence,)
except AttributeError:
pass

ISEED_SEQUENCES = (ISeedSequence, np.random.bit_generator.ISeedSequence)

__all__ = ["interface"]

Expand Down Expand Up @@ -334,18 +323,6 @@ cdef object prepare_ctypes(bitgen_t *bitgen):
ctypes.c_void_p(<uintptr_t>bitgen))
return _ctypes

cdef double kahan_sum(double *darr, np.npy_intp n):
cdef double c, y, t, sum
cdef np.npy_intp i
sum = darr[0]
c = 0.0
for i in range(1, n):
y = darr[i] - c
t = sum + y
c = (t-sum) - y
sum = t
return sum

cpdef object object_to_int(object val, object bits, object name, int default_bits=64,
object allowed_sizes=(64,)):
"""
Expand Down
6 changes: 6 additions & 0 deletions randomgen/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest

from randomgen.aes import AESCounter
from randomgen.common import BitGenerator, interface
from randomgen.entropy import seed_by_array
from randomgen.romu import Romu
Expand Down Expand Up @@ -142,3 +143,8 @@ def test_object_to_int():
allowed_sizes=(32, 64),
)
assert isinstance(res, int)


def test_uncupported_mode():
with pytest.raises(ValueError, match="mode must be"):
AESCounter(mode="unsupported")
19 changes: 19 additions & 0 deletions randomgen/tests/test_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -2231,3 +2231,22 @@ def test_numpy_mode_error_pcg64():
with pytest.raises(ValueError):
with pytest.warns(FutureWarning):
PCG64(0, inc=1, mode="numpy")


def test_test_runner():
from randomgen import test

val = test(["--collect-only"], exit=False)
assert val == 0
val = test("--collect-only", exit=False)
assert val == 0


def test_bitgen_ctor():
import randomgen._pickle as p

assert isinstance(p.__bit_generator_ctor(), MT19937)
assert isinstance(p.__bit_generator_ctor("LXM"), LXM)
assert isinstance(p.__bit_generator_ctor(b"LXM"), LXM)
with pytest.raises(ValueError):
p.__bit_generator_ctor("NewFangledBG")
10 changes: 10 additions & 0 deletions randomgen/tests/test_extended_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,16 @@ def test_random_float_scalar(seed):
assert_array_almost_equal(actual, desired, decimal=7)


def test_random_float_out(seed):
random = ExtendedGenerator(MT19937(seed))
actual = random.random((3, 2), dtype=np.float32)
random = ExtendedGenerator(MT19937(seed))
other = np.empty_like(actual)
other2 = random.random(out=other, dtype=np.float32)
assert other2 is other
assert_allclose(actual, other)


def test_random_long_double_direct(extended_gen):
state = extended_gen.state
actual = extended_gen.random(10, dtype=np.longdouble)
Expand Down

0 comments on commit 16ad4e4

Please sign in to comment.