def test_invalid(): with pytest.raises(TypeError, match="next_raw must be"): UserBitGenerator.from_cfunc("next_raw", "next_64", "next_32", "next_double", "state") with pytest.raises(TypeError, match="next_raw must be"): UserBitGenerator.from_ctypes("next_raw", "next_64", "next_32", "next_double", "state")
def test_32(): def next_raw(vp): return np.iinfo(np.uint32).max bg = UserBitGenerator(next_raw, 32) assert bg.random_raw() == np.iinfo(np.uint32).max gen = Generator(bg) assert gen.integers(0, 2**64, dtype=np.uint64) == np.iinfo(np.uint64).max np.testing.assert_allclose(gen.random(), (2**53 - 1) / (2**53), rtol=1e-14) assert "UserBitGenerator(Python)" in repr(bg)
def test_smoke(python_pcg): bg = UserBitGenerator(python_pcg, 64) gen = Generator(bg) assert isinstance(gen.random(), float) assert isinstance(gen.standard_normal(dtype=np.float32), float) assert isinstance(gen.integers(0, 2**32, dtype=np.uint32), np.integer) assert isinstance(gen.integers(0, 2**64, dtype=np.uint64), np.integer)
def test_no_setter_getter(split_mix): bgf = UserBitGenerator.from_cfunc( split_mix.next_raw, split_mix.next_64, split_mix.next_32, split_mix.next_double, split_mix.state_address, ) gen = Generator(bgf) gen.standard_normal(size=10) gen.standard_normal(size=10, dtype=np.float32) gen.integers(0, 2**63, dtype=np.uint64, size=10) with pytest.raises(NotImplementedError): bgf.state with pytest.raises(NotImplementedError): bgf.state = {"apple"} bgf = UserBitGenerator.from_cfunc( split_mix.next_raw, split_mix.next_64, split_mix.next_32, split_mix.next_double, split_mix.state_address, state_getter=split_mix.state_getter, ) assert isinstance(bgf.state, dict) with pytest.raises(NotImplementedError): bgf.state = {"apple"} bgf = UserBitGenerator.from_cfunc( split_mix.next_raw, split_mix.next_64, split_mix.next_32, split_mix.next_double, split_mix.state_address, state_setter=split_mix.state_setter, ) bgf.state = split_mix.state_getter()
def test_cfunc_smoke(split_mix): bgf = UserBitGenerator.from_cfunc( split_mix.next_raw, split_mix.next_64, split_mix.next_32, split_mix.next_double, split_mix.state_address, state_getter=split_mix.state_getter, state_setter=split_mix.state_setter, ) gen = Generator(bgf) gen.standard_normal(size=10) assert bgf.state == split_mix.state_getter() gen.standard_normal(dtype=np.float32) assert bgf.state == split_mix.state_getter() gen.integers(0, 2**63, dtype=np.uint64, size=10) assert bgf.state == split_mix.state_getter() old_state = bgf.state.copy() old_state["state"] = 1 bgf.state = old_state assert bgf.state == split_mix.state_getter()
def pcg_python(request): bit_gen = _PCG64(PCG64_INITIAL_STATE["state"]["state"], PCG64_INITIAL_STATE["state"]["inc"]) return UserBitGenerator(bit_gen.next_64(), 64, next_32=bit_gen.next_32())