Code reorganisation - segpy is now a proper Python package. Rework of the extended textual header for symmetry with the reader. Some additional tests.

This commit is contained in:
Robert Smallshire
2015-01-28 21:42:52 +01:00
parent 246b8515e4
commit 255a490914
19 changed files with 669 additions and 292 deletions
+64
View File
@@ -0,0 +1,64 @@
from collections import namedtuple, Counter
import random
import unittest
from hypothesis import given
from hypothesis.descriptors import one_of, SampledFrom, Just, sampled_from, just
from hypothesis.searchstrategy import MappedSearchStrategy, StringStrategy
from hypothesis.strategytable import StrategyTable
from segpy.encoding import EBCDIC, ASCII
from segpy.toolkit import format_extended_textual_header, CARDS_PER_HEADER, END_TEXT_STANZA, CARD_LENGTH
# class MultiLineString(str):
# pass
#
#
# class MultiLineStringStrategy(MappedSearchStrategy):
#
# def pack(self, x):
# return '\n'.join(x)
#
# def unpack(self, x):
# return ''.join(x.splitlines())
#
#
# StrategyTable.default().define_specification_for(
# MultiLineString,
# lambda s, d: MultiLineStringStrategy(
# strategy=s.strategy([str]),
# descriptor=MultiLineString))
class TestFormatExtendedTextualHeader(unittest.TestCase):
@given(str,
sampled_from([ASCII, EBCDIC]),
bool)
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(str,
sampled_from([ASCII, EBCDIC]),
bool)
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(str,
sampled_from([ASCII, EBCDIC]),
bool)
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]))
@given(str,
sampled_from([ASCII, EBCDIC]),
just(True))
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(pages[-1][0].startswith(END_TEXT_STANZA))
if __name__ == '__main__':
unittest.main()
+2 -2
View File
@@ -1,5 +1,6 @@
import unittest
from ibm_float import ieee2ibm, ibm2ieee
from segpy.ibm_float import ieee2ibm, ibm2ieee
class Ibm2Ieee(unittest.TestCase):
@@ -21,7 +22,6 @@ class Ibm2Ieee(unittest.TestCase):
self.assertEqual(ibm2ieee((0b11000010, 0b01110110, 0b10100000, 0b00000000)), -118.625)
class Ieee2Ibm(unittest.TestCase):
def test_zero(self):
+43
View File
@@ -0,0 +1,43 @@
import unittest
from hypothesis import given, assume
from segpy.util import batched
class TestBatched(unittest.TestCase):
@given([int], int)
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], int)
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], int, int)
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], int, int)
def test_pad_contents(self, items, batch_size, pad):
assume(len(items) > 0)
assume(0 < batch_size < 1000)
num_left_over = len(items) % batch_size
pad_length = batch_size - num_left_over if num_left_over != 0 else 0
assume(pad_length != 0)
batches = list(batched(items, batch_size, padding=pad))
self.assertEqual(batches[-1][batch_size - pad_length:], [pad] * pad_length)
def test_pad(self):
batches = list(batched([0, 0], 3, 42))
self.assertEqual(batches[-1], [0, 0, 42])
if __name__ == '__main__':
unittest.main()