blob: 3e2d89fa3adee69cb867d7ce9b008217941878c1 [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
21 print ' Position of foo:', string.find(m, 'foo') / float(PAGESIZE), 'pages'
22 assert string.find(m, 'foo') == PAGESIZE
Fred Drake004d5e62000-10-23 17:22:08 +000023
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000024 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 Drake004d5e62000-10-23 17:22:08 +000031
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000032 # 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 Drake004d5e62000-10-23 17:22:08 +000036
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000037 # 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 Peters50699212000-09-04 07:34:06 +000042 print ' Contents of second page:', repr(m[PAGESIZE-1 : PAGESIZE + 7])
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000043 assert m[PAGESIZE-1 : PAGESIZE + 7] == '\0foobar\0'
Fred Drake004d5e62000-10-23 17:22:08 +000044
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000045 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 Drake004d5e62000-10-23 17:22:08 +000053 length = end - start
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000054
55 print ' Regex match on mmap (page start, length of match):',
56 print start / float(PAGESIZE), length
Fred Drake004d5e62000-10-23 17:22:08 +000057
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +000058 assert start == PAGESIZE
59 assert end == PAGESIZE + 6
Andrew M. Kuchlingcf70ea62000-06-18 04:47:08 +000060
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-Kamp7a116712000-07-11 11:24:41 +000071
Andrew M. Kuchlingcf70ea62000-06-18 04:47:08 +000072 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. Kuchlingd3cf6922000-07-11 10:45:28 +000096 # Try resizing map
97 print ' Attempting resize()'
98 try:
Peter Schneider-Kamp7a116712000-07-11 11:24:41 +000099 m.resize( 512 )
Andrew M. Kuchlingd3cf6922000-07-11 10:45:28 +0000100 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-Kamp7a116712000-07-11 11:24:41 +0000106 # resize() is supported
Thomas Woutersa7015972000-07-30 15:38:35 +0000107 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 Drake004d5e62000-10-23 17:22:08 +0000115
Fred Drake605843f2000-04-05 14:17:11 +0000116 m.close()
117 os.unlink("foo")
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +0000118 print ' Test passed'
119
Guido van Rossum767e7752000-03-31 01:09:14 +0000120test_both()