How to use incorporate_int method in hypothesis

Best Python code snippet using hypothesis

lexical.py

Source:lexical.py Github

copy

Full Screen

...34 def check_invariants(self, value):35 assert len(value) == self.size36 def left_is_better(self, left, right):37 return left < right38 def incorporate_int(self, i):39 return self.incorporate(int_to_bytes(i, self.size))40 def incorporate_float(self, f):41 assert self.size == 842 return self.incorporate_int(float_to_lex(f))43 def float_hack(self):44 """Our encoding of floating point numbers does the right thing when you45 lexically shrink it, but there are some highly non-obvious lexical46 shrinks corresponding to natural floating point operations.47 We can't actually tell when the floating point encoding is being used48 (that would break the assumptions that Hypothesis doesn't inspect49 the generated values), but we can cheat: We just guess when it might be50 being used and perform shrinks that are valid regardless of our guess51 is correct.52 So that's what this method does. It's a cheat to give us good shrinking53 of floating at low cost in runtime and only moderate cost in elegance.54 """55 # If the block is of the wrong size then we're certainly not using the56 # float encoding.57 if self.size != 8:58 return59 # If the high bit is zero then we're in the integer representation of60 # floats so we don't need these hacks because it will shrink normally.61 if self.current[0] >> 7 == 0:62 return63 i = self.current_int64 f = lex_to_float(i)65 # This floating point number can be represented in our simple format.66 # So we try converting it to that (which will give the same float, but67 # a different encoding of it). If that doesn't work then the float68 # value of this doesn't unambiguously give the desired predicate, so69 # this approach isn't useful. If it *does* work, then we're now in a70 # situation where we don't need it, so either way we return here.71 if is_simple(f):72 self.incorporate_float(f)73 return74 self.delegate(75 Float,76 convert_to=lambda b: lex_to_float(int_from_bytes(b)),77 convert_from=lambda f: int_to_bytes(float_to_lex(f), self.size),78 )79 @property80 def current_int(self):81 return int_from_bytes(self.current)82 def minimize_as_integer(self, full=False):83 Integer.shrink(84 self.current_int,85 lambda c: c == self.current_int or self.incorporate_int(c),86 random=self.random, full=full,87 )88 def partial_sort(self):89 Ordering.shrink(90 self.current, self.consider,91 random=self.random,92 )93 def short_circuit(self):94 """This is just an assemblage of other shrinkers, so we rely on their95 short circuiting."""96 return False97 def run_step(self):98 self.float_hack()99 self.minimize_as_integer()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful