Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 1 | |
| 2 | import mmap |
| 3 | import string, os, re, sys |
| 4 | |
| 5 | PAGESIZE = mmap.PAGESIZE |
| 6 | |
Guido van Rossum | 767e775 | 2000-03-31 01:09:14 +0000 | [diff] [blame] | 7 | def test_both(): |
| 8 | "Test mmap module on Unix systems and Windows" |
| 9 | |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 10 | # Create an mmap'ed file |
| 11 | f = open('foo', 'w+') |
| 12 | |
| 13 | # Write 2 pages worth of data to the file |
| 14 | f.write('\0'* PAGESIZE) |
| 15 | f.write('foo') |
| 16 | f.write('\0'* (PAGESIZE-3) ) |
Guido van Rossum | 767e775 | 2000-03-31 01:09:14 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 706dbd0 | 2000-03-31 01:20:33 +0000 | [diff] [blame] | 18 | m = mmap.mmap(f.fileno(), 2 * PAGESIZE) |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 19 | f.close() |
| 20 | |
| 21 | # Simple sanity checks |
| 22 | print ' Position of foo:', string.find(m, 'foo') / float(PAGESIZE), 'pages' |
| 23 | assert string.find(m, 'foo') == PAGESIZE |
| 24 | |
| 25 | print ' Length of file:', len(m) / float(PAGESIZE), 'pages' |
| 26 | assert len(m) == 2*PAGESIZE |
| 27 | |
| 28 | print ' Contents of byte 0:', repr(m[0]) |
| 29 | assert m[0] == '\0' |
| 30 | print ' Contents of first 3 bytes:', repr(m[0:3]) |
| 31 | assert m[0:3] == '\0\0\0' |
| 32 | |
| 33 | # Modify the file's content |
| 34 | print "\n Modifying file's content..." |
| 35 | m[0] = '3' |
| 36 | m[PAGESIZE +3: PAGESIZE +3+3]='bar' |
| 37 | |
| 38 | # Check that the modification worked |
| 39 | print ' Contents of byte 0:', repr(m[0]) |
| 40 | assert m[0] == '3' |
| 41 | print ' Contents of first 3 bytes:', repr(m[0:3]) |
| 42 | assert m[0:3] == '3\0\0' |
| 43 | print ' Contents of second page:', m[PAGESIZE-1 : PAGESIZE + 7] |
| 44 | assert m[PAGESIZE-1 : PAGESIZE + 7] == '\0foobar\0' |
| 45 | |
| 46 | m.flush() |
| 47 | |
| 48 | # Test doing a regular expression match in an mmap'ed file |
| 49 | match=re.search('[A-Za-z]+', m) |
| 50 | if match == None: |
| 51 | print ' ERROR: regex match on mmap failed!' |
| 52 | else: |
| 53 | start, end = match.span(0) |
| 54 | length = end - start |
| 55 | |
| 56 | print ' Regex match on mmap (page start, length of match):', |
| 57 | print start / float(PAGESIZE), length |
| 58 | |
| 59 | assert start == PAGESIZE |
| 60 | assert end == PAGESIZE + 6 |
Andrew M. Kuchling | cf70ea6 | 2000-06-18 04:47:08 +0000 | [diff] [blame] | 61 | |
| 62 | # test seeking around (try to overflow the seek implementation) |
| 63 | m.seek(0,0) |
| 64 | print ' Seek to zeroth byte' |
| 65 | assert m.tell() == 0 |
| 66 | m.seek(42,1) |
| 67 | print ' Seek to 42nd byte' |
| 68 | assert m.tell() == 42 |
| 69 | m.seek(0,2) |
| 70 | print ' Seek to last byte' |
| 71 | assert m.tell() == len(m) |
Peter Schneider-Kamp | 7a11671 | 2000-07-11 11:24:41 +0000 | [diff] [blame^] | 72 | |
Andrew M. Kuchling | cf70ea6 | 2000-06-18 04:47:08 +0000 | [diff] [blame] | 73 | print ' Try to seek to negative position...' |
| 74 | try: |
| 75 | m.seek(-1) |
| 76 | except ValueError: |
| 77 | pass |
| 78 | else: |
| 79 | assert 0, 'expected a ValueError but did not get it' |
| 80 | |
| 81 | print ' Try to seek beyond end of mmap...' |
| 82 | try: |
| 83 | m.seek(1,2) |
| 84 | except ValueError: |
| 85 | pass |
| 86 | else: |
| 87 | assert 0, 'expected a ValueError but did not get it' |
| 88 | |
| 89 | print ' Try to seek to negative position...' |
| 90 | try: |
| 91 | m.seek(-len(m)-1,2) |
| 92 | except ValueError: |
| 93 | pass |
| 94 | else: |
| 95 | assert 0, 'expected a ValueError but did not get it' |
| 96 | |
Andrew M. Kuchling | d3cf692 | 2000-07-11 10:45:28 +0000 | [diff] [blame] | 97 | # Try resizing map |
| 98 | print ' Attempting resize()' |
| 99 | try: |
Peter Schneider-Kamp | 7a11671 | 2000-07-11 11:24:41 +0000 | [diff] [blame^] | 100 | m.resize( 512 ) |
Andrew M. Kuchling | d3cf692 | 2000-07-11 10:45:28 +0000 | [diff] [blame] | 101 | except SystemError: |
| 102 | # resize() not supported |
| 103 | # No messages are printed, since the output of this test suite |
| 104 | # would then be different across platforms. |
| 105 | pass |
| 106 | else: |
Peter Schneider-Kamp | 7a11671 | 2000-07-11 11:24:41 +0000 | [diff] [blame^] | 107 | # resize() is supported |
Andrew M. Kuchling | d3cf692 | 2000-07-11 10:45:28 +0000 | [diff] [blame] | 108 | pass |
| 109 | |
Fred Drake | 605843f | 2000-04-05 14:17:11 +0000 | [diff] [blame] | 110 | m.close() |
| 111 | os.unlink("foo") |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 112 | print ' Test passed' |
| 113 | |
Guido van Rossum | 767e775 | 2000-03-31 01:09:14 +0000 | [diff] [blame] | 114 | test_both() |
| 115 | |