blob: 5b6523754b40d9c26ac319954a80b00687ff5bf3 [file] [log] [blame]
Trent Mickf29f47b2000-08-11 19:02:59 +00001#!python
2
3#----------------------------------------------------------------------
4# test largefile support on system where this makes sense
5#
6#XXX how to only run this when support is there
7#XXX how to only optionally run this, it will take along time
8#----------------------------------------------------------------------
9
10import test_support
11import os, struct, stat, sys
12
13
14# only run if the current system support large files
15f = open(test_support.TESTFN, 'w')
16try:
17 # 2**31 == 2147483648
18 f.seek(2147483649L)
19except OverflowError:
20 raise test_support.TestSkipped, "platform does not have largefile support"
21else:
22 f.close()
23
24
25# create >2GB file (2GB = 2147483648 bytes)
26size = 2500000000L
27name = test_support.TESTFN
28
29
30# on Windows this test comsumes large resources:
31# it takes a long time to build the >2GB file and takes >2GB of disk space
32# therefore test_support.use_large_resources must be defined to run this test
33if sys.platform[:3] == 'win' and not test_support.use_large_resources:
34 raise test_support.TestSkipped, \
35 "test requires %s bytes and a long time to run" % str(size)
36
37
38
39def expect(got_this, expect_this):
40 if test_support.verbose:
41 print '%s =?= %s ...' % (`got_this`, `expect_this`),
42 if got_this != expect_this:
43 if test_support.verbose:
44 print 'no'
45 raise test_support.TestFailed, 'got %s, but expected %s' %\
46 (str(got_this), str(expect_this))
47 else:
48 if test_support.verbose:
49 print 'yes'
50
51
52# test that each file function works as expected for a large (i.e. >2GB, do
53# we have to check >4GB) files
54
55if test_support.verbose:
56 print 'create large file via seek (may be sparse file) ...'
57f = open(name, 'w')
58f.seek(size)
59f.write('a')
60f.flush()
61expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
62if test_support.verbose:
63 print 'check file size with os.fstat'
64f.close()
65if test_support.verbose:
66 print 'check file size with os.stat'
67expect(os.stat(name)[stat.ST_SIZE], size+1)
68
69if test_support.verbose:
70 print 'play around with seek() and read() with the built largefile'
71f = open(name, 'r')
72expect(f.tell(), 0)
73expect(f.read(1), '\000')
74expect(f.tell(), 1)
75f.seek(0)
76expect(f.tell(), 0)
77f.seek(0, 0)
78expect(f.tell(), 0)
79f.seek(42)
80expect(f.tell(), 42)
81f.seek(42, 0)
82expect(f.tell(), 42)
83f.seek(42, 1)
84expect(f.tell(), 84)
85f.seek(0, 1)
86expect(f.tell(), 84)
87f.seek(0, 2) # seek from the end
88expect(f.tell(), size + 1 + 0)
89f.seek(-10, 2)
90expect(f.tell(), size + 1 - 10)
91f.seek(-size-1, 2)
92expect(f.tell(), 0)
93f.seek(size)
94expect(f.tell(), size)
95expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
96f.close()
97
98if test_support.verbose:
99 print 'play around with os.lseek() with the built largefile'
100f = open(name, 'r')
101expect(os.lseek(f.fileno(), 0, 0), 0)
102expect(os.lseek(f.fileno(), 42, 0), 42)
103expect(os.lseek(f.fileno(), 42, 1), 84)
104expect(os.lseek(f.fileno(), 0, 1), 84)
105expect(os.lseek(f.fileno(), 0, 2), size+1+0)
106expect(os.lseek(f.fileno(), -10, 2), size+1-10)
107expect(os.lseek(f.fileno(), -size-1, 2), 0)
108expect(os.lseek(f.fileno(), size, 0), size)
109expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
110f.close()
111
112
113# XXX add tests for truncate if it exists
114# XXX has truncate ever worked on Windows? specifically on WinNT I get:
115# "IOError: [Errno 13] Permission denied"
116##try:
117## newsize = size - 10
118## f.seek(newsize)
119## f.truncate()
120## expect(f.tell(), newsize)
121## newsize = newsize - 1
122## f.seek(0)
123## f.truncate(newsize)
124## expect(f.tell(), newsize)
125##except AttributeError:
126## pass
127
128os.unlink(name)
129