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