MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_basic

Method test_basic

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 15

strFunction · 0.85
lenFunction · 0.85
spanMethod · 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