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