How to use draw_bits method in hypothesis

Best Python code snippet using hypothesis

test_data_tree.py

Source:test_data_tree.py Github

copy

Full Screen

...41 return accept42def test_can_lookup_cached_examples():43 @runner_for(b"\0\0", b"\0\1")44 def runner(data):45 data.draw_bits(8)46 data.draw_bits(8)47def test_can_lookup_cached_examples_with_forced():48 @runner_for(b"\0\0", b"\0\1")49 def runner(data):50 data.write(b"\1")51 data.draw_bits(8)52def test_can_detect_when_tree_is_exhausted():53 @runner_for(b"\0", b"\1")54 def runner(data):55 data.draw_bits(1)56 assert runner.tree.is_exhausted57def test_can_detect_when_tree_is_exhausted_variable_size():58 @runner_for(b"\0", b"\1\0", b"\1\1")59 def runner(data):60 if data.draw_bits(1):61 data.draw_bits(1)62 assert runner.tree.is_exhausted63def test_one_dead_branch():64 @runner_for([[0, i] for i in range(16)] + [[i] for i in range(1, 16)])65 def runner(data):66 i = data.draw_bits(4)67 if i > 0:68 data.mark_invalid()69 data.draw_bits(4)70 assert runner.tree.is_exhausted71def test_non_dead_root():72 @runner_for(b"\0\0", b"\1\0", b"\1\1")73 def runner(data):74 data.draw_bits(1)75 data.draw_bits(1)76def test_can_reexecute_dead_examples():77 @runner_for(b"\0\0", b"\0\1", b"\0\0")78 def runner(data):79 data.draw_bits(1)80 data.draw_bits(1)81def test_novel_prefixes_are_novel():82 def tf(data):83 for _ in range(4):84 data.write(b"\0")85 data.draw_bits(2)86 runner = ConjectureRunner(tf, settings=TEST_SETTINGS, random=Random(0))87 for _ in range(100):88 prefix = runner.tree.generate_novel_prefix(runner.random)89 example = prefix + bytes(8 - len(prefix))90 assert runner.tree.rewrite(example)[1] is None91 result = runner.cached_test_function(example)92 assert runner.tree.rewrite(example)[0] == result.buffer93def test_overruns_if_not_enough_bytes_for_block():94 runner = ConjectureRunner(95 lambda data: data.draw_bytes(2), settings=TEST_SETTINGS, random=Random(0)96 )97 runner.cached_test_function(b"\0\0")98 assert runner.tree.rewrite(b"\0")[1] == Status.OVERRUN99def test_overruns_if_prefix():100 runner = ConjectureRunner(101 lambda data: [data.draw_bits(1) for _ in range(2)],102 settings=TEST_SETTINGS,103 random=Random(0),104 )105 runner.cached_test_function(b"\0\0")106 assert runner.tree.rewrite(b"\0")[1] == Status.OVERRUN107def test_stores_the_tree_flat_until_needed():108 @runner_for(bytes(10))109 def runner(data):110 for _ in range(10):111 data.draw_bits(1)112 data.mark_interesting()113 root = runner.tree.root114 assert len(root.bit_lengths) == 10115 assert len(root.values) == 10116 assert root.transition.status == Status.INTERESTING117def test_split_in_the_middle():118 @runner_for([0, 0, 2], [0, 1, 3])119 def runner(data):120 data.draw_bits(1)121 data.draw_bits(1)122 data.draw_bits(4)123 data.mark_interesting()124 root = runner.tree.root125 assert len(root.bit_lengths) == len(root.values) == 1126 assert list(root.transition.children[0].values) == [2]127 assert list(root.transition.children[1].values) == [3]128def test_stores_forced_nodes():129 @runner_for(bytes(3))130 def runner(data):131 data.draw_bits(1, forced=0)132 data.draw_bits(1)133 data.draw_bits(1, forced=0)134 data.mark_interesting()135 root = runner.tree.root136 assert root.forced == {0, 2}137def test_correctly_relocates_forced_nodes():138 @runner_for([0, 0], [1, 0])139 def runner(data):140 data.draw_bits(1)141 data.draw_bits(1, forced=0)142 data.mark_interesting()143 root = runner.tree.root144 assert root.transition.children[1].forced == {0}145 assert root.transition.children[0].forced == {0}146def test_can_go_from_interesting_to_valid():147 tree = DataTree()148 data = ConjectureData.for_buffer(b"", observer=tree.new_observer())149 with pytest.raises(StopTest):150 data.conclude_test(Status.INTERESTING)151 data = ConjectureData.for_buffer(b"", observer=tree.new_observer())152 with pytest.raises(StopTest):153 data.conclude_test(Status.VALID)154def test_going_from_interesting_to_invalid_is_flaky():155 tree = DataTree()156 data = ConjectureData.for_buffer(b"", observer=tree.new_observer())157 with pytest.raises(StopTest):158 data.conclude_test(Status.INTERESTING)159 data = ConjectureData.for_buffer(b"", observer=tree.new_observer())160 with pytest.raises(Flaky):161 data.conclude_test(Status.INVALID)162def test_concluding_at_prefix_is_flaky():163 tree = DataTree()164 data = ConjectureData.for_buffer(b"\1", observer=tree.new_observer())165 data.draw_bits(1)166 with pytest.raises(StopTest):167 data.conclude_test(Status.INTERESTING)168 data = ConjectureData.for_buffer(b"", observer=tree.new_observer())169 with pytest.raises(Flaky):170 data.conclude_test(Status.INVALID)171def test_concluding_with_overrun_at_prefix_is_not_flaky():172 tree = DataTree()173 data = ConjectureData.for_buffer(b"\1", observer=tree.new_observer())174 data.draw_bits(1)175 with pytest.raises(StopTest):176 data.conclude_test(Status.INTERESTING)177 data = ConjectureData.for_buffer(b"", observer=tree.new_observer())178 with pytest.raises(StopTest):179 data.conclude_test(Status.OVERRUN)180def test_changing_n_bits_is_flaky_in_prefix():181 tree = DataTree()182 data = ConjectureData.for_buffer(b"\1", observer=tree.new_observer())183 data.draw_bits(1)184 with pytest.raises(StopTest):185 data.conclude_test(Status.INTERESTING)186 data = ConjectureData.for_buffer(b"\1", observer=tree.new_observer())187 with pytest.raises(Flaky):188 data.draw_bits(2)189def test_changing_n_bits_is_flaky_in_branch():190 tree = DataTree()191 for i in [0, 1]:192 data = ConjectureData.for_buffer([i], observer=tree.new_observer())193 data.draw_bits(1)194 with pytest.raises(StopTest):195 data.conclude_test(Status.INTERESTING)196 data = ConjectureData.for_buffer(b"\1", observer=tree.new_observer())197 with pytest.raises(Flaky):198 data.draw_bits(2)199def test_extending_past_conclusion_is_flaky():200 tree = DataTree()201 data = ConjectureData.for_buffer(b"\1", observer=tree.new_observer())202 data.draw_bits(1)203 with pytest.raises(StopTest):204 data.conclude_test(Status.INTERESTING)205 data = ConjectureData.for_buffer(b"\1\0", observer=tree.new_observer())206 data.draw_bits(1)207 with pytest.raises(Flaky):208 data.draw_bits(1)209def test_changing_to_forced_is_flaky():210 tree = DataTree()211 data = ConjectureData.for_buffer(b"\1", observer=tree.new_observer())212 data.draw_bits(1)213 with pytest.raises(StopTest):214 data.conclude_test(Status.INTERESTING)215 data = ConjectureData.for_buffer(b"\1\0", observer=tree.new_observer())216 with pytest.raises(Flaky):217 data.draw_bits(1, forced=0)218def test_changing_value_of_forced_is_flaky():219 tree = DataTree()220 data = ConjectureData.for_buffer(b"\1", observer=tree.new_observer())221 data.draw_bits(1, forced=1)222 with pytest.raises(StopTest):223 data.conclude_test(Status.INTERESTING)224 data = ConjectureData.for_buffer(b"\1\0", observer=tree.new_observer())225 with pytest.raises(Flaky):226 data.draw_bits(1, forced=0)227def test_does_not_truncate_if_unseen():228 tree = DataTree()229 b = bytes([1, 2, 3, 4])230 assert tree.rewrite(b) == (b, None)231def test_truncates_if_seen():232 tree = DataTree()233 b = bytes([1, 2, 3, 4])234 data = ConjectureData.for_buffer(b, observer=tree.new_observer())235 data.draw_bits(8)236 data.draw_bits(8)237 data.freeze()238 assert tree.rewrite(b) == (b[:2], Status.VALID)239def test_child_becomes_exhausted_after_split():240 tree = DataTree()241 data = ConjectureData.for_buffer([0, 0], observer=tree.new_observer())242 data.draw_bits(8)243 data.draw_bits(8, forced=0)244 data.freeze()245 data = ConjectureData.for_buffer([1, 0], observer=tree.new_observer())246 data.draw_bits(8)247 data.draw_bits(8)248 data.freeze()249 assert not tree.is_exhausted250 assert tree.root.transition.children[0].is_exhausted251def test_will_generate_novel_prefix_to_avoid_exhausted_branches():252 tree = DataTree()253 data = ConjectureData.for_buffer([1], observer=tree.new_observer())254 data.draw_bits(1)255 data.freeze()256 data = ConjectureData.for_buffer([0, 1], observer=tree.new_observer())257 data.draw_bits(1)258 data.draw_bits(8)259 data.freeze()260 prefix = list(tree.generate_novel_prefix(Random(0)))261 assert len(prefix) == 2262 assert prefix[0] == 0263def test_will_mark_changes_in_discard_as_flaky():264 tree = DataTree()265 data = ConjectureData.for_buffer([1, 1], observer=tree.new_observer())266 data.start_example(10)267 data.draw_bits(1)268 data.stop_example()269 data.draw_bits(1)270 data.freeze()271 data = ConjectureData.for_buffer([1, 1], observer=tree.new_observer())272 data.start_example(10)273 data.draw_bits(1)274 with pytest.raises(Flaky):...

Full Screen

Full Screen

tstl_hypothesis.py

Source:tstl_hypothesis.py Github

copy

Full Screen

...44def choice(data, n):45 n_bits = n.bit_length()46 while True:47 data.start_example(CHOICE)48 i = data.draw_bits(n_bits)49 if i >= n:50 data.stop_example(discard=True)51 continue52 data.stop_example()53 return i54FILTERED_ACTIONS = calc_label_from_name("FILTERED ACTIONS")55@click.group()56@click.option('--sut-file')57@click.option('--log-file')58@click.option('--seed')59@click.option('--n-steps')60@click.pass_context61def tstl(ctx, sut_file, log_file, seed, n_steps):62 eng.BUFFER_SIZE = 10 ** 663 eng.MAX_SHRINKS = 10 ** 664 with open(sut_file) as i:65 source = i.read()66 exec_globals = {}67 exec(source, exec_globals)68 sut_class = exec_globals["sut"]69 seed = int(seed)70 n_steps = int(n_steps)71 def run_tstl(data):72 data.extra_information.tstl_steps = getattr(data, 'tstl_steps', [])73 run_checks = False74 count = 075 sut = sut_class()76 sut.restart()77 random.seed(0)78 actions = list(sut.actions())79 actions.sort(key=lambda t: t[0])80 while True:81 # We draw a value which in normal random mode will almost never be82 # zero, but when we are shrinking can be easily turned into a zero.83 data.start_example(STEP_LABEL)84 if count >= n_steps:85 should_continue = data.draw_bits(64, forced=0)86 else:87 should_continue = data.draw_bits(64)88 if not should_continue:89 data.stop_example()90 break91 count_discarded = data.draw_bits(64) == 092 count += 193 if count_discarded:94 data.start_example(CHOICE)95 i = choice(data, len(actions))96 _, guard, _ = actions[i]97 if not guard():98 data.stop_example(discard=False)99 continue100 else:101 data.stop_example(discard=False)102 else:103 for _ in range(3):104 data.start_example(CHOICE)105 i = choice(data, len(actions))106 _, guard, _ = actions[i]107 succeeded = guard()108 data.stop_example(discard=not succeeded)109 if succeeded:110 break111 else:112 data.start_example(FILTERED_ACTIONS)113 valid_actions = [i for i in range(len(actions)) if actions[i][1]()]114 if not valid_actions:115 data.mark_invalid()116 j = choice(data, len(valid_actions))117 i = valid_actions[j]118 data.stop_example(discard=True)119 data.draw_bits(len(actions).bit_length(), forced=i)120 name, guard, action = actions[i]121 assert guard()122 data.stop_example()123 data.extra_information.tstl_steps.append(name)124 failure = None125 try:126 with timeout(5):127 action()128 if run_checks and not sut.check():129 failure = sut.failure()130 except TimeoutExpired:131 data.mark_invalid()132 # FIXME: Sympy specific hack133 except RecursionError:134 data.mark_invalid()135 except StopTest:136 raise137 except Exception:138 failure = sys.exc_info()139 if failure is not None:140 if not data.frozen:141 data.draw_bits(64, forced=0)142 143 error_type, _, tb = failure144 origin = traceback.extract_tb(tb)[-1]145 filename = origin[0]146 lineno = origin[1]147 data.mark_interesting((error_type.__name__, filename, lineno))148 log = open(log_file, "w")149 def log_data(data):150 log_data = [151 hashlib.sha1(data.buffer).hexdigest()[:10],152 len(data.buffer),153 len(data.extra_information.tstl_steps),154 data.status.name,155 ]...

Full Screen

Full Screen

progressbar.py

Source:progressbar.py Github

copy

Full Screen

1import curses2import logging3BLOCK_UNICODE = ' ▏▎▍▌▋▊▉█'4def ProgressBar(win, width, value, max_value, text, bar_color, remainder_color,5 text_color):6 value_bits = int(8 * width * value / max_value)7 left_width = value_bits // 88 if left_width >= len(text):9 win.addstr(text.ljust(left_width), curses.color_pair(bar_color))10 else:11 win.addstr(' ' * left_width, curses.color_pair(bar_color))12 remainer_width = value_bits % 813 if left_width < width:14 win.addstr(BLOCK_UNICODE[remainer_width],15 curses.color_pair(remainder_color))16 right_width = width - left_width - 1 # -1 for remainder17 if left_width >= len(text):18 win.addstr(' ' * right_width, curses.color_pair(text_color))19 else:20 win.addstr(text.ljust(right_width), curses.color_pair(text_color))21def WriteBarMeat(win, width, left_text, middle_text, right_text, color):22 left_text = left_text + ' ' if left_text else ''23 right_text = ' ' + right_text if right_text else ''24 middle_text = middle_text if middle_text else ''25 win.addstr(26 f'{left_text}'27 f'{middle_text.center(width - len(left_text) - len(right_text))}'28 f'{right_text}', curses.color_pair(color))29def WdlBar(win, width, w, d, l, white_bar, draw_bar, black_bar, white_to_draw,30 draw_to_black, white_to_black):31 total = w + d + l32 white_bits = int(8 * width * w / total)33 black_bits = int(8 * width * l / total)34 draw_bits = width * 8 - white_bits - black_bits35 if white_bits % 8 > 0 and white_bits // 8 == (white_bits + draw_bits) // 8:36 white_bits += draw_bits // 237 black_bits += draw_bits - draw_bits // 238 draw_bits = 039 white_width = white_bits // 840 black_width = black_bits // 841 draw_width = max(0, (draw_bits - (8 - white_bits % 8) % 8) // 8)42 white_text = f'W={w}'43 black_text = f'B={l}'44 draw_text = f'D={d}'45 layouts = [46 " W D B ",47 " WD B ",48 " W DB ",49 " WDB ",50 " WD B " if w > l else " W DB ",51 " W DB " if w > l else " WD B ",52 "WDB " if w > l else " WDB",53 " WDB" if w > l else "WDB ",54 ]55 def label_length(layout):56 res = 057 for c in layout:58 if c != ' ':59 if res:60 res += 161 if c == 'W':62 res += len(white_text)63 elif c == 'B':64 res += len(black_text)65 elif c == 'D':66 res += len(draw_text)67 return res68 def get_text(char):69 if char == 'W':70 return white_text71 elif char == 'B':72 return black_text73 elif char == 'D':74 return draw_text75 return None76 def draw_meat(layout, width, color):77 WriteBarMeat(win, width, get_text(layout[0]), get_text(layout[1]),78 get_text(layout[2]), color)79 for layout in layouts:80 (lw, ld, lb) = [layout[i:i + 3] for i in range(0, len(layout), 3)]81 if white_width < label_length(lw): continue82 if draw_width < label_length(ld): continue83 if black_width < label_length(lb): continue84 draw_meat(lw, white_width, white_bar)85 if white_bits % 8 > 0:86 win.addstr(87 BLOCK_UNICODE[white_bits % 8],88 curses.color_pair(89 white_to_draw if draw_bits > 0 else white_to_black))90 draw_meat(ld, draw_width, draw_bar)91 if draw_bits and (white_bits + draw_bits) % 8 > 0:92 win.addstr(BLOCK_UNICODE[(white_bits + draw_bits) % 8],93 curses.color_pair(draw_to_black))94 draw_meat(lb, black_width, black_bar)...

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