test_largefile.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Lib » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Lib » test » test_largefile.py
#!python

#----------------------------------------------------------------------
# test largefile support on system where this makes sense
#
#XXX how to only run this when support is there
#XXX how to only optionally run this, it will take along time
#----------------------------------------------------------------------

import test_support
import os, struct, stat, sys


# only run if the current system support large files
f = open(test_support.TESTFN, 'w')
try:
    # 2**31 == 2147483648
    f.seek(2147483649L)
except (IOError, OverflowError):
    f.close()
    os.unlink(test_support.TESTFN)
    raise test_support.TestSkipped, \
          "platform does not have largefile support"
else:
    f.close()


# create >2GB file (2GB = 2147483648 bytes)
size = 2500000000L
name = test_support.TESTFN


# on Windows this test comsumes large resources:
#  it takes a long time to build the >2GB file and takes >2GB of disk space
# therefore test_support.use_large_resources must be defined to run this test
if sys.platform[:3] == 'win' and not test_support.use_large_resources:
    raise test_support.TestSkipped, \
          "test requires %s bytes and a long time to run" % str(size)



def expect(got_this, expect_this):
    if test_support.verbose:
        print '%s =?= %s ...' % (`got_this`, `expect_this`),
    if got_this != expect_this:
        if test_support.verbose:
            print 'no'
        raise test_support.TestFailed, 'got %s, but expected %s' %\
              (str(got_this), str(expect_this))
    else:
        if test_support.verbose:
            print 'yes'


# test that each file function works as expected for a large (i.e. >2GB, do
# we have to check >4GB) files

if test_support.verbose:
    print 'create large file via seek (may be sparse file) ...'
f = open(name, 'w')
f.seek(size)
f.write('a')
f.flush()
expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
if test_support.verbose:
    print 'check file size with os.fstat'
f.close()
if test_support.verbose:
    print 'check file size with os.stat'
expect(os.stat(name)[stat.ST_SIZE], size+1)

if test_support.verbose:
    print 'play around with seek() and read() with the built largefile'
f = open(name, 'r')
expect(f.tell(), 0)
expect(f.read(1), '\000')
expect(f.tell(), 1)
f.seek(0)
expect(f.tell(), 0)
f.seek(0, 0)
expect(f.tell(), 0)
f.seek(42)
expect(f.tell(), 42)
f.seek(42, 0)
expect(f.tell(), 42)
f.seek(42, 1)
expect(f.tell(), 84)
f.seek(0, 1)
expect(f.tell(), 84)
f.seek(0, 2) # seek from the end
expect(f.tell(), size + 1 + 0)
f.seek(-10, 2)
expect(f.tell(), size + 1 - 10)
f.seek(-size-1, 2)
expect(f.tell(), 0)
f.seek(size)
expect(f.tell(), size)
expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
f.close()

if test_support.verbose:
    print 'play around with os.lseek() with the built largefile'
f = open(name, 'r')
expect(os.lseek(f.fileno(), 0, 0), 0)
expect(os.lseek(f.fileno(), 42, 0), 42)
expect(os.lseek(f.fileno(), 42, 1), 84)
expect(os.lseek(f.fileno(), 0, 1), 84)
expect(os.lseek(f.fileno(), 0, 2), size+1+0)
expect(os.lseek(f.fileno(), -10, 2), size+1-10)
expect(os.lseek(f.fileno(), -size-1, 2), 0)
expect(os.lseek(f.fileno(), size, 0), size)
expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
f.close()


# XXX add tests for truncate if it exists
# XXX has truncate ever worked on Windows? specifically on WinNT I get:
#     "IOError: [Errno 13] Permission denied"
##try:
##      newsize = size - 10
##      f.seek(newsize)
##      f.truncate()
##      expect(f.tell(), newsize)
##      newsize = newsize - 1
##      f.seek(0)
##      f.truncate(newsize)
##      expect(f.tell(), newsize)
##except AttributeError:
##      pass

os.unlink(name)
print >>sys.stderr, name, "exists:", os.path.exists(name)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.