ยปCore Development>Code coverage>Lib/ctypes/test/test_memfunctions.py

Python code coverage for Lib/ctypes/test/test_memfunctions.py

#countcontent
1n/aimport sys
2n/afrom test import support
3n/aimport unittest
4n/afrom ctypes import *
5n/afrom ctypes.test import need_symbol
6n/a
7n/aclass MemFunctionsTest(unittest.TestCase):
8n/a @unittest.skip('test disabled')
9n/a def test_overflow(self):
10n/a # string_at and wstring_at must use the Python calling
11n/a # convention (which acquires the GIL and checks the Python
12n/a # error flag). Provoke an error and catch it; see also issue
13n/a # #3554: <http://bugs.python.org/issue3554>
14n/a self.assertRaises((OverflowError, MemoryError, SystemError),
15n/a lambda: wstring_at(u"foo", sys.maxint - 1))
16n/a self.assertRaises((OverflowError, MemoryError, SystemError),
17n/a lambda: string_at("foo", sys.maxint - 1))
18n/a
19n/a def test_memmove(self):
20n/a # large buffers apparently increase the chance that the memory
21n/a # is allocated in high address space.
22n/a a = create_string_buffer(1000000)
23n/a p = b"Hello, World"
24n/a result = memmove(a, p, len(p))
25n/a self.assertEqual(a.value, b"Hello, World")
26n/a
27n/a self.assertEqual(string_at(result), b"Hello, World")
28n/a self.assertEqual(string_at(result, 5), b"Hello")
29n/a self.assertEqual(string_at(result, 16), b"Hello, World\0\0\0\0")
30n/a self.assertEqual(string_at(result, 0), b"")
31n/a
32n/a def test_memset(self):
33n/a a = create_string_buffer(1000000)
34n/a result = memset(a, ord('x'), 16)
35n/a self.assertEqual(a.value, b"xxxxxxxxxxxxxxxx")
36n/a
37n/a self.assertEqual(string_at(result), b"xxxxxxxxxxxxxxxx")
38n/a self.assertEqual(string_at(a), b"xxxxxxxxxxxxxxxx")
39n/a self.assertEqual(string_at(a, 20), b"xxxxxxxxxxxxxxxx\0\0\0\0")
40n/a
41n/a def test_cast(self):
42n/a a = (c_ubyte * 32)(*map(ord, "abcdef"))
43n/a self.assertEqual(cast(a, c_char_p).value, b"abcdef")
44n/a self.assertEqual(cast(a, POINTER(c_byte))[:7],
45n/a [97, 98, 99, 100, 101, 102, 0])
46n/a self.assertEqual(cast(a, POINTER(c_byte))[:7:],
47n/a [97, 98, 99, 100, 101, 102, 0])
48n/a self.assertEqual(cast(a, POINTER(c_byte))[6:-1:-1],
49n/a [0, 102, 101, 100, 99, 98, 97])
50n/a self.assertEqual(cast(a, POINTER(c_byte))[:7:2],
51n/a [97, 99, 101, 0])
52n/a self.assertEqual(cast(a, POINTER(c_byte))[:7:7],
53n/a [97])
54n/a
55n/a @support.refcount_test
56n/a def test_string_at(self):
57n/a s = string_at(b"foo bar")
58n/a # XXX The following may be wrong, depending on how Python
59n/a # manages string instances
60n/a self.assertEqual(2, sys.getrefcount(s))
61n/a self.assertTrue(s, "foo bar")
62n/a
63n/a self.assertEqual(string_at(b"foo bar", 7), b"foo bar")
64n/a self.assertEqual(string_at(b"foo bar", 3), b"foo")
65n/a
66n/a @need_symbol('create_unicode_buffer')
67n/a def test_wstring_at(self):
68n/a p = create_unicode_buffer("Hello, World")
69n/a a = create_unicode_buffer(1000000)
70n/a result = memmove(a, p, len(p) * sizeof(c_wchar))
71n/a self.assertEqual(a.value, "Hello, World")
72n/a
73n/a self.assertEqual(wstring_at(a), "Hello, World")
74n/a self.assertEqual(wstring_at(a, 5), "Hello")
75n/a self.assertEqual(wstring_at(a, 16), "Hello, World\0\0\0\0")
76n/a self.assertEqual(wstring_at(a, 0), "")
77n/a
78n/aif __name__ == "__main__":
79n/a unittest.main()