Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_bufio.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 3cd1463c938d |
| children | 760a710eb6c1 |
| rev | line source |
|---|---|
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
1 import unittest |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
2 from test import test_support |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4 # Simple test to ensure that optimizations in fileobject.c deliver |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
5 # the expected results. For best testing, run this under a debug-build |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
6 # Python too (to exercise asserts in the C code). |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
7 |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
8 lengths = range(1, 257) + [512, 1000, 1024, 2048, 4096, 8192, 10000, |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
9 16384, 32768, 65536, 1000000] |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
10 |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
11 class BufferSizeTest(unittest.TestCase): |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
12 def try_one(self, s): |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
13 # 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
|
14 # .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
|
15 |
|
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
|
16 # Ensure we can open TESTFN for writing. |
|
3cd1463c938d
Ensure that this test will pass even if another test left an unwritable TESTFN.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40284
diff
changeset
|
17 test_support.unlink(test_support.TESTFN) |
|
3cd1463c938d
Ensure that this test will pass even if another test left an unwritable TESTFN.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40284
diff
changeset
|
18 |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
19 # 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
|
20 # files, use binary mode. |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
21 f = open(test_support.TESTFN, "wb") |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
22 try: |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
23 # write once with \n and once without |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
24 f.write(s) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
25 f.write("\n") |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
26 f.write(s) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
27 f.close() |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
28 f = open(test_support.TESTFN, "rb") |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
29 line = f.readline() |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
30 self.assertEqual(line, s + "\n") |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
31 line = f.readline() |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
32 self.assertEqual(line, s) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
33 line = f.readline() |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
34 self.assert_(not line) # Must be at EOF |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
35 f.close() |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
36 finally: |
|
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
|
37 test_support.unlink(test_support.TESTFN) |
|
40284
ff678669aa6e
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
40216
diff
changeset
|
38 |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
39 def drive_one(self, pattern): |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
40 for length in lengths: |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
41 # 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
|
42 # '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
|
43 # 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
|
44 # 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
|
45 # 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
|
46 q, r = divmod(length, len(pattern)) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
47 teststring = pattern * q + pattern[:r] |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
48 self.assertEqual(len(teststring), length) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
49 self.try_one(teststring) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
50 self.try_one(teststring + "x") |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
51 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
|
52 |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
53 def test_primepat(self): |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
54 # 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
|
55 # stdio buffer sizes. |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
56 self.drive_one("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
|
57 |
|
40216
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
58 def test_nullpat(self): |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
59 self.drive_one("\0" * 1000) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
60 |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
61 def test_main(): |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
62 test_support.run_unittest(BufferSizeTest) |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
63 |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
64 if __name__ == "__main__": |
|
21908c70d264
Port test_bufio to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
65 test_main() |
