0001 from test.test_support import verify, TestFailed, TESTFN 0002 0003 # Simple test to ensure that optimizations in fileobject.c deliver 0004 # the expected results. For best testing, run this under a debug-build 0005 # Python too (to exercise asserts in the C code). 0006 0007 # Repeat string 'pattern' as often as needed to reach total length 0008 # 'length'. Then call try_one with that string, a string one larger 0009 # than that, and a string one smaller than that. The main driver 0010 # feeds this all small sizes and various powers of 2, so we exercise 0011 # all likely stdio buffer sizes, and "off by one" errors on both 0012 # sides. 0013 def drive_one(pattern, length): 0014 q, r = divmod(length, len(pattern)) 0015 teststring = pattern * q + pattern[:r] 0016 verify(len(teststring) == length) 0017 try_one(teststring) 0018 try_one(teststring + "x") 0019 try_one(teststring[:-1]) 0020 0021 # Write s + "\n" + s to file, then open it and ensure that successive 0022 # .readline()s deliver what we wrote. 0023 def try_one(s): 0024 # Since C doesn't guarantee we can write/read arbitrary bytes in text 0025 # files, use binary mode. 0026 f = open(TESTFN, "wb") 0027 # write once with \n and once without 0028 f.write(s) 0029 f.write("\n") 0030 f.write(s) 0031 f.close() 0032 f = open(TESTFN, "rb") 0033 line = f.readline() 0034 if line != s + "\n": 0035 raise TestFailed("Expected %r got %r" % (s + "\n", line)) 0036 line = f.readline() 0037 if line != s: 0038 raise TestFailed("Expected %r got %r" % (s, line)) 0039 line = f.readline() 0040 if line: 0041 raise TestFailed("Expected EOF but got %r" % line) 0042 f.close() 0043 0044 # A pattern with prime length, to avoid simple relationships with 0045 # stdio buffer sizes. 0046 primepat = "1234567890\00\01\02\03\04\05\06" 0047 0048 nullpat = "\0" * 1000 0049 0050 try: 0051 for size in range(1, 257) + [512, 1000, 1024, 2048, 4096, 8192, 10000, 0052 16384, 32768, 65536, 1000000]: 0053 drive_one(primepat, size) 0054 drive_one(nullpat, size) 0055 finally: 0056 try: 0057 import os 0058 os.unlink(TESTFN) 0059 except: 0060 pass 0061
Generated by PyXR 0.9.4