def extended_textual_header(draw, count=-1, end_text_stanza_probability=None): if count == -1: if end_text_stanza_probability is not None: raise ValueError("end_text_stanza_probability {} does not make sense when count is not {}" .format(end_text_stanza_probability, count)) count = draw(integers(min_value=0, max_value=10)) headers = draw(lists(stanza(), min_size=count, max_size=count)) headers.append(END_TEXT_STANZA) return headers if count == 0: return [] # For counted headers, the end-text stanza is optional. We generate it # with the specified probability if end_text_stanza_probability is None: end_text_stanza_probability = 0.5 random = draw(randoms()) x = random.uniform(0.0, 1.0) num_data_stanzas = count - 1 if x <= end_text_stanza_probability else count headers = draw(lists(stanza(), min_size=num_data_stanzas, max_size=num_data_stanzas)) if num_data_stanzas == count - 1: headers.append(END_TEXT_STANZA) assert len(headers) == count return headers
def domains(): def build_domain(complete, random): while complete: piece = complete[:random.randrange(64)] complete = complete[len(piece):] yield piece return strategies.builds( build_domain, complete=idna_text(min_size=1, average_size=12, max_size=255), random=strategies.randoms(), ).map( lambda parts: u".".join(parts) )
[1, 2, 0], [2, 0, 1], [2, 1, 0], ], ), ( [[0, 1], [2, 3]], [[0, 1, 2, 3], [0, 1, 3, 2], [1, 0, 2, 3], [1, 0, 3, 2]], ), ]) def test_partitioned_permutations(partitions, expected_perms): actual_perms = sorted(partitioned_permutations(partitions)) assert actual_perms == expected_perms @hypothesis.given(s_graphs, s.randoms()) def test_graph_address(terms, r): perm = range(1, len(terms)) r.shuffle(perm) hypothesis.assume(perm != sorted(perm)) perm = [0] + perm shuffled_terms = graph_permute(terms, perm) shuffled_address = graph_address(shuffled_terms) address = graph_address(terms) for source, target in enumerate(perm): assert address[source] == shuffled_address[target] @hypothesis.given(s_graphs, s.randoms()) def test_graph_sort(terms, r): perm = range(1, len(terms))
def test_clear_all(entities): entities.clear_all() assert 0 == len(entities.disjoint_mentions) assert 0 == len(entities) @settings(suppress_health_check=[HealthCheck.too_slow]) @given(entitieses()) def test_add(entities): new_entities = Entities(()) for entity in reversed(list(entities)): new_entities.add(entity) assert list(entities) == list(reversed(list(new_entities))) @given(entitieses(), randoms()) def test_remove(entities, random): # For some random order, try removing all entities one by one entities_in_random_order = random.sample(list(entities), k=len(entities)) total_min_1 = len(entities) - 1 for i, entity in enumerate(entities_in_random_order): entities.remove(entity) assert len(entities) == total_min_1 - i assert len(entities) == 0 assert len(list(entities)) == 0 @settings(suppress_health_check=[HealthCheck.too_slow]) @given(entitieses()) def test_discard_ignores(entities): not_there = 'Not in there'
floats(min_value=0.0), floats(min_value=3.14, max_value=3.14), text(), binary(), booleans(), tuples(booleans(), booleans()), frozensets(integers()), sets(frozensets(booleans())), complex_numbers(), fractions(), decimals(), lists(lists(booleans())), lists(lists(booleans(), average_size=100)), lists(floats(0.0, 0.0), average_size=1.0), ordered_pair, constant_list(integers()), streaming(integers()).map(lambda x: list(x[:2]) and x), integers().filter(lambda x: abs(x) > 100), floats(min_value=-sys.float_info.max, max_value=sys.float_info.max), none(), randoms(), tuples().flatmap(lambda x: EvalledIntStream), templates_for(integers(min_value=0, max_value=0).flatmap(lambda x: integers(min_value=0, max_value=0))), booleans().flatmap(lambda x: booleans() if x else complex_numbers()), recursive(base=booleans(), extend=lambda x: lists(x, max_size=3), max_leaves=10), ] def parametrize(args, values): return pytest.mark.parametrize(args, values, ids=list(map(show, values)))
size_strategies = dict( min_size=st.integers(min_value=0, max_value=100) | st.none(), max_size=st.integers(min_value=0, max_value=100) | st.none(), average_size=st.floats(min_value=0.0, max_value=100.0) | st.none() ) values = st.integers() | st.text(average_size=2.0) Strategies = st.recursive( st.one_of( st.sampled_from([ st.none(), st.booleans(), st.randoms(), st.complex_numbers(), st.randoms(), st.fractions(), st.decimals(), ]), st.builds(st.just, values), st.builds(st.sampled_from, st.lists(values, min_size=1)), builds_ignoring_invalid(st.floats, st.floats(), st.floats()), ), lambda x: st.one_of( builds_ignoring_invalid(st.lists, x, **size_strategies), builds_ignoring_invalid(st.sets, x, **size_strategies), builds_ignoring_invalid( lambda v: st.tuples(*v), st.lists(x, average_size=2.0)), builds_ignoring_invalid( lambda v: st.one_of(*v), st.lists(x, average_size=2.0, min_size=1)), builds_ignoring_invalid(
def define_random_strategy(specifier, settings): return st.randoms()
assert breadth(broad) == 20 def test_drawing_many_near_boundary(): ls = find( st.lists(st.recursive( st.booleans(), lambda x: st.lists(x, min_size=8, max_size=10).map(tuple), max_leaves=9)), lambda x: len(set(x)) >= 5, settings=settings(max_examples=10000, database=None, max_shrinks=2000) ) assert len(ls) == 5 @given(st.randoms()) @settings(max_examples=50, max_shrinks=0) @example(Random(-1363972488426139)) @example(Random(-4)) def test_can_use_recursive_data_in_sets(rnd): nested_sets = st.recursive( st.booleans(), lambda js: st.frozensets(js, average_size=2.0), max_leaves=10 ) nested_sets.example(rnd) def flatten(x): if isinstance(x, bool): return frozenset((x,)) else:
binary(), booleans(), tuples(booleans(), booleans()), frozensets(integers()), sets(frozensets(booleans())), complex_numbers(), fractions(), decimals(), lists(lists(booleans(), average_size=10), average_size=10), lists(lists(booleans(), average_size=100)), lists(floats(0.0, 0.0), average_size=1.0), ordered_pair, constant_list(integers()), integers().filter(lambda x: abs(x) > 100), floats(min_value=-sys.float_info.max, max_value=sys.float_info.max), none(), randoms(), booleans().flatmap(lambda x: booleans() if x else complex_numbers()), recursive( base=booleans(), extend=lambda x: lists(x, max_size=3), max_leaves=10, ) ] if pytest is not None: def parametrize(args, values): return pytest.mark.parametrize(args, values, ids=list(map(repr, values)))
assert list(s(Random(1), t2)) == [] def test_can_clone_same_length_items(): ls = find( lists(frozensets(integers(), min_size=10, max_size=10)), lambda x: len(x) >= 20 ) assert len(set(ls)) == 1 def test_unique_lists_error_on_too_large_average_size(): with pytest.raises(InvalidArgument): lists(integers(), unique=True, average_size=10, max_size=5) @given(randoms()) def test_templates_reify_to_same_value_before_and_after(rnd): s = sets(booleans()) t = s.draw_and_produce(rnd) t2 = s.from_basic(s.to_basic(t)) assert s.reify(t) == s.reify(t2) @given(randoms(), settings=Settings(max_examples=5)) def test_can_draw_sets_of_hard_to_find_elements(rnd): rarebool = floats(0, 1).map(lambda x: x <= 0.01) find( sets(rarebool, min_size=2), lambda x: True, random=rnd, settings=Settings(database=None))
class HypothesisSpec(RuleBasedStateMachine): def __init__(self): super(HypothesisSpec, self).__init__() self.database = None strategies = Bundle(u'strategy') strategy_tuples = Bundle(u'tuples') objects = Bundle(u'objects') streaming_strategies = Bundle(u'streams') basic_data = Bundle(u'basic') varied_floats = Bundle(u'varied_floats') strats_with_parameters = Bundle(u'strats_with_parameters') strats_with_templates = Bundle(u'strats_with_templates') strats_with_2_templates = Bundle(u'strats_with_2_templates') def teardown(self): self.clear_database() @rule(target=basic_data, st=strats_with_templates) def to_basic(self, st): return st[0].to_basic(st[1]) @rule(data=basic_data, strat=strategies) def from_basic(self, data, strat): try: template = strat.from_basic(data) except BadData: return strat.reify(template) @rule(target=basic_data, data=basic_data, r=randoms()) def mess_with_basic(self, data, r): return mutate_basic(data, r) @rule() def clear_database(self): if self.database is not None: self.database.close() self.database = None @rule() def set_database(self): self.teardown() self.database = ExampleDatabase() @rule(st=strats_with_templates) def reify(self, st): strat, temp = st strat.reify(temp) @rule(target=strats_with_templates, st=strats_with_templates) def via_basic(self, st): strat, temp = st temp = strat.from_basic(strat.to_basic(temp)) return (strat, temp) @rule(targets=(strategies, streaming_strategies), strat=strategies) def build_stream(self, strat): return strategy(streaming(strat)) @rule(targets=(strategies, streaming_strategies), strat=strategies, i=integers(1, 10)) def evalled_stream(self, strat, i): return strategy(streaming(strat)).map(lambda x: list(x[:i]) and x) @rule(stream_strat=streaming_strategies, index=integers(0, 50)) def eval_stream(self, stream_strat, index): try: stream = stream_strat.example() list(stream[:index]) except NoExamples: pass @rule(target=strats_with_templates, st=strats_with_templates, r=randoms()) def simplify(self, st, r): strat, temp = st for temp in strat.full_simplify(r, temp): break return (strat, temp) @rule(strat=strategies, r=randoms(), mshr=integers(0, 100)) def find_constant_failure(self, strat, r, mshr): with Settings( verbosity=Verbosity.quiet, max_examples=1, min_satisfying_examples=0, database=self.database, max_shrinks=mshr, ): @given( strat, random=r, ) def test(x): assert False try: test() except AssertionError: pass @rule(strat=strategies, r=randoms(), p=floats(0, 1), mex=integers(1, 10), mshr=integers(1, 100)) def find_weird_failure(self, strat, r, mex, p, mshr): with Settings( verbosity=Verbosity.quiet, max_examples=mex, min_satisfying_examples=0, database=self.database, max_shrinks=mshr, ): @given( strat, random=r, ) def test(x): assert Random(hashlib.md5( show(x).encode(u'utf-8')).digest()).random() <= p try: test() except AssertionError: pass @rule(target=strats_with_parameters, strat=strategies, r=randoms()) def draw_parameter(self, strat, r): return (strat, strat.draw_parameter(r)) @rule(target=strats_with_templates, sp=strats_with_parameters, r=randoms()) def draw_template(self, sp, r): strat, param = sp return (strat, strat.draw_template(r, param)) @rule(target=strats_with_2_templates, sp=strats_with_parameters, r=randoms()) def draw_templates(self, sp, r): strat, param = sp return ( strat, strat.draw_template(r, param), strat.draw_template(r, param), ) @rule(st=strats_with_templates) def check_serialization(self, st): strat, template = st as_basic = strat.to_basic(template) assert show(strat.reify(template)) == show( strat.reify(strat.from_basic(as_basic))) assert as_basic == strat.to_basic(strat.from_basic(as_basic)) @rule(target=strategies, spec=sampled_from(( integers(), booleans(), floats(), complex_numbers(), fractions(), decimals(), text(), binary(), none(), StateMachineSearchStrategy(), tuples(), ))) def strategy(self, spec): return spec @rule(target=strategies, values=lists(integers() | text(), min_size=1)) def sampled_from_strategy(self, values): return sampled_from(values) @rule(target=strategies, spec=strategy_tuples) def strategy_for_tupes(self, spec): return tuples(*spec) @rule(target=strategies, source=strategies, level=integers(1, 10), mixer=text()) def filtered_strategy(s, source, level, mixer): def is_good(x): return bool( Random( hashlib.md5( (mixer + show(x)).encode(u'utf-8')).digest()).randint( 0, level)) return source.filter(is_good) @rule(target=strategies, elements=strategies) def list_strategy(self, elements): return lists(elements, average_size=AVERAGE_LIST_LENGTH) @rule(target=strategies, l=strategies, r=strategies) def or_strategy(self, l, r): return l | r @rule(target=strategies, source=strategies, result=strategies, mixer=text()) def mapped_strategy(self, source, result, mixer): cache = {} def do_map(value): rep = show(value) try: return deepcopy(cache[rep]) except KeyError: pass random = Random( hashlib.md5((mixer + rep).encode(u'utf-8')).digest()) outcome_template = result.draw_and_produce(random) cache[rep] = result.reify(outcome_template) return deepcopy(cache[rep]) return source.map(do_map) @rule(target=varied_floats, source=floats()) def float(self, source): return source @rule(target=varied_floats, source=varied_floats, offset=integers(-100, 100)) def adjust_float(self, source, offset): return int_to_float(clamp(0, float_to_int(source) + offset, 2**64 - 1)) @rule(target=strategies, left=varied_floats, right=varied_floats) def float_range(self, left, right): for f in (math.isnan, math.isinf): for x in (left, right): assume(not f(x)) left, right = sorted((left, right)) assert left <= right return strategy(floats(left, right)) @rule(target=strategies, source=strategies, result1=strategies, result2=strategies, mixer=text(), p=floats(0, 1)) def flatmapped_strategy(self, source, result1, result2, mixer, p): assume(result1 is not result2) def do_map(value): rep = show(value) random = Random( hashlib.md5((mixer + rep).encode(u'utf-8')).digest()) if random.random() <= p: return result1 else: return result2 return source.flatmap(do_map) @rule(target=strategies, value=objects) def just_strategy(self, value): return strategy(just(value)) @rule(target=strategy_tuples, source=strategies) def single_tuple(self, source): return (source, ) @rule(target=strategy_tuples, l=strategy_tuples, r=strategy_tuples) def cat_tuples(self, l, r): return l + r @rule(target=objects, strat=strategies) def get_example(self, strat): try: strat.example() except NoExamples: # Because of filtering some strategies we look for don't actually # have any examples. pass @rule(target=strategies, left=integers(), right=integers()) def integer_range(self, left, right): left, right = sorted((left, right)) return strategy(integers(left, right)) @rule(strat=strategies) def repr_is_good(self, strat): assert u' at 0x' not in repr(strat) @rule(strat=strategies) def template_upper_bound_is_valid(self, strat): ub = strat.template_upper_bound assert ub >= 0 if isinstance(ub, float): assert math.isinf(ub) else: assert isinstance(ub, int) @rule(strat=strategies, r=randoms()) def can_find_as_many_templates_as_size(self, strat, r): tempstrat = templates_for(strat) n = min(10, strat.template_upper_bound) found = [] with Settings(verbosity=Verbosity.quiet, timeout=2.0): for i in range(n): try: x = find( tempstrat, lambda t: t not in found, random=r, ) except: print(u'Exception at %d/%d. template_upper_bound=%r' % (i, n, strat.template_upper_bound)) raise found.append(x)
def text_from_regex(draw, regexp): random = draw(st.randoms()) return Rstr(random).xeger(regexp)
class IncrementalUpdater(RuleBasedStateMachine): """Calls self.update with various byte buffers. Child classes are expected to: - define an @initialize rule to set the UMASH state - define reference_value() to compute the reference hash of self.acc - define batch_value() to compute the batch hash of self.acc - define digest_value() to extract the current incremental digest - update(buf, n) to feed buf[0 ... n - 1] to the incremental state """ def __init__(self): super().__init__() self.multipliers = None self.oh = None self.params = None self.state = None self.acc = b"" @invariant() def compare_values(self): if self.state is None: return reference = self.reference_value() batch = self.batch_value() actual = self.digest_value() note((len(self.acc), self.acc)) assert reference == batch == actual, { "ref": reference, "batch": batch, "actual": actual, } def _update(self, buf): self.acc += buf n = len(buf) # Copy to the heap to help ASan copy = FFI.new("char[]", n) FFI.memmove(copy, buf, n) self.update(copy, n) @precondition(lambda self: self.state) @rule(buf=st.binary()) def update_short(self, buf): note("update_short: %s" % len(buf)) self._update(buf) @precondition(lambda self: self.state) @rule( num=st.integers(min_value=1, max_value=1024), byte=st.binary(min_size=1, max_size=1), ) def update_repeat(self, num, byte): buf = byte * num self._update(buf) @precondition(lambda self: self.state) @rule( length=st.integers(min_value=1, max_value=1024), random=st.randoms(use_true_random=True), ) def update_long(self, length, random): buf = bytes((random.getrandbits(8) for _ in range(length))) note("update_long: %s" % buf) self._update(buf)
# Test encoding detection for each file we have of encoding for for file_name in listdir(path): ext = splitext(file_name)[1].lower() if ext not in ['.html', '.txt', '.xml', '.srt']: continue yield check_file_encoding, join(path, file_name), encoding class JustALengthIssue(Exception): pass @given(st.text(min_size=1), st.sampled_from(['ascii', 'utf-8', 'utf-16', 'utf-32', 'iso-8859-7', 'iso-8859-8', 'windows-1255']), st.randoms(), settings=Settings(max_examples=200)) def test_never_fails_to_detect_if_there_is_a_valid_encoding(txt, enc, rnd): try: data = txt.encode(enc) except UnicodeEncodeError: assume(False) detected = chardet.detect(data)['encoding'] if detected is None: @given(st.text(), settings=Settings(verbosity=Verbosity.quiet, max_shrinks=0, max_examples=50), random=rnd) def string_poisons_following_text(suffix): try: extended = (txt + suffix).encode(enc) except UnicodeEncodeError: assume(False)
assert x <= -REALLY_SMALL_FLOAT @fails @given(floats()) def test_can_find_floats_that_do_not_round_trip_through_strings(x): assert float(str(x)) == x @fails @given(floats()) def test_can_find_floats_that_do_not_round_trip_through_reprs(x): assert float(repr(x)) == x @given(floats(), floats(), randoms()) def test_floats_are_in_range(x, y, rand): assume(not (math.isnan(x) or math.isnan(y))) assume(not (math.isinf(x) or math.isinf(y))) x, y = sorted((x, y)) assume(x < y) with Settings(max_examples=10): @given(floats(x, y), random=rand) def test_is_in_range(t): assert x <= t <= y try: test_is_in_range() except Unsatisfiable: assume(False)
assert len(g.colors_used()) == len(g.players) @given( integers(min_value=5,max_value=50),\ integers(min_value=5,max_value=50),\ lists(integers(),min_size=3, max_size=10)) def test_start_score(x,y,l): """ all starting scores same """ g = game.Game(x,y,colors=l) assert all(map(lambda x: x.score == g.players[0].score, g.players)) @given( integers(min_value=5,max_value=20),\ integers(min_value=5,max_value=20),\ lists(integers(),min_size=3, max_size=10),\ integers(min_value=0,max_value=1),integers(),randoms()) def test_valid_turn(x,y,l,p,c,r): """ trying invalid turn (wrong player or color) does not proceed game, but valid one does """ g = game.Game(x,y,colors=l) turn = g.turn if c in g.colors: if c in g.colors_available(g.turn) and p == g.turn: g.command(p,c) assert turn != g.turn, "game does not proceed on valid turn" else: g.command(p,c) assert turn == g.turn, "game proceeds on invalid turn"
class PassGroupTests(TestCase): """ Tests for ``IPassGroup`` and the factories that create them. """ @given(vouchers(), pass_counts(), posix_safe_datetimes()) def test_get(self, voucher, num_passes, now): """ ``IPassFactory.get`` returns an ``IPassGroup`` provider containing the requested number of passes. """ configless = self.useFixture( ConfiglessMemoryVoucherStore( lambda: now, ), ) # Make sure there are enough tokens for us to extract! self.assertThat( configless.redeem(voucher, num_passes), succeeded(Always()), ) pass_factory = SpendingController.for_store( tokens_to_passes=configless.redeemer.tokens_to_passes, store=configless.store, ) group = pass_factory.get(u"message", num_passes) self.assertThat( group, MatchesAll( Provides([IPassGroup]), MatchesStructure( passes=HasLength(num_passes), ), ), ) def _test_token_group_operation( self, operation, matches_tokens, voucher, num_passes, now, random, data, ): configless = self.useFixture( ConfiglessMemoryVoucherStore( lambda: now, ), ) # Make sure there are enough tokens for us to use! self.assertThat( configless.redeem(voucher, num_passes), succeeded(Always()), ) # Figure out some subset, maybe empty, of passes from the group that # we will try to operate on. group_size = data.draw(integers(min_value=0, max_value=num_passes)) indices = range(num_passes) random.shuffle(indices) spent_indices = indices[:group_size] # Get some passes and perform the operation. pass_factory = SpendingController.for_store( tokens_to_passes=configless.redeemer.tokens_to_passes, store=configless.store, ) group = pass_factory.get(u"message", num_passes) spent, rest = group.split(spent_indices) operation(spent) # Verify the expected outcome of the operation using the supplied # matcher factory. self.assertThat( configless.store, matches_tokens(num_passes, spent), ) @given(vouchers(), pass_counts(), posix_safe_datetimes(), randoms(), data()) def test_spent(self, voucher, num_passes, now, random, data): """ Passes in a group can be marked as successfully spent to prevent them from being re-used by a future ``get`` call. """ def matches_tokens(num_passes, group): return AfterPreprocessing( # The use of `backup` here to check is questionable. TODO: # Straight-up query interface for tokens in different states. lambda store: store.backup()[u"unblinded-tokens"], HasLength(num_passes - len(group.passes)), ) return self._test_token_group_operation( lambda group: group.mark_spent(), matches_tokens, voucher, num_passes, now, random, data, ) @given(vouchers(), pass_counts(), posix_safe_datetimes(), randoms(), data()) def test_invalid(self, voucher, num_passes, now, random, data): """ Passes in a group can be marked as invalid to prevent them from being re-used by a future ``get`` call. """ def matches_tokens(num_passes, group): return AfterPreprocessing( # The use of `backup` here to check is questionable. TODO: # Straight-up query interface for tokens in different states. lambda store: store.backup()[u"unblinded-tokens"], HasLength(num_passes - len(group.passes)), ) return self._test_token_group_operation( lambda group: group.mark_invalid(u"reason"), matches_tokens, voucher, num_passes, now, random, data, ) @given(vouchers(), pass_counts(), posix_safe_datetimes(), randoms(), data()) def test_reset(self, voucher, num_passes, now, random, data): """ Passes in a group can be reset to allow them to be re-used by a future ``get`` call. """ def matches_tokens(num_passes, group): return AfterPreprocessing( # They've been reset so we should be able to re-get them. lambda store: store.get_unblinded_tokens(len(group.passes)), Equals(group.unblinded_tokens), ) return self._test_token_group_operation( lambda group: group.reset(), matches_tokens, voucher, num_passes, now, random, data, )
def strategy_test_suite( specifier, max_examples=100, random=None, ): settings = Settings( database=None, max_examples=max_examples, min_satisfying_examples=2, average_list_length=2.0, ) random = random or Random() strat = strategy(specifier, settings) specifier_test = given( templates_for(specifier), randoms(), settings=settings ) class ValidationSuite(TestCase): def __repr__(self): return 'strategy_test_suite(%s)' % ( repr(specifier), ) @given(specifier, settings=settings) def test_does_not_error(self, value): pass def test_can_give_example(self): strat.example() def test_can_give_list_of_examples(self): strategy(lists(strat)).example() def test_will_give_unsatisfiable_if_all_rejected(self): @given(specifier, settings=settings) def nope(x): assume(False) with self.assertRaises(Unsatisfiable): nope() def test_will_find_a_constant_failure(self): @given(specifier, settings=settings) def nope(x): raise Rejected() with self.assertRaises(Rejected): nope() def test_will_find_a_failure_from_the_database(self): db = ExampleDatabase() @given(specifier, settings=Settings(max_examples=10, database=db)) def nope(x): raise Rejected() try: for i in hrange(3): with self.assertRaises(Rejected): nope() # pragma: no branch finally: db.close() @given( templates_for(specifier), templates_for(specifier), settings=settings ) def test_simplicity_is_asymmetric(self, x, y): assert not ( strat.strictly_simpler(x, y) and strat.strictly_simpler(y, x) ) def test_will_handle_a_really_weird_failure(self): db = ExampleDatabase() @given( specifier, settings=Settings( database=db, max_examples=max_examples, min_satisfying_examples=2, average_list_length=2.0, ) ) def nope(x): s = hashlib.sha1(repr(x).encode('utf-8')).digest() if Random(s).randint(0, 1): raise Rejected() try: try: nope() except Rejected: pass try: nope() except Rejected: pass finally: db.close() @specifier_test def test_is_basic(self, value, rnd): def is_basic(v): if v is None or isinstance(v, text_type): return True if isinstance(v, integer_types): return not (abs(v) >> 64) if isinstance(v, list): return all(is_basic(w) for w in v) return False supposedly_basic = strat.to_basic(value) self.assertTrue(is_basic(supposedly_basic), repr(supposedly_basic)) @specifier_test def test_only_raises_bad_data_in_from_basic(self, value, rnd): basic = strat.to_basic(value) messed_basic = mutate_basic(basic, rnd) try: strat.from_basic(messed_basic) except BadData: pass @specifier_test def test_can_round_trip_through_the_database(self, template, rnd): empty_db = ExampleDatabase( backend=SQLiteBackend(':memory:'), ) try: storage = empty_db.storage_for(specifier) storage.save(template) values = list(storage.fetch()) assert len(values) == 1 assert strat.to_basic(template) == strat.to_basic(values[0]) finally: empty_db.close() @specifier_test def test_template_is_hashable(self, template, rnd): hash(template) # It can be easy to forget to convert a list... hash(strat.from_basic(strat.to_basic(template))) @given( templates_for(specifier), randoms(), lists(lists(integers())), settings=settings ) def test_apply_all_simplifiers(self, template, rnd, path): path = list(filter(None, path)) assume(path) current_template = template for local_route in path: simplifiers = list(strat.simplifiers(random, current_template)) if not simplifiers: break for i in local_route: simplify = simplifiers[abs(i) % len(simplifiers)] simpler = list(simplify( rnd, current_template )) if simpler: current_template = random.choice(simpler) @specifier_test def test_can_minimize_to_empty(self, template, rnd): simplest = template try: while True: simplest = next(strat.full_simplify(rnd, simplest)) except StopIteration: pass assert list(strat.full_simplify(rnd, simplest)) == [] @specifier_test def test_full_simplify_completes(self, template, rnd): # Cut off at 1000 for the occasional case where we get # really very large templates which have too many simplifies. for x in islice(strat.full_simplify(rnd, template), 1000): pass @specifier_test def test_does_not_increase_complexity(self, template, rnd): for s in islice(strat.full_simplify(rnd, template), 100): assert not strat.strictly_simpler(template, s) @given(randoms(), settings=Settings(max_examples=1000)) def test_can_create_templates(self, random): parameter = strat.draw_parameter(random) strat.draw_template(random, parameter) return ValidationSuite
floats(), floats(max_value=-0.0), floats(), floats(min_value=0.0), floats(min_value=3.14, max_value=3.14), text(), binary(), booleans(), tuples(booleans(), booleans()), frozensets(integers()), sets(frozensets(booleans())), complex_numbers(), fractions(), decimals(), lists(lists(booleans(), average_size=10), average_size=10), lists(lists(booleans(), average_size=100)), lists(floats(0.0, 0.0), average_size=1.0), ordered_pair, constant_list(integers()), integers().filter(lambda x: abs(x) > 100), floats(min_value=-sys.float_info.max, max_value=sys.float_info.max), none(), randoms(), booleans().flatmap(lambda x: booleans() if x else complex_numbers()), recursive( base=booleans(), extend=lambda x: lists(x, max_size=3), max_leaves=10, ) ] if pytest is not None: def parametrize(args, values): return pytest.mark.parametrize( args, values, ids=list(map(repr, values)))
class ValidationSuite(TestCase): def __repr__(self): return 'strategy_test_suite(%s)' % (repr(specifier), ) @given(specifier, settings=settings) def test_does_not_error(self, value): pass def test_can_give_example(self): strat.example() def test_can_give_list_of_examples(self): strategy(lists(strat)).example() def test_will_give_unsatisfiable_if_all_rejected(self): @given(specifier, settings=settings) def nope(x): assume(False) with self.assertRaises(Unsatisfiable): nope() def test_will_find_a_constant_failure(self): @given(specifier, settings=settings) def nope(x): raise Rejected() with self.assertRaises(Rejected): nope() def test_will_find_a_failure_from_the_database(self): db = ExampleDatabase() @given(specifier, settings=Settings(max_examples=10, database=db)) def nope(x): raise Rejected() try: for i in hrange(3): with self.assertRaises(Rejected): nope() # pragma: no branch finally: db.close() @given(templates_for(specifier), templates_for(specifier), settings=settings) def test_simplicity_is_asymmetric(self, x, y): assert not (strat.strictly_simpler(x, y) and strat.strictly_simpler(y, x)) def test_will_handle_a_really_weird_failure(self): db = ExampleDatabase() @given(specifier, settings=Settings( database=db, max_examples=max_examples, min_satisfying_examples=2, average_list_length=2.0, )) def nope(x): s = hashlib.sha1(repr(x).encode('utf-8')).digest() if Random(s).randint(0, 1): raise Rejected() try: try: nope() except Rejected: pass try: nope() except Rejected: pass finally: db.close() @specifier_test def test_is_basic(self, value, rnd): def is_basic(v): if v is None or isinstance(v, text_type): return True if isinstance(v, integer_types): return not (abs(v) >> 64) if isinstance(v, list): return all(is_basic(w) for w in v) return False supposedly_basic = strat.to_basic(value) self.assertTrue(is_basic(supposedly_basic), repr(supposedly_basic)) @specifier_test def test_only_raises_bad_data_in_from_basic(self, value, rnd): basic = strat.to_basic(value) messed_basic = mutate_basic(basic, rnd) try: strat.from_basic(messed_basic) except BadData: pass @specifier_test def test_can_round_trip_through_the_database(self, template, rnd): empty_db = ExampleDatabase(backend=SQLiteBackend(':memory:'), ) try: storage = empty_db.storage_for(specifier) storage.save(template) values = list(storage.fetch()) assert len(values) == 1 assert strat.to_basic(template) == strat.to_basic(values[0]) finally: empty_db.close() @specifier_test def test_template_is_hashable(self, template, rnd): hash(template) # It can be easy to forget to convert a list... hash(strat.from_basic(strat.to_basic(template))) @given(templates_for(specifier), randoms(), lists(lists(integers())), settings=settings) def test_apply_all_simplifiers(self, template, rnd, path): path = list(filter(None, path)) assume(path) current_template = template for local_route in path: simplifiers = list(strat.simplifiers(random, current_template)) if not simplifiers: break for i in local_route: simplify = simplifiers[abs(i) % len(simplifiers)] simpler = list(simplify(rnd, current_template)) if simpler: current_template = random.choice(simpler) @specifier_test def test_can_minimize_to_empty(self, template, rnd): simplest = template try: while True: simplest = next(strat.full_simplify(rnd, simplest)) except StopIteration: pass assert list(strat.full_simplify(rnd, simplest)) == [] @specifier_test def test_full_simplify_completes(self, template, rnd): # Cut off at 1000 for the occasional case where we get # really very large templates which have too many simplifies. for x in islice(strat.full_simplify(rnd, template), 1000): pass @specifier_test def test_does_not_increase_complexity(self, template, rnd): for s in islice(strat.full_simplify(rnd, template), 100): assert not strat.strictly_simpler(template, s) @given(randoms(), settings=Settings(max_examples=1000)) def test_can_create_templates(self, random): parameter = strat.draw_parameter(random) strat.draw_template(random, parameter)
def choosers() -> Strategy[Chooser]: return (strategies.randoms( use_true_random=True).map(lambda random: random.choice))
from hypothesis import given, strategies as st @given(st.randoms(), st.lists(st.integers())) def test_something_invariant(rand, items): """ The set of items in a collection does not change when shuffling. """ orig_items = list(items) rand.shuffle(items) for item in items: orig_items.remove(item) assert orig_items == []
from hypothesis.control import BuildContext from hypothesis.internal.tracker import Tracker from hypothesis.searchstrategy.morphers import Morpher, MorpherStrategy morphers = MorpherStrategy() intlists = s.lists(s.integers()) def test_can_simplify_through_a_morpher(): m = find(morphers, lambda x: bool(x.become(intlists))) assert m.clean_slate().become(intlists) == [0] @example(Random(187)) @example(Random(0)) @given(s.randoms(), settings=Settings(max_examples=10)) def test_can_simplify_text_through_a_morpher(rnd): m = find(morphers, lambda x: bool(x.become(s.text())), random=rnd, settings=Settings(database=None)) with BuildContext(): assert m.clean_slate().become(s.text()) == u'0' def test_can_simplify_lists_of_morphers_of_single_type(): ms = find(s.lists(morphers), lambda x: sum(t.become(s.integers()) for t in x) >= 100, settings=Settings(database=None)) with BuildContext():
assert x <= -REALLY_SMALL_FLOAT @fails @given(floats(), settings=TRY_HARDER) def test_can_find_floats_that_do_not_round_trip_through_strings(x): assert float(str(x)) == x @fails @given(floats(), settings=TRY_HARDER) def test_can_find_floats_that_do_not_round_trip_through_reprs(x): assert float(repr(x)) == x @given(floats(), floats(), randoms()) def test_floats_are_in_range(x, y, rand): assume(not (math.isnan(x) or math.isnan(y))) assume(not (math.isinf(x) or math.isinf(y))) x, y = sorted((x, y)) assume(x < y) with Settings(max_examples=10): @given(floats(x, y), random=rand) def test_is_in_range(t): assert x <= t <= y try: test_is_in_range() except Unsatisfiable: assume(False)
def test_ints_cancel(int_x, int_y): assert ((int_x + int_y) - int_y) == int_x @with_setup(setup_func1) @with_teardown(teardown_func1) @given(st.lists(st.integers())) def test_reverse_reverse_id(xss): yss = list(reversed(xss)) yss.reverse() assert xss == yss @with_setup(setup_func1) @with_teardown(teardown_func1) @given(st.lists(st.integers()), st.randoms()) def test_shuffle_is_noop(xss, rnd): yss = list(xss) rnd.shuffle(yss) note("Shuffle: {0}".format(yss)) assert xss == yss @with_setup(setup_func1) @with_teardown(teardown_func1) @given(st.tuples(st.booleans(), st.text())) def test_tuple_bool_text(tup): assert len(tup) == 2 assert isinstance(tup[0], bool) assert isinstance(tup[1], str)
from hypothesis.control import BuildContext from hypothesis.internal.tracker import Tracker from hypothesis.searchstrategy.morphers import Morpher, MorpherStrategy morphers = MorpherStrategy() intlists = s.lists(s.integers()) def test_can_simplify_through_a_morpher(): m = find(morphers, lambda x: bool(x.become(intlists))) assert m.clean_slate().become(intlists) == [0] @example(Random(187)) @example(Random(0)) @given(s.randoms(), settings=Settings(max_examples=10)) def test_can_simplify_text_through_a_morpher(rnd): m = find( morphers, lambda x: bool(x.become(s.text())), random=rnd, settings=Settings(database=None) ) with BuildContext(): assert m.clean_slate().become(s.text()) == u'0' def test_can_simplify_lists_of_morphers_of_single_type(): ms = find( s.lists(morphers), lambda x: sum(t.become(s.integers()) for t in x) >= 100, settings=Settings(database=None) )
st.random_module(), ) @settings(timeout=10, max_shrinks=0) def test_intervals_shrink_to_center(inter, rnd): lower, center, upper = inter s = interval(lower, upper, center) with settings(database=None, max_shrinks=2000): assert find(s, lambda x: True) == center if lower < center: assert find(s, lambda x: x < center) == center - 1 if center < upper: assert find(s, lambda x: x > center) == center + 1 @given( st.tuples( st.integers(), st.integers(), st.integers(), st.integers() ).map(sorted), st.randoms(), ) @settings(timeout=10, max_shrinks=0) def test_distribution_is_correctly_translated(inter, rnd): assert inter == sorted(inter) lower, c1, c2, upper = inter d = TestData( draw_bytes=lambda data, n, distribution: distribution(rnd, n), max_length=10 ** 6 ) assert d.draw(interval(lower, upper, c1, lambda r: c2)) == c2 assert d.draw(interval(lower, upper, c2, lambda r: c1)) == c1
def test_random_only_produces_special_random(): st = randoms() assert isinstance(st.example(), RandomWithSeed)
TestSampled1 = strategy_test_suite(sampled_from(elements=(1,))) TestSampled2 = strategy_test_suite(sampled_from(elements=(1, 2))) TestIntegersFrom = strategy_test_suite(integers(min_value=13)) TestIntegersFrom = strategy_test_suite(integers(min_value=1 << 1024)) TestOneOf = strategy_test_suite(one_of( integers(), integers(), booleans())) TestOneOfSameType = strategy_test_suite( one_of( integers(min_value=1, max_value=10), integers(min_value=8, max_value=15), ) ) TestRandom = strategy_test_suite(randoms()) TestInts = strategy_test_suite(integers()) TestBoolLists = strategy_test_suite(lists(booleans())) TestDictionaries = strategy_test_suite( dictionaries(keys=tuples(integers(), integers()), values=booleans())) TestOrderedDictionaries = strategy_test_suite( dictionaries( keys=integers(), values=integers(), dict_class=OrderedDict)) TestString = strategy_test_suite(text()) BinaryString = strategy_test_suite(binary()) TestIntBool = strategy_test_suite(tuples(integers(), booleans())) TestFloats = strategy_test_suite(floats()) TestComplex = strategy_test_suite(complex_numbers()) TestJust = strategy_test_suite(just(u'hi')) TestTemplates = strategy_test_suite(templates_for(sets(integers())))
has_voted, ) from eth2.beacon.exceptions import ( NoCommitteeAssignment, ) from eth2.beacon.helpers import ( get_epoch_start_slot, ) from eth2.beacon.tools.builder.validator import ( aggregate_votes, get_next_epoch_committee_assignment, verify_votes, ) @settings(max_examples=1) @given(random=st.randoms()) @pytest.mark.parametrize( ('votes_count'), [ (0), (9), ], ) def test_aggregate_votes(votes_count, random, privkeys, pubkeys): bit_count = 10 pre_bitfield = get_empty_bitfield(bit_count) pre_sigs = () domain = 0 random_votes = random.sample(range(bit_count), votes_count) message = b'hello'
class ChainStateStateMachine(RuleBasedStateMachine): def __init__(self): self.replay_path: bool = False self.address_to_privkey: Dict[Address, PrivateKey] = dict() self.address_to_client: Dict[Address, Client] = dict() self.initial_number_of_channels = 1 self.number_of_clients = 1 super().__init__() def new_channel(self, client_address: Address) -> AddressPair: """Create a new partner address with private key and channel. The private key and channels are listed in the instance's dictionaries, the address is returned and should be added to the partners Bundle. """ partner_privkey, partner_address = factories.make_privkey_address() self.address_to_privkey[partner_address] = partner_privkey client = self.address_to_client[client_address] client.address_to_channel[partner_address] = factories.create( factories.NettingChannelStateProperties( our_state=factories.NettingChannelEndStateProperties( balance=TokenAmount(1000), address=client_address), partner_state=factories.NettingChannelEndStateProperties( balance=TokenAmount(1000), address=partner_address), canonical_identifier=factories.make_canonical_identifier( token_network_address=self.token_network_address), )) return AddressPair(our_address=client_address, partner_address=partner_address) def new_channel_with_transaction(self, client_address: Address) -> AddressPair: client = self.address_to_client[client_address] address_pair = self.new_channel(client_address) partner_address = address_pair.partner_address channel_state = client.address_to_channel[partner_address] assert isinstance(channel_state, NettingChannelState) channel_new_state_change = ContractReceiveChannelNew( transaction_hash=factories.make_transaction_hash(), channel_state=channel_state, block_number=self.block_number, block_hash=factories.make_block_hash(), ) node.state_transition(client.chain_state, channel_new_state_change) node.state_transition(client.chain_state, channel_new_state_change) client.chain_state.nodeaddresses_to_networkstates[ partner_address] = NetworkState.REACHABLE return address_pair @initialize( target=address_pairs, block_number=integers(min_value=GENESIS_BLOCK_NUMBER + 1), random=randoms(), random_seed=random_module(), ) def initialize_all(self, block_number, random, random_seed): self.random_seed = random_seed self.block_number = block_number self.block_hash = factories.make_block_hash() self.random = random self.token_network_address = factories.UNIT_TOKEN_NETWORK_ADDRESS self.token_id = factories.UNIT_TOKEN_ADDRESS self.token_network_state = TokenNetworkState( address=self.token_network_address, token_address=self.token_id, network_graph=TokenNetworkGraphState(self.token_network_address), ) self.token_network_registry_address = factories.make_token_network_registry_address( ) self.token_network_registry_state = TokenNetworkRegistryState( self.token_network_registry_address, [self.token_network_state]) channels: List[NettingChannelState] = [] for _ in range(self.number_of_clients): private_key, address = factories.make_privkey_address() self.address_to_privkey[address] = private_key chain_state = ChainState( pseudo_random_generator=self.random, block_number=self.block_number, block_hash=self.block_hash, our_address=address, chain_id=factories.UNIT_CHAIN_ID, ) chain_state.identifiers_to_tokennetworkregistries[ self. token_network_registry_address] = self.token_network_registry_state chain_state.tokennetworkaddresses_to_tokennetworkregistryaddresses[ self. token_network_address] = self.token_network_registry_address self.address_to_client[address] = Client(chain_state=chain_state) channels.extend( self.new_channel_with_transaction(client_address=address) for _ in range(self.initial_number_of_channels)) return multiple(*channels) def event(self, description): """ Wrapper for hypothesis' event function. hypothesis.event raises an exception when invoked outside of hypothesis context, so skip it when we are replaying a failed path. """ if not self.replay_path: event(description) @invariant() def chain_state_invariants(self): for client in self.address_to_client.values(): client.assert_monotonicity_invariants() client.assert_channel_state_invariants() def channel_opened(self, partner_address, client_address): client = self.address_to_client[client_address] needed_channel = client.address_to_channel[partner_address] return channel.get_status(needed_channel) == ChannelState.STATE_OPENED
"""Strategies for Hypothesis tests.""" import re from collections import OrderedDict from hypothesis import assume, strategies as st from bidict import IGNORE, OVERWRITE, RAISE, OrderedBidictBase, namedbidict from bidict.compat import ItemsView, PY2, izip import _types as t # pylint: disable=invalid-name DATA = st.data() RAND = st.randoms() BIDICT_TYPES = st.sampled_from(t.BIDICT_TYPES) MUTABLE_BIDICT_TYPES = st.sampled_from(t.MUTABLE_BIDICT_TYPES) FROZEN_BIDICT_TYPES = st.sampled_from(t.FROZEN_BIDICT_TYPES) ORDERED_BIDICT_TYPES = st.sampled_from(t.ORDERED_BIDICT_TYPES) MAPPING_TYPES = st.sampled_from(t.MAPPING_TYPES) NON_BIDICT_MAPPING_TYPES = st.sampled_from(t.NON_BIDICT_MAPPING_TYPES) ORDERED_MAPPING_TYPES = st.sampled_from(t.ORDERED_MAPPING_TYPES) HASHABLE_MAPPING_TYPES = st.sampled_from(t.HASHABLE_MAPPING_TYPES) DUP_POLICIES = st.sampled_from((IGNORE, OVERWRITE, RAISE)) DUP_POLICIES_DICT = st.fixed_dictionaries(dict( on_dup_key=DUP_POLICIES, on_dup_val=DUP_POLICIES, on_dup_kv=DUP_POLICIES, ))
assert breadth(broad) == 20 def test_drawing_many_near_boundary(): ls = find( st.lists(st.recursive( st.booleans(), lambda x: st.lists(x, min_size=8, max_size=10).map(tuple), max_leaves=9)), lambda x: len(set(x)) >= 5, settings=settings(max_examples=10000, database=None, max_shrinks=2000) ) assert len(ls) == 5 @given(st.randoms()) @settings( max_examples=50, max_shrinks=0, perform_health_check=False, deadline=None ) @example(Random(-1363972488426139)) @example(Random(-4)) def test_can_use_recursive_data_in_sets(rnd): nested_sets = st.recursive( st.booleans(), lambda js: st.frozensets(js, average_size=2.0), max_leaves=10 ) find_any(nested_sets, random=rnd) def flatten(x): if isinstance(x, bool):
while sum(buff) >= t: if sum(buff) == t: return [lx, rx] buff.popleft() lx += 1 return [-1, -1] def test_answer(): assert answer([4, 3, 10, 2, 8], 12) == [2, 3] assert answer([1, 2, 3, 4], 15) == [-1, -1] assert answer([12], 12) == [0, 0] assert answer([], 15) == [-1, -1] assert answer([1], 15) == [-1, -1] assert answer([1] * 12, 12) == [0, 11] assert answer([0] * 100 + [1] * 12 + [4, 3, 10, 2, 8], 12) == [0, 111] assert answer([13] * 100 + [1] * 12 + [4, 3, 10, 2, 8], 12) == [100, 111] assert answer([13] * 100 + [12] + [4, 3, 10, 2, 8], 12) == [100, 100] @given(st.lists(st.integers(min_value=1), min_size=1), st.randoms()) def test_answer_hyp(xs, r): lx = r.randint(0, len(xs) - 1) rx = r.randint(lx, len(xs) - 1) print("lx %d, rx %d, xs %s" % (lx, rx, xs)) print("xs[lx] %d, xs[rx] %d, lx %d, rx %d" % (xs[lx], xs[rx], lx, rx)) if len(xs) > 1 and lx != rx: print(xs[lx] + xs[rx]) assert answer(xs, sum(xs[lx:rx])) != [-1, -1] else: assert answer(xs, xs[lx]) != [-1, -1]
from hypothesis import find, given, example, settings from hypothesis.control import BuildContext from hypothesis.searchstrategy.morphers import MorpherStrategy morphers = MorpherStrategy() intlists = s.lists(s.integers()) def test_can_simplify_through_a_morpher(): m = find(morphers, lambda x: bool(x.become(intlists))) assert m.become(intlists) == [0] @example(Random(187)) @example(Random(0)) @given(s.randoms()) @settings(max_examples=10) def test_can_simplify_text_through_a_morpher(rnd): m = find( morphers, lambda x: bool(x.become(s.text())), random=rnd, settings=settings(database=None) ) with BuildContext(): assert m.become(s.text()) == u'0' def test_can_simplify_lists_of_morphers_of_single_type(): ms = find( s.lists(morphers), lambda x: sum(t.become(s.integers()) for t in x) >= 100, settings=settings(database=None)
# This file is part of Hypothesis, which may be found at # https://github.com/HypothesisWorks/hypothesis-python # # Most of this work is copyright (C) 2013-2015 David R. MacIver # ([email protected]), but it contains contributions by others. See # CONTRIBUTING.rst for a full list of people who may hold copyright, and # consult the git log if you need to determine who owns an individual # contribution. # # This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at http://mozilla.org/MPL/2.0/. # # END HEADER from __future__ import division, print_function, absolute_import import hypothesis.strategies as st from hypothesis import find, given @given(st.lists(st.uuids())) def test_are_unique(ls): assert len(set(ls)) == len(ls) @given(st.lists(st.uuids()), st.randoms()) def test_retains_uniqueness_in_simplify(ls, rnd): ts = find(st.lists(st.uuids()), lambda x: len(x) >= 5, random=rnd) assert len(ts) == len(set(ts)) == 5
def test_drawing_many_near_boundary(): ls = find( st.lists(st.recursive( st.booleans(), lambda x: st.lists(x, min_size=8, max_size=10).map(tuple), max_leaves=9)), lambda x: len(set(x)) >= 5) assert len(ls) == 5 def test_recursive_call_validates_expand_returns_strategies(): with pytest.raises(InvalidArgument): st.recursive(st.booleans(), lambda x: 1) @given(st.randoms()) def test_can_use_recursive_data_in_sets(rnd): nested_sets = st.recursive( st.booleans(), lambda js: st.frozensets(js), max_leaves=10 ) nested_sets.example() def flatten(x): if isinstance(x, bool): return frozenset((x,)) else: result = frozenset() for t in x: result |= flatten(t)
import hypothesis.strategies as strat from slyther.types import Symbol as s, String as st # noqa from slyther.parser import ControlToken, LParen, RParen, Quote, lex random = strat.randoms().example() lp, rp, q = LParen(), RParen(), Quote() token_sequences = [[lp, lp, lp, lp, lp, lp], [lp, 10, 30, 50.0, rp, s('hello-world'), q, q, rp, rp, lp], [ rp, -10, -70.0, s('&123456789'), q, s('#!#!#!#!#!#!#!#'), s('#t'), s('NIL'), s('初音ミクでてむてららるちょむちょめぷ'), q, q, s('+'), q, s('ZZZZ'), q, s('`'), q, rp, rp, q, lp, rp, lp, q, q, rp ], [ s('OHEA'), st('OHEA'), s('OHEA'), st('OHEA'), st('OHEA'), q, q ], [ st(';;;;;;;;;;;;;;;;;;;;;;'),
def make_random_trie(random): trie = HexaryTrie({}) contents = {} for _ in range(1000): key_length = random.randint(2, 32) key = bytes([random.randint(0, 255) for _ in range(key_length)]) value_length = random.randint(2, 64) value = bytes([random.randint(0, 255) for _ in range(value_length)]) trie[key] = value contents[key] = value return trie, contents @given(random=strategies.randoms()) @settings( max_examples=10, deadline=4000, ) @example(random=RandomWithSeed(EXAMPLE_37968)) @example(random=RandomWithSeed(EXAMPLE_809368)) def test_trie_sync(random, event_loop): # Apparently hypothesis tests cannot be used in conjunction with pytest-asyncio yet, so do it # like this for now. https://github.com/HypothesisWorks/hypothesis/pull/1343 async def _test_trie_sync(): src_trie, contents = make_random_trie(random) dest_db = FakeAsyncAtomicDB() nodes_cache = AtomicDB() scheduler = HexaryTrieSync(src_trie.root_hash, dest_db, nodes_cache,
"differences: \n%s" % (encoding, result, file_name, diff)) if HAVE_HYPOTHESIS: class JustALengthIssue(Exception): pass @pytest.mark.xfail @given(st.text(min_size=1), st.sampled_from(['ascii', 'utf-8', 'utf-16', 'utf-32', 'iso-8859-7', 'iso-8859-8', 'windows-1255']), st.randoms()) @settings(max_examples=200) def test_never_fails_to_detect_if_there_is_a_valid_encoding(txt, enc, rnd): try: data = txt.encode(enc) except UnicodeEncodeError: assume(False) detected = chardet.detect(data)['encoding'] if detected is None: with pytest.raises(JustALengthIssue): @given(st.text(), random=rnd) @settings(verbosity=Verbosity.quiet, max_shrinks=0, max_examples=50) def string_poisons_following_text(suffix): try: extended = (txt + suffix).encode(enc) except UnicodeEncodeError:
from __future__ import division, print_function, absolute_import import pytest from hypothesis import find, given, settings from hypothesis.errors import InvalidArgument from hypothesis.strategies import sets, lists, floats, randoms, integers def test_unique_lists_error_on_too_large_average_size(): with pytest.raises(InvalidArgument): lists(integers(), unique=True, average_size=10, max_size=5).example() @given(randoms()) @settings(max_examples=5) def test_can_draw_sets_of_hard_to_find_elements(rnd): rarebool = floats(0, 1).map(lambda x: x <= 0.01) find(sets(rarebool, min_size=2), lambda x: True, random=rnd, settings=settings(database=None)) def test_sets_of_small_average_size(): assert len(sets(integers(), average_size=1.0).example()) <= 10 @given(sets(max_size=0)) def test_empty_sets(x):
def strategy_test_suite( specifier, max_examples=20, random=None, ): settings = Settings( database=None, max_examples=max_examples, min_satisfying_examples=2, average_list_length=2.0, ) random = random or Random() strat = strategy(specifier, settings) specifier_test = given( templates_for(specifier), randoms(), settings=settings ) class ValidationSuite(TestCase): def __repr__(self): return u'strategy_test_suite(%s)' % ( repr(specifier), ) @given(specifier, settings=settings) def test_does_not_error(self, value): pass def test_can_give_example(self): strat.example() def test_can_give_list_of_examples(self): strategy(lists(strat)).example() def test_will_give_unsatisfiable_if_all_rejected(self): @given(specifier, settings=settings) def nope(x): assume(False) self.assertRaises(Unsatisfiable, nope) def test_will_find_a_constant_failure(self): @given(specifier, settings=settings) def nope(x): raise Rejected() self.assertRaises(Rejected, nope) def test_will_find_a_failure_from_the_database(self): db = ExampleDatabase() @given(specifier, settings=Settings(max_examples=10, database=db)) def nope(x): raise Rejected() try: for i in hrange(3): self.assertRaises(Rejected, nope) # pragma: no cover finally: db.close() @given( templates_for(specifier), templates_for(specifier), settings=settings ) def test_simplicity_is_asymmetric(self, x, y): assert not ( strat.strictly_simpler(x, y) and strat.strictly_simpler(y, x) ) @given(integers(), settings=settings) def test_templates_generated_from_same_random_are_equal(self, i): try: t1 = strat.draw_and_produce(Random(i)) t2 = strat.draw_and_produce(Random(i)) except BadTemplateDraw: assume(False) if t1 is not t2: assert t1 == t2 assert hash(t1) == hash(t2) @given(integers(), settings=settings) def test_templates_generated_from_same_random_are_equal_after_reify( self, i ): try: t1 = strat.draw_and_produce(Random(i)) t2 = strat.draw_and_produce(Random(i)) except BadTemplateDraw: assume(False) if t1 is not t2: with BuildContext(): strat.reify(t1) with BuildContext(): strat.reify(t2) assert t1 == t2 assert hash(t1) == hash(t2) @given(randoms(), settings=settings) def test_will_handle_a_really_weird_failure(self, rnd): db = ExampleDatabase() @given( specifier, settings=Settings( database=db, max_examples=max_examples, min_satisfying_examples=2, average_list_length=2.0, ), random=rnd ) def nope(x): s = hashlib.sha1(repr(x).encode(u'utf-8')).digest() assert Random(s).randint(0, 1) == Random(s).randint(0, 1) if Random(s).randint(0, 1): raise Rejected(u'%r with digest %r' % ( x, s )) try: try: nope() except Rejected: pass try: nope() except Rejected: pass finally: db.close() @specifier_test def test_is_basic(self, value, rnd): def is_basic(v): if v is None or isinstance(v, text_type): return True if isinstance(v, integer_types): return not (abs(v) >> 64) if isinstance(v, list): return all(is_basic(w) for w in v) return False supposedly_basic = strat.to_basic(value) self.assertTrue(is_basic(supposedly_basic), repr(supposedly_basic)) @specifier_test def test_only_raises_bad_data_in_from_basic(self, value, rnd): basic = strat.to_basic(value) messed_basic = mutate_basic(basic, rnd) try: strat.from_basic(messed_basic) except BadData: pass @specifier_test def test_can_round_trip_through_the_database(self, template, rnd): empty_db = ExampleDatabase( backend=SQLiteBackend(u':memory:'), ) try: storage = empty_db.storage(u'round trip') storage.save(template, strat) values = list(storage.fetch(strat)) assert len(values) == 1 assert strat.to_basic(template) == strat.to_basic(values[0]) finally: empty_db.close() @specifier_test def test_template_is_hashable(self, template, rnd): hash(template) # It can be easy to forget to convert a list... hash(strat.from_basic(strat.to_basic(template))) @specifier_test def test_can_minimize_to_empty(self, template, rnd): simplest = template tracker = Tracker() while True: for t in strat.full_simplify(rnd, simplest): if tracker.track(t) == 1: simplest = t break else: break assert list(strat.full_simplify(rnd, simplest)) == [] @specifier_test def test_full_simplify_completes(self, template, rnd): # Cut off at 1000 for the occasional case where we get # really very large templates which have too many simplifies. for x in islice(strat.full_simplify(rnd, template), 1000): pass @specifier_test def test_does_not_increase_complexity(self, template, rnd): for s in islice(strat.full_simplify(rnd, template), 100): assert not strat.strictly_simpler(template, s) @given(randoms(), settings=Settings(max_examples=1000)) def test_can_create_templates(self, random): parameter = strat.draw_parameter(random) try: strat.draw_template(random, parameter) except BadTemplateDraw: assume(False) return ValidationSuite
text(), binary(), booleans(), tuples(booleans(), booleans()), frozensets(integers()), sets(frozensets(booleans())), complex_numbers(), fractions(), decimals(), lists(lists(booleans())), lists(lists(booleans(), average_size=100)), lists(floats(0.0, 0.0), average_size=1.0), ordered_pair, constant_list(integers()), streaming(integers()).map(lambda x: list(x[:2]) and x), integers().filter(lambda x: abs(x) > 100), floats(min_value=-sys.float_info.max, max_value=sys.float_info.max), none(), randoms(), tuples().flatmap(lambda x: EvalledIntStream), templates_for(integers(min_value=0, max_value=0).flatmap( lambda x: integers(min_value=0, max_value=0))), booleans().flatmap(lambda x: booleans() if x else complex_numbers()), recursive( base=booleans(), extend=lambda x: lists(x, max_size=3), max_leaves=10, ) ] if pytest is not None: def parametrize(args, values): return pytest.mark.parametrize( args, values, ids=list(map(show, values)))
def test_random_repr_has_seed(): rnd = randoms().example() seed = rnd.seed assert text_type(seed) in repr(rnd)
def in_db(): return set( v for vs in db.data.values() for v in vs ) assert len(in_db()) == n + 1 runner = ConjectureRunner( lambda data: data.draw_bytes(4), settings=local_settings, database_key=key) runner.run() assert 0 < len(in_db()) < n @given(st.randoms(), st.random_module()) @settings(max_shrinks=0) def test_maliciously_bad_generator(rnd, seed): @run_to_buffer def x(data): for _ in range(rnd.randint(1, 100)): data.draw_bytes(rnd.randint(1, 10)) if rnd.randint(0, 1): data.mark_invalid() else: data.mark_interesting() @given(st.random_module()) def test_lot_of_dead_nodes(rnd): @run_to_buffer
floats(min_value=-2.0, max_value=3.0), floats(), floats(min_value=-2.0), floats(), floats(max_value=-0.0), floats(), floats(min_value=0.0), floats(min_value=3.14, max_value=3.14), text(), binary(), booleans(), tuples(booleans(), booleans()), frozensets(integers()), sets(frozensets(booleans())), complex_numbers(), fractions(), decimals(), lists(lists(booleans())), lists(floats(0.0, 0.0)), ordered_pair, constant_list(integers()), integers().filter(lambda x: abs(x) > 100), floats(min_value=-sys.float_info.max, max_value=sys.float_info.max), none(), randoms(use_true_random=True), booleans().flatmap(lambda x: booleans() if x else complex_numbers()), recursive(base=booleans(), extend=lambda x: lists(x, max_size=3), max_leaves=10), ]
TestSampled2 = strategy_test_suite(sampled_from(elements=(1, 2))) TestIntegersFrom = strategy_test_suite(integers(min_value=13)) TestIntegersFrom = strategy_test_suite(integers(min_value=1 << 1024)) TestOneOf = strategy_test_suite(one_of( integers(), integers(), booleans())) TestOneOfSameType = strategy_test_suite( one_of( integers(min_value=1, max_value=10), integers(min_value=8, max_value=15), ) ) TestRandom = strategy_test_suite(randoms()) TestInts = strategy_test_suite(integers()) TestBoolLists = strategy_test_suite(lists(booleans(), average_size=5.0)) TestDictionaries = strategy_test_suite( dictionaries(keys=tuples(integers(), integers()), values=booleans())) TestOrderedDictionaries = strategy_test_suite( dictionaries( keys=integers(), values=integers(), dict_class=OrderedDict)) TestString = strategy_test_suite(text()) BinaryString = strategy_test_suite(binary()) TestIntBool = strategy_test_suite(tuples(integers(), booleans())) TestFloats = strategy_test_suite(floats()) TestComplex = strategy_test_suite(complex_numbers()) TestJust = strategy_test_suite(just(u'hi')) TestTemplates = strategy_test_suite(templates_for(sets(integers())))
copy = FFI.new("struct umash_params[1]") FFI.memmove(copy, params, size) assert C.umash_params_prepare(copy) == True # The copy should still be the same as params. assert size % 8 == 0 for i in range(size // 8): assert FFI.cast("uint64_t *", params)[i] == FFI.cast("uint64_t *", copy)[i] @example(multipliers=[0, FIELD], random=random.Random(1)) @given( multipliers=st.lists(st.integers(min_value=1, max_value=FIELD - 1) | U64S, min_size=2, max_size=2), random=st.randoms(note_method_calls=True, use_true_random=True), ) def test_public_multiplier_reduction(multipliers, random): """Make sure multipliers are correctly reduced and rejected.""" params = FFI.new("struct umash_params[1]") params[0].poly[0][0] = random.getrandbits(64) params[0].poly[0][1] = multipliers[0] params[0].poly[1][0] = random.getrandbits(64) params[0].poly[1][1] = multipliers[1] for i in range(PH_COUNT): params[0].ph[i] = i assert C.umash_params_prepare(params) == True assert_idempotent(params) for i in range(2):
from hypothesis.strategies import just, sets, text, lists, binary, \ floats, one_of, tuples, randoms, booleans, integers, frozensets, \ sampled_from, complex_numbers, fixed_dictionaries from hypothesis.searchstrategy.narytree import n_ary_tree from hypothesis.searchstrategy.strategies import BadData, strategy @pytest.mark.parametrize(('specifier', 'data'), [ (sets(text()), 0j), (complex_numbers(), set('hi')), (lists(sets(booleans())), 0), (just(1), 'hi'), (binary(), 0.0), (binary(), frozenset()), (fixed_dictionaries({True: sets(integers())}), []), (randoms(), []), (integers(), ''), (integers(), [0, '']), (text(), 'kittens'), (tuples(integers(), integers(), integers()), (1, 2)), (sampled_from((1, 2, 3)), 'fish'), (sampled_from((1, 2, 3)), 5), (sampled_from((1, 2, 3)), -2), (one_of(integers(), floats()), 1), (one_of(integers(), floats()), 'tv'), (one_of(integers(), floats()), [-2, 0]), (binary(), '1'), (floats(), -1), (lists(one_of(frozensets(floats()), frozensets(floats()))), [[8, 0], []]), (floats(), 252010555201342071294067021251680995120), (tuples(integers(), integers()), 10),
# This file is part of Hypothesis, which may be found at # https://github.com/HypothesisWorks/hypothesis-python # # Most of this work is copyright (C) 2013-2017 David R. MacIver # ([email protected]), but it contains contributions by others. See # CONTRIBUTING.rst for a full list of people who may hold copyright, and # consult the git log if you need to determine who owns an individual # contribution. # # This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at http://mozilla.org/MPL/2.0/. # # END HEADER from __future__ import division, print_function, absolute_import import hypothesis.strategies as st from hypothesis import find, given @given(st.lists(st.uuids())) def test_are_unique(ls): assert len(set(ls)) == len(ls) @given(st.lists(st.uuids()), st.randoms()) def test_retains_uniqueness_in_simplify(ls, rnd): ts = find(st.lists(st.uuids()), lambda x: len(x) >= 5, random=rnd) assert len(ts) == len(set(ts)) == 5
def test_is_equal_to_sequential_insertion(imp): equiv = IntSet.empty() for i in imp: equiv = equiv.insert(i) assert imp == equiv @given(SmallIntSets) def test_is_equal_to_reverse_insertion(imp): equiv = IntSet.empty() for i in reversed(list(imp)): equiv = equiv.insert(i) assert imp == equiv @given(SmallIntSets, st.randoms()) def test_is_equal_to_random_insertion(imp, rnd): items = list(imp) rnd.shuffle(items) equiv = IntSet.empty() for i in items: equiv = equiv.insert(i) assert imp == equiv @given(SmallIntSets) def test_an_intset_is_consistent_with_its_index(imp): for index, value in enumerate(imp): assert imp[index] == value
normalize_zero, ) from tests.common.debug import find_any from tests.common.utils import capture_out def test_implements_all_random_methods(): for name in dir(HypothesisRandom): if not name.startswith("_") or name == "_randbelow": f = getattr(HypothesisRandom, name) if inspect.isfunction(f): assert f.__module__ == "hypothesis.strategies._internal.random", name any_random = st.booleans().flatmap(lambda i: st.randoms(use_true_random=i)) beta_param = st.floats(0.01, 1000) seq_param = st.lists(st.integers(), min_size=1) METHOD_STRATEGIES = {} def define_method_strategy(name, **kwargs): METHOD_STRATEGIES[name] = kwargs define_method_strategy("betavariate", alpha=beta_param, beta=beta_param) define_method_strategy("gammavariate", alpha=beta_param, beta=beta_param) define_method_strategy("weibullvariate", alpha=beta_param, beta=beta_param) define_method_strategy("choice", seq=seq_param)
seen.add(x) counter[0] += 1 if x in seen: data.mark_interesting() local_settings = settings(database=db, max_shrinks=2 * n, timeout=-1) runner = ConjectureRunner(f, settings=local_settings, database_key=key) runner.run() assert runner.last_data.status == Status.INTERESTING assert len(seen) == n assert set(db.fetch(key)) == seen go = False runner = ConjectureRunner(f, settings=local_settings, database_key=key) runner.run() assert 0 < len(set(db.fetch(key))) < n @given(st.randoms(), st.random_module()) def test_maliciously_bad_generator(rnd, seed): rnd = Random() @run_to_buffer def x(data): for _ in range(rnd.randint(0, 100)): data.draw_bytes(rnd.randint(0, 10)) if rnd.randint(0, 1): data.mark_invalid() else: data.mark_interesting()
def test_duplicate_containment(): ls, i = minimal( tuples(lists(integers()), integers()), lambda s: s[0].count(s[1]) > 1, timeout_after=100) assert ls == [0, 0] assert i == 0 def test_unique_lists_of_single_characters(): x = minimal( lists(text(max_size=1), unique=True, min_size=5) ) assert sorted(x) == ['', '0', '1', '2', '3'] @given(randoms()) @settings(max_examples=10, database=None, max_shrinks=0) @example(rnd=Random(340282366920938463462798146679426884207)) def test_can_simplify_hard_recursive_data_into_boolean_alternative(rnd): """This test forces us to exercise the simplification through redrawing functionality, thus testing that we can deal with bad templates.""" def leaves(ls): if isinstance(ls, (bool,) + integer_types): return [ls] else: return sum(map(leaves, ls), []) def hard(base): return recursive( base, lambda x: lists(x, max_size=5), max_leaves=20) r = find(
assert encoding_match, ("Expected %s, but got %s for %s. Character " "differences: \n%s" % (encoding, result, file_name, diff)) if HAVE_HYPOTHESIS: class JustALengthIssue(Exception): pass @pytest.mark.xfail @given(st.text(min_size=1), st.sampled_from([ 'ascii', 'utf-8', 'utf-16', 'utf-32', 'iso-8859-7', 'iso-8859-8', 'windows-1255' ]), st.randoms()) @settings(max_examples=200) def test_never_fails_to_detect_if_there_is_a_valid_encoding(txt, enc, rnd): try: data = txt.encode(enc) except UnicodeEncodeError: assume(False) detected = chardet.detect(data)['encoding'] if detected is None: with pytest.raises(JustALengthIssue): @given(st.text(), random=rnd) @settings(verbosity=Verbosity.quiet, max_shrinks=0, max_examples=50) def string_poisons_following_text(suffix):