1
from test.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"
52
# for size in range(1, 257) + [512, 1000, 1024, 2048, 4096, 8192, 10000,
53
# 16384, 32768, 65536, 1000000]:
54
for size in range(1, 9) + [9, 63, 64, 65, 128, 129, 254, 255, 256, 512,
55
1000, 1024, 2048, 4096, 8192, 10000, 16384]:
56
drive_one(primepat, size)
57
drive_one(nullpat, size)