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