2
from test import test_support
4
# Simple test to ensure that optimizations in fileobject.c deliver
5
# the expected results. For best testing, run this under a debug-build
6
# Python too (to exercise asserts in the C code).
8
lengths = range(1, 257) + [512, 1000, 1024, 2048, 4096, 8192, 10000,
9
16384, 32768, 65536, 1000000]
11
class BufferSizeTest(unittest.TestCase):
13
# Write s + "\n" + s to file, then open it and ensure that successive
14
# .readline()s deliver what we wrote.
16
# Ensure we can open TESTFN for writing.
17
test_support.unlink(test_support.TESTFN)
19
# Since C doesn't guarantee we can write/read arbitrary bytes in text
20
# files, use binary mode.
21
f = open(test_support.TESTFN, "wb")
23
# write once with \n and once without
28
f = open(test_support.TESTFN, "rb")
30
self.assertEqual(line, s + "\n")
32
self.assertEqual(line, s)
34
self.assert_(not line) # Must be at EOF
37
test_support.unlink(test_support.TESTFN)
39
def drive_one(self, pattern):
40
for length in lengths:
41
# Repeat string 'pattern' as often as needed to reach total length
42
# 'length'. Then call try_one with that string, a string one larger
43
# than that, and a string one smaller than that. Try this with all
44
# small sizes and various powers of 2, so we exercise all likely
45
# stdio buffer sizes, and "off by one" errors on both sides.
46
q, r = divmod(length, len(pattern))
47
teststring = pattern * q + pattern[:r]
48
self.assertEqual(len(teststring), length)
49
self.try_one(teststring)
50
self.try_one(teststring + "x")
51
self.try_one(teststring[:-1])
53
def test_primepat(self):
54
# A pattern with prime length, to avoid simple relationships with
56
self.drive_one("1234567890\00\01\02\03\04\05\06")
58
def test_nullpat(self):
59
self.drive_one("\0" * 1000)
62
test_support.run_unittest(BufferSizeTest)
64
if __name__ == "__main__":