How to use for_buffer method in hypothesis

Best Python code snippet using hypothesis

test_utils.py

Source:test_utils.py Github

copy

Full Screen

...30from hypothesis.internal.conjecture import utils as cu31from hypothesis.internal.conjecture.data import ConjectureData, Status, StopTest32from hypothesis.internal.coverage import IN_COVERAGE_TESTS33def test_does_draw_data_for_empty_range():34 data = ConjectureData.for_buffer(b"\1")35 assert cu.integer_range(data, 1, 1) == 136 data.freeze()37 assert data.buffer == b"\0"38def test_uniform_float_shrinks_to_zero():39 d = ConjectureData.for_buffer(bytes(7))40 assert cu.fractional_float(d) == 0.041 assert len(d.buffer) == 742def test_uniform_float_can_draw_1():43 d = ConjectureData.for_buffer(bytes([255] * 7))44 assert cu.fractional_float(d) == 1.045 assert len(d.buffer) == 746def test_coin_biased_towards_truth():47 p = 1 - 1.0 / 50048 for i in range(1, 255):49 assert cu.biased_coin(ConjectureData.for_buffer([0, i, 0, 0]), p)50 assert not cu.biased_coin(ConjectureData.for_buffer([0, 0, 0, 1]), p)51def test_coin_biased_towards_falsehood():52 p = 1.0 / 50053 for i in range(255):54 if i != 1:55 assert not cu.biased_coin(ConjectureData.for_buffer([0, i, 0, 1]), p)56 assert cu.biased_coin(ConjectureData.for_buffer([0, 1, 0, 0]), p)57def test_unbiased_coin_has_no_second_order():58 counts = Counter()59 for i in range(256):60 buf = bytes([i])61 data = ConjectureData.for_buffer(buf)62 result = cu.biased_coin(data, 0.5)63 if data.buffer == buf:64 counts[result] += 165 assert counts[False] == counts[True] > 066def test_drawing_certain_coin_still_writes():67 data = ConjectureData.for_buffer([0, 1])68 assert not data.buffer69 assert cu.biased_coin(data, 1)70 assert data.buffer71def test_drawing_impossible_coin_still_writes():72 data = ConjectureData.for_buffer([1, 0])73 assert not data.buffer74 assert not cu.biased_coin(data, 0)75 assert data.buffer76def test_drawing_an_exact_fraction_coin():77 count = 078 total = 079 p = Fraction(3, 8)80 for i in range(4):81 for j in range(4):82 total += 183 if cu.biased_coin(ConjectureData.for_buffer([i, j, 0]), p):84 count += 185 assert p == Fraction(count, total)86def test_too_small_to_be_useful_coin():87 assert not cu.biased_coin(ConjectureData.for_buffer([1]), 0.5 ** 65)88@example([Fraction(1, 3), Fraction(1, 3), Fraction(1, 3)])89@example([Fraction(1, 1), Fraction(1, 2)])90@example([Fraction(1, 2), Fraction(4, 10)])91@example([Fraction(1, 1), Fraction(3, 5), Fraction(1, 1)])92@example([Fraction(2, 257), Fraction(2, 5), Fraction(1, 11)])93@example([0, 2, 47])94@settings(95 deadline=None,96 suppress_health_check=HealthCheck.all(),97 phases=[Phase.explicit] if IN_COVERAGE_TESTS else tuple(Phase),98)99@given(st.lists(st.fractions(min_value=0, max_value=1), min_size=1))100def test_sampler_distribution(weights):101 total = sum(weights)102 n = len(weights)103 assume(total > 0)104 probabilities = [w / total for w in weights]105 sampler = cu.Sampler(weights)106 calculated = [Fraction(0)] * n107 for base, alternate, p_alternate in sampler.table:108 calculated[base] += (1 - p_alternate) / n109 calculated[alternate] += p_alternate / n110 for expected, actual in zip(probabilities, calculated):111 if isinstance(actual, Fraction):112 assert expected == actual113 else:114 assert abs(expected - actual) < 0.001115def test_sampler_does_not_draw_minimum_if_zero():116 sampler = cu.Sampler([0, 2, 47])117 assert sampler.sample(ConjectureData.for_buffer([0, 0])) != 0118def test_integer_range_center_upper():119 assert (120 cu.integer_range(ConjectureData.for_buffer([0]), lower=0, upper=10, center=10)121 == 10122 )123def test_integer_range_center_lower():124 assert (125 cu.integer_range(ConjectureData.for_buffer([0]), lower=0, upper=10, center=0)126 == 0127 )128def test_integer_range_lower_equals_upper():129 data = ConjectureData.for_buffer([0])130 assert cu.integer_range(data, lower=0, upper=0, center=0) == 0131 assert len(data.buffer) == 1132def test_integer_range_center_default():133 assert (134 cu.integer_range(ConjectureData.for_buffer([0]), lower=0, upper=10, center=None)135 == 0136 )137def test_center_in_middle_below():138 assert (139 cu.integer_range(ConjectureData.for_buffer([0, 0]), lower=0, upper=10, center=5)140 == 5141 )142def test_center_in_middle_above():143 assert (144 cu.integer_range(ConjectureData.for_buffer([1, 0]), lower=0, upper=10, center=5)145 == 5146 )147def test_restricted_bits():148 assert (149 cu.integer_range(150 ConjectureData.for_buffer([1, 0, 0, 0, 0]), lower=0, upper=2 ** 64 - 1151 )152 == 0153 )154def test_sampler_shrinks():155 sampler = cu.Sampler([4.0, 8.0, 1.0, 1.0, 0.5])156 assert sampler.sample(ConjectureData.for_buffer([0] * 3)) == 0157def test_combine_labels_is_distinct():158 x = 10159 y = 100160 assert cu.combine_labels(x, y) not in (x, y)161def test_invalid_numpy_sample():162 with pytest.raises(InvalidArgument):163 cu.check_sample(np.array([[1, 1], [1, 1]]), "array")164def test_valid_numpy_sample():165 cu.check_sample(np.array([1, 2, 3]), "array")166def test_invalid_set_sample():167 with pytest.raises(InvalidArgument):168 cu.check_sample({1, 2, 3}, "array")169def test_valid_list_sample():170 cu.check_sample([1, 2, 3], "array")171def test_choice():172 assert cu.choice(ConjectureData.for_buffer([1]), [1, 2, 3]) == 2173def test_fractional_float():174 assert cu.fractional_float(ConjectureData.for_buffer([0] * 8)) == 0.0175def test_fixed_size_draw_many():176 many = cu.many(177 ConjectureData.for_buffer([]), min_size=3, max_size=3, average_size=3178 )179 assert many.more()180 assert many.more()181 assert many.more()182 assert not many.more()183def test_rejection_eventually_terminates_many():184 many = cu.many(185 ConjectureData.for_buffer([1] * 1000),186 min_size=0,187 max_size=1000,188 average_size=100,189 )190 count = 0191 while many.more():192 count += 1193 many.reject()194 assert count <= 100195def test_rejection_eventually_terminates_many_invalid_for_min_size():196 data = ConjectureData.for_buffer([1] * 1000)197 many = cu.many(data, min_size=1, max_size=1000, average_size=100)198 with pytest.raises(StopTest):199 while many.more():200 many.reject()201 assert data.status == Status.INVALID202def test_many_with_min_size():203 many = cu.many(204 ConjectureData.for_buffer([0] * 10), min_size=2, average_size=10, max_size=1000205 )206 assert many.more()207 assert many.more()208 assert not many.more()209def test_many_with_max_size():210 many = cu.many(211 ConjectureData.for_buffer([1] * 10), min_size=0, average_size=1, max_size=2212 )213 assert many.more()214 assert many.more()215 assert not many.more()216def test_biased_coin_can_be_forced():217 assert cu.biased_coin(ConjectureData.for_buffer([0]), p=0.5, forced=True)218 assert not cu.biased_coin(ConjectureData.for_buffer([1]), p=0.5, forced=False)219def test_assert_biased_coin_always_treats_one_as_true():220 assert cu.biased_coin(ConjectureData.for_buffer([0, 1]), p=1.0 / 257)221@example(p=0.31250000000000006, b=b"\x03\x03\x00")222@example(p=0.4375000000000001, b=b"\x03\x00")223@given(st.floats(0, 1), st.binary())224def test_can_draw_arbitrary_fractions(p, b):225 try:226 cu.biased_coin(ConjectureData.for_buffer(b), p)227 except StopTest:228 reject()229def test_samples_from_a_range_directly():230 s = cu.check_sample(range(10 ** 1000), "")...

Full Screen

Full Screen

test_conjecture_test_data.py

Source:test_conjecture_test_data.py Github

copy

Full Screen

...24def bogus_dist(dist, n):25 assert False26@given(st.binary())27def test_buffer_draws_as_self(buf):28 x = TestData.for_buffer(buf)29 assert x.draw_bytes(len(buf), bogus_dist) == buf30def test_cannot_draw_after_freeze():31 x = TestData.for_buffer(b'hi')32 x.draw_bytes(1)33 x.freeze()34 with pytest.raises(Frozen):35 x.draw_bytes(1)36def test_can_double_freeze():37 x = TestData.for_buffer(b'hi')38 x.freeze()39 assert x.frozen40 x.freeze()41 assert x.frozen42def test_can_draw_zero_bytes():43 x = TestData.for_buffer(b'')44 for _ in range(10):45 assert x.draw_bytes(0) == b''46def test_draw_past_end_sets_overflow():47 x = TestData.for_buffer(bytes(5))48 with pytest.raises(StopTest) as e:49 x.draw_bytes(6)50 assert e.value.testcounter == x.testcounter51 assert x.frozen52 assert x.status == Status.OVERRUN53def test_notes_repr():54 x = TestData.for_buffer(b'')55 x.note(b'hi')56 assert repr(b'hi') in x.output57def test_can_mark_interesting():58 x = TestData.for_buffer(bytes())59 with pytest.raises(StopTest):60 x.mark_interesting()61 assert x.frozen62 assert x.status == Status.INTERESTING63def test_can_mark_invalid():64 x = TestData.for_buffer(bytes())65 with pytest.raises(StopTest):66 x.mark_invalid()67 assert x.frozen68 assert x.status == Status.INVALID69class BoomStrategy(SearchStrategy):70 def do_draw(self, data):71 data.draw_bytes(1)72 raise ValueError()73def test_closes_interval_on_error_in_strategy():74 x = TestData.for_buffer(b'hi')75 with pytest.raises(ValueError):76 x.draw(BoomStrategy())77 x.freeze()78 assert len(x.intervals) == 179class BigStrategy(SearchStrategy):80 def do_draw(self, data):81 data.draw_bytes(10 ** 6)82def test_does_not_double_freeze_in_interval_close():83 x = TestData.for_buffer(b'hi')84 with pytest.raises(StopTest):85 x.draw(BigStrategy())86 assert x.frozen...

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