blob: 352fae07ce2aa7d796c73f5b65047a5c7e4266bb [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#
Trent Mickf29f47b2000-08-11 19:02:59 +00006#----------------------------------------------------------------------
7
8import test_support
9import os, struct, stat, sys
10
11
Guido van Rossuma31ddbb2001-09-10 15:03:18 +000012# create >2GB file (2GB = 2147483648 bytes)
13size = 2500000000L
14name = test_support.TESTFN
15
16
Barry Warsaw7fdfc382001-08-20 22:37:34 +000017# On Windows this test comsumes large resources; It takes a long time to build
18# the >2GB file and takes >2GB of disk space therefore the resource must be
19# enabled to run this test. If not, nothing after this line stanza will be
20# executed.
21if sys.platform[:3] == 'win':
22 test_support.requires(
23 'largefile',
24 'test requires %s bytes and a long time to run' % str(size))
Guido van Rossum47f40342001-09-10 13:34:12 +000025else:
26 # Only run if the current filesystem supports large files.
27 # (Skip this test on Windows, since we now always support large files.)
28 f = open(test_support.TESTFN, 'wb')
29 try:
30 # 2**31 == 2147483648
31 f.seek(2147483649L)
32 # Seeking is not enough of a test: you must write and flush, too!
33 f.write("x")
34 f.flush()
35 except (IOError, OverflowError):
36 f.close()
37 os.unlink(test_support.TESTFN)
38 raise test_support.TestSkipped, \
39 "filesystem does not have largefile support"
40 else:
41 f.close()
42
43
Trent Mickf29f47b2000-08-11 19:02:59 +000044def expect(got_this, expect_this):
Fred Drakedce56412000-10-23 16:38:20 +000045 if test_support.verbose:
Tim Petersb8c02302001-09-06 01:17:45 +000046 print '%r =?= %r ...' % (got_this, expect_this),
Fred Drakedce56412000-10-23 16:38:20 +000047 if got_this != expect_this:
48 if test_support.verbose:
49 print 'no'
Tim Petersb8c02302001-09-06 01:17:45 +000050 raise test_support.TestFailed, 'got %r, but expected %r' %\
51 (got_this, expect_this)
Fred Drakedce56412000-10-23 16:38:20 +000052 else:
53 if test_support.verbose:
54 print 'yes'
Trent Mickf29f47b2000-08-11 19:02:59 +000055
56
57# test that each file function works as expected for a large (i.e. >2GB, do
58# we have to check >4GB) files
59
60if test_support.verbose:
Fred Drake004d5e62000-10-23 17:22:08 +000061 print 'create large file via seek (may be sparse file) ...'
Tim Peters6e13a562001-09-06 00:32:15 +000062f = open(name, 'wb')
Tim Petersb8c02302001-09-06 01:17:45 +000063f.write('z')
64f.seek(0)
Trent Mickf29f47b2000-08-11 19:02:59 +000065f.seek(size)
66f.write('a')
67f.flush()
Trent Mickf29f47b2000-08-11 19:02:59 +000068if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000069 print 'check file size with os.fstat'
Tim Peters6e13a562001-09-06 00:32:15 +000070expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
Trent Mickf29f47b2000-08-11 19:02:59 +000071f.close()
72if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000073 print 'check file size with os.stat'
Trent Mickf29f47b2000-08-11 19:02:59 +000074expect(os.stat(name)[stat.ST_SIZE], size+1)
75
76if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000077 print 'play around with seek() and read() with the built largefile'
Tim Peters6e13a562001-09-06 00:32:15 +000078f = open(name, 'rb')
Trent Mickf29f47b2000-08-11 19:02:59 +000079expect(f.tell(), 0)
Tim Petersb8c02302001-09-06 01:17:45 +000080expect(f.read(1), 'z')
Trent Mickf29f47b2000-08-11 19:02:59 +000081expect(f.tell(), 1)
82f.seek(0)
83expect(f.tell(), 0)
84f.seek(0, 0)
85expect(f.tell(), 0)
86f.seek(42)
87expect(f.tell(), 42)
88f.seek(42, 0)
89expect(f.tell(), 42)
90f.seek(42, 1)
91expect(f.tell(), 84)
92f.seek(0, 1)
93expect(f.tell(), 84)
94f.seek(0, 2) # seek from the end
95expect(f.tell(), size + 1 + 0)
96f.seek(-10, 2)
97expect(f.tell(), size + 1 - 10)
98f.seek(-size-1, 2)
99expect(f.tell(), 0)
100f.seek(size)
101expect(f.tell(), size)
102expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
Tim Petersb8c02302001-09-06 01:17:45 +0000103f.seek(-size-1, 1)
104expect(f.read(1), 'z')
105expect(f.tell(), 1)
Trent Mickf29f47b2000-08-11 19:02:59 +0000106f.close()
107
108if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +0000109 print 'play around with os.lseek() with the built largefile'
Tim Petersb8c02302001-09-06 01:17:45 +0000110f = open(name, 'rb')
Trent Mickf29f47b2000-08-11 19:02:59 +0000111expect(os.lseek(f.fileno(), 0, 0), 0)
112expect(os.lseek(f.fileno(), 42, 0), 42)
113expect(os.lseek(f.fileno(), 42, 1), 84)
114expect(os.lseek(f.fileno(), 0, 1), 84)
115expect(os.lseek(f.fileno(), 0, 2), size+1+0)
116expect(os.lseek(f.fileno(), -10, 2), size+1-10)
117expect(os.lseek(f.fileno(), -size-1, 2), 0)
118expect(os.lseek(f.fileno(), size, 0), size)
119expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
120f.close()
121
122
123# XXX add tests for truncate if it exists
124# XXX has truncate ever worked on Windows? specifically on WinNT I get:
125# "IOError: [Errno 13] Permission denied"
126##try:
Fred Drake004d5e62000-10-23 17:22:08 +0000127## newsize = size - 10
128## f.seek(newsize)
129## f.truncate()
130## expect(f.tell(), newsize)
131## newsize = newsize - 1
132## f.seek(0)
133## f.truncate(newsize)
134## expect(f.tell(), newsize)
Trent Mickf29f47b2000-08-11 19:02:59 +0000135##except AttributeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000136## pass
Trent Mickf29f47b2000-08-11 19:02:59 +0000137
138os.unlink(name)