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