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