MCPcopy Index your code
hub / github.com/python/cpython / test_basic

Method test_basic

Lib/test/test_mmap.py:49–117  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

47 pass
48
49 def test_basic(self):
50 # Test mmap module on Unix systems and Windows
51
52 # Create a file to be mmap'ed.
53 f = open(TESTFN, 'bw+')
54 try:
55 # Write 2 pages worth of data to the file
56 f.write(b'\0'* PAGESIZE)
57 f.write(b'foo')
58 f.write(b'\0'* (PAGESIZE-3) )
59 f.flush()
60 m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
61 self.addCleanup(m.close)
62 finally:
63 f.close()
64
65 # Simple sanity checks
66
67 tp = str(type(m)) # SF bug 128713: segfaulted on Linux
68 self.assertEqual(m.find(b'foo'), PAGESIZE)
69
70 self.assertEqual(len(m), 2*PAGESIZE)
71
72 self.assertEqual(m[0], 0)
73 self.assertEqual(m[0:3], b'\0\0\0')
74
75 # Shouldn't crash on boundary (Issue #5292)
76 self.assertRaises(IndexError, m.__getitem__, len(m))
77 self.assertRaises(IndexError, m.__setitem__, len(m), b'\0')
78
79 # Modify the file's content
80 m[0] = b'3'[0]
81 m[PAGESIZE +3: PAGESIZE +3+3] = b'bar'
82
83 # Check that the modification worked
84 self.assertEqual(m[0], b'3'[0])
85 self.assertEqual(m[0:3], b'3\0\0')
86 self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], b'\0foobar\0')
87
88 m.flush()
89
90 # Test doing a regular expression match in an mmap'ed file
91 match = re.search(b'[A-Za-z]+', m)
92 if match is None:
93 self.fail('regex match on mmap failed!')
94 else:
95 start, end = match.span(0)
96 length = end - start
97
98 self.assertEqual(start, PAGESIZE)
99 self.assertEqual(end, PAGESIZE + 6)
100
101 # test seeking around (try to overflow the seek implementation)
102 self.assertTrue(m.seekable())
103 self.assertEqual(m.seek(0, 0), 0)
104 self.assertEqual(m.tell(), 0)
105 self.assertEqual(m.seek(42, 1), 42)
106 self.assertEqual(m.tell(), 42)

Callers

nothing calls this directly

Calls 15

strFunction · 0.85
addCleanupMethod · 0.80
assertTrueMethod · 0.80
openFunction · 0.50
writeMethod · 0.45
flushMethod · 0.45
filenoMethod · 0.45
closeMethod · 0.45
assertEqualMethod · 0.45
findMethod · 0.45
assertRaisesMethod · 0.45
searchMethod · 0.45

Tested by

no test coverage detected