Home | History | Annotate | Download | only in test
      1 import unittest
      2 
      3 from ctypes import *
      4 import _ctypes_test
      5 
      6 lib = CDLL(_ctypes_test.__file__)
      7 
      8 class LibTest(unittest.TestCase):
      9     def test_sqrt(self):
     10         lib.my_sqrt.argtypes = c_double,
     11         lib.my_sqrt.restype = c_double
     12         self.assertEqual(lib.my_sqrt(4.0), 2.0)
     13         import math
     14         self.assertEqual(lib.my_sqrt(2.0), math.sqrt(2.0))
     15 
     16     def test_qsort(self):
     17         comparefunc = CFUNCTYPE(c_int, POINTER(c_char), POINTER(c_char))
     18         lib.my_qsort.argtypes = c_void_p, c_size_t, c_size_t, comparefunc
     19         lib.my_qsort.restype = None
     20 
     21         def sort(a, b):
     22             return cmp(a[0], b[0])
     23 
     24         chars = create_string_buffer("spam, spam, and spam")
     25         lib.my_qsort(chars, len(chars)-1, sizeof(c_char), comparefunc(sort))
     26         self.assertEqual(chars.raw, "   ,,aaaadmmmnpppsss\x00")
     27 
     28 if __name__ == "__main__":
     29     unittest.main()
     30