Ejemplo n.º 1
0
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
    )
Ejemplo n.º 2
0
def test_finding_decimals_with_defined_precision():
    def is_integral(x):
        try:
            return x == int(x)
        except (ValueError, OverflowError):
            return False

    assert minimal(Decimal, is_integral) == Decimal(0)
    minimal(Decimal, lambda x: is_integral(x * 100) and 0 < x < 1)
Ejemplo n.º 3
0
def test_legacy_api():
    with hs.Settings(strict=False):
        x = minimal(datetime)
        assert x.tzinfo == pytz.UTC
        assert x.year == 2000

        assert minimal(naive_datetime).tzinfo is None
        assert minimal(timezone_aware_datetime) == x
        assert minimal(any_datetime) == x
Ejemplo n.º 4
0
def test_minimal_infinite_float_is_positive():
    assert minimal(float, math.isinf) == float('inf')

    def list_of_infinities(xs):
        assume(len(xs) >= 10)
        return len([
            t for t in xs if (math.isinf(t) or math.isnan(t))
        ]) >= 10

    assert minimal([float], list_of_infinities) == [float('inf')] * 10
Ejemplo n.º 5
0
def test_dictionary(dict_class):
    assert minimal(dictionary(int, text_type, dict_class)) == dict_class()

    x = minimal(dictionary(int, text_type, dict_class), lambda t: len(t) >= 3)
    assert isinstance(x, dict_class)
    assert set(x.values()) == {''}
    for k in x:
        if k < 0:
            assert k + 1 in x
        if k > 0:
            assert k - 1 in x
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
def test_list_of_fractional_float():
    assert set(minimal(
        [float], lambda x: len([t for t in x if t >= 1.5]) >= 10
    )) in (
        {1.5},
        {1.5, 2.0}
    )
Ejemplo n.º 8
0
def test_increasing_sequence():
    xs = minimal(
        [int], lambda t: length_of_longest_ordered_sequence(t) >= 5,
        timeout_after=60,
    )
    start = xs[0]
    assert xs == list(range(start, start + 5))
Ejemplo n.º 9
0
def test_non_reversible_decimals():
    def not_reversible(xs):
        assume(all(x.is_finite() for x in xs))
        return sum(xs) != sum(reversed(xs))

    sigh = minimal(lists(decimals()), not_reversible, timeout_after=30)
    assert len(sigh) < 10
Ejemplo n.º 10
0
def test_small_sum_lists():
    xs = minimal(
        lists(floats()),
        lambda x: len(x) >= 100 and sum(t for t in x if float("inf") > t >= 0) >= 1,
        settings=Settings(average_list_length=200),
    )
    assert 1.0 <= sum(t for t in xs if t >= 0) <= 1.5
Ejemplo n.º 11
0
def test_non_reversible_ints_as_decimals():
    def not_reversible(xs):
        ts = list(map(Decimal, xs))
        return sum(ts) != sum(reversed(ts))

    sigh = minimal(lists(integers()), not_reversible, timeout_after=30)
    assert len(sigh) <= 25
Ejemplo n.º 12
0
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)
    )
    assert x.year == 2004
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
Ejemplo n.º 14
0
def test_anti_sorted_ordered_pair():
    result = minimal(
        lists(ordered_pair),
        lambda x: (
            len(x) >= 30 and
            2 < length_of_longest_ordered_sequence(x) <= 10))
    assert len(result) == 30
Ejemplo n.º 15
0
def test_non_reversible_fractions_as_decimals():
    def not_reversible(xs):
        xs = [Decimal(x.numerator) / x.denominator for x in xs]
        return sum(xs) != sum(reversed(xs))

    sigh = minimal(lists(fractions()), not_reversible, timeout_after=20)
    assert len(sigh) <= 25
Ejemplo n.º 16
0
def test_increasing_sequence():
    k = 6
    xs = minimal(
        lists(integers()), lambda t: (len(t) <= 30 and length_of_longest_ordered_sequence(t) >= k), timeout_after=60
    )
    start = xs[0]
    assert xs == list(range(start, start + k))
Ejemplo n.º 17
0
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)
Ejemplo n.º 18
0
def test_non_reversible_ints_as_decimals():
    def not_reversible(xs):
        xs = list(map(Decimal, xs))
        return sum(xs) != sum(reversed(xs))

    sigh = minimal([int], not_reversible, timeout_after=20)
    assert len(sigh) < 10
Ejemplo n.º 19
0
def test_minimize_list_of_sets_on_large_structure():
    def test_list_in_range(xs):
        assume(len(xs) >= 50)
        return len(list(filter(None, xs))) >= 50

    x = minimal([frozenset({int})], test_list_in_range)
    assert len(x) == 50
    assert len(set(x)) == 1
Ejemplo n.º 20
0
def test_minimize_list_of_floats_on_large_structure():
    def test_list_in_range(xs):
        assume(len(xs) >= 50)
        return len([x for x in xs if x >= 3]) >= 30

    result = minimal(lists(floats()), test_list_in_range)
    result.sort()
    assert result == [0.0] * 20 + [3.0] * 30
Ejemplo n.º 21
0
def test_minimize_list_of_sets_on_large_structure():
    def test_list_in_range(xs):
        assume(len(xs) >= 50)
        return len(list(filter(None, xs))) >= 50

    x = minimal(lists(frozensets(integers())), test_list_in_range, timeout_after=20)
    assert len(x) == 50
    assert len(set(x)) == 1
Ejemplo n.º 22
0
def test_finds_list_with_plenty_duplicates():
    def is_good(xs):
        return max(Counter(xs).values()) >= 3

    result = minimal(
        lists(text(min_size=1), average_size=50, min_size=1), is_good
    )
    assert result == [u'0'] * 3
Ejemplo n.º 23
0
def test_tuples_do_not_block_cloning():
    assert (
        minimal(
            lists(tuples(booleans() | tuples(integers()))),
            lambda x: len(x) >= 50 and any(isinstance(t[0], bool) for t in x),
        )
        == [(False,)] * 50
    )
Ejemplo n.º 24
0
def test_minimal_fractions_4():
    x = minimal(
        lists(
            fractions(max_denominator=100, max_value=100, min_value=-100),
            min_size=20),
        lambda s: len([t for t in s if t >= 1]) >= 20
    )
    assert x == [Fraction(1)] * 20
Ejemplo n.º 25
0
def test_increasing_float_sequence():
    xs = minimal(
        lists(floats()), lambda x: length_of_longest_ordered_sequence([
            t for t in x if t >= 0
        ]) >= 7 and len([t for t in x if t >= 500.0]) >= 4
    )
    assert max(xs) < 1000
    assert not any(math.isinf(x) for x in xs)
Ejemplo n.º 26
0
def test_small_sum_lists():
    xs = minimal(
        lists(floats(), min_size=100, average_size=200),
        lambda x:
            sum(t for t in x if float(u'inf') > t >= 0) >= 1,
        timeout_after=60,
    )
    assert 1.0 <= sum(t for t in xs if t >= 0) <= 1.5
Ejemplo n.º 27
0
def test_finds_non_reversible_floats():
    t = minimal(
        lists(floats()), lambda xs:
            not math.isnan(sum(xs)) and sum(xs) != sum(reversed(xs)),
        timeout_after=40,
        settings=settings(database=None)
    )
    assert len(repr(t)) <= 200
    print(t)
Ejemplo n.º 28
0
def test_minimize_list_on_large_structure():
    def test_list_in_range(xs):
        assume(len(xs) >= 30)
        return len([
            x for x in xs
            if x >= 10
        ]) >= 60

    assert minimal(lists(integers()), test_list_in_range) == [10] * 60
Ejemplo n.º 29
0
def test_can_simplify_bitfields(i):
    bitfield = basic_strategy(
        parameter=lambda r: r.getrandbits(128),
        generate=lambda r, p: r.getrandbits(128) & p,
        simplify=simplify_bitfield,
        copy=lambda x: x,
    )

    assert minimal(bitfield, lambda x: x & (1 << i)) == 1 << i
Ejemplo n.º 30
0
def test_constant_lists_of_diverse_length():
    # This does not currently work very well. We delete, but we don't actually
    # get all that far with simplification of the individual elements.
    result = minimal(
        lists(constant_list(integers())),
        lambda x: len(set(map(len, x))) >= 20,
        timeout_after=30,
    )
    assert len(result) == 20
Ejemplo n.º 31
0
def test_can_generate_non_naive_datetime():
    assert minimal(datetimes(allow_naive=True),
                   lambda d: d.tzinfo).tzinfo == pytz.UTC
Ejemplo n.º 32
0
def test_restricts_to_allowed_set_of_timezones():
    timezones = list(map(pytz.timezone, list(pytz.all_timezones)[:3]))
    x = minimal(datetimes(timezones=timezones))
    assert any(tz.zone == x.tzinfo.zone for tz in timezones)
Ejemplo n.º 33
0
def test_max_year_is_respected():
    assert minimal(datetimes(max_year=1998)).year == 1998
Ejemplo n.º 34
0
def test_can_find_before_the_year_2000():
    assert minimal(datetimes(), lambda x: x.year < 2000).year == 1999
Ejemplo n.º 35
0
def test_minimize_list_on_large_structure():
    def test_list_in_range(xs):
        assume(len(xs) >= 30)
        return len([x for x in xs if x >= 10]) >= 60

    assert minimal(lists(integers()), test_list_in_range) == [10] * 60
Ejemplo n.º 36
0
def test_minimize_3_set_of_tuples():
    assert minimal(sets(tuples(integers())), lambda x: len(x) >= 2) == set(
        ((0, ), (1, )))
Ejemplo n.º 37
0
def test_minimize_3_set():
    assert minimal(sets(integers()), lambda x: len(x) >= 3) in (
        set((0, 1, 2)),
        set((-1, 0, 1)),
    )
Ejemplo n.º 38
0
def test_minimize_longer_list_of_strings():
    assert minimal(lists(text()), lambda x: len(x) >= 10) == [u''] * 10
Ejemplo n.º 39
0
def test_minimize_longer_string():
    assert minimal(text(), lambda x: len(x) >= 10) == u'0' * 10
Ejemplo n.º 40
0
def test_can_find_midnight():
    minimal(
        datetimes(),
        lambda x: (x.hour == 0 and x.minute == 0 and x.second == 0),
    )
Ejemplo n.º 41
0
def test_can_generate_non_utc():
    minimal(datetimes(), lambda d: assume(d.tzinfo) and d.tzinfo.zone != 'UTC')
Ejemplo n.º 42
0
def test_can_find_on_the_minute():
    minimal(datetimes(), lambda x: x.second != 0)
Ejemplo n.º 43
0
def test_minimize_long():
    assert minimal(integers(),
                   lambda x: type(x).__name__ == u'long') == sys.maxint + 1
Ejemplo n.º 44
0
def test_tuples_do_not_block_cloning():
    assert minimal(
        lists(tuples(booleans() | tuples(integers()))),
        lambda x: len(x) >= 50 and any(isinstance(t[0], bool)
                                       for t in x)) == [(False, )] * 50
Ejemplo n.º 45
0
def test_anti_sorted_ordered_pair():
    result = minimal(
        lists(ordered_pair), lambda x:
        (len(x) >= 30 and 2 < length_of_longest_ordered_sequence(x) <= 10))
    assert len(result) == 30
Ejemplo n.º 46
0
def test_can_simplify_flatmap_with_bounded_left_hand_size():
    assert minimal(booleans().flatmap(lambda x: lists(just(x))),
                   lambda x: len(x) >= 10) == [False] * 10
Ejemplo n.º 47
0
def test_can_find_each_month():
    for i in hrange(1, 12):
        minimal(datetimes(), lambda x: x.month == i)
Ejemplo n.º 48
0
def test_can_simplify_across_flatmap_of_just():
    assert minimal(integers().flatmap(just)) == 0
Ejemplo n.º 49
0
def test_can_find_after_the_year_2000():
    assert minimal(datetimes(), lambda x: x.year > 2000).year == 2001
Ejemplo n.º 50
0
def test_can_simplify_on_right_hand_strategy_of_flatmap():
    assert minimal(integers().flatmap(lambda x: lists(just(x)))) == []
Ejemplo n.º 51
0
def test_min_year_is_respected():
    assert minimal(datetimes(min_year=2003)).year == 2003
Ejemplo n.º 52
0
def test_simplifies_towards_midnight():
    d = minimal(datetimes())
    assert d.hour == 0
    assert d.minute == 0
    assert d.second == 0
    assert d.microsecond == 0
Ejemplo n.º 53
0
def test_can_ignore_left_hand_side_of_flatmap():
    assert minimal(integers().flatmap(lambda x: lists(integers())),
                   lambda x: len(x) >= 10) == [0] * 10
Ejemplo n.º 54
0
def test_can_generate_naive_datetime():
    minimal(datetimes(allow_naive=True), lambda d: not d.tzinfo)
Ejemplo n.º 55
0
def test_can_find_off_the_minute():
    minimal(times(), lambda x: x.second == 0)
Ejemplo n.º 56
0
def test_can_simplify_on_both_sides_of_flatmap():
    assert minimal(integers().flatmap(lambda x: lists(just(x))),
                   lambda x: len(x) >= 10) == [0] * 10
Ejemplo n.º 57
0
def test_minimize_single_element_in_silly_large_int_range():
    ir = integers(-(2**256), 2**256)
    assert minimal(ir, lambda x: x >= -(2**255)) == -(2**255)
Ejemplo n.º 58
0
def test_minimize_one_of_distinct_types():
    y = booleans() | binary()
    x = minimal(tuples(y, y), lambda x: type(x[0]) != type(x[1]))
    assert x in ((False, b''), (b'', False))
Ejemplo n.º 59
0
def test_minimize_mixed_list():
    mixed = minimal(lists(integers() | text()), lambda x: len(x) >= 10)
    assert set(mixed).issubset(set((0, u'')))
Ejemplo n.º 60
0
def test_can_find_non_midnight():
    assert minimal(datetimes(), lambda x: x.hour != 0).hour == 1