def test_flatmap_retrieve_from_db(): constant_float_lists = strategy(floats_in_range(0, 1)).flatmap( lambda x: [just(x)] ) track = [] db = ExampleDatabase() @given(constant_float_lists, settings=Settings(database=db)) def record_and_test_size(xs): track.append(xs) assert sum(xs) < 1 with pytest.raises(AssertionError): record_and_test_size() assert track example = track[-1] while track: track.pop() with pytest.raises(AssertionError): record_and_test_size() assert track[0] == example
def test_flatmap_retrieve_from_db(): constant_float_lists = strategy(floats_in_range( 0, 1)).flatmap(lambda x: [just(x)]) track = [] db = ExampleDatabase() @given(constant_float_lists, settings=Settings(database=db)) def record_and_test_size(xs): track.append(xs) assert sum(xs) < 1 with pytest.raises(AssertionError): record_and_test_size() assert track example = track[-1] while track: track.pop() with pytest.raises(AssertionError): record_and_test_size() assert track[0] == example
def test_check_serialization_preserves_changed_marker(): strat = strategy( streaming(floats_in_range(0.0, 2.2250738585072014e-308))) template = strat.draw_template( BuildContext(Random(0)), strat.draw_parameter(Random(0))) strat.reify(template)[0] simpler = next(strat.full_simplify( Random(0), template )) as_basic = strat.to_basic(simpler) assert as_basic == strat.to_basic(strat.from_basic(as_basic))
def test_float_ranges(): with pytest.raises(InvalidArgument): floats_in_range(0, float('inf')) with pytest.raises(InvalidArgument): floats_in_range(float('nan'), 0) with pytest.raises(InvalidArgument): floats_in_range(1, -1)
def test_floats_are_in_range(x, y, rand): assume(not (math.isnan(x) or math.isnan(y))) assume(not (math.isinf(x) or math.isinf(y))) x, y = sorted((x, y)) assume(x < y) with Settings(max_examples=10): @given(floats_in_range(x, y), random=rand) def test_is_in_range(t): assert x <= t <= y try: test_is_in_range() except Unsatisfiable: assume(False)
def test_single_float_in_range(): assert strategy(s.floats_in_range(1, 1)).example() == 1
from hypothesis import strategy from tests.common.basic import Bitfields, BoringBitfields, \ simplify_bitfield from hypothesis.specifiers import just, one_of, strings, streaming, \ dictionary, sampled_from, integers_from, floats_in_range, \ integers_in_range from tests.common.specifiers import Descriptor from hypothesis.strategytests import TemplatesFor, strategy_test_suite from hypothesis.internal.compat import text_type, binary_type from hypothesis.searchstrategy.basic import basic_strategy from hypothesis.searchstrategy.narytree import NAryTree TestIntegerRange = strategy_test_suite(integers_in_range(0, 5)) TestGiantIntegerRange = strategy_test_suite( integers_in_range(-(2**129), 2**129)) TestFloatRange = strategy_test_suite(floats_in_range(0.5, 10)) TestSampled10 = strategy_test_suite(sampled_from(elements=list(range(10)))) TestSampled1 = strategy_test_suite(sampled_from(elements=(1, ))) TestSampled2 = strategy_test_suite(sampled_from(elements=(1, 2))) TestIntegersFrom = strategy_test_suite(integers_from(13)) TestOneOf = strategy_test_suite(one_of((int, int, bool))) TestOneOfSameType = strategy_test_suite( one_of((integers_in_range(1, 10), integers_in_range(8, 15)))) TestRandom = strategy_test_suite(Random) TestInts = strategy_test_suite(int) TestBoolLists = strategy_test_suite([bool]) TestDictionaries = strategy_test_suite(dictionary((int, int), bool)) TestOrderedDictionaries = strategy_test_suite(dictionary( int, int, OrderedDict))
def test_find_non_boundary_float(): x = find(floats_in_range(1, 9), lambda x: x > 2) assert 2 < x < 3
assert all(not l for l in lines) @given(sampled_from([1])) def test_can_sample_from_single_element(x): assert x == 1 @fails @given([int]) def test_list_is_sorted(xs): assert sorted(xs) == xs @fails @given(floats_in_range(1.0, 2.0)) def test_is_an_endpoint(x): assert x == 1.0 or x == 2.0 @pytest.mark.parametrize('t', [1, 10, 100, 1000]) @fails @given(x=int) def test_is_bounded(t, x): assert x < t @given(x=bool) def test_can_test_kwargs_only_methods(**kwargs): assert isinstance(kwargs['x'], bool)
def test_finds_boundary_floats(): assert find(floats_in_range(-1, 1), lambda x: True) == -1
ABC = namedtuple('ABC', ('a', 'b', 'c')) standard_types = [ Bitfields, [], (), set(), frozenset(), {}, NAryTree(bool, bool, bool), ABC(bool, bool, bool), ABC(bool, bool, int), {'a': int, 'b': bool}, one_of((int, (bool,))), sampled_from(range(10)), one_of((just('a'), just('b'), just('c'))), sampled_from(('a', 'b', 'c')), int, integers_from(3), integers_in_range(-2 ** 32, 2 ** 64), float, floats_in_range(-2.0, 3.0), text_type, binary_type, bool, (bool, bool), frozenset({int}), complex, Fraction, Decimal, [[bool]], OrderedPair, ConstantList(int), strategy(streaming(int)).map(lambda x: list(x[:2]) and x), strategy(int).filter(lambda x: abs(x) > 100), floats_in_range(-sys.float_info.max, sys.float_info.max), None, Random, ]
@given(float) def test_is_float(x): assert isinstance(x, float) @fails @given(float) def test_inversion_is_imperfect(x): assume(x != 0.0) y = 1.0 / x assert x * y == 1.0 @given(floats_in_range(-sys.float_info.max, sys.float_info.max)) def test_largest_range(x): assert not math.isinf(x) @given(float) def test_negation_is_self_inverse(x): assume(not math.isnan(x)) y = -x assert -y == x @fails @given([float]) def test_is_not_nan(xs): assert not any(math.isnan(x) for x in xs)
def test_sampled_from_one(): assert strategy(s.sampled_from((1, ))).example() == 1 def test_basic(): assert isinstance(strategy(Bitfields).example(), integer_types) assert isinstance(strategy(Bitfields()).example(), integer_types) def test_tree(): tree = strategy(NAryTree(bool, bool, bool)).example() assert isinstance(tree, (Branch, Leaf)) @pytest.mark.parametrize(u'r', [ s.floats_in_range(0, 1), s.floats_in_range(1, 2), s.floats_in_range(1, 1), s.floats_in_range(-sys.float_info.max, sys.float_info.max), ]) def test_float_ranges(r): assert r.start <= strategy(r).example() <= r.end def test_strings(): x = strategy(s.strings(u'a')).example() assert set(x).issubset(set(u'a')) def test_empty_strings(): assert strategy(s.strings(u'')).example() == u''
def test_out_of_range_floats_are_bad(): with pytest.raises(BadData): strategy(floats_in_range(11, 12)).from_basic( strategy(floats_in_range(-1, 1)).to_basic(0.0) )
def test_float_range_validates(): with pytest.raises(InvalidArgument): s.floats_in_range(2, 1) with pytest.raises(InvalidArgument): s.floats_in_range(1, float(u'inf'))
def test_finds_boundary_floats(): def f(x): print(x) return True assert find(floats_in_range(-1, 1), f) == -1
NAryTree(bool, bool, bool), ABC(bool, bool, bool), ABC(bool, bool, int), { 'a': int, 'b': bool }, one_of((int, (bool, ))), sampled_from(range(10)), one_of((just('a'), just('b'), just('c'))), sampled_from(('a', 'b', 'c')), int, integers_from(3), integers_in_range(-2**32, 2**64), float, floats_in_range(-2.0, 3.0), text_type, binary_type, bool, (bool, bool), frozenset({int}), complex, Fraction, Decimal, [[bool]], OrderedPair, ConstantList(int), strategy(streaming(int)).map(lambda x: list(x[:2]) and x), strategy(int).filter(lambda x: abs(x) > 100), floats_in_range(-sys.float_info.max, sys.float_info.max), None,
from tests.common.basic import Bitfields, BoringBitfields, \ simplify_bitfield from hypothesis.specifiers import just, one_of, strings, streaming, \ dictionary, sampled_from, integers_from, floats_in_range, \ integers_in_range from tests.common.specifiers import Descriptor from hypothesis.strategytests import TemplatesFor, strategy_test_suite from hypothesis.internal.compat import text_type, binary_type from hypothesis.searchstrategy.basic import basic_strategy from hypothesis.searchstrategy.narytree import NAryTree TestIntegerRange = strategy_test_suite(integers_in_range(0, 5)) TestGiantIntegerRange = strategy_test_suite( integers_in_range(-(2 ** 129), 2 ** 129) ) TestFloatRange = strategy_test_suite(floats_in_range(0.5, 10)) TestSampled10 = strategy_test_suite(sampled_from(elements=list(range(10)))) TestSampled1 = strategy_test_suite(sampled_from(elements=(1,))) TestSampled2 = strategy_test_suite(sampled_from(elements=(1, 2))) TestIntegersFrom = strategy_test_suite(integers_from(13)) TestOneOf = strategy_test_suite(one_of((int, int, bool))) TestOneOfSameType = strategy_test_suite( one_of((integers_in_range(1, 10), integers_in_range(8, 15))) ) TestRandom = strategy_test_suite(Random) TestInts = strategy_test_suite(int) TestBoolLists = strategy_test_suite([bool]) TestDictionaries = strategy_test_suite(dictionary((int, int), bool)) TestOrderedDictionaries = strategy_test_suite(
test_can_produce_nan = define_test( float, 0.02, math.isnan ) test_can_produce_long_lists_of_positive_integers = define_test( [int], 0.05, lambda x: all(t >= 0 for t in x), condition=long_list ) test_can_produce_long_lists_of_negative_integers = define_test( [int], 0.05, lambda x: all(t <= 0 for t in x), condition=lambda x: len(x) >= 20 ) test_can_produce_floats_near_left = define_test( specifiers.floats_in_range(0, 1), 0.1, lambda t: t < 0.2 ) test_can_produce_floats_near_right = define_test( specifiers.floats_in_range(0, 1), 0.1, lambda t: t > 0.8 ) test_can_produce_floats_in_middle = define_test( specifiers.floats_in_range(0, 1), 0.3, lambda t: 0.2 <= t <= 0.8 ) test_can_produce_long_lists = define_test( [int], 0.5, long_list
def test_sampled_from_one(): assert strategy(s.sampled_from((1,))).example() == 1 def test_basic(): assert isinstance(strategy(Bitfields).example(), integer_types) assert isinstance(strategy(Bitfields()).example(), integer_types) def test_tree(): tree = strategy(NAryTree(bool, bool, bool)).example() assert isinstance(tree, (Branch, Leaf)) @pytest.mark.parametrize(u'r', [ s.floats_in_range(0, 1), s.floats_in_range(1, 2), s.floats_in_range(1, 1), s.floats_in_range(-sys.float_info.max, sys.float_info.max), ]) def test_float_ranges(r): assert r.start <= strategy(r).example() <= r.end def test_strings(): x = strategy(s.strings(u'a')).example() assert set(x).issubset(set(u'a')) def test_empty_strings(): assert strategy(s.strings(u'')).example() == u''
class TestIBMFloat(unittest.TestCase): def test_zero_from_float(self): zero = IBMFloat.from_float(0.0) self.assertTrue(zero.is_zero()) def test_zero_from_bytes(self): zero = IBMFloat.from_bytes(b'\x00\x00\x00\x00') self.assertTrue(zero.is_zero()) def test_subnormal(self): ibm = IBMFloat.from_float(1.6472184286297693e-83) self.assertTrue(ibm.is_subnormal()) def test_smallest_subnormal(self): ibm = IBMFloat.from_float(5.147557589468029e-85) self.assertEqual(bytes(ibm), bytes((0x00, 0x00, 0x00, 0x01))) def test_too_small_subnormal(self): with self.assertRaises(FloatingPointError): IBMFloat.from_float(1e-86) def test_nan(self): with self.assertRaises(ValueError): IBMFloat.from_float(float('nan')) def test_inf(self): with self.assertRaises(ValueError): IBMFloat.from_float(float('inf')) def test_too_large(self): with self.assertRaises(OverflowError): IBMFloat.from_float(MAX_IBM_FLOAT * 10) def test_too_small(self): with self.assertRaises(OverflowError): IBMFloat.from_float(MIN_IBM_FLOAT * 10) @given(floats_in_range(MIN_IBM_FLOAT, MAX_IBM_FLOAT)) def test_bool(self, f): self.assertEqual(bool(IBMFloat.from_float(f)), bool(f)) @given(integers_in_range(0, 255), integers_in_range(0, 255), integers_in_range(0, 255), integers_in_range(0, 255)) def test_bytes_roundtrip(self, a, b, c, d): b = bytes((a, b, c, d)) ibm = IBMFloat.from_bytes(b) self.assertEqual(bytes(ibm), b) @given(floats_in_range(MIN_IBM_FLOAT, MAX_IBM_FLOAT)) def test_floats_roundtrip(self, f): ibm = IBMFloat.from_float(f) self.assertTrue(almost_equal(f, float(ibm), epsilon=EPSILON_IBM_FLOAT)) @given(integers_in_range(0, MAX_EXACT_INTEGER_IBM_FLOAT - 1), floats_in_range(0.0, 1.0)) def test_trunc_above_zero(self, i, f): assume(f != 1.0) ieee = i + f ibm = IBMFloat.from_float(ieee) self.assertEqual(trunc(ibm), i) @given(integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT + 1, 0), floats_in_range(0.0, 1.0)) def test_trunc_below_zero(self, i, f): assume(f != 1.0) ieee = i - f ibm = IBMFloat.from_float(ieee) self.assertEqual(trunc(ibm), i) @given( integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT, MAX_EXACT_INTEGER_IBM_FLOAT - 1), floats_in_range(0.0, 1.0)) def test_ceil(self, i, f): assume(f != 1.0) ieee = i + f ibm = IBMFloat.from_float(ieee) self.assertEqual(math.ceil(ibm), i + 1) @given( integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT, MAX_EXACT_INTEGER_IBM_FLOAT - 1), floats_in_range(0.0, 1.0)) def test_floor(self, i, f): assume(f != 1.0) ieee = i + f ibm = IBMFloat.from_float(ieee) self.assertEqual(math.floor(ibm), i) def test_normalise_subnormal_expect_failure(self): # This float has an base-16 exponent of -64 (the minimum) and cannot be normalised ibm = IBMFloat.from_float(1.6472184286297693e-83) assert ibm.is_subnormal() with self.assertRaises(FloatingPointError): ibm.normalize() def test_normalise_subnormal1(self): ibm = IBMFloat.from_bytes( (0b01000000, 0b00000000, 0b11111111, 0b00000000)) assert ibm.is_subnormal() normalized = ibm.normalize() self.assertFalse(normalized.is_subnormal()) def test_normalise_subnormal2(self): ibm = IBMFloat.from_bytes((64, 1, 0, 0)) assert ibm.is_subnormal() normalized = ibm.normalize() self.assertFalse(normalized.is_subnormal()) @given(integers_in_range(128, 255), integers_in_range(0, 255), integers_in_range(0, 255), integers_in_range(4, 23)) def test_normalise_subnormal(self, b, c, d, shift): mantissa = (b << 16) | (c << 8) | d assume(mantissa != 0) mantissa >>= shift assert mantissa != 0 sa = EXPONENT_BIAS sb = (mantissa >> 16) & 0xff sc = (mantissa >> 8) & 0xff sd = mantissa & 0xff ibm = IBMFloat.from_bytes((sa, sb, sc, sd)) assert ibm.is_subnormal() normalized = ibm.normalize() self.assertFalse(normalized.is_subnormal()) @given(integers_in_range(128, 255), integers_in_range(0, 255), integers_in_range(0, 255), integers_in_range(4, 23)) def test_zero_subnormal(self, b, c, d, shift): mantissa = (b << 16) | (c << 8) | d assume(mantissa != 0) mantissa >>= shift assert mantissa != 0 sa = EXPONENT_BIAS sb = (mantissa >> 16) & 0xff sc = (mantissa >> 8) & 0xff sd = mantissa & 0xff ibm = IBMFloat.from_bytes((sa, sb, sc, sd)) assert ibm.is_subnormal() z = ibm.zero_subnormal() self.assertTrue(z.is_zero()) @given(integers_in_range(0, 255), integers_in_range(0, 255), integers_in_range(0, 255), integers_in_range(0, 255)) def test_abs(self, a, b, c, d): ibm = IBMFloat.from_bytes((a, b, c, d)) abs_ibm = abs(ibm) self.assertGreaterEqual(abs_ibm.signbit, 0) @given(integers_in_range(0, 255), integers_in_range(0, 255), integers_in_range(0, 255), integers_in_range(0, 255)) def test_negate_non_zero(self, a, b, c, d): ibm = IBMFloat.from_bytes((a, b, c, d)) assume(not ibm.is_zero()) negated = -ibm self.assertNotEqual(ibm.signbit, negated.signbit) def test_negate_zero(self): zero = IBMFloat.from_float(0.0) negated = -zero self.assertTrue(negated.is_zero()) @given(floats_in_range(MIN_IBM_FLOAT, MAX_IBM_FLOAT)) def test_signbit(self, f): ltz = f < 0 ibm = IBMFloat.from_float(f) self.assertEqual(ltz, ibm.signbit) @given(floats_in_range(-1.0, +1.0), integers_in_range(-256, 255)) def test_ldexp_frexp(self, fraction, exponent): try: ibm = IBMFloat.ldexp(fraction, exponent) except OverflowError: assume(False) else: f, e = ibm.frexp() self.assertTrue( almost_equal(fraction * 2**exponent, f * 2**e, epsilon=EPSILON_IBM_FLOAT)) @given(floats_in_range(MIN_IBM_FLOAT, MAX_IBM_FLOAT), floats_in_range(0.0, 1.0)) def test_add(self, f, p): a = f * p b = f - a ibm_a = IBMFloat.from_float(a) ibm_b = IBMFloat.from_float(b) ibm_c = ibm_a + ibm_b ieee_a = float(ibm_a) ieee_b = float(ibm_b) ieee_c = ieee_a + ieee_b self.assertTrue( almost_equal(ieee_c, ibm_c, epsilon=EPSILON_IBM_FLOAT * 4)) @given(floats_in_range(0, MAX_IBM_FLOAT), floats_in_range(0, MAX_IBM_FLOAT)) def test_sub(self, a, b): ibm_a = IBMFloat.from_float(a) ibm_b = IBMFloat.from_float(b) ibm_c = ibm_a - ibm_b ieee_a = float(ibm_a) ieee_b = float(ibm_b) ieee_c = ieee_a - ieee_b self.assertTrue(almost_equal(ieee_c, ibm_c, epsilon=EPSILON_IBM_FLOAT))
test_can_produce_negative_infinity = define_test(float, 0.02, lambda x: x == float('-inf')) test_can_produce_nan = define_test(float, 0.02, math.isnan) test_can_produce_long_lists_of_positive_integers = define_test( [int], 0.07, lambda x: all(t >= 0 for t in x), condition=long_list) test_can_produce_long_lists_of_negative_integers = define_test( [int], 0.07, lambda x: all(t <= 0 for t in x), condition=lambda x: len(x) >= 20) test_can_produce_floats_near_left = define_test( specifiers.floats_in_range(0, 1), 0.1, lambda t: t < 0.2) test_can_produce_floats_near_right = define_test( specifiers.floats_in_range(0, 1), 0.1, lambda t: t > 0.8) test_can_produce_floats_in_middle = define_test( specifiers.floats_in_range(0, 1), 0.3, lambda t: 0.2 <= t <= 0.8) test_can_produce_long_lists = define_test([int], 0.5, long_list) test_can_produce_short_lists = define_test([int], 0.2, lambda x: len(x) <= 10) test_can_produce_lists_bunched_near_left = define_test( [specifiers.floats_in_range(0, 1)], 0.1, lambda ts: all(t < 0.2 for t in ts),
def float_range(self, left, right): for f in (math.isnan, math.isinf): for x in (left, right): assume(not f(x)) left, right = sorted((left, right)) return strategy(floats_in_range(left, right))