How to use attempt_replace method in hypothesis

Best Python code snippet using hypothesis

optimiser.py

Source:optimiser.py Github

copy

Full Screen

...89 existing_as_int = int_from_bytes(existing)90 max_int_value = (256 ** len(existing)) - 191 if existing_as_int == max_int_value:92 continue93 def attempt_replace(v):94 """Try replacing the current block in the current best test case95 with an integer of value i. Note that we use the *current*96 best and not the one we started with. This helps ensure that97 if we luck into a good draw when making random choices we get98 to keep the good bits."""99 if v < 0 or v > max_int_value:100 return False101 v_as_bytes = int_to_bytes(v, len(existing))102 # We make a couple attempts at replacement. This only matters103 # if we end up growing the buffer - otherwise we exit the loop104 # early - but in the event that there *is* some randomized105 # component we want to give it a couple of tries to succeed.106 for _ in range(3):107 attempt = self.engine.cached_test_function(108 prefix109 + v_as_bytes110 + self.current_data.buffer[block.end :]111 + bytes(BUFFER_SIZE),112 )113 if self.consider_new_test_data(attempt):114 return True115 if attempt.status < Status.INVALID or len(attempt.buffer) == len(116 self.current_data.buffer117 ):118 return False119 for i, ex in enumerate(self.current_data.examples):120 if ex.start >= block.end:121 break122 if ex.end <= block.start:123 continue124 ex_attempt = attempt.examples[i]125 if ex.length == ex_attempt.length:126 continue127 replacement = attempt.buffer[ex_attempt.start : ex_attempt.end]128 if self.consider_new_test_data(129 self.engine.cached_test_function(130 prefix131 + replacement132 + self.current_data.buffer[ex.end :]133 )134 ):135 return True136 return False137 # We unconditionally scan both upwards and downwards. The reason138 # for this is that we allow "lateral" moves that don't increase the139 # score but instead leave it constant. All else being equal we'd140 # like to leave the test case closer to shrunk, so afterwards we141 # try lowering the value towards zero even if we've just raised it.142 if not attempt_replace(max_int_value):143 find_integer(lambda k: attempt_replace(k + existing_as_int))144 existing = self.current_data.buffer[block.start : block.end]145 existing_as_int = int_from_bytes(existing)146 if not attempt_replace(0):...

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