blob: 84d772ef07acd137640df35b0a5596c78a708959 [file] [log] [blame]
Thomas Wouters89f507f2006-12-13 04:49:30 +00001import unittest
2from test import test_support
Tim Peters86821b22001-01-07 21:19:34 +00003
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).
7
Guido van Rossum805365e2007-05-07 22:24:25 +00008lengths = list(range(1, 257)) + [512, 1000, 1024, 2048, 4096, 8192, 10000,
9 16384, 32768, 65536, 1000000]
Tim Peters86821b22001-01-07 21:19:34 +000010
Thomas Wouters89f507f2006-12-13 04:49:30 +000011class BufferSizeTest(unittest.TestCase):
12 def try_one(self, s):
13 # Write s + "\n" + s to file, then open it and ensure that successive
14 # .readline()s deliver what we wrote.
Tim Peters86821b22001-01-07 21:19:34 +000015
Thomas Wouters89f507f2006-12-13 04:49:30 +000016 # Since C doesn't guarantee we can write/read arbitrary bytes in text
17 # files, use binary mode.
18 f = open(test_support.TESTFN, "wb")
19 try:
20 # write once with \n and once without
21 f.write(s)
Guido van Rossum305b9242007-05-24 16:11:18 +000022 f.write(b"\n")
Thomas Wouters89f507f2006-12-13 04:49:30 +000023 f.write(s)
24 f.close()
25 f = open(test_support.TESTFN, "rb")
26 line = f.readline()
Guido van Rossum305b9242007-05-24 16:11:18 +000027 self.assertEqual(line, s + b"\n")
Thomas Wouters89f507f2006-12-13 04:49:30 +000028 line = f.readline()
29 self.assertEqual(line, s)
30 line = f.readline()
31 self.assert_(not line) # Must be at EOF
32 f.close()
33 finally:
34 try:
35 import os
36 os.unlink(test_support.TESTFN)
37 except:
38 pass
Tim Peters86821b22001-01-07 21:19:34 +000039
Thomas Wouters89f507f2006-12-13 04:49:30 +000040 def drive_one(self, pattern):
41 for length in lengths:
42 # Repeat string 'pattern' as often as needed to reach total length
43 # 'length'. Then call try_one with that string, a string one larger
44 # than that, and a string one smaller than that. Try this with all
45 # small sizes and various powers of 2, so we exercise all likely
46 # stdio buffer sizes, and "off by one" errors on both sides.
47 q, r = divmod(length, len(pattern))
48 teststring = pattern * q + pattern[:r]
49 self.assertEqual(len(teststring), length)
50 self.try_one(teststring)
Guido van Rossum305b9242007-05-24 16:11:18 +000051 self.try_one(teststring + b"x")
Thomas Wouters89f507f2006-12-13 04:49:30 +000052 self.try_one(teststring[:-1])
Tim Peters86821b22001-01-07 21:19:34 +000053
Thomas Wouters89f507f2006-12-13 04:49:30 +000054 def test_primepat(self):
55 # A pattern with prime length, to avoid simple relationships with
56 # stdio buffer sizes.
Guido van Rossum305b9242007-05-24 16:11:18 +000057 self.drive_one(b"1234567890\00\01\02\03\04\05\06")
Thomas Wouters89f507f2006-12-13 04:49:30 +000058
59 def test_nullpat(self):
Guido van Rossum305b9242007-05-24 16:11:18 +000060 self.drive_one(bytes(1000))
Thomas Wouters89f507f2006-12-13 04:49:30 +000061
62def test_main():
63 test_support.run_unittest(BufferSizeTest)
64
65if __name__ == "__main__":
66 test_main()