def data_funcs(wrapper): intfuncs = streaming(sampled_from([ lambda a: a * 2, lambda a: a + 1, ])) floatfuncs = streaming(sampled_from([ lambda a: a / 2, lambda a: a + 1.5, ])) stringfuncs = streaming( sampled_from([ lambda a: a + '!', lambda a: (a + '#')[0], ])) unistringfuncs = streaming( sampled_from([ lambda a: a + u'!', lambda a: (a + u'#')[0], ])) def make_option(strategy, funcs): return strat.tuples(strategy, wrapper(strategy), funcs) return one_of( make_option(strat.integers(), intfuncs), make_option(strat.floats(allow_nan=False), floatfuncs), make_option(strat.text(), stringfuncs), make_option(strat.characters(), unistringfuncs), )
class TestDecompressor_stream_reader_fuzzing(unittest.TestCase): @hypothesis.given(original=strategies.sampled_from(random_input_data()), level=strategies.integers(min_value=1, max_value=5), source_read_size=strategies.integers(1, 16384), read_sizes=strategies.streaming( strategies.integers(min_value=1, max_value=16384))) def test_stream_source_read_variance(self, original, level, source_read_size, read_sizes): read_sizes = iter(read_sizes) cctx = zstd.ZstdCompressor(level=level) frame = cctx.compress(original) dctx = zstd.ZstdDecompressor() source = io.BytesIO(frame) chunks = [] with dctx.stream_reader(source, read_size=source_read_size) as reader: while True: chunk = reader.read(next(read_sizes)) if not chunk: break chunks.append(chunk) self.assertEqual(b''.join(chunks), original) @hypothesis.given(original=strategies.sampled_from(random_input_data()), level=strategies.integers(min_value=1, max_value=5), source_read_size=strategies.integers(1, 16384), read_sizes=strategies.streaming( strategies.integers(min_value=1, max_value=16384))) def test_buffer_source_read_variance(self, original, level, source_read_size, read_sizes): read_sizes = iter(read_sizes) cctx = zstd.ZstdCompressor(level=level) frame = cctx.compress(original) dctx = zstd.ZstdDecompressor() chunks = [] with dctx.stream_reader(frame, read_size=source_read_size) as reader: while True: chunk = reader.read(next(read_sizes)) if not chunk: break chunks.append(chunk) self.assertEqual(b''.join(chunks), original)
class TestDecompressor_write_to_fuzzing(unittest.TestCase): @hypothesis.given(original=strategies.sampled_from(random_input_data()), level=strategies.integers(min_value=1, max_value=5), write_size=strategies.integers(min_value=1, max_value=8192), input_sizes=strategies.streaming( strategies.integers(min_value=1, max_value=4096))) def test_write_size_variance(self, original, level, write_size, input_sizes): input_sizes = iter(input_sizes) cctx = zstd.ZstdCompressor(level=level) frame = cctx.compress(original) dctx = zstd.ZstdDecompressor() source = io.BytesIO(frame) dest = io.BytesIO() with dctx.write_to(dest, write_size=write_size) as decompressor: while True: chunk = source.read(next(input_sizes)) if not chunk: break decompressor.write(chunk) self.assertEqual(dest.getvalue(), original)
class TestCompressor_compressobj_fuzzing(unittest.TestCase): @hypothesis.given(original=strategies.sampled_from(random_input_data()), level=strategies.integers(min_value=1, max_value=5), chunk_sizes=strategies.streaming( strategies.integers(min_value=1, max_value=4096))) def test_random_input_sizes(self, original, level, chunk_sizes): chunk_sizes = iter(chunk_sizes) refctx = zstd.ZstdCompressor(level=level) ref_frame = refctx.compress(original) cctx = zstd.ZstdCompressor(level=level) cobj = cctx.compressobj(size=len(original)) chunks = [] i = 0 while True: chunk_size = next(chunk_sizes) source = original[i:i + chunk_size] if not source: break chunks.append(cobj.compress(source)) i += chunk_size chunks.append(cobj.flush()) self.assertEqual(b''.join(chunks), ref_frame)
class TestDecompressor_decompressobj_fuzzing(unittest.TestCase): @hypothesis.given(original=strategies.sampled_from(random_input_data()), level=strategies.integers(min_value=1, max_value=5), chunk_sizes=strategies.streaming( strategies.integers(min_value=1, max_value=4096))) def test_random_input_sizes(self, original, level, chunk_sizes): chunk_sizes = iter(chunk_sizes) cctx = zstd.ZstdCompressor(level=level) frame = cctx.compress(original) source = io.BytesIO(frame) dctx = zstd.ZstdDecompressor() dobj = dctx.decompressobj() chunks = [] while True: chunk = source.read(next(chunk_sizes)) if not chunk: break chunks.append(dobj.decompress(chunk)) self.assertEqual(b''.join(chunks), original)
def test_streaming_flatmap_past_point_of_read(): s = find( streaming(integers().flatmap(lambda n: integers(min_value=n))), lambda x: x[0]) assert s[0] == 1 for i in hrange(100): s[i]
def test_streams_are_arbitrarily_long(): @settings(suppress_health_check=[HealthCheck.too_slow]) @given(streaming(integers())) def test(ss): for i in islice(ss, 100): assert isinstance(i, integer_types) test()
def test_fetched_repr_is_in_stream_repr(): @given(streaming(text())) def test(s): assert repr(s) == u'Stream(...)' assert repr(next(iter(s))) in repr(s) test()
def test_streams_are_arbitrarily_long(): @given(streaming(integers())) def test(ss): for i in islice(ss, 100): assert isinstance(i, integer_types) test()
def data_funcs(wrapper): intfuncs = streaming(sampled_from([lambda a: a * 2, lambda a: a + 1, ])) floatfuncs = streaming(sampled_from([ lambda a: a / 2, lambda a: a + 1.5, ])) stringfuncs = streaming(sampled_from([ lambda a: a + '!', lambda a: (a + '#')[0], ])) return one_of( strat.tuples(strat.integers(), wrapper(strat.integers()), intfuncs), strat.tuples( strat.floats(allow_nan=False), wrapper(strat.floats(allow_nan=False)), floatfuncs), strat.tuples(strat.text(), wrapper(strat.text()), stringfuncs), )
def test_streams_copy_as_self(): x = streaming(booleans()).example() assert copy(x) is x assert deepcopy(x) is x y = x.map(lambda x: not x) assert copy(y) is y assert deepcopy(y) is y
def test_check_serialization_preserves_changed_marker(): strat = strategy(streaming(floats(min_value=0.0, max_value=2.2250738585072014e-308))) template = strat.draw_template(Random(0), strat.draw_parameter(Random(0))) strat.reify(template)[0] simpler = next(strat.full_simplify(Random(0), template)) as_basic = strat.to_basic(simpler) assert as_basic == strat.to_basic(strat.from_basic(as_basic))
def test_check_serialization_preserves_changed_marker(): strat = streaming(floats(min_value=0.0, max_value=2.2250738585072014e-308)) template = strat.draw_template(Random(0), strat.draw_parameter(Random(0))) with BuildContext(): strat.reify(template)[0] simpler = next(strat.full_simplify(Random(0), template)) as_basic = strat.to_basic(simpler) assert as_basic == strat.to_basic(strat.from_basic(as_basic))
def data_funcs(wrapper): intfuncs = streaming(sampled_from([ lambda a: a * 2, lambda a: a + 1, ])) floatfuncs = streaming(sampled_from([ lambda a: a / 2, lambda a: a + 1.5, ])) stringfuncs = streaming( sampled_from([ lambda a: a + '!', lambda a: (a + '#')[0], ])) return one_of( strat.tuples(strat.integers(), wrapper(strat.integers()), intfuncs), strat.tuples(strat.floats(allow_nan=False), wrapper(strat.floats(allow_nan=False)), floatfuncs), strat.tuples(strat.text(), wrapper(strat.text()), stringfuncs), )
def test_template_equality(): t = some_template(streaming(booleans())) t2 = StreamTemplate(t.seed, t.parameter_seed, Stream(t.stream)) while True: t3 = some_template(streaming(booleans())) if t3.seed != t.seed: break assert t == t2 assert t != t3 assert t != 1 v = t.stream[11] t4 = t2.with_value(11, not v) assert t4 != t t5 = StreamTemplate(t.seed, t.parameter_seed + 1, Stream(t.stream)) assert t2 != t5 assert len(set((t, t2, t3, t4, t5))) == 4
def test_can_eval_stream_inside_find(): @given(st.streaming(st.integers(min_value=0)), st.random_module()) @settings(buffer_size=200, max_shrinks=5, max_examples=10) def test(stream, rnd): x = find(st.lists(st.integers(min_value=0), min_size=10), lambda t: any(t > s for (t, s) in zip(t, stream)), settings=settings(database=None, max_shrinks=2000, max_examples=2000)) note('x: %r' % (x, )) note('Evalled: %r' % (stream, )) assert len([1 for i, v in enumerate(x) if stream[i] < v]) == 1 test()
def test_can_eval_stream_inside_find(): @given(st.streaming(st.integers(min_value=0)), st.random_module()) @settings(buffer_size=200, max_shrinks=5, max_examples=10) def test(stream, rnd): x = find( st.lists(st.integers(min_value=0), min_size=10), lambda t: any(t > s for (t, s) in zip(t, stream)), settings=settings( database=None, max_shrinks=2000, max_examples=2000) ) note('x: %r' % (x,)) note('Evalled: %r' % (stream,)) assert len([1 for i, v in enumerate(x) if stream[i] < v]) == 1 test()
# # Most of this work is copyright (C) 2013-2016 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 from hypothesis import strategies as st from hypothesis import find, note, given, settings @given(st.streaming(st.integers(min_value=0)), st.random_module()) @settings(buffer_size=200, max_shrinks=5, max_examples=10) def test_can_eval_stream_inside_find(stream, rnd): x = find( st.lists(st.integers(min_value=0), min_size=10), lambda t: any(t > s for (t, s) in zip(t, stream)), settings=settings(database=None, max_shrinks=2000, max_examples=2000), ) note("x: %r" % (x,)) note("Evalled: %r" % (stream,)) assert len([1 for i, v in enumerate(x) if stream[i] < v]) == 1
def test_can_zip_streams_with_self(xs): s = Stream(iter(xs)) assert list(zip(s, s)) == list(zip(xs, xs)) def loop(x): while True: yield x def test_can_stream_infinite(): s = Stream(loop(False)) assert list(islice(s, 100)) == [False] * 100 @given(streaming(text())) def test_fetched_repr_is_in_stream_repr(s): assert repr(s) == u'Stream(...)' assert show(next(iter(s))) in show(s) def test_cannot_thunk_past_end_of_list(): with pytest.raises(IndexError): Stream([1])._thunk_to(5) def test_thunking_evaluates_initial_list(): x = Stream([1, 2, 3]) x._thunk_to(1) assert len(x.fetched) == 1
def test_streaming_streams(): for v in ds.streaming(ds.integers(max_value=1000)).example()[:10]: assert v <= 1000
# # Most of this work is copyright (C) 2013-2015 David R. MacIver # ([email protected]), but it contains contributions by others. See # https://github.com/DRMacIver/hypothesis/blob/master/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 from hypothesis import strategies as st from hypothesis import find, note, given, settings @given(st.streaming(st.integers(min_value=0)), st.random_module()) @settings(buffer_size=200, max_shrinks=5, max_examples=10) def test_can_eval_stream_inside_find(stream, rnd): x = find(st.lists(st.integers(min_value=0), min_size=10), lambda t: any(t > s for (t, s) in zip(t, stream)), settings=settings(database=None, max_shrinks=2000, max_examples=2000)) note('x: %r' % (x, )) note('Evalled: %r' % (stream, )) assert len([1 for i, v in enumerate(x) if stream[i] < v]) == 1
OrderedPair = namedtuple(u'OrderedPair', (u'left', u'right')) ordered_pair = integers().flatmap( lambda right: integers(min_value=0).map( lambda length: OrderedPair(right - length, right))) def constant_list(strat): return strat.flatmap( lambda v: lists(just(v)), ) EvalledIntStream = streaming(integers()).map(lambda x: list(x[:5]) and x) ABC = namedtuple(u'ABC', (u'a', u'b', u'c')) def abc(x, y, z): return builds(ABC, x, y, z) with Settings(average_list_length=10.0): standard_types = [ basic(Bitfields), EvalledIntStream, lists(max_size=0), tuples(), sets(max_size=0), frozensets(max_size=0), fixed_dictionaries({}), n_ary_tree(booleans(), booleans(), booleans()), n_ary_tree(integers(), integers(), integers()),
def test_streams_with_distinct_values(): x = find(streaming(integers()), lambda x: len(set(x[:10])) >= 10) elts = sorted(set(x[:10])) assert len(elts) == 10 assert elts == list(range(min(elts), max(elts) + 1))
return integers(min_value=x) TestManyFlatmaps = strategy_test_suite( integers() .flatmap(integers_from) .flatmap(integers_from) .flatmap(integers_from) .flatmap(integers_from) ) TestBareMorphers = strategy_test_suite(MorpherStrategy()) TestMasqueradingMorphers = strategy_test_suite( MorpherStrategy().map(lambda m: m.become( lists(integers(), average_size=5.0)))) TestIntStreams = strategy_test_suite(streaming(integers())) TestStreamLists = strategy_test_suite(streaming(integers())) TestIntStreamStreams = strategy_test_suite( streaming(streaming(integers()))) TestRecursiveLowLeaves = strategy_test_suite( recursive( booleans(), lambda x: tuples(x, x), max_leaves=3, ) ) TestRecursiveHighLeaves = strategy_test_suite( recursive( booleans(),
def build_stream(self, strat): return strategy(streaming(strat))
def test_decreasing_streams(): n = 10 x = find(streaming(integers()), lambda x: all(x[i] >= n - i for i in range(n + 1))) assert list(x[: (n + 1)]) == list(range(n, -1, -1))
def test_lists_of_streams(): x = find(lists(streaming(integers()), min_size=10), lambda x: all(t[3] for t in x)) assert [list(t[:4]) for t in x] == [[0] * 3 + [1]] * 10
def test_can_zip_streams_with_self(xs): s = Stream(iter(xs)) assert list(zip(s, s)) == list(zip(xs, xs)) def loop(x): while True: yield x def test_can_stream_infinite(): s = Stream(loop(False)) assert list(islice(s, 100)) == [False] * 100 @given(streaming(text())) def test_fetched_repr_is_in_stream_repr(s): assert repr(s) == u"Stream(...)" assert show(next(iter(s))) in show(s) def test_cannot_thunk_past_end_of_list(): with pytest.raises(IndexError): Stream([1])._thunk_to(5) def test_thunking_evaluates_initial_list(): x = Stream([1, 2, 3]) x._thunk_to(1) assert len(x.fetched) == 1
def evalled_stream(self, strat, i): return strategy(streaming(strat)).map(lambda x: list(x[:i]) and x)
# Connection delivers message to server, if there is any. The server # then acks it. Do this after ack delivery so it takes another tick for # acks to be delivered. if self.connection_to_server: message = self.connection_to_server.pop() self.server_received.add(message.payload) if message.sync: # Send ack since sync flag was set: self.connection_to_client.appendleft(message.sequence) # Tell client time has passed: self.client.onPump(SendToServer(self)) @given(st.streaming(st.integers(min_value=1, max_value=5))) def test_sendWithAcks_delivery(disconnect_intervals): """ Demonstrate that sendWithAcks algorithm delivers messages even though connections are dropped. Basic test setup: 1. SendWithAcks is told to send 10 messages. 2. Messages get added to a Connection. 3. Once a (fake) second a message is moved from Connection to Server. 4. Once a second the Server sends an Ack to Connection. 5. Once a second the Acks from Connection are delivered to SendWithAcks. Every once in a while the Connection disconnects, and everything in its buffers is wiped and never delivered. It then reconnects.
__all__ = [u"small_verifier", u"timeout", u"standard_types", u"OrderedPair"] OrderedPair = namedtuple(u"OrderedPair", (u"left", u"right")) ordered_pair = integers().flatmap( lambda right: integers(min_value=0).map(lambda length: OrderedPair(right - length, right)) ) def constant_list(strat): return strat.flatmap(lambda v: lists(just(v))) EvalledIntStream = streaming(integers()).map(lambda x: list(x[:5]) and x) ABC = namedtuple(u"ABC", (u"a", u"b", u"c")) def abc(x, y, z): return builds(ABC, x, y, z) with Settings(average_list_length=10.0): standard_types = [ basic(Bitfields), EvalledIntStream, lists(max_size=0), tuples(), sets(max_size=0),
def test_validates_argument(): with pytest.raises(InvalidArgument): streaming(bool).example()
# coding=utf-8 # # This file is part of Hypothesis (https://github.com/DRMacIver/hypothesis) # # Most of this work is copyright (C) 2013-2015 David R. MacIver # ([email protected]), but it contains contributions by others. See # https://github.com/DRMacIver/hypothesis/blob/master/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 from itertools import islice from hypothesis import given from hypothesis.strategies import integers, streaming from hypothesis.internal.compat import integer_types @given(streaming(integers())) def test_streams_are_arbitrarily_long(ss): for i in islice(ss, 100): assert isinstance(i, integer_types)
def test_streaming_errors_in_find(): with pytest.raises(InvalidArgument): find(streaming(booleans()), lambda x: True)
def test_find_streaming_int(): n = 100 r = find(streaming(integers()), lambda x: all(t >= 1 for t in x[:n])) assert list(r[:n]) == [1] * n
def test_streaming_flatmap_past_point_of_read(): s = find(streaming(integers().flatmap(lambda n: integers(min_value=n))), lambda x: x[0]) assert s[0] == 1 for i in hrange(100): s[i]
TestOrderedPairs = strategy_test_suite( strategy(integers(min_value=1, max_value=200)).flatmap( lambda e: tuples(integers(min_value=0, max_value=e - 1), just(e)))) TestMappedSampling = strategy_test_suite( lists(integers(), min_size=1).flatmap(sampled_from)) def integers_from(x): return integers(min_value=x) TestManyFlatmaps = strategy_test_suite( integers().flatmap(integers_from).flatmap(integers_from).flatmap( integers_from).flatmap(integers_from)) TestIntStreams = strategy_test_suite(streaming(integers())) TestStreamLists = strategy_test_suite(streaming(integers())) TestIntStreamStreams = strategy_test_suite(streaming(streaming( integers()))) TestBoringBitfieldsClass = strategy_test_suite(basic(BoringBitfields)) TestBitfieldsClass = strategy_test_suite(basic(Bitfields)) TestBitfieldsInstance = strategy_test_suite(basic(Bitfields())) TestBitfields = strategy_test_suite( lists( basic( generate=lambda r, p: r.getrandbits(128), simplify=simplify_bitfield, copy=lambda x: x, )))
def stream_strategy(stream, settings): return st.streaming(strategy(stream.data, settings))
def test_can_minimize(): x = minimal(streaming(integers()), lambda x: x[10] >= 1) ts = list(x[:11]) assert ts == [0] * 10 + [1]
def many_one_of(*strategies): return one_of(*(streaming(s) for s in strategies))
def test_decreasing_streams(): n = 10 x = find(streaming(integers()), lambda x: all(x[i] >= n - i for i in range(n + 1))) assert list(x[:(n + 1)]) == list(range(n, -1, -1))