File: test_ctypes.py

package info (click to toggle)
mpi4py 4.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,544 kB
  • sloc: python: 34,453; ansic: 16,475; makefile: 614; sh: 325; cpp: 193; f90: 178
file content (65 lines) | stat: -rw-r--r-- 1,734 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import mpiunittest as unittest

from mpi4py import MPI

try:
    import ctypes
except ImportError:
    ctypes = None


@unittest.skipIf(ctypes is None, "ctypes")
class TestCTYPES(unittest.TestCase):
    #
    objects = [
        MPI.DATATYPE_NULL,
        MPI.INT,
        MPI.DOUBLE,
        MPI.REQUEST_NULL,
        MPI.INFO_NULL,
        MPI.INFO_ENV,
        MPI.ERRHANDLER_NULL,
        MPI.ERRORS_RETURN,
        MPI.ERRORS_ARE_FATAL,
        MPI.GROUP_NULL,
        MPI.GROUP_EMPTY,
        MPI.WIN_NULL,
        MPI.OP_NULL,
        MPI.SUM,
        MPI.MIN,
        MPI.MAX,
        MPI.FILE_NULL,
        MPI.MESSAGE_NULL,
        MPI.MESSAGE_NO_PROC,
        MPI.COMM_NULL,
        MPI.COMM_SELF,
        MPI.COMM_WORLD,
    ]

    def testHandleAdress(self):
        typemap = {
            ctypes.sizeof(ctypes.c_int): ctypes.c_int,
            ctypes.sizeof(ctypes.c_void_p): ctypes.c_void_p,
        }
        for obj in self.objects:
            handle_t = typemap[MPI._sizeof(obj)]
            oldobj = obj
            newobj = type(obj)()
            handle_old = handle_t.from_address(MPI._addressof(oldobj))
            handle_new = handle_t.from_address(MPI._addressof(newobj))
            handle_new.value = handle_old.value
            self.assertEqual(obj, newobj)

    def testHandleValue(self):
        typemap = {
            ctypes.sizeof(ctypes.c_uint32): ctypes.c_uint32,
            ctypes.sizeof(ctypes.c_uint64): ctypes.c_uint64,
        }
        for obj in self.objects:
            uintptr_t = typemap[MPI._sizeof(obj)]
            handle = uintptr_t.from_address(MPI._addressof(obj))
            self.assertEqual(handle.value, MPI._handleof(obj))


if __name__ == "__main__":
    unittest.main()