blob: 17151b13615e944ee97fce32c177fdc0f9fa6a04 [file] [log] [blame]
Thomas Wouters89f507f2006-12-13 04:49:30 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Hai Shi883bc632020-07-06 17:12:49 +08003from test.support import os_helper
Tim Peters86821b22001-01-07 21:19:34 +00004
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00005import io # C implementation.
6import _pyio as pyio # Python implementation.
7
8# Simple test to ensure that optimizations in the IO library deliver the
9# expected results. For best testing, run this under a debug-build Python too
10# (to exercise asserts in the C code).
Tim Peters86821b22001-01-07 21:19:34 +000011
Guido van Rossum805365e2007-05-07 22:24:25 +000012lengths = list(range(1, 257)) + [512, 1000, 1024, 2048, 4096, 8192, 10000,
13 16384, 32768, 65536, 1000000]
Tim Peters86821b22001-01-07 21:19:34 +000014
Ezio Melottib21af552013-01-10 06:11:34 +020015class BufferSizeTest:
Thomas Wouters89f507f2006-12-13 04:49:30 +000016 def try_one(self, s):
17 # Write s + "\n" + s to file, then open it and ensure that successive
18 # .readline()s deliver what we wrote.
Tim Peters86821b22001-01-07 21:19:34 +000019
Guido van Rossum8ce8a782007-11-01 19:42:39 +000020 # Ensure we can open TESTFN for writing.
Hai Shi883bc632020-07-06 17:12:49 +080021 os_helper.unlink(os_helper.TESTFN)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000022
Thomas Wouters89f507f2006-12-13 04:49:30 +000023 # Since C doesn't guarantee we can write/read arbitrary bytes in text
24 # files, use binary mode.
Hai Shi883bc632020-07-06 17:12:49 +080025 f = self.open(os_helper.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +000026 try:
27 # write once with \n and once without
28 f.write(s)
Guido van Rossum305b9242007-05-24 16:11:18 +000029 f.write(b"\n")
Thomas Wouters89f507f2006-12-13 04:49:30 +000030 f.write(s)
31 f.close()
Hai Shi883bc632020-07-06 17:12:49 +080032 f = open(os_helper.TESTFN, "rb")
Thomas Wouters89f507f2006-12-13 04:49:30 +000033 line = f.readline()
Guido van Rossum305b9242007-05-24 16:11:18 +000034 self.assertEqual(line, s + b"\n")
Thomas Wouters89f507f2006-12-13 04:49:30 +000035 line = f.readline()
36 self.assertEqual(line, s)
37 line = f.readline()
Serhiy Storchaka0dcd80a2015-08-02 15:17:49 +030038 self.assertFalse(line) # Must be at EOF
Thomas Wouters89f507f2006-12-13 04:49:30 +000039 f.close()
40 finally:
Hai Shi883bc632020-07-06 17:12:49 +080041 os_helper.unlink(os_helper.TESTFN)
Tim Peters86821b22001-01-07 21:19:34 +000042
Thomas Wouters89f507f2006-12-13 04:49:30 +000043 def drive_one(self, pattern):
44 for length in lengths:
45 # Repeat string 'pattern' as often as needed to reach total length
46 # 'length'. Then call try_one with that string, a string one larger
47 # than that, and a string one smaller than that. Try this with all
48 # small sizes and various powers of 2, so we exercise all likely
49 # stdio buffer sizes, and "off by one" errors on both sides.
50 q, r = divmod(length, len(pattern))
51 teststring = pattern * q + pattern[:r]
52 self.assertEqual(len(teststring), length)
53 self.try_one(teststring)
Guido van Rossum305b9242007-05-24 16:11:18 +000054 self.try_one(teststring + b"x")
Thomas Wouters89f507f2006-12-13 04:49:30 +000055 self.try_one(teststring[:-1])
Tim Peters86821b22001-01-07 21:19:34 +000056
Thomas Wouters89f507f2006-12-13 04:49:30 +000057 def test_primepat(self):
58 # A pattern with prime length, to avoid simple relationships with
59 # stdio buffer sizes.
Guido van Rossum305b9242007-05-24 16:11:18 +000060 self.drive_one(b"1234567890\00\01\02\03\04\05\06")
Thomas Wouters89f507f2006-12-13 04:49:30 +000061
62 def test_nullpat(self):
Serhiy Storchaka5f1a5182016-09-11 14:41:02 +030063 self.drive_one(b'\0' * 1000)
Thomas Wouters89f507f2006-12-13 04:49:30 +000064
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000065
Ezio Melottib21af552013-01-10 06:11:34 +020066class CBufferSizeTest(BufferSizeTest, unittest.TestCase):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000067 open = io.open
68
Ezio Melottib21af552013-01-10 06:11:34 +020069class PyBufferSizeTest(BufferSizeTest, unittest.TestCase):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000070 open = staticmethod(pyio.open)
71
72
Thomas Wouters89f507f2006-12-13 04:49:30 +000073if __name__ == "__main__":
Ezio Melottib21af552013-01-10 06:11:34 +020074 unittest.main()