1 from test_support
import verify
, TestFailed
, TESTFN
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).
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
13 def drive_one(pattern
, length
):
14 q
, r
= divmod(length
, len(pattern
))
15 teststring
= pattern
* q
+ pattern
[:r
]
16 verify(len(teststring
) == length
)
18 try_one(teststring
+ "x")
19 try_one(teststring
[:-1])
21 # Write s + "\n" + s to file, then open it and ensure that successive
22 # .readline()s deliver what we wrote.
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
32 f
= open(TESTFN
, "rb")
35 raise TestFailed("Expected %r got %r" % (s
+ "\n", line
))
38 raise TestFailed("Expected %r got %r" % (s
, line
))
41 raise TestFailed("Expected EOF but got %r" % line
)
44 # A pattern with prime length, to avoid simple relationships with
46 primepat
= "1234567890\00\01\02\03\04\05\06"
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
)