blob: 1b404b6fdc9fcd0d1ad07a37cfe195d0c47f5200 [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:
Fred Drakedce56412000-10-23 16:38:20 +000017 # 2**31 == 2147483648
18 f.seek(2147483649L)
Guido van Rossuma5af2142001-04-10 14:50:51 +000019except (IOError, OverflowError):
Fred Drakedce56412000-10-23 16:38:20 +000020 f.close()
21 os.unlink(test_support.TESTFN)
22 raise test_support.TestSkipped, \
23 "platform does not have largefile support"
Trent Mickf29f47b2000-08-11 19:02:59 +000024else:
Fred Drakedce56412000-10-23 16:38:20 +000025 f.close()
Trent Mickf29f47b2000-08-11 19:02:59 +000026
27
28# create >2GB file (2GB = 2147483648 bytes)
29size = 2500000000L
30name = test_support.TESTFN
31
32
33# on Windows this test comsumes large resources:
34# it takes a long time to build the >2GB file and takes >2GB of disk space
35# therefore test_support.use_large_resources must be defined to run this test
36if sys.platform[:3] == 'win' and not test_support.use_large_resources:
Fred Drakedce56412000-10-23 16:38:20 +000037 raise test_support.TestSkipped, \
38 "test requires %s bytes and a long time to run" % str(size)
Trent Mickf29f47b2000-08-11 19:02:59 +000039
40
41
42def expect(got_this, expect_this):
Fred Drakedce56412000-10-23 16:38:20 +000043 if test_support.verbose:
44 print '%s =?= %s ...' % (`got_this`, `expect_this`),
45 if got_this != expect_this:
46 if test_support.verbose:
47 print 'no'
48 raise test_support.TestFailed, 'got %s, but expected %s' %\
49 (str(got_this), str(expect_this))
50 else:
51 if test_support.verbose:
52 print 'yes'
Trent Mickf29f47b2000-08-11 19:02:59 +000053
54
55# test that each file function works as expected for a large (i.e. >2GB, do
56# we have to check >4GB) files
57
58if test_support.verbose:
Fred Drake004d5e62000-10-23 17:22:08 +000059 print 'create large file via seek (may be sparse file) ...'
Trent Mickf29f47b2000-08-11 19:02:59 +000060f = open(name, 'w')
61f.seek(size)
62f.write('a')
63f.flush()
64expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
65if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000066 print 'check file size with os.fstat'
Trent Mickf29f47b2000-08-11 19:02:59 +000067f.close()
68if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000069 print 'check file size with os.stat'
Trent Mickf29f47b2000-08-11 19:02:59 +000070expect(os.stat(name)[stat.ST_SIZE], size+1)
71
72if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000073 print 'play around with seek() and read() with the built largefile'
Trent Mickf29f47b2000-08-11 19:02:59 +000074f = open(name, 'r')
75expect(f.tell(), 0)
76expect(f.read(1), '\000')
77expect(f.tell(), 1)
78f.seek(0)
79expect(f.tell(), 0)
80f.seek(0, 0)
81expect(f.tell(), 0)
82f.seek(42)
83expect(f.tell(), 42)
84f.seek(42, 0)
85expect(f.tell(), 42)
86f.seek(42, 1)
87expect(f.tell(), 84)
88f.seek(0, 1)
89expect(f.tell(), 84)
90f.seek(0, 2) # seek from the end
91expect(f.tell(), size + 1 + 0)
92f.seek(-10, 2)
93expect(f.tell(), size + 1 - 10)
94f.seek(-size-1, 2)
95expect(f.tell(), 0)
96f.seek(size)
97expect(f.tell(), size)
98expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
99f.close()
100
101if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +0000102 print 'play around with os.lseek() with the built largefile'
Trent Mickf29f47b2000-08-11 19:02:59 +0000103f = open(name, 'r')
104expect(os.lseek(f.fileno(), 0, 0), 0)
105expect(os.lseek(f.fileno(), 42, 0), 42)
106expect(os.lseek(f.fileno(), 42, 1), 84)
107expect(os.lseek(f.fileno(), 0, 1), 84)
108expect(os.lseek(f.fileno(), 0, 2), size+1+0)
109expect(os.lseek(f.fileno(), -10, 2), size+1-10)
110expect(os.lseek(f.fileno(), -size-1, 2), 0)
111expect(os.lseek(f.fileno(), size, 0), size)
112expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
113f.close()
114
115
116# XXX add tests for truncate if it exists
117# XXX has truncate ever worked on Windows? specifically on WinNT I get:
118# "IOError: [Errno 13] Permission denied"
119##try:
Fred Drake004d5e62000-10-23 17:22:08 +0000120## newsize = size - 10
121## f.seek(newsize)
122## f.truncate()
123## expect(f.tell(), newsize)
124## newsize = newsize - 1
125## f.seek(0)
126## f.truncate(newsize)
127## expect(f.tell(), newsize)
Trent Mickf29f47b2000-08-11 19:02:59 +0000128##except AttributeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000129## pass
Trent Mickf29f47b2000-08-11 19:02:59 +0000130
131os.unlink(name)
Fred Drakedce56412000-10-23 16:38:20 +0000132print >>sys.stderr, name, "exists:", os.path.exists(name)