~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to lib-python/modified-2.4.1/test/test_bufio.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from test.test_support import verify, TestFailed, TESTFN
 
2
 
 
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).
 
6
 
 
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
 
12
# sides.
 
13
def drive_one(pattern, length):
 
14
    q, r = divmod(length, len(pattern))
 
15
    teststring = pattern * q + pattern[:r]
 
16
    verify(len(teststring) == length)
 
17
    try_one(teststring)
 
18
    try_one(teststring + "x")
 
19
    try_one(teststring[:-1])
 
20
 
 
21
# Write s + "\n" + s to file, then open it and ensure that successive
 
22
# .readline()s deliver what we wrote.
 
23
def try_one(s):
 
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
 
28
    f.write(s)
 
29
    f.write("\n")
 
30
    f.write(s)
 
31
    f.close()
 
32
    f = open(TESTFN, "rb")
 
33
    line = f.readline()
 
34
    if line != s + "\n":
 
35
        raise TestFailed("Expected %r got %r" % (s + "\n", line))
 
36
    line = f.readline()
 
37
    if line != s:
 
38
        raise TestFailed("Expected %r got %r" % (s, line))
 
39
    line = f.readline()
 
40
    if line:
 
41
        raise TestFailed("Expected EOF but got %r" % line)
 
42
    f.close()
 
43
 
 
44
# A pattern with prime length, to avoid simple relationships with
 
45
# stdio buffer sizes.
 
46
primepat = "1234567890\00\01\02\03\04\05\06"
 
47
 
 
48
nullpat = "\0" * 1000
 
49
 
 
50
try:
 
51
# Too slow for PyPy.
 
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)
 
58
finally:
 
59
    try:
 
60
        import os
 
61
        os.unlink(TESTFN)
 
62
    except:
 
63
        pass