summaryrefslogtreecommitdiff
path: root/lib/python2.7/ctypes/test/test_memfunctions.py
blob: d0726032856e2d6330f0ef35d4c9d790dc604a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import sys
import unittest
from ctypes import *

class MemFunctionsTest(unittest.TestCase):
##    def test_overflow(self):
##        # string_at and wstring_at must use the Python calling
##        # convention (which acquires the GIL and checks the Python
##        # error flag).  Provoke an error and catch it; see also issue
##        # #3554: <http://bugs.python.org/issue3554>
##        self.assertRaises((OverflowError, MemoryError, SystemError),
##                          lambda: wstring_at(u"foo", sys.maxint - 1))
##        self.assertRaises((OverflowError, MemoryError, SystemError),
##                          lambda: string_at("foo", sys.maxint - 1))

    def test_memmove(self):
        # large buffers apparently increase the chance that the memory
        # is allocated in high address space.
        a = create_string_buffer(1000000)
        p = "Hello, World"
        result = memmove(a, p, len(p))
        self.assertEqual(a.value, "Hello, World")

        self.assertEqual(string_at(result), "Hello, World")
        self.assertEqual(string_at(result, 5), "Hello")
        self.assertEqual(string_at(result, 16), "Hello, World\0\0\0\0")
        self.assertEqual(string_at(result, 0), "")

    def test_memset(self):
        a = create_string_buffer(1000000)
        result = memset(a, ord('x'), 16)
        self.assertEqual(a.value, "xxxxxxxxxxxxxxxx")

        self.assertEqual(string_at(result), "xxxxxxxxxxxxxxxx")
        self.assertEqual(string_at(a), "xxxxxxxxxxxxxxxx")
        self.assertEqual(string_at(a, 20), "xxxxxxxxxxxxxxxx\0\0\0\0")

    def test_cast(self):
        a = (c_ubyte * 32)(*map(ord, "abcdef"))
        self.assertEqual(cast(a, c_char_p).value, "abcdef")
        self.assertEqual(cast(a, POINTER(c_byte))[:7],
                             [97, 98, 99, 100, 101, 102, 0])
        self.assertEqual(cast(a, POINTER(c_byte))[:7:],
                             [97, 98, 99, 100, 101, 102, 0])
        self.assertEqual(cast(a, POINTER(c_byte))[6:-1:-1],
                             [0, 102, 101, 100, 99, 98, 97])
        self.assertEqual(cast(a, POINTER(c_byte))[:7:2],
                             [97, 99, 101, 0])
        self.assertEqual(cast(a, POINTER(c_byte))[:7:7],
                             [97])

    def test_string_at(self):
        s = string_at("foo bar")
        # XXX The following may be wrong, depending on how Python
        # manages string instances
        self.assertEqual(2, sys.getrefcount(s))
        self.assertTrue(s, "foo bar")

        self.assertEqual(string_at("foo bar", 8), "foo bar\0")
        self.assertEqual(string_at("foo bar", 3), "foo")

    try:
        create_unicode_buffer
    except NameError:
        pass
    else:
        def test_wstring_at(self):
            p = create_unicode_buffer("Hello, World")
            a = create_unicode_buffer(1000000)
            result = memmove(a, p, len(p) * sizeof(c_wchar))
            self.assertEqual(a.value, "Hello, World")

            self.assertEqual(wstring_at(a), "Hello, World")
            self.assertEqual(wstring_at(a, 5), "Hello")
            self.assertEqual(wstring_at(a, 16), "Hello, World\0\0\0\0")
            self.assertEqual(wstring_at(a, 0), "")

if __name__ == "__main__":
    unittest.main()