Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 3 | |
| 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 Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 8 | lengths = list(range(1, 257)) + [512, 1000, 1024, 2048, 4096, 8192, 10000, |
| 9 | 16384, 32768, 65536, 1000000] |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 11 | class 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 Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 16 | # 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) |
| 22 | f.write("\n") |
| 23 | f.write(s) |
| 24 | f.close() |
| 25 | f = open(test_support.TESTFN, "rb") |
| 26 | line = f.readline() |
| 27 | self.assertEqual(line, s + "\n") |
| 28 | 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 Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 39 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 40 | 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) |
| 51 | self.try_one(teststring + "x") |
| 52 | self.try_one(teststring[:-1]) |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 53 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 54 | def test_primepat(self): |
| 55 | # A pattern with prime length, to avoid simple relationships with |
| 56 | # stdio buffer sizes. |
| 57 | self.drive_one("1234567890\00\01\02\03\04\05\06") |
| 58 | |
| 59 | def test_nullpat(self): |
| 60 | self.drive_one("\0" * 1000) |
| 61 | |
| 62 | def test_main(): |
| 63 | test_support.run_unittest(BufferSizeTest) |
| 64 | |
| 65 | if __name__ == "__main__": |
| 66 | test_main() |