1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
import gc
import random
import pytest
from hypothesis import core, find, given, register_random, strategies as st
from hypothesis.errors import HypothesisWarning, InvalidArgument
from hypothesis.internal import entropy
from hypothesis.internal.compat import PYPY
from hypothesis.internal.entropy import deterministic_PRNG
def gc_on_pypy():
# CPython uses reference counting, so objects (without circular refs)
# are collected immediately on `del`, breaking weak references.
# PyPy doesn't, so we use this function in tests before counting the
# surviving references to ensure that they're deterministic.
if PYPY:
gc.collect()
def test_can_seed_random():
@given(st.random_module())
def test(r):
raise AssertionError
with pytest.raises(AssertionError) as err:
test()
assert "RandomSeeder(0)" in "\n".join(err.value.__notes__)
@given(st.random_module(), st.random_module())
def test_seed_random_twice(r, r2):
assert repr(r) == repr(r2)
@given(st.random_module())
def test_does_not_fail_health_check_if_randomness_is_used(r):
random.getrandbits(128)
def test_cannot_register_non_Random():
with pytest.raises(InvalidArgument):
register_random("not a Random instance")
@pytest.mark.filterwarnings(
"ignore:It looks like `register_random` was passed an object that could be garbage collected"
)
def test_registering_a_Random_is_idempotent():
gc_on_pypy()
n_registered = len(entropy.RANDOMS_TO_MANAGE)
r = random.Random()
register_random(r)
register_random(r)
assert len(entropy.RANDOMS_TO_MANAGE) == n_registered + 1
del r
gc_on_pypy()
assert len(entropy.RANDOMS_TO_MANAGE) == n_registered
def test_manages_registered_Random_instance():
r = random.Random()
register_random(r)
state = r.getstate()
result = []
@given(st.integers())
def inner(x):
v = r.random()
if result:
assert v == result[0]
else:
result.append(v)
inner()
assert state == r.getstate()
def test_registered_Random_is_seeded_by_random_module_strategy():
r = random.Random()
register_random(r)
state = r.getstate()
results = set()
count = [0]
@given(st.integers())
def inner(x):
results.add(r.random())
count[0] += 1
inner()
assert count[0] > len(results) * 0.9, "too few unique random numbers"
assert state == r.getstate()
@given(st.random_module())
def test_will_actually_use_the_random_seed(rnd):
a = random.randint(0, 100)
b = random.randint(0, 100)
random.seed(rnd.seed)
assert a == random.randint(0, 100)
assert b == random.randint(0, 100)
def test_given_does_not_pollute_state():
with deterministic_PRNG():
@given(st.random_module())
def test(r):
pass
test()
state_a = random.getstate()
state_a2 = core._hypothesis_global_random.getstate()
test()
state_b = random.getstate()
state_b2 = core._hypothesis_global_random.getstate()
assert state_a == state_b
assert state_a2 != state_b2
def test_find_does_not_pollute_state():
with deterministic_PRNG():
find(st.random_module(), lambda r: True)
state_a = random.getstate()
state_a2 = core._hypothesis_global_random.getstate()
find(st.random_module(), lambda r: True)
state_b = random.getstate()
state_b2 = core._hypothesis_global_random.getstate()
assert state_a == state_b
assert state_a2 != state_b2
@pytest.mark.filterwarnings(
"ignore:It looks like `register_random` was passed an object that could be garbage collected"
)
def test_evil_prng_registration_nonsense():
gc_on_pypy()
n_registered = len(entropy.RANDOMS_TO_MANAGE)
r1, r2, r3 = random.Random(1), random.Random(2), random.Random(3)
s2 = r2.getstate()
# We're going to be totally evil here: register two randoms, then
# drop one and add another, and finally check that we reset only
# the states that we collected before we started
register_random(r1)
k = max(entropy.RANDOMS_TO_MANAGE) # get a handle to check if r1 still exists
register_random(r2)
assert len(entropy.RANDOMS_TO_MANAGE) == n_registered + 2
with deterministic_PRNG(0):
del r1
gc_on_pypy()
assert k not in entropy.RANDOMS_TO_MANAGE, "r1 has been garbage-collected"
assert len(entropy.RANDOMS_TO_MANAGE) == n_registered + 1
r2.seed(4)
register_random(r3)
r3.seed(4)
s4 = r3.getstate()
# Implicit check, no exception was raised in __exit__
assert r2.getstate() == s2, "reset previously registered random state"
assert r3.getstate() == s4, "retained state when registered within the context"
@pytest.mark.skipif(
PYPY, reason="We can't guard against bad no-reference patterns in pypy."
)
def test_passing_unreferenced_instance_raises():
with pytest.raises(ReferenceError):
register_random(random.Random(0))
@pytest.mark.skipif(
PYPY, reason="We can't guard against bad no-reference patterns in pypy."
)
def test_passing_unreferenced_instance_within_function_scope_raises():
def f():
register_random(random.Random(0))
with pytest.raises(ReferenceError):
f()
@pytest.mark.skipif(
PYPY, reason="We can't guard against bad no-reference patterns in pypy."
)
def test_passing_referenced_instance_within_function_scope_warns():
def f():
r = random.Random(0)
register_random(r)
with pytest.warns(
HypothesisWarning,
match="It looks like `register_random` was passed an object that could be"
" garbage collected",
):
f()
@pytest.mark.filterwarnings(
"ignore:It looks like `register_random` was passed an object that could be garbage collected"
)
@pytest.mark.skipif(
PYPY, reason="We can't guard against bad no-reference patterns in pypy."
)
def test_register_random_within_nested_function_scope():
n_registered = len(entropy.RANDOMS_TO_MANAGE)
def f():
r = random.Random()
register_random(r)
assert len(entropy.RANDOMS_TO_MANAGE) == n_registered + 1
f()
gc_on_pypy()
assert len(entropy.RANDOMS_TO_MANAGE) == n_registered
|