blob: 804a401a25077bd3a94f37e8a98416bcf3cdd539 [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
Guido van Rossum8ce8a782007-11-01 19:42:39 +000016 # Ensure we can open TESTFN for writing.
17 test_support.unlink(test_support.TESTFN)
18
Thomas Wouters89f507f2006-12-13 04:49:30 +000019 # 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")
22 try:
23 # write once with \n and once without
24 f.write(s)
Guido van Rossum305b9242007-05-24 16:11:18 +000025 f.write(b"\n")
Thomas Wouters89f507f2006-12-13 04:49:30 +000026 f.write(s)
27 f.close()
28 f = open(test_support.TESTFN, "rb")
29 line = f.readline()
Guido van Rossum305b9242007-05-24 16:11:18 +000030 self.assertEqual(line, s + b"\n")
Thomas Wouters89f507f2006-12-13 04:49:30 +000031 line = f.readline()
32 self.assertEqual(line, s)
33 line = f.readline()
34 self.assert_(not line) # Must be at EOF
35 f.close()
36 finally:
Guido van Rossum8ce8a782007-11-01 19:42:39 +000037 test_support.unlink(test_support.TESTFN)
Tim Peters86821b22001-01-07 21:19:34 +000038
Thomas Wouters89f507f2006-12-13 04:49:30 +000039 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)
Guido van Rossum305b9242007-05-24 16:11:18 +000050 self.try_one(teststring + b"x")
Thomas Wouters89f507f2006-12-13 04:49:30 +000051 self.try_one(teststring[:-1])
Tim Peters86821b22001-01-07 21:19:34 +000052
Thomas Wouters89f507f2006-12-13 04:49:30 +000053 def test_primepat(self):
54 # A pattern with prime length, to avoid simple relationships with
55 # stdio buffer sizes.
Guido van Rossum305b9242007-05-24 16:11:18 +000056 self.drive_one(b"1234567890\00\01\02\03\04\05\06")
Thomas Wouters89f507f2006-12-13 04:49:30 +000057
58 def test_nullpat(self):
Guido van Rossum305b9242007-05-24 16:11:18 +000059 self.drive_one(bytes(1000))
Thomas Wouters89f507f2006-12-13 04:49:30 +000060
61def test_main():
62 test_support.run_unittest(BufferSizeTest)
63
64if __name__ == "__main__":
65 test_main()