blob: 7c49cf2acb04c024ed048de7725bd4c2be90a053 [file] [log] [blame]
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +00001import mmap
2import string, os, re, sys
3
4PAGESIZE = mmap.PAGESIZE
5
Guido van Rossum767e7752000-03-31 01:09:14 +00006def test_both():
7 "Test mmap module on Unix systems and Windows"
Fred Drake004d5e62000-10-23 17:22:08 +00008
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +00009 # Create an mmap'ed file
10 f = open('foo', 'w+')
Fred Drake004d5e62000-10-23 17:22:08 +000011
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000012 # 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 Rossum767e7752000-03-31 01:09:14 +000016
Guido van Rossum706dbd02000-03-31 01:20:33 +000017 m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000018 f.close()
Fred Drake004d5e62000-10-23 17:22:08 +000019
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000020 # Simple sanity checks
Tim Peters2caf8df2001-01-14 05:05:51 +000021
22 print type(m) # SF bug 128713: segfaulted on Linux
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000023 print ' Position of foo:', string.find(m, 'foo') / float(PAGESIZE), 'pages'
24 assert string.find(m, 'foo') == PAGESIZE
Fred Drake004d5e62000-10-23 17:22:08 +000025
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000026 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 Drake004d5e62000-10-23 17:22:08 +000033
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000034 # 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 Drake004d5e62000-10-23 17:22:08 +000038
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000039 # 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 Peters50699212000-09-04 07:34:06 +000044 print ' Contents of second page:', repr(m[PAGESIZE-1 : PAGESIZE + 7])
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000045 assert m[PAGESIZE-1 : PAGESIZE + 7] == '\0foobar\0'
Fred Drake004d5e62000-10-23 17:22:08 +000046
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000047 m.flush()
48
49 # Test doing a regular expression match in an mmap'ed file
50 match=re.search('[A-Za-z]+', m)
Fred Drake132dce22000-12-12 23:11:42 +000051 if match is None:
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000052 print ' ERROR: regex match on mmap failed!'
53 else:
54 start, end = match.span(0)
Fred Drake004d5e62000-10-23 17:22:08 +000055 length = end - start
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000056
57 print ' Regex match on mmap (page start, length of match):',
58 print start / float(PAGESIZE), length
Fred Drake004d5e62000-10-23 17:22:08 +000059
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000060 assert start == PAGESIZE
61 assert end == PAGESIZE + 6
Andrew M. Kuchlingcf70ea62000-06-18 04:47:08 +000062
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-Kamp7a116712000-07-11 11:24:41 +000073
Andrew M. Kuchlingcf70ea62000-06-18 04:47:08 +000074 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. Kuchlingd3cf6922000-07-11 10:45:28 +000098 # Try resizing map
99 print ' Attempting resize()'
100 try:
Peter Schneider-Kamp7a116712000-07-11 11:24:41 +0000101 m.resize( 512 )
Andrew M. Kuchlingd3cf6922000-07-11 10:45:28 +0000102 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-Kamp7a116712000-07-11 11:24:41 +0000108 # resize() is supported
Thomas Woutersa7015972000-07-30 15:38:35 +0000109 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 Drake004d5e62000-10-23 17:22:08 +0000117
Fred Drake605843f2000-04-05 14:17:11 +0000118 m.close()
119 os.unlink("foo")
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +0000120 print ' Test passed'
121
Guido van Rossum767e7752000-03-31 01:09:14 +0000122test_both()