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