Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 1 | #!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 | |
| 10 | import test_support |
| 11 | import os, struct, stat, sys |
| 12 | |
| 13 | |
| 14 | # only run if the current system support large files |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame^] | 15 | f = open(test_support.TESTFN, 'wb') |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 16 | try: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 17 | # 2**31 == 2147483648 |
| 18 | f.seek(2147483649L) |
Guido van Rossum | a5af214 | 2001-04-10 14:50:51 +0000 | [diff] [blame] | 19 | except (IOError, OverflowError): |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 20 | f.close() |
| 21 | os.unlink(test_support.TESTFN) |
| 22 | raise test_support.TestSkipped, \ |
| 23 | "platform does not have largefile support" |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 24 | else: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 25 | f.close() |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | # create >2GB file (2GB = 2147483648 bytes) |
| 29 | size = 2500000000L |
| 30 | name = test_support.TESTFN |
| 31 | |
| 32 | |
Barry Warsaw | 7fdfc38 | 2001-08-20 22:37:34 +0000 | [diff] [blame] | 33 | # 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. |
| 37 | if sys.platform[:3] == 'win': |
| 38 | test_support.requires( |
| 39 | 'largefile', |
| 40 | 'test requires %s bytes and a long time to run' % str(size)) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def expect(got_this, expect_this): |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 44 | 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 Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 54 | |
| 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 | |
| 59 | if test_support.verbose: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 60 | print 'create large file via seek (may be sparse file) ...' |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame^] | 61 | f = open(name, 'wb') |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 62 | f.seek(size) |
| 63 | f.write('a') |
| 64 | f.flush() |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 65 | if test_support.verbose: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 66 | print 'check file size with os.fstat' |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame^] | 67 | expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 68 | f.close() |
| 69 | if test_support.verbose: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 70 | print 'check file size with os.stat' |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 71 | expect(os.stat(name)[stat.ST_SIZE], size+1) |
| 72 | |
| 73 | if test_support.verbose: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 74 | print 'play around with seek() and read() with the built largefile' |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame^] | 75 | f = open(name, 'rb') |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 76 | expect(f.tell(), 0) |
| 77 | expect(f.read(1), '\000') |
| 78 | expect(f.tell(), 1) |
| 79 | f.seek(0) |
| 80 | expect(f.tell(), 0) |
| 81 | f.seek(0, 0) |
| 82 | expect(f.tell(), 0) |
| 83 | f.seek(42) |
| 84 | expect(f.tell(), 42) |
| 85 | f.seek(42, 0) |
| 86 | expect(f.tell(), 42) |
| 87 | f.seek(42, 1) |
| 88 | expect(f.tell(), 84) |
| 89 | f.seek(0, 1) |
| 90 | expect(f.tell(), 84) |
| 91 | f.seek(0, 2) # seek from the end |
| 92 | expect(f.tell(), size + 1 + 0) |
| 93 | f.seek(-10, 2) |
| 94 | expect(f.tell(), size + 1 - 10) |
| 95 | f.seek(-size-1, 2) |
| 96 | expect(f.tell(), 0) |
| 97 | f.seek(size) |
| 98 | expect(f.tell(), size) |
| 99 | expect(f.read(1), 'a') # the 'a' that was written at the end of the file above |
| 100 | f.close() |
| 101 | |
| 102 | if test_support.verbose: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 103 | print 'play around with os.lseek() with the built largefile' |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 104 | f = open(name, 'r') |
| 105 | expect(os.lseek(f.fileno(), 0, 0), 0) |
| 106 | expect(os.lseek(f.fileno(), 42, 0), 42) |
| 107 | expect(os.lseek(f.fileno(), 42, 1), 84) |
| 108 | expect(os.lseek(f.fileno(), 0, 1), 84) |
| 109 | expect(os.lseek(f.fileno(), 0, 2), size+1+0) |
| 110 | expect(os.lseek(f.fileno(), -10, 2), size+1-10) |
| 111 | expect(os.lseek(f.fileno(), -size-1, 2), 0) |
| 112 | expect(os.lseek(f.fileno(), size, 0), size) |
| 113 | expect(f.read(1), 'a') # the 'a' that was written at the end of the file above |
| 114 | f.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 Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 121 | ## 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 Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 129 | ##except AttributeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 130 | ## pass |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 131 | |
| 132 | os.unlink(name) |