From 18434779e3371eb12be07f69092cac3aae973948 Mon Sep 17 00:00:00 2001 From: "David R. MacIver" Date: Fri, 25 Sep 2015 15:45:57 +0100 Subject: [PATCH] Upgrade to new Hypothesis API. --- test/strategies.py | 16 +++--- test/test-requirements.txt | 2 +- test/test_catalog.py | 46 ++++++++--------- test/test_extended_textual_header.py | 10 ++-- test/test_float.py | 76 ++++++++++++++-------------- test/test_roundtrip.py | 4 +- test/test_util.py | 28 +++++----- 7 files changed, 91 insertions(+), 91 deletions(-) diff --git a/test/strategies.py b/test/strategies.py index c942389..1c283cd 100644 --- a/test/strategies.py +++ b/test/strategies.py @@ -1,6 +1,5 @@ from itertools import accumulate, starmap -from hypothesis import strategy -from hypothesis.specifiers import integers_in_range, just +from hypothesis.strategies import integers, just, fixed_dictionaries, lists from segpy.trace_header import TraceHeaderRev0 from segpy.util import batched @@ -19,8 +18,8 @@ def multiline_ascii_encodable_text(min_num_lines, max_num_lines): and characters which are encodable as printable 7-bit ASCII characters. """ - return strategy(integers_in_range(min_num_lines, max_num_lines)) \ - .flatmap(lambda n: ([integers_in_range(*PRINTABLE_ASCII_RANGE)],) * n) \ + return integers(min_num_lines, max_num_lines) \ + .flatmap(lambda n: lists(integers(*PRINTABLE_ASCII_RANGE), min_size=n, max_size=n)) \ .map(lambda xs: '\n'.join(bytes(x).decode('ascii') for x in xs)) @@ -33,9 +32,9 @@ def spaced_ranges(min_num_ranges, max_num_ranges, min_interval, max_interval): min_interval: The minimum interval used for the lengths of the alternating ranges and spaces. max_interval: The maximum interval used for the lengths of the alternating ranges and spaces. """ - return strategy(integers_in_range(min_num_ranges, max_num_ranges)) \ + return integers(min_num_ranges, max_num_ranges) \ .map(lambda n: 2*n) \ - .flatmap(lambda n: (integers_in_range(min_interval, max_interval),) * n) \ + .flatmap(lambda n: lists(integers(min_interval, max_interval), min_size=n, max_size=n)) \ .map(list).map(lambda lst: list(accumulate(lst))) \ .map(lambda lst: list(batched(lst, 2))) \ .map(lambda pairs: list(starmap(range, pairs))) @@ -59,7 +58,7 @@ def header(header_class, **kwargs): field_strategy = just(kwargs.pop(field_name)) else: value_type = getattr(header_class, field_name).value_type - field_strategy = integers_in_range(value_type.MINIMUM, value_type.MAXIMUM) + field_strategy = integers(value_type.MINIMUM, value_type.MAXIMUM) field_strategies[field_name] = field_strategy if len(kwargs) > 0: @@ -67,4 +66,5 @@ def header(header_class, **kwargs): ', '.join(kwargs.keys()), header_class.__name__)) - return strategy(field_strategies).map(lambda kw: header_class(**kw)) + return fixed_dictionaries(field_strategies) \ + .map(lambda kw: header_class(**kw)) diff --git a/test/test-requirements.txt b/test/test-requirements.txt index bcf2f58..77e16f8 100644 --- a/test/test-requirements.txt +++ b/test/test-requirements.txt @@ -1 +1 @@ -hypothesis>=1.2 +hypothesis>=1.11 diff --git a/test/test_catalog.py b/test/test_catalog.py index ef88345..acf1bb3 100644 --- a/test/test_catalog.py +++ b/test/test_catalog.py @@ -1,30 +1,30 @@ import unittest from hypothesis import given, example, assume -from hypothesis.specifiers import dictionary, just, integers_in_range, streaming +from hypothesis.strategies import dictionaries, just, integers, streaming, tuples from segpy.catalog import CatalogBuilder class TestCatalogBuilder(unittest.TestCase): - @given(dictionary(int, int)) + @given(dictionaries(integers(), integers())) def test_arbitrary_mapping(self, mapping): builder = CatalogBuilder(mapping) catalog = builder.create() shared_items = set(mapping.items()) & set(catalog.items()) self.assertEqual(len(shared_items), len(mapping)) - @given(dictionary(int, just(42))) + @given(dictionaries(integers(), just(42))) def test_constant_mapping(self, mapping): builder = CatalogBuilder(mapping) catalog = builder.create() shared_items = set(mapping.items()) & set(catalog.items()) self.assertEqual(len(shared_items), len(mapping)) - @given(start=int, - num=integers_in_range(0, 10000), - step=integers_in_range(-10000, 10000), - value=int) + @given(start=integers(), + num=integers(0, 10000), + step=integers(-10000, 10000), + value=integers()) def test_regular_constant_mapping(self, start, num, step, value): assume(step != 0) mapping = {key: value for key in range(start, start + num*step, step)} @@ -33,10 +33,10 @@ class TestCatalogBuilder(unittest.TestCase): shared_items = set(mapping.items()) & set(catalog.items()) self.assertEqual(len(shared_items), len(mapping)) - @given(start=int, - num=integers_in_range(0, 10000), - step=integers_in_range(-10000, 10000), - values=streaming(int)) + @given(start=integers(), + num=integers(0, 10000), + step=integers(-10000, 10000), + values=streaming(integers())) def test_regular_mapping(self, start, num, step, values): assume(step != 0) mapping = {key: value for key, value in zip(range(start, start + num*step, step), values)} @@ -45,11 +45,11 @@ class TestCatalogBuilder(unittest.TestCase): shared_items = set(mapping.items()) & set(catalog.items()) self.assertEqual(len(shared_items), len(mapping)) - @given(num=integers_in_range(0, 10000), - key_start=int, - key_step=integers_in_range(-10000, 10000), - value_start=int, - value_step=integers_in_range(-10000, 10000)) + @given(num=integers(0, 10000), + key_start=integers(), + key_step=integers(-10000, 10000), + value_start=integers(), + value_step=integers(-10000, 10000)) def test_linear_regular_mapping(self, num, key_start, key_step, value_start, value_step): assume(key_step != 0) assume(value_step != 0) @@ -60,20 +60,20 @@ class TestCatalogBuilder(unittest.TestCase): shared_items = set(mapping.items()) & set(catalog.items()) self.assertEqual(len(shared_items), len(mapping)) - @given(dictionary((int, int), int)) + @given(dictionaries(tuples(integers(), integers()), integers())) def test_arbitrary_mapping(self, mapping): builder = CatalogBuilder(mapping) catalog = builder.create() shared_items = set(mapping.items()) & set(catalog.items()) self.assertEqual(len(shared_items), len(mapping)) - @given(i_start=integers_in_range(0, 10), - i_num=integers_in_range(1, 10), + @given(i_start=integers(0, 10), + i_num=integers(1, 10), i_step=just(1), - j_start=integers_in_range(0, 10), - j_num=integers_in_range(1, 10), + j_start=integers(0, 10), + j_num=integers(1, 10), j_step=just(1), - c=integers_in_range(1, 10)) + c=integers(1, 10)) def test_linear_regular_mapping_2d(self, i_start, i_num, i_step, j_start, j_num, j_step, c): assume(i_step != 0) assume(j_step != 0) @@ -88,4 +88,4 @@ class TestCatalogBuilder(unittest.TestCase): builder = CatalogBuilder(mapping) catalog = builder.create() shared_items = set(mapping.items()) & set(catalog.items()) - self.assertEqual(len(shared_items), len(mapping)) \ No newline at end of file + self.assertEqual(len(shared_items), len(mapping)) diff --git a/test/test_extended_textual_header.py b/test/test_extended_textual_header.py index 81adb86..bf458ca 100644 --- a/test/test_extended_textual_header.py +++ b/test/test_extended_textual_header.py @@ -1,7 +1,7 @@ import unittest from hypothesis import given -from hypothesis.specifiers import sampled_from, just +from hypothesis.strategies import sampled_from, just, booleans from segpy.encoding import EBCDIC, ASCII from segpy.toolkit import format_extended_textual_header, CARDS_PER_HEADER, END_TEXT_STANZA, CARD_LENGTH @@ -12,21 +12,21 @@ class TestFormatExtendedTextualHeader(unittest.TestCase): @given(multiline_ascii_encodable_text(0, 100), sampled_from([ASCII, EBCDIC]), - bool) + booleans()) def test_forty_lines_per_page(self, text, encoding, include_text_stop): pages = format_extended_textual_header(text, encoding, include_text_stop) self.assertTrue(all(len(page) == CARDS_PER_HEADER for page in pages)) @given(multiline_ascii_encodable_text(0, 100), sampled_from([ASCII, EBCDIC]), - bool) + booleans()) def test_eighty_bytes_per_encoded_line(self, text, encoding, include_text_stop): pages = format_extended_textual_header(text, encoding, include_text_stop) self.assertTrue(all([len(line.encode(encoding)) == CARD_LENGTH for page in pages for line in page])) @given(multiline_ascii_encodable_text(0, 100), sampled_from([ASCII, EBCDIC]), - bool) + booleans()) def test_lines_end_with_cr_lf(self, text, encoding, include_text_stop): pages = format_extended_textual_header(text, encoding, include_text_stop) self.assertTrue(all([line.endswith('\r\n') for page in pages for line in page])) @@ -40,4 +40,4 @@ class TestFormatExtendedTextualHeader(unittest.TestCase): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/test/test_float.py b/test/test_float.py index 629bca7..51db1bd 100644 --- a/test/test_float.py +++ b/test/test_float.py @@ -3,7 +3,7 @@ from math import trunc import unittest from hypothesis import given, assume -from hypothesis.specifiers import integers_in_range, floats_in_range +from hypothesis.strategies import integers, floats from segpy.ibm_float import (ieee2ibm, ibm2ieee, MAX_IBM_FLOAT, SMALLEST_POSITIVE_NORMAL_IBM_FLOAT, LARGEST_NEGATIVE_NORMAL_IBM_FLOAT, MIN_IBM_FLOAT, IBMFloat, EPSILON_IBM_FLOAT, @@ -178,49 +178,49 @@ class TestIBMFloat(unittest.TestCase): with self.assertRaises(OverflowError): IBMFloat.from_float(MIN_IBM_FLOAT * 10) - @given(floats_in_range(MIN_IBM_FLOAT, MAX_IBM_FLOAT)) + @given(floats(MIN_IBM_FLOAT, MAX_IBM_FLOAT)) def test_bool(self, f): self.assertEqual(bool(IBMFloat.from_float(f)), bool(f)) - @given(integers_in_range(0, 255), - integers_in_range(0, 255), - integers_in_range(0, 255), - integers_in_range(0, 255)) + @given(integers(0, 255), + integers(0, 255), + integers(0, 255), + integers(0, 255)) def test_bytes_roundtrip(self, a, b, c, d): b = bytes((a, b, c, d)) ibm = IBMFloat.from_bytes(b) self.assertEqual(bytes(ibm), b) - @given(floats_in_range(MIN_IBM_FLOAT, MAX_IBM_FLOAT)) + @given(floats(MIN_IBM_FLOAT, MAX_IBM_FLOAT)) def test_floats_roundtrip(self, f): ibm = IBMFloat.from_float(f) self.assertTrue(almost_equal(f, float(ibm), epsilon=EPSILON_IBM_FLOAT)) - @given(integers_in_range(0, MAX_EXACT_INTEGER_IBM_FLOAT - 1), - floats_in_range(0.0, 1.0)) + @given(integers(0, MAX_EXACT_INTEGER_IBM_FLOAT - 1), + floats(0.0, 1.0)) def test_trunc_above_zero(self, i, f): assume(f != 1.0) ieee = i + f ibm = IBMFloat.from_float(ieee) self.assertEqual(trunc(ibm), i) - @given(integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT + 1, 0), - floats_in_range(0.0, 1.0)) + @given(integers(MIN_EXACT_INTEGER_IBM_FLOAT + 1, 0), + floats(0.0, 1.0)) def test_trunc_below_zero(self, i, f): assume(f != 1.0) ieee = i - f ibm = IBMFloat.from_float(ieee) self.assertEqual(trunc(ibm), i) - @given(integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT, MAX_EXACT_INTEGER_IBM_FLOAT - 1), - floats_in_range(EPSILON_IBM_FLOAT, 1 - EPSILON_IBM_FLOAT)) + @given(integers(MIN_EXACT_INTEGER_IBM_FLOAT, MAX_EXACT_INTEGER_IBM_FLOAT - 1), + floats(EPSILON_IBM_FLOAT, 1 - EPSILON_IBM_FLOAT)) def test_ceil(self, i, f): ieee = i + f ibm = IBMFloat.from_float(ieee) self.assertEqual(math.ceil(ibm), i + 1) - @given(integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT, MAX_EXACT_INTEGER_IBM_FLOAT - 1), - floats_in_range(EPSILON_IBM_FLOAT, 1 - EPSILON_IBM_FLOAT)) + @given(integers(MIN_EXACT_INTEGER_IBM_FLOAT, MAX_EXACT_INTEGER_IBM_FLOAT - 1), + floats(EPSILON_IBM_FLOAT, 1 - EPSILON_IBM_FLOAT)) def test_floor(self, i, f): ieee = i + f ibm = IBMFloat.from_float(ieee) @@ -245,10 +245,10 @@ class TestIBMFloat(unittest.TestCase): normalized = ibm.normalize() self.assertFalse(normalized.is_subnormal()) - @given(integers_in_range(128, 255), - integers_in_range(0, 255), - integers_in_range(0, 255), - integers_in_range(4, 23)) + @given(integers(128, 255), + integers(0, 255), + integers(0, 255), + integers(4, 23)) def test_normalise_subnormal(self, b, c, d, shift): mantissa = (b << 16) | (c << 8) | d assume(mantissa != 0) @@ -265,10 +265,10 @@ class TestIBMFloat(unittest.TestCase): normalized = ibm.normalize() self.assertFalse(normalized.is_subnormal()) - @given(integers_in_range(128, 255), - integers_in_range(0, 255), - integers_in_range(0, 255), - integers_in_range(4, 23)) + @given(integers(128, 255), + integers(0, 255), + integers(0, 255), + integers(4, 23)) def test_zero_subnormal(self, b, c, d, shift): mantissa = (b << 16) | (c << 8) | d assume(mantissa != 0) @@ -285,19 +285,19 @@ class TestIBMFloat(unittest.TestCase): z = ibm.zero_subnormal() self.assertTrue(z.is_zero()) - @given(integers_in_range(0, 255), - integers_in_range(0, 255), - integers_in_range(0, 255), - integers_in_range(0, 255)) + @given(integers(0, 255), + integers(0, 255), + integers(0, 255), + integers(0, 255)) def test_abs(self, a, b, c, d): ibm = IBMFloat.from_bytes((a, b, c, d)) abs_ibm = abs(ibm) self.assertGreaterEqual(abs_ibm.signbit, 0) - @given(integers_in_range(0, 255), - integers_in_range(0, 255), - integers_in_range(0, 255), - integers_in_range(0, 255)) + @given(integers(0, 255), + integers(0, 255), + integers(0, 255), + integers(0, 255)) def test_negate_non_zero(self, a, b, c, d): ibm = IBMFloat.from_bytes((a, b, c, d)) assume(not ibm.is_zero()) @@ -309,14 +309,14 @@ class TestIBMFloat(unittest.TestCase): negated = -zero self.assertTrue(negated.is_zero()) - @given(floats_in_range(MIN_IBM_FLOAT, MAX_IBM_FLOAT)) + @given(floats(MIN_IBM_FLOAT, MAX_IBM_FLOAT)) def test_signbit(self, f): ltz = f < 0 ibm = IBMFloat.from_float(f) self.assertEqual(ltz, ibm.signbit) - @given(floats_in_range(-1.0, +1.0), - integers_in_range(-256, 255)) + @given(floats(-1.0, +1.0), + integers(-256, 255)) def test_ldexp_frexp(self, fraction, exponent): try: ibm = IBMFloat.ldexp(fraction, exponent) @@ -326,8 +326,8 @@ class TestIBMFloat(unittest.TestCase): f, e = ibm.frexp() self.assertTrue(almost_equal(fraction * 2**exponent, f * 2**e, epsilon=EPSILON_IBM_FLOAT)) - @given(floats_in_range(MIN_IBM_FLOAT, MAX_IBM_FLOAT), - floats_in_range(0.0, 1.0)) + @given(floats(MIN_IBM_FLOAT, MAX_IBM_FLOAT), + floats(0.0, 1.0)) def test_add(self, f, p): a = f * p b = f - a @@ -342,8 +342,8 @@ class TestIBMFloat(unittest.TestCase): self.assertTrue(almost_equal(ieee_c, ibm_c, epsilon=EPSILON_IBM_FLOAT * 4)) - @given(floats_in_range(0, MAX_IBM_FLOAT), - floats_in_range(0, MAX_IBM_FLOAT)) + @given(floats(0, MAX_IBM_FLOAT), + floats(0, MAX_IBM_FLOAT)) def test_sub(self, a, b): ibm_a = IBMFloat.from_float(a) ibm_b = IBMFloat.from_float(b) diff --git a/test/test_roundtrip.py b/test/test_roundtrip.py index 86cdd05..70716e1 100644 --- a/test/test_roundtrip.py +++ b/test/test_roundtrip.py @@ -2,7 +2,7 @@ from itertools import zip_longest import unittest from hypothesis import given from io import BytesIO -from hypothesis.specifiers import sampled_from +from hypothesis.strategies import sampled_from from segpy.binary_reel_header import BinaryReelHeader from segpy.encoding import ASCII, EBCDIC from segpy.header import are_equal @@ -70,4 +70,4 @@ class TestTextualReelHeader(unittest.TestCase): with BytesIO(written_stream) as read_stream: read_header_lines = read_textual_reel_header(read_stream, encoding) - self.assertTrue(all(len(line) == CARD_LENGTH for line in read_header_lines)) \ No newline at end of file + self.assertTrue(all(len(line) == CARD_LENGTH for line in read_header_lines)) diff --git a/test/test_util.py b/test/test_util.py index ad2e397..b0d6807 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -1,39 +1,39 @@ import unittest from hypothesis import given, assume, example -from hypothesis.specifiers import integers_in_range +from hypothesis.strategies import integers, lists from segpy.util import batched, complementary_intervals, flatten, intervals_are_contiguous, roundrobin from test.strategies import spaced_ranges class TestBatched(unittest.TestCase): - @given([int], - integers_in_range(1, 1000)) + @given(lists(integers()), + integers(1, 1000)) def test_batch_sizes_unpadded(self, items, batch_size): assume(batch_size > 0) batches = list(batched(items, batch_size)) self.assertTrue(all(len(batch) == batch_size for batch in batches[:-1])) - @given([int], - integers_in_range(1, 1000)) + @given(lists(integers()), + integers(1, 1000)) def test_final_batch_sizes(self, items, batch_size): assume(len(items) > 0) assume(batch_size > 0) batches = list(batched(items, batch_size)) self.assertTrue(len(batches[-1]) <= batch_size) - @given([int], - integers_in_range(1, 1000), - int) + @given(lists(integers()), + integers(1, 1000), + integers()) def test_batch_sizes_padded(self, items, batch_size, pad): assume(batch_size > 0) batches = list(batched(items, batch_size, padding=pad)) self.assertTrue(all(len(batch) == batch_size for batch in batches)) - @given([int], - integers_in_range(1, 1000), - int) + @given(lists(integers()), + integers(1, 1000), + integers()) def test_pad_contents(self, items, batch_size, pad): assume(len(items) > 0) assume(0 < batch_size < 1000) @@ -59,7 +59,7 @@ class TestComplementaryIntervals(unittest.TestCase): @given(spaced_ranges(min_num_ranges=1, max_num_ranges=10, min_interval=0, max_interval=10), - integers_in_range(0, 10)) + integers(0, 10)) def test_contiguous_with_offset_start(self, intervals, start_offset): first_interval_start = intervals[0].start start_index = first_interval_start - start_offset @@ -68,7 +68,7 @@ class TestComplementaryIntervals(unittest.TestCase): @given(spaced_ranges(min_num_ranges=1, max_num_ranges=10, min_interval=0, max_interval=10), - integers_in_range(0, 10)) + integers(0, 10)) @example(intervals=[range(0, 0)], end_offset=1) def test_contiguous_with_offset_end(self, intervals, end_offset): last_interval_end = intervals[-1].stop @@ -77,4 +77,4 @@ class TestComplementaryIntervals(unittest.TestCase): self.assertEqual(complements[-1], range(last_interval_end, end_index)) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()