Back to index

python3.2  3.2.2
test_simplesubclasses.py
Go to the documentation of this file.
00001 import unittest
00002 from ctypes import *
00003 
00004 class MyInt(c_int):
00005     def __eq__(self, other):
00006         if type(other) != MyInt:
00007             return NotImplementedError
00008         return self.value == other.value
00009 
00010 class Test(unittest.TestCase):
00011 
00012     def test_compare(self):
00013         self.assertEqual(MyInt(3), MyInt(3))
00014         self.assertNotEqual(MyInt(42), MyInt(43))
00015 
00016     def test_ignore_retval(self):
00017         # Test if the return value of a callback is ignored
00018         # if restype is None
00019         proto = CFUNCTYPE(None)
00020         def func():
00021             return (1, "abc", None)
00022 
00023         cb = proto(func)
00024         self.assertEqual(None, cb())
00025 
00026 
00027     def test_int_callback(self):
00028         args = []
00029         def func(arg):
00030             args.append(arg)
00031             return arg
00032 
00033         cb = CFUNCTYPE(None, MyInt)(func)
00034 
00035         self.assertEqual(None, cb(42))
00036         self.assertEqual(type(args[-1]), MyInt)
00037 
00038         cb = CFUNCTYPE(c_int, c_int)(func)
00039 
00040         self.assertEqual(42, cb(42))
00041         self.assertEqual(type(args[-1]), int)
00042 
00043     def test_int_struct(self):
00044         class X(Structure):
00045             _fields_ = [("x", MyInt)]
00046 
00047         self.assertEqual(X().x, MyInt())
00048 
00049         s = X()
00050         s.x = MyInt(42)
00051 
00052         self.assertEqual(s.x, MyInt(42))
00053 
00054 if __name__ == "__main__":
00055     unittest.main()