blob: 3f3b8f16fc0c222852b7ebea18938f6f885bf5dd [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:
45 print '%s =?= %s ...' % (`got_this`, `expect_this`),
46 if got_this != expect_this:
47 if test_support.verbose:
48 print 'no'
49 raise test_support.TestFailed, 'got %s, but expected %s' %\
50 (str(got_this), str(expect_this))
51 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')
Trent Mickf29f47b2000-08-11 19:02:59 +000062f.seek(size)
63f.write('a')
64f.flush()
Trent Mickf29f47b2000-08-11 19:02:59 +000065if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000066 print 'check file size with os.fstat'
Tim Peters6e13a562001-09-06 00:32:15 +000067expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
Trent Mickf29f47b2000-08-11 19:02:59 +000068f.close()
69if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000070 print 'check file size with os.stat'
Trent Mickf29f47b2000-08-11 19:02:59 +000071expect(os.stat(name)[stat.ST_SIZE], size+1)
72
73if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000074 print 'play around with seek() and read() with the built largefile'
Tim Peters6e13a562001-09-06 00:32:15 +000075f = open(name, 'rb')
Trent Mickf29f47b2000-08-11 19:02:59 +000076expect(f.tell(), 0)
77expect(f.read(1), '\000')
78expect(f.tell(), 1)
79f.seek(0)
80expect(f.tell(), 0)
81f.seek(0, 0)
82expect(f.tell(), 0)
83f.seek(42)
84expect(f.tell(), 42)
85f.seek(42, 0)
86expect(f.tell(), 42)
87f.seek(42, 1)
88expect(f.tell(), 84)
89f.seek(0, 1)
90expect(f.tell(), 84)
91f.seek(0, 2) # seek from the end
92expect(f.tell(), size + 1 + 0)
93f.seek(-10, 2)
94expect(f.tell(), size + 1 - 10)
95f.seek(-size-1, 2)
96expect(f.tell(), 0)
97f.seek(size)
98expect(f.tell(), size)
99expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
100f.close()
101
102if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +0000103 print 'play around with os.lseek() with the built largefile'
Trent Mickf29f47b2000-08-11 19:02:59 +0000104f = open(name, 'r')
105expect(os.lseek(f.fileno(), 0, 0), 0)
106expect(os.lseek(f.fileno(), 42, 0), 42)
107expect(os.lseek(f.fileno(), 42, 1), 84)
108expect(os.lseek(f.fileno(), 0, 1), 84)
109expect(os.lseek(f.fileno(), 0, 2), size+1+0)
110expect(os.lseek(f.fileno(), -10, 2), size+1-10)
111expect(os.lseek(f.fileno(), -size-1, 2), 0)
112expect(os.lseek(f.fileno(), size, 0), size)
113expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
114f.close()
115
116
117# XXX add tests for truncate if it exists
118# XXX has truncate ever worked on Windows? specifically on WinNT I get:
119# "IOError: [Errno 13] Permission denied"
120##try:
Fred Drake004d5e62000-10-23 17:22:08 +0000121## newsize = size - 10
122## f.seek(newsize)
123## f.truncate()
124## expect(f.tell(), newsize)
125## newsize = newsize - 1
126## f.seek(0)
127## f.truncate(newsize)
128## expect(f.tell(), newsize)
Trent Mickf29f47b2000-08-11 19:02:59 +0000129##except AttributeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000130## pass
Trent Mickf29f47b2000-08-11 19:02:59 +0000131
132os.unlink(name)