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