def test_count_lines(): # Create a set of 'i' lines and read 'count' lines at a time. # Either count divides i or it doesn't. If it does, the reader # should go to completion. If it does not, the reader should # have a remainder whose size can be verified. print "Testing CountLines" for ending in ("\n", "\r", "\r\n"): print " ... exhaustive testing against %r" % ending s = "" for i in range(25): for count in range(1, i + 1): for look in (0, 2, 5): rep, final = divmod(i, count) reader = RecordReader.CountLines(StringIO(s[look:]), count, lookahead=s[:look], sizehint=1) all = "" while rep > 0: rec = reader.next() lines = string.split(rec, ending) assert len(lines)-1 == count, \ "Expecting %d lines, got %d in %r using %r" % \ (count, len(lines)-1, rec, ending) all = all + rec rep = rep - 1 if final == 0: # Reader should be at the end of input rec = reader.next() assert rec is None, \ "Should be at end of reader, got %r" % rec rec = reader.next() assert rec is None, \ "data after end of reader, got %r" % rec else: # There should be a remainder of size i % count lines infile, remainder = reader.remainder() text = remainder + infile.read() all = all + text lines = string.split(text, ending) assert len(lines)-1 == final, \ "Expecting %d final lines, got %d" % \ (final, len(lines)-1) try: rec = reader.next() raise AssertionError, \ "Got unexpected final record, %r" % rec except RecordReader.ReaderError: pass assert all == s, \ "record data %r doesn't rebuild input %r" % \ (all, s) s = s + str(i) + ending
def test_count_lines_lines(): lookahead = "1\n2\n3\n4\n5\n6\n7\n8\n" reader = RecordReader.CountLines(StringIO(""), 2, lookahead = lookahead) assert test_count(reader) == 4
def test_count_lines(): s = "1\n2\n3\n4\n5\n6\n7\n8\n" reader = RecordReader.CountLines(StringIO(s), 2) assert test_count(reader) == 4