def test_converts_floats_to_integer_form(f): assert flt.is_simple(f) buf = int_to_bytes(flt.base_float_to_lex(f), 8) runner = float_runner(f, lambda g: g == f) runner.shrink_interesting_examples() (v, ) = runner.interesting_examples.values() assert v.buffer[:-1] < buf
def test_converts_floats_to_integer_form(f): assert flt.is_simple(f) buf = int_to_bytes(flt.base_float_to_lex(f), 8) def parse_buf(b): return flt.lex_to_float(int_from_bytes(b)) shrunk = Lexical.shrink(buf, lambda b: parse_buf(b) == f, full=True, random=Random(0)) assert shrunk < buf
def test_converts_floats_to_integer_form(f): assert flt.is_simple(f) buf = int_to_bytes(flt.base_float_to_lex(f), 8) def parse_buf(b): return flt.lex_to_float(int_from_bytes(b)) shrunk = Lexical.shrink( buf, lambda b: parse_buf(b) == f, full=True, random=Random(0) ) assert shrunk < buf
def float_hack(self): """Our encoding of floating point numbers does the right thing when you lexically shrink it, but there are some highly non-obvious lexical shrinks corresponding to natural floating point operations. We can't actually tell when the floating point encoding is being used (that would break the assumptions that Hypothesis doesn't inspect the generated values), but we can cheat: We just guess when it might be being used and perform shrinks that are valid regardless of our guess is correct. So that's what this method does. It's a cheat to give us good shrinking of floating at low cost in runtime and only moderate cost in elegance. """ # If the block is of the wrong size then we're certainly not using the # float encoding. if self.size != 8: return # If the high bit is zero then we're in the integer representation of # floats so we don't need these hacks because it will shrink normally. if self.current[0] >> 7 == 0: return i = self.current_int f = lex_to_float(i) # This floating point number can be represented in our simple format. # So we try converting it to that (which will give the same float, but # a different encoding of it). If that doesn't work then the float # value of this doesn't unambiguously give the desired predicate, so # this approach isn't useful. If it *does* work, then we're now in a # situation where we don't need it, so either way we return here. if is_simple(f): self.incorporate_float(f) return self.delegate( Float, convert_to=lambda b: lex_to_float(int_from_bytes(b)), convert_from=lambda f: int_to_bytes(float_to_lex(f), self.size), )
def float_hack(self): """Our encoding of floating point numbers does the right thing when you lexically shrink it, but there are some highly non-obvious lexical shrinks corresponding to natural floating point operations. We can't actually tell when the floating point encoding is being used (that would break the assumptions that Hypothesis doesn't inspect the generated values), but we can cheat: We just guess when it might be being used and perform shrinks that are valid regardless of our guess is correct. So that's what this method does. It's a cheat to give us good shrinking of floating at low cost in runtime and only moderate cost in elegance. """ # If the block is of the wrong size then we're certainly not using the # float encoding. if self.size != 8: return # If the high bit is zero then we're in the integer representation of # floats so we don't need these hacks because it will shrink normally. if self.current[0] >> 7 == 0: return i = self.current_int f = lex_to_float(i) # This floating point number can be represented in our simple format. # So we try converting it to that (which will give the same float, but # a different encoding of it). If that doesn't work then the float # value of this doesn't unambiguously give the desired predicate, so # this approach isn't useful. If it *does* work, then we're now in a # situation where we don't need it, so either way we return here. if is_simple(f): self.incorporate_float(f) return self.delegate( Float, convert_to=lambda b: lex_to_float(int_from_bytes(b)), convert_from=lambda f: int_to_bytes(float_to_lex(f), self.size), )
def float_hack(self): """Our encoding of floating point numbers does the right thing when you lexically shrink it, but there are some highly non-obvious lexical shrinks corresponding to natural floating point operations. We can't actually tell when the floating point encoding is being used (that would break the assumptions that Hypothesis doesn't inspect the generated values), but we can cheat: We just guess when it might be being used and perform shrinks that are valid regardless of our guess is correct. So that's what this method does. It's a cheat to give us good shrinking of floating at low cost in runtime and only moderate cost in elegance. """ # If the block is of the wrong size then we're certainly not using the # float encoding. if self.size != 8: return # If the high bit is zero then we're in the integer representation of # floats so we don't need these hacks because it will shrink normally. if self.current[0] >> 7 == 0: return i = self.current_int f = lex_to_float(i) # This floating point number can be represented in our simple format. # So we try converting it to that (which will give the same float, but # a different encoding of it). If that doesn't work then the float # value of this doesn't unambiguously give the desired predicate, so # this approach isn't useful. If it *does* work, then we're now in a # situation where we don't need it, so either way we return here. if is_simple(f): self.incorporate_float(f) return # We check for a bunch of standard "large" floats. If we're currently # worse than them and the shrink downwards doesn't help, abort early # because there's not much useful we can do here. for g in [ float('nan'), float('inf'), sys.float_info.max, ]: j = float_to_lex(g) if j < i: if self.incorporate_int(j): f = g i = j if math.isinf(f) or math.isnan(f): return # Finally we get to the important bit: Each of these is a small change # to the floating point number that corresponds to a large change in # the lexical representation. Trying these ensures that our floating # point shrink can always move past these obstacles. In particular it # ensures we can always move to integer boundaries and shrink past a # change that would require shifting the exponent while not changing # the float value much. for g in [floor(f), ceil(f)]: if self.incorporate_float(g): return if f > 2: self.incorporate_float(f - 1)
def float_hack(self): """Our encoding of floating point numbers does the right thing when you lexically shrink it, but there are some highly non-obvious lexical shrinks corresponding to natural floating point operations. We can't actually tell when the floating point encoding is being used (that would break the assumptions that Hypothesis doesn't inspect the generated values), but we can cheat: We just guess when it might be being used and perform shrinks that are valid regardless of our guess is correct. So that's what this method does. It's a cheat to give us good shrinking of floating at low cost in runtime and only moderate cost in elegance. """ # If the block is of the wrong size then we're certainly not using the # float encoding. if self.size != 8: return # If the high bit is zero then we're in the integer representation of # floats so we don't need these hacks because it will shrink normally. if self.current[0] >> 7 == 0: return i = int_from_bytes(self.current) f = lex_to_float(i) # This floating point number can be represented in our simple format. # So we try converting it to that (which will give the same float, but # a different encoding of it). If that doesn't work then the float # value of this doesn't unambiguously give the desired predicate, so # this approach isn't useful. If it *does* work, then we're now in a # situation where we don't need it, so either way we return here. if is_simple(f): self.incorporate_float(f) return # We check for a bunch of standard "large" floats. If we're currently # worse than them and the shrink downwards doesn't help, abort early # because there's not much useful we can do here. for g in [ float('nan'), float('inf'), sys.float_info.max, ]: j = float_to_lex(g) if j < i: if self.incorporate_int(j): f = g i = j if math.isinf(f) or math.isnan(f): return # Finally we get to the important bit: Each of these is a small change # to the floating point number that corresponds to a large change in # the lexical representation. Trying these ensures that our floating # point shrink can always move past these obstacles. In particular it # ensures we can always move to integer boundaries and shrink past a # change that would require shifting the exponent while not changing # the float value much. for g in [ floor(f), ceil(f), ]: if self.incorporate_float(g): return if f > 2: self.incorporate_float(f - 1)