mirror of
https://github.com/wassname/segpy.git
synced 2026-08-11 11:25:46 +08:00
Use the HeaderPacker from toolkit to read and write the trace header.
This commit is contained in:
+21
-1
@@ -1,5 +1,7 @@
|
||||
from itertools import accumulate, starmap
|
||||
from hypothesis import strategy
|
||||
from hypothesis.specifiers import integers_in_range
|
||||
from segpy.util import batched
|
||||
|
||||
PRINTABLE_ASCII_RANGE = (32, 127)
|
||||
|
||||
@@ -16,6 +18,24 @@ 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)) \
|
||||
return strategy(integers_in_range(min_num_lines, max_num_lines)) \
|
||||
.flatmap(lambda n: ([integers_in_range(*PRINTABLE_ASCII_RANGE)],) * n) \
|
||||
.map(lambda xs: '\n'.join(bytes(x).decode('ascii') for x in xs))
|
||||
|
||||
|
||||
def spaced_ranges(min_num_ranges, max_num_ranges, min_interval, max_interval):
|
||||
"""A Hypothesis strategy to produce separated, non-overlapping ranges.
|
||||
|
||||
Args:
|
||||
min_num_ranges: The minimum number of ranges to produce. TODO: Correct?
|
||||
max_num_ranges: The maximum number of ranges to produce.
|
||||
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)) \
|
||||
.map(lambda n: 2*n) \
|
||||
.flatmap(lambda n: (integers_in_range(min_interval, max_interval),) * n) \
|
||||
.map(list).map(lambda lst: list(accumulate(lst))) \
|
||||
.map(lambda lst: list(batched(lst, 2))) \
|
||||
.map(lambda pairs: list(starmap(range, pairs)))
|
||||
|
||||
|
||||
+32
-2
@@ -1,8 +1,9 @@
|
||||
import unittest
|
||||
|
||||
from hypothesis import given, assume
|
||||
from hypothesis import given, assume, example
|
||||
from hypothesis.specifiers import integers_in_range
|
||||
from segpy.util import batched
|
||||
from segpy.util import batched, complementary_intervals, flatten, intervals_are_contiguous, roundrobin
|
||||
from test.strategies import spaced_ranges
|
||||
|
||||
|
||||
class TestBatched(unittest.TestCase):
|
||||
@@ -46,5 +47,34 @@ class TestBatched(unittest.TestCase):
|
||||
batches = list(batched([0, 0], 3, 42))
|
||||
self.assertEqual(batches[-1], [0, 0, 42])
|
||||
|
||||
|
||||
class TestComplementaryIntervals(unittest.TestCase):
|
||||
|
||||
@given(spaced_ranges(min_num_ranges=1, max_num_ranges=10,
|
||||
min_interval=0, max_interval=10))
|
||||
def test_contiguous(self, intervals):
|
||||
complements = complementary_intervals(intervals)
|
||||
interleaved = list(roundrobin(complements, intervals))
|
||||
self.assertTrue(intervals_are_contiguous(interleaved))
|
||||
|
||||
@given(spaced_ranges(min_num_ranges=1, max_num_ranges=10,
|
||||
min_interval=0, max_interval=10),
|
||||
integers_in_range(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
|
||||
complements = list(complementary_intervals(intervals, start=start_index))
|
||||
self.assertEqual(complements[0], range(start_index, first_interval_start))
|
||||
|
||||
@given(spaced_ranges(min_num_ranges=1, max_num_ranges=10,
|
||||
min_interval=0, max_interval=10),
|
||||
integers_in_range(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
|
||||
end_index = last_interval_end + end_offset
|
||||
complements = list(complementary_intervals(intervals, stop=end_index))
|
||||
self.assertEqual(complements[-1], range(last_interval_end, end_index))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user