def test_get_random_seed_rng(): def make_mock_random_state(return_value): class MockRandomState(np.random.RandomState): def __init__(self): self.min_bound = None self.max_bound = None super().__init__() def randint(self, min_bound, max_bound): self.min_bound = min_bound self.max_bound = max_bound return return_value return MockRandomState() rng = make_mock_random_state(42) assert get_random_seed(rng) == 42 assert rng.min_bound == SEED_BOUNDS.min_bound assert rng.max_bound == SEED_BOUNDS.max_bound
def test_get_random_seed_int(): # ensure the invariant "min_bound < max_bound" is enforced with pytest.raises(ValueError): get_random_seed(0, min_bound=0, max_bound=0) with pytest.raises(ValueError): get_random_seed(0, min_bound=0, max_bound=-1) # test default boundaries to show the provided value should modulate within the default range assert get_random_seed(SEED_BOUNDS.max_bound - 2) == SEED_BOUNDS.max_bound - 2 assert get_random_seed(SEED_BOUNDS.max_bound - 1) == SEED_BOUNDS.max_bound - 1 assert get_random_seed(SEED_BOUNDS.max_bound) == SEED_BOUNDS.min_bound assert get_random_seed(SEED_BOUNDS.max_bound + 1) == SEED_BOUNDS.min_bound + 1 assert get_random_seed(SEED_BOUNDS.max_bound + 2) == SEED_BOUNDS.min_bound + 2 assert get_random_seed(SEED_BOUNDS.min_bound - 2) == SEED_BOUNDS.max_bound - 2 assert get_random_seed(SEED_BOUNDS.min_bound - 1) == SEED_BOUNDS.max_bound - 1 assert get_random_seed(SEED_BOUNDS.min_bound) == SEED_BOUNDS.min_bound assert get_random_seed(SEED_BOUNDS.min_bound + 1) == SEED_BOUNDS.min_bound + 1 assert get_random_seed(SEED_BOUNDS.min_bound + 2) == SEED_BOUNDS.min_bound + 2 # vectorize get_random_seed via a wrapper for easy evaluation default_min_bound = inspect.signature(get_random_seed).parameters['min_bound'].default default_max_bound = inspect.signature(get_random_seed).parameters['max_bound'].default assert default_min_bound == SEED_BOUNDS.min_bound assert default_max_bound == SEED_BOUNDS.max_bound def get_random_seed_vec(min_bound=None, max_bound=None): # passing None for either means no value is provided to get_random_seed def get_random_seed_wrapper(random_seed): return get_random_seed(random_seed, min_bound=min_bound if min_bound is not None else default_min_bound, max_bound=max_bound if max_bound is not None else default_max_bound) return np.vectorize(get_random_seed_wrapper) # ensure that regardless of the setting of min_bound and max_bound, the output of get_random_seed always stays # between the min_bound (inclusive) and max_bound (exclusive), and wraps neatly around that range using modular arithmetic. vals = np.arange(-100, 100) def make_expected_values(vals, min_bound, max_bound): return np.array([i if (min_bound <= i and i < max_bound) else ((i - min_bound) % (max_bound - min_bound)) + min_bound for i in vals]) np.testing.assert_equal(get_random_seed_vec(min_bound=None, max_bound=None)(vals), make_expected_values(vals, min_bound=SEED_BOUNDS.min_bound, max_bound=SEED_BOUNDS.max_bound)) np.testing.assert_equal(get_random_seed_vec(min_bound=None, max_bound=10)(vals), make_expected_values(vals, min_bound=SEED_BOUNDS.min_bound, max_bound=10)) np.testing.assert_equal(get_random_seed_vec(min_bound=-10, max_bound=None)(vals), make_expected_values(vals, min_bound=-10, max_bound=SEED_BOUNDS.max_bound)) np.testing.assert_equal(get_random_seed_vec(min_bound=0, max_bound=5)(vals), make_expected_values(vals, min_bound=0, max_bound=5)) np.testing.assert_equal(get_random_seed_vec(min_bound=-5, max_bound=0)(vals), make_expected_values(vals, min_bound=-5, max_bound=0)) np.testing.assert_equal(get_random_seed_vec(min_bound=-5, max_bound=5)(vals), make_expected_values(vals, min_bound=-5, max_bound=5)) np.testing.assert_equal(get_random_seed_vec(min_bound=5, max_bound=10)(vals), make_expected_values(vals, min_bound=5, max_bound=10)) np.testing.assert_equal(get_random_seed_vec(min_bound=-10, max_bound=-5)(vals), make_expected_values(vals, min_bound=-10, max_bound=-5))
def get_random_seed_wrapper(random_seed): return get_random_seed(random_seed, min_bound=min_bound if min_bound is not None else default_min_bound, max_bound=max_bound if max_bound is not None else default_max_bound)