Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 3 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 4 | import io # C implementation. |
| 5 | import _pyio as pyio # Python implementation. |
| 6 | |
| 7 | # Simple test to ensure that optimizations in the IO library deliver the |
| 8 | # expected results. For best testing, run this under a debug-build Python too |
| 9 | # (to exercise asserts in the C code). |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 11 | lengths = list(range(1, 257)) + [512, 1000, 1024, 2048, 4096, 8192, 10000, |
| 12 | 16384, 32768, 65536, 1000000] |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 13 | |
Ezio Melotti | b21af55 | 2013-01-10 06:11:34 +0200 | [diff] [blame] | 14 | class BufferSizeTest: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 15 | def try_one(self, s): |
| 16 | # Write s + "\n" + s to file, then open it and ensure that successive |
| 17 | # .readline()s deliver what we wrote. |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 19 | # Ensure we can open TESTFN for writing. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 20 | support.unlink(support.TESTFN) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 21 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 22 | # Since C doesn't guarantee we can write/read arbitrary bytes in text |
| 23 | # files, use binary mode. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 24 | f = self.open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 25 | try: |
| 26 | # write once with \n and once without |
| 27 | f.write(s) |
Guido van Rossum | 305b924 | 2007-05-24 16:11:18 +0000 | [diff] [blame] | 28 | f.write(b"\n") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 29 | f.write(s) |
| 30 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 31 | f = open(support.TESTFN, "rb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 32 | line = f.readline() |
Guido van Rossum | 305b924 | 2007-05-24 16:11:18 +0000 | [diff] [blame] | 33 | self.assertEqual(line, s + b"\n") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 34 | line = f.readline() |
| 35 | self.assertEqual(line, s) |
| 36 | line = f.readline() |
Serhiy Storchaka | 0dcd80a | 2015-08-02 15:17:49 +0300 | [diff] [blame] | 37 | self.assertFalse(line) # Must be at EOF |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 38 | f.close() |
| 39 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 40 | support.unlink(support.TESTFN) |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 42 | def drive_one(self, pattern): |
| 43 | for length in lengths: |
| 44 | # Repeat string 'pattern' as often as needed to reach total length |
| 45 | # 'length'. Then call try_one with that string, a string one larger |
| 46 | # than that, and a string one smaller than that. Try this with all |
| 47 | # small sizes and various powers of 2, so we exercise all likely |
| 48 | # stdio buffer sizes, and "off by one" errors on both sides. |
| 49 | q, r = divmod(length, len(pattern)) |
| 50 | teststring = pattern * q + pattern[:r] |
| 51 | self.assertEqual(len(teststring), length) |
| 52 | self.try_one(teststring) |
Guido van Rossum | 305b924 | 2007-05-24 16:11:18 +0000 | [diff] [blame] | 53 | self.try_one(teststring + b"x") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 54 | self.try_one(teststring[:-1]) |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 55 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 56 | def test_primepat(self): |
| 57 | # A pattern with prime length, to avoid simple relationships with |
| 58 | # stdio buffer sizes. |
Guido van Rossum | 305b924 | 2007-05-24 16:11:18 +0000 | [diff] [blame] | 59 | self.drive_one(b"1234567890\00\01\02\03\04\05\06") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 60 | |
| 61 | def test_nullpat(self): |
Guido van Rossum | 305b924 | 2007-05-24 16:11:18 +0000 | [diff] [blame] | 62 | self.drive_one(bytes(1000)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 63 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 64 | |
Ezio Melotti | b21af55 | 2013-01-10 06:11:34 +0200 | [diff] [blame] | 65 | class CBufferSizeTest(BufferSizeTest, unittest.TestCase): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 66 | open = io.open |
| 67 | |
Ezio Melotti | b21af55 | 2013-01-10 06:11:34 +0200 | [diff] [blame] | 68 | class PyBufferSizeTest(BufferSizeTest, unittest.TestCase): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 69 | open = staticmethod(pyio.open) |
| 70 | |
| 71 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 72 | if __name__ == "__main__": |
Ezio Melotti | b21af55 | 2013-01-10 06:11:34 +0200 | [diff] [blame] | 73 | unittest.main() |