ยปCore Development>Code coverage>Lib/test/test_bufio.py

Python code coverage for Lib/test/test_bufio.py

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