Kaydet (Commit) 3a3d8ea4 authored tarafından Walter Dörwald's avatar Walter Dörwald

Port test_bufio to unittest.

üst 27562783
from test.test_support import verify, TestFailed, TESTFN import unittest
from test import test_support
# Simple test to ensure that optimizations in fileobject.c deliver # Simple test to ensure that optimizations in fileobject.c deliver
# the expected results. For best testing, run this under a debug-build # the expected results. For best testing, run this under a debug-build
# Python too (to exercise asserts in the C code). # Python too (to exercise asserts in the C code).
# Repeat string 'pattern' as often as needed to reach total length lengths = range(1, 257) + [512, 1000, 1024, 2048, 4096, 8192, 10000,
# 'length'. Then call try_one with that string, a string one larger 16384, 32768, 65536, 1000000]
# than that, and a string one smaller than that. The main driver
# feeds this all small sizes and various powers of 2, so we exercise class BufferSizeTest(unittest.TestCase):
# all likely stdio buffer sizes, and "off by one" errors on both def try_one(self, s):
# sides. # Write s + "\n" + s to file, then open it and ensure that successive
def drive_one(pattern, length): # .readline()s deliver what we wrote.
q, r = divmod(length, len(pattern))
teststring = pattern * q + pattern[:r] # Since C doesn't guarantee we can write/read arbitrary bytes in text
verify(len(teststring) == length) # files, use binary mode.
try_one(teststring) f = open(test_support.TESTFN, "wb")
try_one(teststring + "x") try:
try_one(teststring[:-1]) # write once with \n and once without
f.write(s)
# Write s + "\n" + s to file, then open it and ensure that successive f.write("\n")
# .readline()s deliver what we wrote. f.write(s)
def try_one(s): f.close()
# Since C doesn't guarantee we can write/read arbitrary bytes in text f = open(test_support.TESTFN, "rb")
# files, use binary mode. line = f.readline()
f = open(TESTFN, "wb") self.assertEqual(line, s + "\n")
# write once with \n and once without line = f.readline()
f.write(s) self.assertEqual(line, s)
f.write("\n") line = f.readline()
f.write(s) self.assert_(not line) # Must be at EOF
f.close() f.close()
f = open(TESTFN, "rb") finally:
line = f.readline() try:
if line != s + "\n": import os
raise TestFailed("Expected %r got %r" % (s + "\n", line)) os.unlink(test_support.TESTFN)
line = f.readline() except:
if line != s: pass
raise TestFailed("Expected %r got %r" % (s, line))
line = f.readline() def drive_one(self, pattern):
if line: for length in lengths:
raise TestFailed("Expected EOF but got %r" % line) # Repeat string 'pattern' as often as needed to reach total length
f.close() # 'length'. Then call try_one with that string, a string one larger
# than that, and a string one smaller than that. Try this with all
# A pattern with prime length, to avoid simple relationships with # small sizes and various powers of 2, so we exercise all likely
# stdio buffer sizes. # stdio buffer sizes, and "off by one" errors on both sides.
primepat = "1234567890\00\01\02\03\04\05\06" q, r = divmod(length, len(pattern))
teststring = pattern * q + pattern[:r]
nullpat = "\0" * 1000 self.assertEqual(len(teststring), length)
self.try_one(teststring)
try: self.try_one(teststring + "x")
for size in range(1, 257) + [512, 1000, 1024, 2048, 4096, 8192, 10000, self.try_one(teststring[:-1])
16384, 32768, 65536, 1000000]:
drive_one(primepat, size) def test_primepat(self):
drive_one(nullpat, size) # A pattern with prime length, to avoid simple relationships with
finally: # stdio buffer sizes.
try: self.drive_one("1234567890\00\01\02\03\04\05\06")
import os
os.unlink(TESTFN) def test_nullpat(self):
except: self.drive_one("\0" * 1000)
pass
def test_main():
test_support.run_unittest(BufferSizeTest)
if __name__ == "__main__":
test_main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment