symbian-qemu-0.9.1-12/python-2.6.1/Lib/test/test_bufio.py
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 import unittest
       
     2 from test import test_support
       
     3 
       
     4 # Simple test to ensure that optimizations in fileobject.c deliver
       
     5 # the expected results.  For best testing, run this under a debug-build
       
     6 # Python too (to exercise asserts in the C code).
       
     7 
       
     8 lengths = range(1, 257) + [512, 1000, 1024, 2048, 4096, 8192, 10000,
       
     9                            16384, 32768, 65536, 1000000]
       
    10 
       
    11 class BufferSizeTest(unittest.TestCase):
       
    12     def try_one(self, s):
       
    13         # Write s + "\n" + s to file, then open it and ensure that successive
       
    14         # .readline()s deliver what we wrote.
       
    15 
       
    16         # Ensure we can open TESTFN for writing.
       
    17         test_support.unlink(test_support.TESTFN)
       
    18 
       
    19         # Since C doesn't guarantee we can write/read arbitrary bytes in text
       
    20         # files, use binary mode.
       
    21         f = open(test_support.TESTFN, "wb")
       
    22         try:
       
    23             # write once with \n and once without
       
    24             f.write(s)
       
    25             f.write("\n")
       
    26             f.write(s)
       
    27             f.close()
       
    28             f = open(test_support.TESTFN, "rb")
       
    29             line = f.readline()
       
    30             self.assertEqual(line, s + "\n")
       
    31             line = f.readline()
       
    32             self.assertEqual(line, s)
       
    33             line = f.readline()
       
    34             self.assert_(not line) # Must be at EOF
       
    35             f.close()
       
    36         finally:
       
    37             test_support.unlink(test_support.TESTFN)
       
    38 
       
    39     def drive_one(self, pattern):
       
    40         for length in lengths:
       
    41             # Repeat string 'pattern' as often as needed to reach total length
       
    42             # 'length'.  Then call try_one with that string, a string one larger
       
    43             # than that, and a string one smaller than that.  Try this with all
       
    44             # small sizes and various powers of 2, so we exercise all likely
       
    45             # stdio buffer sizes, and "off by one" errors on both sides.
       
    46             q, r = divmod(length, len(pattern))
       
    47             teststring = pattern * q + pattern[:r]
       
    48             self.assertEqual(len(teststring), length)
       
    49             self.try_one(teststring)
       
    50             self.try_one(teststring + "x")
       
    51             self.try_one(teststring[:-1])
       
    52 
       
    53     def test_primepat(self):
       
    54         # A pattern with prime length, to avoid simple relationships with
       
    55         # stdio buffer sizes.
       
    56         self.drive_one("1234567890\00\01\02\03\04\05\06")
       
    57 
       
    58     def test_nullpat(self):
       
    59         self.drive_one("\0" * 1000)
       
    60 
       
    61 def test_main():
       
    62     test_support.run_unittest(BufferSizeTest)
       
    63 
       
    64 if __name__ == "__main__":
       
    65     test_main()