Skip to content

Commit

Permalink
Merge pull request #391 from bashtage/final-cov
Browse files Browse the repository at this point in the history
Final set of corner coverage
  • Loading branch information
bashtage authored Oct 4, 2024
2 parents abacda9 + ebcc672 commit 4033e56
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
7 changes: 2 additions & 5 deletions randomgen/entropy.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,11 @@ def seed_by_array(object seed, Py_ssize_t n):
obj = np.asarray(seed).astype(object)
if obj.ndim != 1:
raise ValueError("Array-valued seeds must be 1-dimensional")
if not np.isreal(obj).all():
if not all((np.isscalar(v) and np.isreal(v)) for v in obj):
raise TypeError(err_msg)
if ((obj > int(2**64 - 1)) | (obj < 0)).any():
raise ValueError(err_msg)
try:
obj_int = obj.astype(np.uint64, casting="unsafe")
except ValueError:
raise ValueError(err_msg)
obj_int = obj.astype(np.uint64, casting="unsafe")
if not (obj == obj_int).all():
raise TypeError(err_msg)
seed_array = obj_int
Expand Down
4 changes: 2 additions & 2 deletions randomgen/pcg32.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ cdef class PCG32(BitGenerator):
if inc is not None:
err_msg = "inc must be a scalar integer between 0 and " \
"{ub}".format(ub=ub)
if inc < 0 or inc > ub or int(np.squeeze(inc)) != inc:
raise ValueError(err_msg)
if not np.isscalar(inc):
raise TypeError(err_msg)
if inc < 0 or inc > ub or int(np.squeeze(inc)) != inc:
raise ValueError(err_msg)
BitGenerator._seed_with_seed_sequence(self, seed, inc=inc)

@property
Expand Down
4 changes: 2 additions & 2 deletions randomgen/pcg64.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ cdef class PCG64(BitGenerator):
if inc is not None:
err_msg = "inc must be a scalar integer between 0 and " \
"{ub}".format(ub=ub)
if inc < 0 or inc > ub or int(np.squeeze(inc)) != inc:
raise ValueError(err_msg)
if not np.isscalar(inc):
raise TypeError(err_msg)
if inc < 0 or inc > ub or int(np.squeeze(inc)) != inc:
raise ValueError(err_msg)
BitGenerator._seed_with_seed_sequence(self, seed, inc=inc)

@property
Expand Down
2 changes: 2 additions & 0 deletions randomgen/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ def test_seed_array_errors():
seed_by_array(np.array([0.0 + 1j]), 1)
with pytest.raises(TypeError):
seed_by_array("1", 1)
with pytest.raises(TypeError):
seed_by_array(1.2, 1)
with pytest.raises(ValueError):
seed_by_array(-1, 1)
with pytest.raises(ValueError):
Expand Down
4 changes: 4 additions & 0 deletions randomgen/tests/test_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -2174,3 +2174,7 @@ def test_pcg64_errors():
p = PCG64(0)
with pytest.raises(TypeError):
p.seed(seed=0, inc=[3, 4])
with pytest.raises(ValueError):
p.seed(seed=0, inc=-1)
with pytest.raises(ValueError):
p.seed(seed=0, inc=sum(2**i for i in range(129)))
28 changes: 23 additions & 5 deletions randomgen/tests/test_extended_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,29 @@ def test_mv_complex_normal(extended_gen, ex_gamma, ex_rel, ex_loc, size):
loc = np.zeros(2)
if ex_loc:
loc = np.tile(loc, (4, 1, 1, 1))
extended_gen.multivariate_complex_normal(np.zeros(2), size=size)
extended_gen.multivariate_complex_normal(np.zeros(2), size=size)
extended_gen.multivariate_complex_normal(np.zeros(2), gamma, size=size)
extended_gen.multivariate_complex_normal(np.zeros(2), gamma, rel, size=size)
extended_gen.multivariate_complex_normal(np.zeros(2), gamma, rel, size=size)
extended_gen.multivariate_complex_normal(loc, size=size)
extended_gen.multivariate_complex_normal(loc, gamma, size=size)
extended_gen.multivariate_complex_normal(loc, gamma, rel, size=size)


def test_mv_complex_normal_scalar_size(extended_gen):
gamma = np.array([[2, 0 + 1.0j], [-0 - 1.0j, 2]])
rel = np.array([[0.22, 0 + 0.1j], [+0 + 0.1j, 0.22]])
loc = np.zeros(2)
extended_gen.multivariate_complex_normal(loc, size=4)
extended_gen.multivariate_complex_normal(loc, gamma, size=4)
extended_gen.multivariate_complex_normal(loc, gamma, rel, size=4)


def test_extended_gen_state(extended_gen):
def _assert_state_equal(actual, target):
for key in actual:
if isinstance(actual[key], dict):
_assert_state_equal(actual[key], target[key])
assert actual[key] == target[key]

val = extended_gen.__getstate__()
_assert_state_equal(val, extended_gen.state)


def test_mv_complex_normal_exceptions(extended_gen):
Expand Down
5 changes: 3 additions & 2 deletions randomgen/tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ def params_1(f, bounded=False):

def comp_state(state1, state2):
identical = True
if type(state1) is not type(state2):
return False
if isinstance(state1, dict):
for key in state1:
identical &= comp_state(state1[key], state2[key])
elif type(state1) is not type(state2):
identical &= type(state1) is type(state2)
else:
if isinstance(state1, (list, tuple, np.ndarray)) and isinstance(
state2, (list, tuple, np.ndarray)
Expand Down Expand Up @@ -903,6 +903,7 @@ def test_numpy_state(self):
state2 = self.rg.bit_generator.state
assert_((state[1] == state2["state"]["key"]).all())
assert_(state[2] == state2["state"]["pos"])
assert not comp_state(state, state2)


class TestMT64(RNG):
Expand Down

0 comments on commit 4033e56

Please sign in to comment.