• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2from ctypes import *
3
4class MyInt(c_int):
5    def __eq__(self, other):
6        if type(other) != MyInt:
7            return NotImplementedError
8        return self.value == other.value
9
10class Test(unittest.TestCase):
11
12    def test_compare(self):
13        self.assertEqual(MyInt(3), MyInt(3))
14        self.assertNotEqual(MyInt(42), MyInt(43))
15
16    def test_ignore_retval(self):
17        # Test if the return value of a callback is ignored
18        # if restype is None
19        proto = CFUNCTYPE(None)
20        def func():
21            return (1, "abc", None)
22
23        cb = proto(func)
24        self.assertEqual(None, cb())
25
26
27    def test_int_callback(self):
28        args = []
29        def func(arg):
30            args.append(arg)
31            return arg
32
33        cb = CFUNCTYPE(None, MyInt)(func)
34
35        self.assertEqual(None, cb(42))
36        self.assertEqual(type(args[-1]), MyInt)
37
38        cb = CFUNCTYPE(c_int, c_int)(func)
39
40        self.assertEqual(42, cb(42))
41        self.assertEqual(type(args[-1]), int)
42
43    def test_int_struct(self):
44        class X(Structure):
45            _fields_ = [("x", MyInt)]
46
47        self.assertEqual(X().x, MyInt())
48
49        s = X()
50        s.x = MyInt(42)
51
52        self.assertEqual(s.x, MyInt(42))
53
54if __name__ == "__main__":
55    unittest.main()
56