blob: b2bec1fa9f7a32a06752c5e171afaec9eb7dd8f1 [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
Tim Peters6e13a562001-09-06 00:32:15 +000015f = open(test_support.TESTFN, 'wb')
Trent Mickf29f47b2000-08-11 19:02:59 +000016try:
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
Barry Warsaw7fdfc382001-08-20 22:37:34 +000033# On Windows this test comsumes large resources; It takes a long time to build
34# the >2GB file and takes >2GB of disk space therefore the resource must be
35# enabled to run this test. If not, nothing after this line stanza will be
36# executed.
37if sys.platform[:3] == 'win':
38 test_support.requires(
39 'largefile',
40 'test requires %s bytes and a long time to run' % str(size))
Trent Mickf29f47b2000-08-11 19:02:59 +000041
42
43def expect(got_this, expect_this):
Fred Drakedce56412000-10-23 16:38:20 +000044 if test_support.verbose:
Tim Petersb8c02302001-09-06 01:17:45 +000045 print '%r =?= %r ...' % (got_this, expect_this),
Fred Drakedce56412000-10-23 16:38:20 +000046 if got_this != expect_this:
47 if test_support.verbose:
48 print 'no'
Tim Petersb8c02302001-09-06 01:17:45 +000049 raise test_support.TestFailed, 'got %r, but expected %r' %\
50 (got_this, expect_this)
Fred Drakedce56412000-10-23 16:38:20 +000051 else:
52 if test_support.verbose:
53 print 'yes'
Trent Mickf29f47b2000-08-11 19:02:59 +000054
55
56# test that each file function works as expected for a large (i.e. >2GB, do
57# we have to check >4GB) files
58
59if test_support.verbose:
Fred Drake004d5e62000-10-23 17:22:08 +000060 print 'create large file via seek (may be sparse file) ...'
Tim Peters6e13a562001-09-06 00:32:15 +000061f = open(name, 'wb')
Tim Petersb8c02302001-09-06 01:17:45 +000062f.write('z')
63f.seek(0)
Trent Mickf29f47b2000-08-11 19:02:59 +000064f.seek(size)
65f.write('a')
66f.flush()
Trent Mickf29f47b2000-08-11 19:02:59 +000067if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000068 print 'check file size with os.fstat'
Tim Peters6e13a562001-09-06 00:32:15 +000069expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
Trent Mickf29f47b2000-08-11 19:02:59 +000070f.close()
71if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000072 print 'check file size with os.stat'
Trent Mickf29f47b2000-08-11 19:02:59 +000073expect(os.stat(name)[stat.ST_SIZE], size+1)
74
75if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000076 print 'play around with seek() and read() with the built largefile'
Tim Peters6e13a562001-09-06 00:32:15 +000077f = open(name, 'rb')
Trent Mickf29f47b2000-08-11 19:02:59 +000078expect(f.tell(), 0)
Tim Petersb8c02302001-09-06 01:17:45 +000079expect(f.read(1), 'z')
Trent Mickf29f47b2000-08-11 19:02:59 +000080expect(f.tell(), 1)
81f.seek(0)
82expect(f.tell(), 0)
83f.seek(0, 0)
84expect(f.tell(), 0)
85f.seek(42)
86expect(f.tell(), 42)
87f.seek(42, 0)
88expect(f.tell(), 42)
89f.seek(42, 1)
90expect(f.tell(), 84)
91f.seek(0, 1)
92expect(f.tell(), 84)
93f.seek(0, 2) # seek from the end
94expect(f.tell(), size + 1 + 0)
95f.seek(-10, 2)
96expect(f.tell(), size + 1 - 10)
97f.seek(-size-1, 2)
98expect(f.tell(), 0)
99f.seek(size)
100expect(f.tell(), size)
101expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
Tim Petersb8c02302001-09-06 01:17:45 +0000102f.seek(-size-1, 1)
103expect(f.read(1), 'z')
104expect(f.tell(), 1)
Trent Mickf29f47b2000-08-11 19:02:59 +0000105f.close()
106
107if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +0000108 print 'play around with os.lseek() with the built largefile'
Tim Petersb8c02302001-09-06 01:17:45 +0000109f = open(name, 'rb')
Trent Mickf29f47b2000-08-11 19:02:59 +0000110expect(os.lseek(f.fileno(), 0, 0), 0)
111expect(os.lseek(f.fileno(), 42, 0), 42)
112expect(os.lseek(f.fileno(), 42, 1), 84)
113expect(os.lseek(f.fileno(), 0, 1), 84)
114expect(os.lseek(f.fileno(), 0, 2), size+1+0)
115expect(os.lseek(f.fileno(), -10, 2), size+1-10)
116expect(os.lseek(f.fileno(), -size-1, 2), 0)
117expect(os.lseek(f.fileno(), size, 0), size)
118expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
119f.close()
120
121
122# XXX add tests for truncate if it exists
123# XXX has truncate ever worked on Windows? specifically on WinNT I get:
124# "IOError: [Errno 13] Permission denied"
125##try:
Fred Drake004d5e62000-10-23 17:22:08 +0000126## newsize = size - 10
127## f.seek(newsize)
128## f.truncate()
129## expect(f.tell(), newsize)
130## newsize = newsize - 1
131## f.seek(0)
132## f.truncate(newsize)
133## expect(f.tell(), newsize)
Trent Mickf29f47b2000-08-11 19:02:59 +0000134##except AttributeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000135## pass
Trent Mickf29f47b2000-08-11 19:02:59 +0000136
137os.unlink(name)