Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | from test.test_support import verify, TestFailed, TESTFN |
2 | ||
3 | # Simple test to ensure that optimizations in fileobject.c deliver | |
4 | # the expected results. For best testing, run this under a debug-build | |
5 | # Python too (to exercise asserts in the C code). | |
6 | ||
7 | # Repeat string 'pattern' as often as needed to reach total length | |
8 | # 'length'. Then call try_one with that string, a string one larger | |
9 | # than that, and a string one smaller than that. The main driver | |
10 | # feeds this all small sizes and various powers of 2, so we exercise | |
11 | # all likely stdio buffer sizes, and "off by one" errors on both | |
12 | # sides. | |
13 | def drive_one(pattern, length): | |
14 | q, r = divmod(length, len(pattern)) | |
15 | teststring = pattern * q + pattern[:r] | |
16 | verify(len(teststring) == length) | |
17 | try_one(teststring) | |
18 | try_one(teststring + "x") | |
19 | try_one(teststring[:-1]) | |
20 | ||
21 | # Write s + "\n" + s to file, then open it and ensure that successive | |
22 | # .readline()s deliver what we wrote. | |
23 | def try_one(s): | |
24 | # Since C doesn't guarantee we can write/read arbitrary bytes in text | |
25 | # files, use binary mode. | |
26 | f = open(TESTFN, "wb") | |
27 | # write once with \n and once without | |
28 | f.write(s) | |
29 | f.write("\n") | |
30 | f.write(s) | |
31 | f.close() | |
32 | f = open(TESTFN, "rb") | |
33 | line = f.readline() | |
34 | if line != s + "\n": | |
35 | raise TestFailed("Expected %r got %r" % (s + "\n", line)) | |
36 | line = f.readline() | |
37 | if line != s: | |
38 | raise TestFailed("Expected %r got %r" % (s, line)) | |
39 | line = f.readline() | |
40 | if line: | |
41 | raise TestFailed("Expected EOF but got %r" % line) | |
42 | f.close() | |
43 | ||
44 | # A pattern with prime length, to avoid simple relationships with | |
45 | # stdio buffer sizes. | |
46 | primepat = "1234567890\00\01\02\03\04\05\06" | |
47 | ||
48 | nullpat = "\0" * 1000 | |
49 | ||
50 | try: | |
51 | for size in range(1, 257) + [512, 1000, 1024, 2048, 4096, 8192, 10000, | |
52 | 16384, 32768, 65536, 1000000]: | |
53 | drive_one(primepat, size) | |
54 | drive_one(nullpat, size) | |
55 | finally: | |
56 | try: | |
57 | import os | |
58 | os.unlink(TESTFN) | |
59 | except: | |
60 | pass |