How to use minimize_duplicated_blocks method in hypothesis

Best Python code snippet using hypothesis

engine.py

Source:engine.py Github

copy

Full Screen

...687 self.incorporate_new_buffer(688 buf[:u] + hbytes(v - u) + buf[v:]689 )690 i += 1691 def minimize_duplicated_blocks(self):692 """Find blocks that have been duplicated in multiple places and attempt693 to minimize all of the duplicates simultaneously."""694 self.debug('Simultaneous shrinking of duplicated blocks')695 counts = Counter(696 self.last_data.buffer[u:v] for u, v in self.last_data.blocks697 )698 blocks = [699 k for k, count in700 counts.items()701 if count > 1702 ]703 thresholds = {}704 for u, v in self.last_data.blocks:705 b = self.last_data.buffer[u:v]706 thresholds[b] = v707 blocks.sort(reverse=True)708 blocks.sort(key=lambda b: counts[b] * len(b), reverse=True)709 for block in blocks:710 parts = [711 self.last_data.buffer[r:s]712 for r, s in self.last_data.blocks713 ]714 def replace(b):715 return hbytes(EMPTY_BYTES.join(716 hbytes(b if c == block else c) for c in parts717 ))718 threshold = thresholds[block]719 minimize(720 block,721 lambda b: self.try_buffer_with_rewriting_from(722 replace(b), threshold),723 random=self.random, full=False724 )725 def minimize_individual_blocks(self):726 self.debug('Shrinking of individual blocks')727 i = 0728 while i < len(self.last_data.blocks):729 u, v = self.last_data.blocks[i]730 minimize(731 self.last_data.buffer[u:v],732 lambda b: self.try_buffer_with_rewriting_from(733 self.last_data.buffer[:u] + b +734 self.last_data.buffer[v:], v735 ),736 random=self.random, full=False,737 )738 i += 1739 def reorder_blocks(self):740 self.debug('Reordering blocks')741 block_lengths = sorted(self.last_data.block_starts, reverse=True)742 for n in block_lengths:743 i = 1744 while i < len(self.last_data.block_starts.get(n, ())):745 j = i746 while j > 0:747 buf = self.last_data.buffer748 blocks = self.last_data.block_starts[n]749 a_start = blocks[j - 1]750 b_start = blocks[j]751 a = buf[a_start:a_start + n]752 b = buf[b_start:b_start + n]753 if a <= b:754 break755 swapped = (756 buf[:a_start] + b + buf[a_start + n:b_start] +757 a + buf[b_start + n:])758 assert len(swapped) == len(buf)759 assert swapped < buf760 if self.incorporate_new_buffer(swapped):761 j -= 1762 else:763 break764 i += 1765 def shrink(self):766 # We assume that if an all-zero block of bytes is an interesting767 # example then we're not going to do better than that.768 # This might not technically be true: e.g. for integers() | booleans()769 # the simplest example is actually [1, 0]. Missing this case is fairly770 # harmless and this allows us to make various simplifying assumptions771 # about the structure of the data (principally that we're never772 # operating on a block of all zero bytes so can use non-zeroness as a773 # signpost of complexity).774 if (775 not any(self.last_data.buffer) or776 self.incorporate_new_buffer(hbytes(len(self.last_data.buffer)))777 ):778 self.exit_reason = ExitReason.finished779 return780 if self.has_existing_examples():781 corpus = sorted(782 self.settings.database.fetch(self.database_key),783 key=sort_key784 )785 # We always have self.last_data.buffer in the database because786 # we save every interesting example. This means we will always787 # trigger the first break and thus never exit the loop normally.788 for c in corpus: # pragma: no branch789 if sort_key(c) >= sort_key(self.last_data.buffer):790 break791 elif self.incorporate_new_buffer(c):792 break793 else:794 self.settings.database.delete(self.database_key, c)795 # Coarse passes that are worth running once when the example is likely796 # to be "far from shrunk" but not worth repeating in a loop because797 # they are subsumed by more fine grained passes.798 self.delta_interval_deletion()799 self.coarse_block_replacement()800 change_counter = -1801 while self.shrinks > change_counter:802 change_counter = self.shrinks803 self.minimize_duplicated_blocks()804 self.minimize_individual_blocks()805 self.reorder_blocks()806 self.greedy_interval_deletion()807 self.exit_reason = ExitReason.finished808 def event_to_string(self, event):809 if isinstance(event, str):810 return event811 try:812 return self.events_to_strings[event]813 except KeyError:814 pass815 result = str(event)816 self.events_to_strings[event] = result817 return result...

Full Screen

Full Screen

test_shrinker.py

Source:test_shrinker.py Github

copy

Full Screen

1# This file is part of Hypothesis, which may be found at2# https://github.com/HypothesisWorks/hypothesis/3#4# Most of this work is copyright (C) 2013-2020 David R. MacIver5# (david@drmaciver.com), but it contains contributions by others. See6# CONTRIBUTING.rst for a full list of people who may hold copyright, and7# consult the git log if you need to determine who owns an individual8# contribution.9#10# This Source Code Form is subject to the terms of the Mozilla Public License,11# v. 2.0. If a copy of the MPL was not distributed with this file, You can12# obtain one at https://mozilla.org/MPL/2.0/.13#14# END HEADER15import time16import pytest17from hypothesis.internal.compat import int_to_bytes18from hypothesis.internal.conjecture import floats as flt19from hypothesis.internal.conjecture.engine import ConjectureRunner20from hypothesis.internal.conjecture.shrinker import (21 Shrinker,22 ShrinkPass,23 StopShrinking,24 block_program,25)26from hypothesis.internal.conjecture.shrinking import Float27from hypothesis.internal.conjecture.utils import Sampler28from tests.conjecture.common import SOME_LABEL, run_to_buffer, shrinking_from29@pytest.mark.parametrize("n", [1, 5, 8, 15])30def test_can_shrink_variable_draws_with_just_deletion(n, monkeypatch):31 @shrinking_from([n] + [0] * (n - 1) + [1])32 def shrinker(data):33 n = data.draw_bits(4)34 b = [data.draw_bits(8) for _ in range(n)]35 if any(b):36 data.mark_interesting()37 shrinker.fixate_shrink_passes(["minimize_individual_blocks"])38 assert list(shrinker.shrink_target.buffer) == [1, 1]39def test_deletion_and_lowering_fails_to_shrink(monkeypatch):40 monkeypatch.setattr(41 Shrinker,42 "shrink",43 lambda self: self.fixate_shrink_passes(["minimize_individual_blocks"]),44 )45 def gen(self):46 self.cached_test_function(10)47 monkeypatch.setattr(ConjectureRunner, "generate_new_examples", gen)48 @run_to_buffer49 def x(data):50 for _ in range(10):51 data.draw_bytes(1)52 data.mark_interesting()53 assert x == bytes(10)54def test_duplicate_blocks_that_go_away():55 @shrinking_from([1, 1, 1, 2] * 2 + [5] * 2)56 def shrinker(data):57 x = data.draw_bits(32)58 y = data.draw_bits(32)59 if x != y:60 data.mark_invalid()61 b = [data.draw_bytes(1) for _ in range(x & 255)]62 if len(set(b)) <= 1:63 data.mark_interesting()64 shrinker.fixate_shrink_passes(["minimize_duplicated_blocks"])65 assert shrinker.shrink_target.buffer == bytes(8)66def test_accidental_duplication(monkeypatch):67 @shrinking_from([18] * 20)68 def shrinker(data):69 x = data.draw_bits(8)70 y = data.draw_bits(8)71 if x != y:72 data.mark_invalid()73 if x < 5:74 data.mark_invalid()75 b = [data.draw_bytes(1) for _ in range(x)]76 if len(set(b)) == 1:77 data.mark_interesting()78 shrinker.fixate_shrink_passes(["minimize_duplicated_blocks"])79 assert list(shrinker.buffer) == [5] * 780def test_can_zero_subintervals(monkeypatch):81 @shrinking_from(bytes([3, 0, 0, 0, 1]) * 10)82 def shrinker(data):83 for _ in range(10):84 data.start_example(SOME_LABEL)85 n = data.draw_bits(8)86 data.draw_bytes(n)87 data.stop_example()88 if data.draw_bits(8) != 1:89 return90 data.mark_interesting()91 shrinker.shrink()92 assert list(shrinker.buffer) == [0, 1] * 1093def test_can_pass_to_an_indirect_descendant(monkeypatch):94 def tree(data):95 data.start_example(1)96 n = data.draw_bits(1)97 label = data.draw_bits(8)98 if n:99 tree(data)100 tree(data)101 data.stop_example(1)102 return label103 initial = bytes([1, 10, 0, 0, 1, 0, 0, 10, 0, 0])104 target = bytes([0, 10])105 good = {initial, target}106 @shrinking_from(initial)107 def shrinker(data):108 tree(data)109 if bytes(data.buffer) in good:110 data.mark_interesting()111 shrinker.fixate_shrink_passes(["pass_to_descendant"])112 assert shrinker.shrink_target.buffer == target113def shrink(buffer, *passes):114 def accept(f):115 shrinker = shrinking_from(buffer)(f)116 shrinker.fixate_shrink_passes(passes)117 return list(shrinker.buffer)118 return accept119def test_shrinking_blocks_from_common_offset():120 @shrinking_from([11, 10])121 def shrinker(data):122 m = data.draw_bits(8)123 n = data.draw_bits(8)124 if abs(m - n) <= 1 and max(m, n) > 0:125 data.mark_interesting()126 shrinker.mark_changed(0)127 shrinker.mark_changed(1)128 shrinker.lower_common_block_offset()129 x = shrinker.shrink_target.buffer130 assert sorted(x) == [0, 1]131def test_handle_empty_draws():132 @run_to_buffer133 def x(data):134 while True:135 data.start_example(SOME_LABEL)136 n = data.draw_bits(1)137 data.start_example(SOME_LABEL)138 data.stop_example()139 data.stop_example(discard=n > 0)140 if not n:141 break142 data.mark_interesting()143 assert x == bytes([0])144def test_can_reorder_examples():145 @shrinking_from([1, 0, 1, 1, 0, 1, 0, 0, 0])146 def shrinker(data):147 total = 0148 for _ in range(5):149 data.start_example(0)150 if data.draw_bits(8):151 total += data.draw_bits(9)152 data.stop_example(0)153 if total == 2:154 data.mark_interesting()155 shrinker.fixate_shrink_passes(["reorder_examples"])156 assert list(shrinker.buffer) == [0, 0, 0, 1, 0, 1, 1, 0, 1]157def test_permits_but_ignores_raising_order(monkeypatch):158 monkeypatch.setattr(159 ConjectureRunner,160 "generate_new_examples",161 lambda runner: runner.cached_test_function([1]),162 )163 monkeypatch.setattr(164 Shrinker, "shrink", lambda self: self.incorporate_new_buffer(bytes([2]))165 )166 @run_to_buffer167 def x(data):168 data.draw_bits(2)169 data.mark_interesting()170 assert list(x) == [1]171def test_block_deletion_can_delete_short_ranges(monkeypatch):172 @shrinking_from([v for i in range(5) for _ in range(i + 1) for v in [0, i]])173 def shrinker(data):174 while True:175 n = data.draw_bits(16)176 for _ in range(n):177 if data.draw_bits(16) != n:178 data.mark_invalid()179 if n == 4:180 data.mark_interesting()181 shrinker.fixate_shrink_passes([block_program("X" * i) for i in range(1, 5)])182 assert list(shrinker.shrink_target.buffer) == [0, 4] * 5183def test_try_shrinking_blocks_ignores_overrun_blocks(monkeypatch):184 monkeypatch.setattr(185 ConjectureRunner,186 "generate_new_examples",187 lambda runner: runner.cached_test_function([3, 3, 0, 1]),188 )189 monkeypatch.setattr(190 Shrinker,191 "shrink",192 lambda self: self.try_shrinking_blocks((0, 1, 5), bytes([2])),193 )194 @run_to_buffer195 def x(data):196 n1 = data.draw_bits(8)197 data.draw_bits(8)198 if n1 == 3:199 data.draw_bits(8)200 k = data.draw_bits(8)201 if k == 1:202 data.mark_interesting()203 assert list(x) == [2, 2, 1]204def test_dependent_block_pairs_is_up_to_shrinking_integers():205 # Unit test extracted from a failure in tests/nocover/test_integers.py206 distribution = Sampler([4.0, 8.0, 1.0, 1.0, 0.5])207 sizes = [8, 16, 32, 64, 128]208 @shrinking_from(b"\x03\x01\x00\x00\x00\x00\x00\x01\x00\x02\x01")209 def shrinker(data):210 size = sizes[distribution.sample(data)]211 result = data.draw_bits(size)212 sign = (-1) ** (result & 1)213 result = (result >> 1) * sign214 cap = data.draw_bits(8)215 if result >= 32768 and cap == 1:216 data.mark_interesting()217 shrinker.fixate_shrink_passes(["minimize_individual_blocks"])218 assert list(shrinker.shrink_target.buffer) == [1, 1, 0, 1, 0, 0, 1]219def test_finding_a_minimal_balanced_binary_tree():220 # Tests iteration while the shape of the thing being iterated over can221 # change. In particular the current example can go from trivial to non222 # trivial.223 def tree(data):224 # Returns height of a binary tree and whether it is height balanced.225 data.start_example("tree")226 n = data.draw_bits(1)227 if n == 0:228 result = (1, True)229 else:230 h1, b1 = tree(data)231 h2, b2 = tree(data)232 result = (1 + max(h1, h2), b1 and b2 and abs(h1 - h2) <= 1)233 data.stop_example("tree")234 return result235 # Starting from an unbalanced tree of depth six236 @shrinking_from([1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0])237 def shrinker(data):238 _, b = tree(data)239 if not b:240 data.mark_interesting()241 shrinker.shrink()242 assert list(shrinker.shrink_target.buffer) == [1, 0, 1, 0, 1, 0, 0]243def test_float_shrink_can_run_when_canonicalisation_does_not_work(monkeypatch):244 # This should be an error when called245 monkeypatch.setattr(Float, "shrink", None)246 base_buf = int_to_bytes(flt.base_float_to_lex(1000.0), 8) + bytes(1)247 @shrinking_from(base_buf)248 def shrinker(data):249 flt.draw_float(data)250 if bytes(data.buffer) == base_buf:251 data.mark_interesting()252 shrinker.fixate_shrink_passes(["minimize_floats"])253 assert shrinker.shrink_target.buffer == base_buf254def test_try_shrinking_blocks_out_of_bounds():255 @shrinking_from(bytes([1]))256 def shrinker(data):257 data.draw_bits(1)258 data.mark_interesting()259 assert not shrinker.try_shrinking_blocks((1,), bytes([1]))260def test_block_programs_are_adaptive():261 @shrinking_from(bytes(1000) + bytes([1]))262 def shrinker(data):263 while not data.draw_bits(1):264 pass265 data.mark_interesting()266 p = shrinker.add_new_pass(block_program("X"))267 shrinker.fixate_shrink_passes([p.name])268 assert len(shrinker.shrink_target.buffer) == 1269 assert shrinker.calls <= 60270def test_zero_examples_with_variable_min_size():271 @shrinking_from(bytes([255]) * 100)272 def shrinker(data):273 any_nonzero = False274 for i in range(1, 10):275 any_nonzero |= data.draw_bits(i * 8) > 0276 if not any_nonzero:277 data.mark_invalid()278 data.mark_interesting()279 shrinker.shrink()280 assert len([d for d in shrinker.shrink_target.blocks if not d.all_zero]) == 1281def test_zero_contained_examples():282 @shrinking_from(bytes([1]) * 8)283 def shrinker(data):284 for _ in range(4):285 data.start_example(1)286 if data.draw_bits(8) == 0:287 data.mark_invalid()288 data.start_example(1)289 data.draw_bits(8)290 data.stop_example()291 data.stop_example()292 data.mark_interesting()293 shrinker.shrink()294 assert list(shrinker.shrink_target.buffer) == [1, 0] * 4295def test_zig_zags_quickly():296 @shrinking_from(bytes([255]) * 4)297 def shrinker(data):298 m = data.draw_bits(16)299 n = data.draw_bits(16)300 if m == 0 or n == 0:301 data.mark_invalid()302 if abs(m - n) <= 1:303 data.mark_interesting(0)304 # Two different interesting origins for avoiding slipping in the305 # shrinker.306 if abs(m - n) <= 10:307 data.mark_interesting(1)308 shrinker.fixate_shrink_passes(["minimize_individual_blocks"])309 assert shrinker.engine.valid_examples <= 100310 assert list(shrinker.shrink_target.buffer) == [0, 1, 0, 1]311def test_zero_irregular_examples():312 @shrinking_from([255] * 6)313 def shrinker(data):314 data.start_example(1)315 data.draw_bits(8)316 data.draw_bits(16)317 data.stop_example()318 data.start_example(1)319 interesting = data.draw_bits(8) > 0 and data.draw_bits(16) > 0320 data.stop_example()321 if interesting:322 data.mark_interesting()323 shrinker.shrink()324 assert list(shrinker.shrink_target.buffer) == [0] * 3 + [1, 0, 1]325def test_retain_end_of_buffer():326 @shrinking_from([1, 2, 3, 4, 5, 6, 0])327 def shrinker(data):328 interesting = False329 while True:330 n = data.draw_bits(8)331 if n == 6:332 interesting = True333 if n == 0:334 break335 if interesting:336 data.mark_interesting()337 shrinker.shrink()338 assert list(shrinker.buffer) == [6, 0]339def test_can_expand_zeroed_region():340 @shrinking_from([255] * 5)341 def shrinker(data):342 seen_non_zero = False343 for _ in range(5):344 if data.draw_bits(8) == 0:345 if seen_non_zero:346 data.mark_invalid()347 else:348 seen_non_zero = True349 data.mark_interesting()350 shrinker.shrink()351 assert list(shrinker.shrink_target.buffer) == [0] * 5352def test_can_expand_deleted_region():353 @shrinking_from([1, 2, 3, 4, 0, 0])354 def shrinker(data):355 def t():356 data.start_example(1)357 data.start_example(1)358 m = data.draw_bits(8)359 data.stop_example()360 data.start_example(1)361 n = data.draw_bits(8)362 data.stop_example()363 data.stop_example()364 return (m, n)365 v1 = t()366 if v1 == (1, 2):367 if t() != (3, 4):368 data.mark_invalid()369 if v1 == (0, 0) or t() == (0, 0):370 data.mark_interesting()371 shrinker.shrink()372 assert list(shrinker.buffer) == [0, 0]373def test_shrink_pass_method_is_idempotent():374 @shrinking_from([255])375 def shrinker(data):376 data.draw_bits(8)377 data.mark_interesting()378 sp = shrinker.shrink_pass(block_program("X"))379 assert isinstance(sp, ShrinkPass)380 assert shrinker.shrink_pass(sp) is sp381def test_will_terminate_stalled_shrinks():382 # Suppress the time based slow shrinking check - we only want383 # the one that checks if we're in a stall where we've shrunk384 # as far as we're going to get.385 time.freeze()386 @shrinking_from([255] * 100)387 def shrinker(data):388 count = 0389 for _ in range(100):390 if data.draw_bits(8) != 255:391 count += 1392 if count >= 10:393 return394 data.mark_interesting()395 shrinker.shrink()396 assert shrinker.calls <= 1 + 2 * shrinker.max_stall397def test_will_let_fixate_shrink_passes_do_a_full_run_through():398 @shrinking_from(range(50))399 def shrinker(data):400 for i in range(50):401 if data.draw_bits(8) != i:402 data.mark_invalid()403 data.mark_interesting()404 shrinker.max_stall = 5405 passes = [block_program("X" * i) for i in range(1, 11)]406 with pytest.raises(StopShrinking):407 shrinker.fixate_shrink_passes(passes)408 assert shrinker.shrink_pass(passes[-1]).calls > 0409@pytest.mark.parametrize("n_gap", [0, 1, 2, 3])410def test_can_simultaneously_lower_non_duplicated_nearby_blocks(n_gap):411 @shrinking_from([1, 1] + [0] * n_gap + [0, 2])412 def shrinker(data):413 # Block off lowering the whole buffer414 if data.draw_bits(1) == 0:415 data.mark_invalid()416 m = data.draw_bits(8)417 for _ in range(n_gap):418 data.draw_bits(8)419 n = data.draw_bits(16)420 if n == m + 1:421 data.mark_interesting()422 shrinker.fixate_shrink_passes(["lower_blocks_together"])...

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