def test_overflow_in_simplify(): """This is a test that we don't trigger a pytz bug when we're simplifying around MINYEAR where valid dates can produce an overflow error.""" minimal( datetimes(max_year=MINYEAR), lambda x: x.tzinfo != pytz.UTC )
def test_does_not_simplify_into_surrogates(): f = minimal(text(), lambda x: x >= u"\udfff") assert f == u"\ue000" size = 5 f = minimal(text(min_size=size), lambda x: sum(t >= u"\udfff" for t in x) >= size) assert f == u"\ue000" * size
def test_intervals_shrink_to_center(inter): lower, center, upper = inter s = interval(lower, upper, center) assert minimal(s, lambda x: True) == center if lower < center: assert minimal(s, lambda x: x < center) == center - 1 if center < upper: assert minimal(s, lambda x: x > center) == center + 1
def test_minimizes_ints_from_down_to_boundary(boundary): def is_good(x): assert x >= boundary - 10 return x >= boundary assert minimal(integers(min_value=boundary - 10), is_good) == boundary assert minimal(integers(min_value=boundary), lambda x: True) == boundary
def test_overflow_in_simplify(): # we shouldn't trigger a pytz bug when we're simplifying minimal( datetimes( min_value=dt.datetime.max - dt.timedelta(days=3), timezones=timezones() ), lambda x: x.tzinfo != pytz.UTC, )
def test_non_trivial_json(): json = st.deferred(lambda: st.none() | st.floats() | st.text() | lists | objects) lists = st.lists(json) objects = st.dictionaries(st.text(), json) assert minimal(json) is None small_list = minimal(json, lambda x: isinstance(x, list) and x) assert small_list == [None] x = minimal(json, lambda x: isinstance(x, dict) and isinstance(x.get(""), list)) assert x == {"": []}
def test_dictionary(dict_class): assert minimal(dictionaries( keys=integers(), values=text(), dict_class=dict_class)) == dict_class() x = minimal( dictionaries(keys=integers(), values=text(), dict_class=dict_class), lambda t: len(t) >= 3) assert isinstance(x, dict_class) assert set(x.values()) == set((u'',)) for k in x: if k < 0: assert k + 1 in x if k > 0: assert k - 1 in x
def test_can_delete_in_middle_of_a_binding(n): bool_lists = integers(1, 100).flatmap( lambda k: lists(booleans(), min_size=k, max_size=k)) assert minimal( bool_lists, lambda x: x[0] and x[-1] and x.count(False) >= n ) == [True] + [False] * n + [True]
def test_mutual_recursion(): t = st.deferred(lambda: a | b) a = st.deferred(lambda: st.none() | st.tuples(st.just("a"), b)) b = st.deferred(lambda: st.none() | st.tuples(st.just("b"), a)) for c in ("a", "b"): assert minimal(t, lambda x: x is not None and x[0] == c) == (c, None)
def test_can_find_unique_lists_of_non_set_order(): ls = minimal( lists(text(), unique=True), lambda x: list(set(reversed(x))) != x ) assert len(set(ls)) == len(ls) assert len(ls) == 2
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
def test_can_shrink_matrices_with_length_param(): @st.composite def matrix(draw): rows = draw(st.integers(1, 10)) columns = draw(st.integers(1, 10)) return [ [draw(st.integers(0, 10000)) for _ in range(columns)] for _ in range(rows) ] def transpose(m): rows = len(m) columns = len(m[0]) result = [ [None] * rows for _ in range(columns) ] for i in range(rows): for j in range(columns): result[j][i] = m[i][j] return result def is_square(m): return len(m) == len(m[0]) value = minimal(matrix(), lambda m: is_square(m) and transpose(m) != m) assert len(value) == 2 assert len(value[0]) == 2 assert sorted(value[0] + value[1]) == [0, 0, 0, 1]
def test_can_find_nested(): x = minimal( st.recursive(st.booleans(), lambda x: st.tuples(x, x)), lambda x: isinstance(x, tuple) and isinstance(x[0], tuple), ) assert x == ((False, False), False)
def test_can_minimize_large_arrays(): x = minimal( nps.arrays(u'uint32', 100), lambda x: np.any(x) and not np.all(x), timeout_after=60 ) assert np.logical_or(x == 0, x == 1).all() assert np.count_nonzero(x) in (1, len(x) - 1)
def test_can_find_mixed_ascii_and_non_ascii_strings(): s = minimal( text(), lambda x: ( any(t >= u'☃' for t in x) and any(ord(t) <= 127 for t in x))) assert len(s) == 2 assert sorted(s) == [u'0', u'☃']
def test_simplify_shared_linked_to_size(): xs = minimal( st.lists(st.shared(st.integers())), lambda t: sum(t) >= 1000 ) assert sum(xs[:-1]) < 1000 assert (xs[0] - 1) * len(xs) < 1000
def test_can_shrink_through_a_binding(n): bool_lists = integers(0, 100).flatmap( lambda k: lists(booleans(), min_size=k, max_size=k)) assert minimal( bool_lists, lambda x: len(list(filter(bool, x))) >= n ) == [True] * n
def test_containment(n): iv = minimal( tuples(lists(integers()), integers()), lambda x: x[1] in x[0] and x[1] >= n, timeout_after=60 ) assert iv == ([n], n)
def test_bordering_on_a_leap_year(): x = minimal( datetimes(min_year=2002, max_year=2005), lambda x: x.month == 2 and x.day == 29, settings=settings( database=None, max_examples=10 ** 7, timeout=unlimited) ) assert x.year == 2004
def test_shrinks_downwards_to_integers(f): g = minimal( st.floats(), lambda x: x >= f, random=Random(0), settings=settings(verbosity=Verbosity.quiet), ) assert g == ceil(f)
def test_shrinks_downwards_to_integers_when_fractional(b): g = minimal( st.floats(), lambda x: assume((0 < x < (2 ** 53)) and int(x) != x) and x >= b, random=Random(0), settings=settings(verbosity=Verbosity.quiet), ) assert g == b + 0.5
def test_resampling(): x = minimal( st.lists(st.integers(), min_size=1).flatmap( lambda x: st.lists(st.sampled_from(x)) ), lambda x: len(x) >= 10 and len(set(x)) == 1, ) assert x == [0] * 10
def test_sets_of_fixed_length(n): x = minimal(sets(integers(), min_size=n, max_size=n), lambda x: True) assert len(x) == n if not n: assert x == set() else: assert x == set(range(min(x), min(x) + n))
def test_blacklisted_characters(): bad_chars = u'te02тест49st' st = characters(min_codepoint=ord('0'), max_codepoint=ord('9'), blacklist_characters=bad_chars) assert '1' == minimal(st, lambda c: True) assert_no_examples(st, lambda c: c in bad_chars)
def test_lists_of_lower_bounded_length(n): x = minimal( lists(integers(), min_size=n), lambda x: sum(x) >= 2 * n ) assert n <= len(x) <= 2 * n assert all(t >= 0 for t in x) assert len(x) == n or all(t > 0 for t in x) assert sum(x) == 2 * n
def test_list_of_fractional_float(): assert set( minimal( lists(floats(), min_size=5), lambda x: len([t for t in x if t >= 1.5]) >= 5, timeout_after=60, ) ).issubset([1.5, 2.0])
def test_should_have_correct_ordering(): def offset(timezone): return abs(timezone.utcoffset(dt.datetime(2000, 1, 1))) next_interesting_tz = minimal( timezones(), lambda tz: offset(tz) > dt.timedelta(0) ) assert offset(next_interesting_tz) == dt.timedelta(seconds=3600)
def test_can_form_sets_of_recursive_data(): size = 3 trees = st.sets(st.recursive( st.booleans(), lambda x: st.lists(x, min_size=size).map(tuple), max_leaves=20)) xs = minimal(trees, lambda x: len(x) >= size, timeout_after=None) assert len(xs) == size
def test_blacklisted_characters(): bad_chars = u"te02тест49st" st = characters( min_codepoint=ord("0"), max_codepoint=ord("9"), blacklist_characters=bad_chars ) assert "1" == minimal(st, lambda c: True) assert_no_examples(st, lambda c: c in bad_chars)
def test_bordering_on_a_leap_year(): x = minimal( datetimes( dt.datetime.min.replace(year=2003), dt.datetime.max.replace(year=2005) ), lambda x: x.month == 2 and x.day == 29, timeout_after=60, ) assert x.year == 2004
def test_can_find_positive_delta(): assert minimal(timedeltas(), lambda x: x.days > 0) == dt.timedelta(1)
def test_simplifies_towards_zero_delta(): d = minimal(timedeltas()) assert d.days == d.seconds == d.microseconds == 0
def test_lists_of_fixed_length(n): assert minimal(lists(integers(), min_size=n, max_size=n), lambda x: True) == [0] * n
def test_find_empty_collection_gives_empty(col, strat): assert minimal(strat, lambda x: True) == col
def test_minimizes_to_empty(coltype, strat): assert minimal(strat(integers()), lambda x: True) == coltype()
def test_non_float_decimal(): minimal(ds.decimals(), lambda d: d.is_finite() and decimal.Decimal(float(d)) != d)
def test_decimals(): assert minimal(ds.decimals(), lambda f: f.is_finite() and f >= 1) == 1
def test_fractions(): assert minimal(ds.fractions(), lambda f: f >= 1) == 1
def test_can_find_none_list(): assert minimal(ds.lists(ds.none()), lambda x: len(x) >= 3) == [None] * 3
def test_minimise_scalar_dtypes(): assert minimal(nps.scalar_dtypes()) == np.dtype(u"bool")
def test_float_can_find_max_value_inf(): assert minimal(ds.floats(max_value=math.inf), math.isinf) == float("inf") assert minimal(ds.floats(min_value=0.0), math.isinf) == math.inf
def test_simplifies_towards_midnight(): d = minimal(times()) assert d.hour == d.minute == d.second == d.microsecond == 0
def test_float_can_find_max_value_inf(): assert minimal(ds.floats(max_value=float("inf")), lambda x: math.isinf(x)) == float("inf") assert minimal(ds.floats(min_value=0.0), lambda x: math.isinf(x)) == float("inf")
def test_produces_dictionaries_of_at_least_minimum_size(): t = minimal(ds.dictionaries(ds.booleans(), ds.integers(), min_size=2), lambda x: True) assert t == {False: 0, True: 0}
def test_find_non_empty_collection_gives_single_zero(coltype, strat): assert minimal(strat(integers()), bool) == coltype((0, ))
def test_minimal_iterable(): assert list(minimal(ds.iterables(ds.integers()), lambda x: True)) == []
def test_lists_of_lower_bounded_length(n): x = minimal(lists(integers(), min_size=n), lambda x: sum(x) >= 2 * n) assert n <= len(x) <= 2 * n assert all(t >= 0 for t in x) assert len(x) == n or all(t > 0 for t in x) assert sum(x) == 2 * n
def test_generates_and_minimizes(): assert (minimal(nps.arrays(float, (2, 2))) == np.zeros(shape=(2, 2))).all()
def test_bordering_on_a_leap_year(): x = minimal(datetimes(dt.datetime.min.replace(year=2003), dt.datetime.max.replace(year=2005)), lambda x: x.month == 2 and x.day == 29, timeout_after=60) assert x.year == 2004
def test_can_minimize_float_arrays(): x = minimal(nps.arrays(float, 50), lambda t: t.sum() >= 1.0) assert x.sum() in (1, 50)
def test_simplifies_towards_millenium(): d = minimal(datetimes()) assert d.year == 2000 assert d.month == d.day == 1 assert d.hour == d.minute == d.second == d.microsecond == 0
def test_can_create_arrays_of_composite_types(): arr = minimal(nps.arrays(object, 100, foos)) for x in arr: assert isinstance(x, Foo)
def test_max_value_is_respected(): assert minimal(timedeltas(max_value=dt.timedelta(days=-10))).days == -10
def test_can_create_arrays_of_tuples(): arr = minimal( nps.arrays(object, 10, st.tuples(st.integers(), st.integers())), lambda x: all(t0 != t1 for t0, t1 in x), ) assert all(a in ((1, 0), (0, 1)) for a in arr)
def test_can_find_negative_delta(): assert minimal(timedeltas(max_value=dt.timedelta(10**6)), lambda x: x.days < 0) == dt.timedelta(-1)
def test_minimise_array_shapes(min_dims, dim_range, min_side, side_range): smallest = minimal( nps.array_shapes(min_dims, min_dims + dim_range, min_side, min_side + side_range)) assert len(smallest) == min_dims and all(k == min_side for k in smallest)
def test_deprecated_min_date_is_respected(): assert minimal(dates(min_date=dt.date.min.replace(2003))).year == 2003
def test_minimizes_list_of_lists(): xs = minimal(lists(lists(booleans())), lambda x: any(x) and not all(x)) xs.sort() assert xs == [[], [False]]
def test_float_can_find_min_value_inf(): minimal(ds.floats(), lambda x: x < 0 and math.isinf(x)) minimal(ds.floats(min_value=-math.inf, max_value=0.0), math.isinf)
def test_minimise_nested_types(): assert minimal(nps.nested_dtypes()) == np.dtype(u"bool")