blob: 407aba81e09fe4c218a8fe420a29a897eab52930 [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
Barry Warsaw7fdfc382001-08-20 22:37:34 +000014# On Windows this test comsumes large resources; It takes a long time to build
15# the >2GB file and takes >2GB of disk space therefore the resource must be
16# enabled to run this test. If not, nothing after this line stanza will be
17# executed.
18if sys.platform[:3] == 'win':
19 test_support.requires(
20 'largefile',
21 'test requires %s bytes and a long time to run' % str(size))
Guido van Rossum47f40342001-09-10 13:34:12 +000022else:
23 # Only run if the current filesystem supports large files.
24 # (Skip this test on Windows, since we now always support large files.)
25 f = open(test_support.TESTFN, 'wb')
26 try:
27 # 2**31 == 2147483648
28 f.seek(2147483649L)
29 # Seeking is not enough of a test: you must write and flush, too!
30 f.write("x")
31 f.flush()
32 except (IOError, OverflowError):
33 f.close()
34 os.unlink(test_support.TESTFN)
35 raise test_support.TestSkipped, \
36 "filesystem does not have largefile support"
37 else:
38 f.close()
39
40
41# create >2GB file (2GB = 2147483648 bytes)
42size = 2500000000L
43name = test_support.TESTFN
Trent Mickf29f47b2000-08-11 19:02:59 +000044
45
46def expect(got_this, expect_this):
Fred Drakedce56412000-10-23 16:38:20 +000047 if test_support.verbose:
Tim Petersb8c02302001-09-06 01:17:45 +000048 print '%r =?= %r ...' % (got_this, expect_this),
Fred Drakedce56412000-10-23 16:38:20 +000049 if got_this != expect_this:
50 if test_support.verbose:
51 print 'no'
Tim Petersb8c02302001-09-06 01:17:45 +000052 raise test_support.TestFailed, 'got %r, but expected %r' %\
53 (got_this, expect_this)
Fred Drakedce56412000-10-23 16:38:20 +000054 else:
55 if test_support.verbose:
56 print 'yes'
Trent Mickf29f47b2000-08-11 19:02:59 +000057
58
59# test that each file function works as expected for a large (i.e. >2GB, do
60# we have to check >4GB) files
61
62if test_support.verbose:
Fred Drake004d5e62000-10-23 17:22:08 +000063 print 'create large file via seek (may be sparse file) ...'
Tim Peters6e13a562001-09-06 00:32:15 +000064f = open(name, 'wb')
Tim Petersb8c02302001-09-06 01:17:45 +000065f.write('z')
66f.seek(0)
Trent Mickf29f47b2000-08-11 19:02:59 +000067f.seek(size)
68f.write('a')
69f.flush()
Trent Mickf29f47b2000-08-11 19:02:59 +000070if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000071 print 'check file size with os.fstat'
Tim Peters6e13a562001-09-06 00:32:15 +000072expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
Trent Mickf29f47b2000-08-11 19:02:59 +000073f.close()
74if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000075 print 'check file size with os.stat'
Trent Mickf29f47b2000-08-11 19:02:59 +000076expect(os.stat(name)[stat.ST_SIZE], size+1)
77
78if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000079 print 'play around with seek() and read() with the built largefile'
Tim Peters6e13a562001-09-06 00:32:15 +000080f = open(name, 'rb')
Trent Mickf29f47b2000-08-11 19:02:59 +000081expect(f.tell(), 0)
Tim Petersb8c02302001-09-06 01:17:45 +000082expect(f.read(1), 'z')
Trent Mickf29f47b2000-08-11 19:02:59 +000083expect(f.tell(), 1)
84f.seek(0)
85expect(f.tell(), 0)
86f.seek(0, 0)
87expect(f.tell(), 0)
88f.seek(42)
89expect(f.tell(), 42)
90f.seek(42, 0)
91expect(f.tell(), 42)
92f.seek(42, 1)
93expect(f.tell(), 84)
94f.seek(0, 1)
95expect(f.tell(), 84)
96f.seek(0, 2) # seek from the end
97expect(f.tell(), size + 1 + 0)
98f.seek(-10, 2)
99expect(f.tell(), size + 1 - 10)
100f.seek(-size-1, 2)
101expect(f.tell(), 0)
102f.seek(size)
103expect(f.tell(), size)
104expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
Tim Petersb8c02302001-09-06 01:17:45 +0000105f.seek(-size-1, 1)
106expect(f.read(1), 'z')
107expect(f.tell(), 1)
Trent Mickf29f47b2000-08-11 19:02:59 +0000108f.close()
109
110if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +0000111 print 'play around with os.lseek() with the built largefile'
Tim Petersb8c02302001-09-06 01:17:45 +0000112f = open(name, 'rb')
Trent Mickf29f47b2000-08-11 19:02:59 +0000113expect(os.lseek(f.fileno(), 0, 0), 0)
114expect(os.lseek(f.fileno(), 42, 0), 42)
115expect(os.lseek(f.fileno(), 42, 1), 84)
116expect(os.lseek(f.fileno(), 0, 1), 84)
117expect(os.lseek(f.fileno(), 0, 2), size+1+0)
118expect(os.lseek(f.fileno(), -10, 2), size+1-10)
119expect(os.lseek(f.fileno(), -size-1, 2), 0)
120expect(os.lseek(f.fileno(), size, 0), size)
121expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
122f.close()
123
124
125# XXX add tests for truncate if it exists
126# XXX has truncate ever worked on Windows? specifically on WinNT I get:
127# "IOError: [Errno 13] Permission denied"
128##try:
Fred Drake004d5e62000-10-23 17:22:08 +0000129## newsize = size - 10
130## f.seek(newsize)
131## f.truncate()
132## expect(f.tell(), newsize)
133## newsize = newsize - 1
134## f.seek(0)
135## f.truncate(newsize)
136## expect(f.tell(), newsize)
Trent Mickf29f47b2000-08-11 19:02:59 +0000137##except AttributeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000138## pass
Trent Mickf29f47b2000-08-11 19:02:59 +0000139
140os.unlink(name)