Skip to content

Commit

Permalink
Clean up minimal() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Aug 24, 2024
1 parent e8cea04 commit 94c666a
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 42 deletions.
6 changes: 1 addition & 5 deletions hypothesis-python/tests/array_api/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,7 @@ def test_minimize_large_uint_arrays(xp, xps):
example."""
if not hasattr(xp, "nonzero"):
pytest.skip("optional API")
smallest = minimal(
xps.arrays(xp.uint8, 100),
lambda x: xp.any(x) and not xp.all(x),
timeout_after=60,
)
smallest = minimal(xps.arrays(xp.uint8, 100), lambda x: xp.any(x) and not xp.all(x))
assert xp.all(xp.logical_or(smallest == 0, smallest == 1))
idx = xp.nonzero(smallest)[0]
assert idx.size in (1, smallest.size - 1)
Expand Down
21 changes: 6 additions & 15 deletions hypothesis-python/tests/common/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,21 @@ class Timeout(BaseException):
pass


def minimal(definition, condition=lambda x: True, settings=None, timeout_after=10):
def minimal(definition, condition=lambda x: True, settings=None):
definition.validate()
runtime = None
result = None

def wrapped_condition(x):
# This sure seems pointless, but `test_sum_of_pair` fails otherwise...
return condition(x)

if (
context := _current_build_context.value
) and context.data.provider.avoid_realization:
raise SkipTest("`minimal()` helper not supported under symbolic execution")

def wrapped_condition(x):
nonlocal runtime
if timeout_after is not None:
if runtime:
runtime += TIME_INCREMENT
if runtime >= timeout_after:
raise Timeout
result = condition(x)
if result and not runtime:
runtime = 0.0
return result

if settings is None:
settings = Settings(max_examples=50000, phases=(Phase.generate, Phase.shrink))
settings = Settings(max_examples=500, phases=(Phase.generate, Phase.shrink))

verbosity = settings.verbosity
if verbosity == Verbosity.normal:
Expand Down
2 changes: 1 addition & 1 deletion hypothesis-python/tests/cover/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_bordering_on_a_leap_year():
dt.datetime.min.replace(year=2003), dt.datetime.max.replace(year=2005)
),
lambda x: x.month == 2 and x.day == 29,
timeout_after=60,
settings=settings(max_examples=1200),
)
assert x.year == 2004

Expand Down
12 changes: 3 additions & 9 deletions hypothesis-python/tests/nocover/test_recursive.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def flatten(x):
max_leaves=size * 2,
),
lambda x: isinstance(x, list) and len(flatten(x)) >= size,
timeout_after=None,
)
assert flatten(xs) == [0] * size

Expand All @@ -46,11 +45,7 @@ def depth(x):
else:
return 1

xs = minimal(
st.recursive(st.integers(), st.lists),
lambda x: depth(x) > 1,
timeout_after=None,
)
xs = minimal(st.recursive(st.integers(), st.lists), lambda x: depth(x) > 1)
assert xs in ([0], [[]])


Expand All @@ -67,7 +62,6 @@ def breadth(x):
st.recursive(st.booleans(), lambda x: st.lists(x, max_size=target // 2)),
lambda x: breadth(x) >= target,
settings=settings(max_examples=10000),
timeout_after=None,
)
assert breadth(broad) == target

Expand All @@ -79,7 +73,7 @@ def test_drawing_many_near_boundary():
lambda x: st.lists(x, min_size=2 * (size - 1), max_size=2 * size).map(tuple),
max_leaves=2 * size - 1,
)
ls = minimal(st.lists(elems), lambda x: len(set(x)) >= size, timeout_after=None)
ls = minimal(st.lists(elems), lambda x: len(set(x)) >= size)
assert len(ls) == size


Expand Down Expand Up @@ -117,7 +111,7 @@ def test_can_form_sets_of_recursive_data():
max_leaves=20,
)
)
xs = minimal(trees, lambda x: len(x) >= size, timeout_after=None)
xs = minimal(trees, lambda x: len(x) >= size)
assert len(xs) == size


Expand Down
2 changes: 0 additions & 2 deletions hypothesis-python/tests/nocover/test_simple_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ def test_list_of_fractional_float():
minimal(
lists(floats(), min_size=5),
lambda x: len([t for t in x if t >= 1.5]) >= 5,
timeout_after=60,
)
) == {2}

Expand All @@ -158,7 +157,6 @@ def test_minimizes_lists_of_negative_ints_up_to_boundary():
result = minimal(
lists(integers(), min_size=10),
lambda x: len([t for t in x if t <= -1]) >= 10,
timeout_after=60,
)
assert result == [-1] * 10

Expand Down
6 changes: 1 addition & 5 deletions hypothesis-python/tests/numpy/test_gen_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ def test_generates_and_minimizes():


def test_can_minimize_large_arrays():
x = minimal(
nps.arrays("uint32", 100),
lambda x: np.any(x) and not np.all(x),
timeout_after=60,
)
x = minimal(nps.arrays("uint32", 100), lambda x: np.any(x) and not np.all(x))
assert np.logical_or(x == 0, x == 1).all()
assert np.count_nonzero(x) in (1, len(x) - 1)

Expand Down
6 changes: 1 addition & 5 deletions hypothesis-python/tests/quality/test_shrink_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_minimize_multiple_elements_in_silly_large_int_range():
desired_result = [0] * 20

ir = integers(-(2**256), 2**256)
x = minimal(lists(ir), lambda x: len(x) >= 20, timeout_after=20)
x = minimal(lists(ir), lambda x: len(x) >= 20)
assert x == desired_result


Expand All @@ -196,7 +196,6 @@ def test_minimize_multiple_elements_in_silly_large_int_range_min_is_not_dupe():
x = minimal(
lists(ir),
lambda x: (assume(len(x) >= 20) and all(x[i] >= target[i] for i in target)),
timeout_after=60,
)
assert x == target

Expand All @@ -211,7 +210,6 @@ def large_mostly_non_overlapping(xs):
result = minimal(
lists(sets(integers(), min_size=1), min_size=1),
large_mostly_non_overlapping,
timeout_after=120,
)
assert len(result) == 1
union = reduce(set.union, result)
Expand All @@ -227,7 +225,6 @@ def test_containment(n, seed):
iv = minimal(
tuples(lists(integers()), integers()),
lambda x: x[1] in x[0] and x[1] >= n,
timeout_after=60,
)
assert iv == ([n], n)

Expand All @@ -236,7 +233,6 @@ def test_duplicate_containment():
ls, i = minimal(
tuples(lists(integers()), integers()),
lambda s: s[0].count(s[1]) > 1,
timeout_after=100,
)
assert ls == [0, 0]
assert i == 0
Expand Down

0 comments on commit 94c666a

Please sign in to comment.