Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_bufio.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 6768b2350c57 |
| children |
| rev | line source |
|---|---|
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
1 import unittest |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
2 from test import test_support as support |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
3 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
4 import io # C implementation. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
5 import _pyio as pyio # Python implementation. |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
6 |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
7 # Simple test to ensure that optimizations in the IO library deliver the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
8 # expected results. For best testing, run this under a debug-build Python too |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
9 # (to exercise asserts in the C code). |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
10 |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
11 lengths = list(range(1, 257)) + [512, 1000, 1024, 2048, 4096, 8192, 10000, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
12 16384, 32768, 65536, 1000000] |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
13 |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
14 class BufferSizeTest(unittest.TestCase): |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
15 def try_one(self, s): |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
16 # Write s + "\n" + s to file, then open it and ensure that successive |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
17 # .readline()s deliver what we wrote. |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
18 |
|
43484
3cd1463c938d
Ensure that this test will pass even if another test left an unwritable TESTFN.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40284
diff
changeset
|
19 # Ensure we can open TESTFN for writing. |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
20 support.unlink(support.TESTFN) |
|
43484
3cd1463c938d
Ensure that this test will pass even if another test left an unwritable TESTFN.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40284
diff
changeset
|
21 |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
22 # Since C doesn't guarantee we can write/read arbitrary bytes in text |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
23 # files, use binary mode. |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
24 f = self.open(support.TESTFN, "wb") |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
25 try: |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
26 # write once with \n and once without |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
27 f.write(s) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
28 f.write(b"\n") |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
29 f.write(s) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
30 f.close() |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
31 f = open(support.TESTFN, "rb") |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
32 line = f.readline() |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
33 self.assertEqual(line, s + b"\n") |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
34 line = f.readline() |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
35 self.assertEqual(line, s) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
36 line = f.readline() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54720
diff
changeset
|
37 self.assertTrue(not line) # Must be at EOF |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
38 f.close() |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
39 finally: |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
40 support.unlink(support.TESTFN) |
|
40284
ff678669aa6e
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
40216
diff
changeset
|
41 |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
42 def drive_one(self, pattern): |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
43 for length in lengths: |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
44 # Repeat string 'pattern' as often as needed to reach total length |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
45 # 'length'. Then call try_one with that string, a string one larger |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
46 # than that, and a string one smaller than that. Try this with all |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
47 # small sizes and various powers of 2, so we exercise all likely |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
48 # stdio buffer sizes, and "off by one" errors on both sides. |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
49 q, r = divmod(length, len(pattern)) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
50 teststring = pattern * q + pattern[:r] |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
51 self.assertEqual(len(teststring), length) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
52 self.try_one(teststring) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
53 self.try_one(teststring + b"x") |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
54 self.try_one(teststring[:-1]) |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
55 |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
56 def test_primepat(self): |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
57 # A pattern with prime length, to avoid simple relationships with |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
58 # stdio buffer sizes. |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
59 self.drive_one(b"1234567890\00\01\02\03\04\05\06") |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
60 |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
61 def test_nullpat(self): |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
62 self.drive_one(bytes(1000)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
63 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
64 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
65 class CBufferSizeTest(BufferSizeTest): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
66 open = io.open |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
67 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
68 class PyBufferSizeTest(BufferSizeTest): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
69 open = staticmethod(pyio.open) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
43484
diff
changeset
|
70 |
|
54720
24c8d1aabade
Re-enable testing of builtin open() in test_bufio in test_largefile
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
71 class BuiltinBufferSizeTest(BufferSizeTest): |
|
24c8d1aabade
Re-enable testing of builtin open() in test_bufio in test_largefile
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
72 open = open |
|
24c8d1aabade
Re-enable testing of builtin open() in test_bufio in test_largefile
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
73 |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
74 |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
75 def test_main(): |
|
54720
24c8d1aabade
Re-enable testing of builtin open() in test_bufio in test_largefile
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
76 support.run_unittest(CBufferSizeTest, PyBufferSizeTest, BuiltinBufferSizeTest) |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
77 |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
78 if __name__ == "__main__": |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
79 test_main() |
