blob: eb31dc3a8d9aa4fb0d3753dab52a74066253d2c7 [file] [log] [blame]
Fred Drake62787992001-05-11 14:29:21 +00001from test_support import verify, TESTFN
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +00002import mmap
Fred Drake62787992001-05-11 14:29:21 +00003import os, re
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +00004
5PAGESIZE = mmap.PAGESIZE
6
Guido van Rossum767e7752000-03-31 01:09:14 +00007def test_both():
8 "Test mmap module on Unix systems and Windows"
Fred Drake004d5e62000-10-23 17:22:08 +00009
Tim Petersfd692082001-05-10 20:03:04 +000010 # Create a file to be mmap'ed.
Fred Drake62787992001-05-11 14:29:21 +000011 if os.path.exists(TESTFN):
12 os.unlink(TESTFN)
Tim Petersfd692082001-05-10 20:03:04 +000013 f = open(TESTFN, 'w+')
Fred Drake004d5e62000-10-23 17:22:08 +000014
Tim Petersfd692082001-05-10 20:03:04 +000015 try: # unlink TESTFN no matter what
16 # Write 2 pages worth of data to the file
17 f.write('\0'* PAGESIZE)
18 f.write('foo')
19 f.write('\0'* (PAGESIZE-3) )
Guido van Rossum767e7752000-03-31 01:09:14 +000020
Tim Petersfd692082001-05-10 20:03:04 +000021 m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
22 f.close()
Fred Drake004d5e62000-10-23 17:22:08 +000023
Tim Petersfd692082001-05-10 20:03:04 +000024 # Simple sanity checks
Tim Peters2caf8df2001-01-14 05:05:51 +000025
Tim Petersfd692082001-05-10 20:03:04 +000026 print type(m) # SF bug 128713: segfaulted on Linux
27 print ' Position of foo:', m.find('foo') / float(PAGESIZE), 'pages'
28 verify(m.find('foo') == PAGESIZE)
Fred Drake004d5e62000-10-23 17:22:08 +000029
Tim Petersfd692082001-05-10 20:03:04 +000030 print ' Length of file:', len(m) / float(PAGESIZE), 'pages'
31 verify(len(m) == 2*PAGESIZE)
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000032
Tim Petersfd692082001-05-10 20:03:04 +000033 print ' Contents of byte 0:', repr(m[0])
34 verify(m[0] == '\0')
35 print ' Contents of first 3 bytes:', repr(m[0:3])
36 verify(m[0:3] == '\0\0\0')
Fred Drake004d5e62000-10-23 17:22:08 +000037
Tim Petersfd692082001-05-10 20:03:04 +000038 # Modify the file's content
39 print "\n Modifying file's content..."
40 m[0] = '3'
Fred Drake62787992001-05-11 14:29:21 +000041 m[PAGESIZE +3: PAGESIZE +3+3] = 'bar'
Fred Drake004d5e62000-10-23 17:22:08 +000042
Tim Petersfd692082001-05-10 20:03:04 +000043 # Check that the modification worked
44 print ' Contents of byte 0:', repr(m[0])
45 verify(m[0] == '3')
46 print ' Contents of first 3 bytes:', repr(m[0:3])
47 verify(m[0:3] == '3\0\0')
48 print ' Contents of second page:', repr(m[PAGESIZE-1 : PAGESIZE + 7])
49 verify(m[PAGESIZE-1 : PAGESIZE + 7] == '\0foobar\0')
Fred Drake004d5e62000-10-23 17:22:08 +000050
Tim Petersfd692082001-05-10 20:03:04 +000051 m.flush()
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000052
Tim Petersfd692082001-05-10 20:03:04 +000053 # Test doing a regular expression match in an mmap'ed file
Fred Drake62787992001-05-11 14:29:21 +000054 match = re.search('[A-Za-z]+', m)
Tim Petersfd692082001-05-10 20:03:04 +000055 if match is None:
56 print ' ERROR: regex match on mmap failed!'
57 else:
58 start, end = match.span(0)
59 length = end - start
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000060
Tim Petersfd692082001-05-10 20:03:04 +000061 print ' Regex match on mmap (page start, length of match):',
62 print start / float(PAGESIZE), length
Fred Drake004d5e62000-10-23 17:22:08 +000063
Tim Petersfd692082001-05-10 20:03:04 +000064 verify(start == PAGESIZE)
65 verify(end == PAGESIZE + 6)
Andrew M. Kuchlingcf70ea62000-06-18 04:47:08 +000066
Tim Petersfd692082001-05-10 20:03:04 +000067 # test seeking around (try to overflow the seek implementation)
68 m.seek(0,0)
69 print ' Seek to zeroth byte'
70 verify(m.tell() == 0)
71 m.seek(42,1)
72 print ' Seek to 42nd byte'
73 verify(m.tell() == 42)
74 m.seek(0,2)
75 print ' Seek to last byte'
76 verify(m.tell() == len(m))
Peter Schneider-Kamp7a116712000-07-11 11:24:41 +000077
Tim Petersfd692082001-05-10 20:03:04 +000078 print ' Try to seek to negative position...'
Thomas Woutersa7015972000-07-30 15:38:35 +000079 try:
Tim Petersfd692082001-05-10 20:03:04 +000080 m.seek(-1)
Thomas Woutersa7015972000-07-30 15:38:35 +000081 except ValueError:
82 pass
83 else:
Tim Petersfd692082001-05-10 20:03:04 +000084 verify(0, 'expected a ValueError but did not get it')
Fred Drake004d5e62000-10-23 17:22:08 +000085
Tim Petersfd692082001-05-10 20:03:04 +000086 print ' Try to seek beyond end of mmap...'
87 try:
88 m.seek(1,2)
89 except ValueError:
90 pass
91 else:
92 verify(0, 'expected a ValueError but did not get it')
93
94 print ' Try to seek to negative position...'
95 try:
96 m.seek(-len(m)-1,2)
97 except ValueError:
98 pass
99 else:
100 verify(0, 'expected a ValueError but did not get it')
101
102 # Try resizing map
103 print ' Attempting resize()'
104 try:
105 m.resize( 512 )
106 except SystemError:
107 # resize() not supported
108 # No messages are printed, since the output of this test suite
109 # would then be different across platforms.
110 pass
111 else:
112 # resize() is supported
113 verify(len(m) == 512,
114 "len(m) is %d, but expecting 512" % (len(m),) )
115 # Check that we can no longer seek beyond the new size.
116 try:
117 m.seek(513,0)
118 except ValueError:
119 pass
120 else:
121 verify(0, 'Could seek beyond the new size')
122
123 m.close()
124
125 finally:
126 try:
127 f.close()
128 except OSError:
129 pass
130 try:
Fred Drake62787992001-05-11 14:29:21 +0000131 os.unlink(TESTFN)
Tim Petersfd692082001-05-10 20:03:04 +0000132 except OSError:
133 pass
134
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +0000135 print ' Test passed'
136
Guido van Rossum767e7752000-03-31 01:09:14 +0000137test_both()