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 |
| 15 | f = open(test_support.TESTFN, 'w') |
| 16 | try: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 17 | # 2**31 == 2147483648 |
| 18 | f.seek(2147483649L) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 19 | except 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 | |
| 33 | # on Windows this test comsumes large resources: |
| 34 | # it takes a long time to build the >2GB file and takes >2GB of disk space |
| 35 | # therefore test_support.use_large_resources must be defined to run this test |
| 36 | if sys.platform[:3] == 'win' and not test_support.use_large_resources: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 37 | raise test_support.TestSkipped, \ |
| 38 | "test requires %s bytes and a long time to run" % str(size) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | |
| 42 | def expect(got_this, expect_this): |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 43 | if test_support.verbose: |
| 44 | print '%s =?= %s ...' % (`got_this`, `expect_this`), |
| 45 | if got_this != expect_this: |
| 46 | if test_support.verbose: |
| 47 | print 'no' |
| 48 | raise test_support.TestFailed, 'got %s, but expected %s' %\ |
| 49 | (str(got_this), str(expect_this)) |
| 50 | else: |
| 51 | if test_support.verbose: |
| 52 | print 'yes' |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | # test that each file function works as expected for a large (i.e. >2GB, do |
| 56 | # we have to check >4GB) files |
| 57 | |
| 58 | if test_support.verbose: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 59 | print 'create large file via seek (may be sparse file) ...' |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 60 | f = open(name, 'w') |
| 61 | f.seek(size) |
| 62 | f.write('a') |
| 63 | f.flush() |
| 64 | expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1) |
| 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' |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 67 | f.close() |
| 68 | if test_support.verbose: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 69 | print 'check file size with os.stat' |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 70 | expect(os.stat(name)[stat.ST_SIZE], size+1) |
| 71 | |
| 72 | if test_support.verbose: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 73 | print 'play around with seek() and read() with the built largefile' |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 74 | f = open(name, 'r') |
| 75 | expect(f.tell(), 0) |
| 76 | expect(f.read(1), '\000') |
| 77 | expect(f.tell(), 1) |
| 78 | f.seek(0) |
| 79 | expect(f.tell(), 0) |
| 80 | f.seek(0, 0) |
| 81 | expect(f.tell(), 0) |
| 82 | f.seek(42) |
| 83 | expect(f.tell(), 42) |
| 84 | f.seek(42, 0) |
| 85 | expect(f.tell(), 42) |
| 86 | f.seek(42, 1) |
| 87 | expect(f.tell(), 84) |
| 88 | f.seek(0, 1) |
| 89 | expect(f.tell(), 84) |
| 90 | f.seek(0, 2) # seek from the end |
| 91 | expect(f.tell(), size + 1 + 0) |
| 92 | f.seek(-10, 2) |
| 93 | expect(f.tell(), size + 1 - 10) |
| 94 | f.seek(-size-1, 2) |
| 95 | expect(f.tell(), 0) |
| 96 | f.seek(size) |
| 97 | expect(f.tell(), size) |
| 98 | expect(f.read(1), 'a') # the 'a' that was written at the end of the file above |
| 99 | f.close() |
| 100 | |
| 101 | if test_support.verbose: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 102 | print 'play around with os.lseek() with the built largefile' |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 103 | f = open(name, 'r') |
| 104 | expect(os.lseek(f.fileno(), 0, 0), 0) |
| 105 | expect(os.lseek(f.fileno(), 42, 0), 42) |
| 106 | expect(os.lseek(f.fileno(), 42, 1), 84) |
| 107 | expect(os.lseek(f.fileno(), 0, 1), 84) |
| 108 | expect(os.lseek(f.fileno(), 0, 2), size+1+0) |
| 109 | expect(os.lseek(f.fileno(), -10, 2), size+1-10) |
| 110 | expect(os.lseek(f.fileno(), -size-1, 2), 0) |
| 111 | expect(os.lseek(f.fileno(), size, 0), size) |
| 112 | expect(f.read(1), 'a') # the 'a' that was written at the end of the file above |
| 113 | f.close() |
| 114 | |
| 115 | |
| 116 | # XXX add tests for truncate if it exists |
| 117 | # XXX has truncate ever worked on Windows? specifically on WinNT I get: |
| 118 | # "IOError: [Errno 13] Permission denied" |
| 119 | ##try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 120 | ## newsize = size - 10 |
| 121 | ## f.seek(newsize) |
| 122 | ## f.truncate() |
| 123 | ## expect(f.tell(), newsize) |
| 124 | ## newsize = newsize - 1 |
| 125 | ## f.seek(0) |
| 126 | ## f.truncate(newsize) |
| 127 | ## expect(f.tell(), newsize) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 128 | ##except AttributeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 129 | ## pass |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 130 | |
| 131 | os.unlink(name) |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 132 | print >>sys.stderr, name, "exists:", os.path.exists(name) |