Пример #1
0
 def test_arrayoffloat(self):
     a = lltype.malloc(rffi.FLOATP.TO, 3, flavor='raw')
     a[0] = rffi.r_singlefloat(0.0)
     a[1] = rffi.r_singlefloat(1.1)
     a[2] = rffi.r_singlefloat(2.2)
     ac = lltype2ctypes(a, normalize=False)
     assert ac.contents.items[0] == 0.0
     assert abs(ac.contents.items[1] - 1.1) < 1E-6
     assert abs(ac.contents.items[2] - 2.2) < 1E-6
     b = ctypes2lltype(rffi.FLOATP, ac)
     assert isinstance(b[0], rffi.r_singlefloat)
     assert float(b[0]) == 0.0
     assert isinstance(b[1], rffi.r_singlefloat)
     assert abs(float(b[1]) - 1.1) < 1E-6
     assert isinstance(b[2], rffi.r_singlefloat)
     assert abs(float(b[2]) - 2.2) < 1E-6
Пример #2
0
 def test_arrayoffloat(self):
     a = lltype.malloc(rffi.FLOATP.TO, 3, flavor='raw')
     a[0] = rffi.r_singlefloat(0.0)
     a[1] = rffi.r_singlefloat(1.1)
     a[2] = rffi.r_singlefloat(2.2)
     ac = lltype2ctypes(a, normalize=False)
     assert ac.contents.items[0] == 0.0
     assert abs(ac.contents.items[1] - 1.1) < 1E-6
     assert abs(ac.contents.items[2] - 2.2) < 1E-6
     b = ctypes2lltype(rffi.FLOATP, ac)
     assert isinstance(b[0], rffi.r_singlefloat)
     assert float(b[0]) == 0.0
     assert isinstance(b[1], rffi.r_singlefloat)
     assert abs(float(b[1]) - 1.1) < 1E-6
     assert isinstance(b[2], rffi.r_singlefloat)
     assert abs(float(b[2]) - 2.2) < 1E-6
Пример #3
0
    def test_primitive(self):
        assert lltype2ctypes(5) == 5
        assert lltype2ctypes('?') == ord('?')
        assert lltype2ctypes('\xE0') == 0xE0
        assert lltype2ctypes(unichr(1234)) == 1234
        assert ctypes2lltype(lltype.Signed, 5) == 5
        assert ctypes2lltype(lltype.Char, ord('a')) == 'a'
        assert ctypes2lltype(lltype.UniChar, ord(u'x')) == u'x'
        assert ctypes2lltype(lltype.Char, 0xFF) == '\xFF'
        assert lltype2ctypes(5.25) == 5.25
        assert ctypes2lltype(lltype.Float, 5.25) == 5.25
        assert lltype2ctypes(u'x') == ord(u'x')
        res = lltype2ctypes(rffi.r_singlefloat(-3.5))
        assert isinstance(res, ctypes.c_float)
        assert res.value == -3.5
        res = ctypes2lltype(lltype.SingleFloat, ctypes.c_float(-3.5))
        assert isinstance(res, rffi.r_singlefloat)
        assert float(res) == -3.5
        assert lltype2ctypes(rffi.r_ulong(-1)) == sys.maxint * 2 + 1
        res = ctypes2lltype(lltype.Unsigned, sys.maxint * 2 + 1)
        assert (res, type(res)) == (rffi.r_ulong(-1), rffi.r_ulong)

        res = lltype2ctypes(llmemory.sizeof(lltype.Signed))
        assert res == struct.calcsize("l")
        S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed))
        res = lltype2ctypes(llmemory.sizeof(S))
        assert res == struct.calcsize("ll")

        p = lltype.nullptr(S)
        cptr = lltype2ctypes(p)
        assert not cptr
        py.test.raises(ValueError, 'cptr.contents')   # NULL pointer access
        res = ctypes2lltype(lltype.Ptr(S), cptr)
        assert res == p
        assert not ALLOCATED     # detects memory leaks in the test
Пример #4
0
    def test_primitive(self):
        assert lltype2ctypes(5) == 5
        assert lltype2ctypes('?') == ord('?')
        assert lltype2ctypes('\xE0') == 0xE0
        assert lltype2ctypes(unichr(1234)) == 1234
        assert ctypes2lltype(lltype.Signed, 5) == 5
        assert ctypes2lltype(lltype.Char, ord('a')) == 'a'
        assert ctypes2lltype(lltype.UniChar, ord(u'x')) == u'x'
        assert ctypes2lltype(lltype.Char, 0xFF) == '\xFF'
        assert lltype2ctypes(5.25) == 5.25
        assert ctypes2lltype(lltype.Float, 5.25) == 5.25
        assert lltype2ctypes(u'x') == ord(u'x')
        res = lltype2ctypes(rffi.r_singlefloat(-3.5))
        assert isinstance(res, ctypes.c_float)
        assert res.value == -3.5
        res = ctypes2lltype(lltype.SingleFloat, ctypes.c_float(-3.5))
        assert isinstance(res, rffi.r_singlefloat)
        assert float(res) == -3.5
        assert lltype2ctypes(rffi.r_ulong(-1)) == sys.maxint * 2 + 1
        res = ctypes2lltype(lltype.Unsigned, sys.maxint * 2 + 1)
        assert (res, type(res)) == (rffi.r_ulong(-1), rffi.r_ulong)

        res = lltype2ctypes(llmemory.sizeof(lltype.Signed))
        assert res == struct.calcsize("l")
        S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed))
        res = lltype2ctypes(llmemory.sizeof(S))
        assert res == struct.calcsize("ll")

        p = lltype.nullptr(S)
        cptr = lltype2ctypes(p)
        assert not cptr
        py.test.raises(ValueError, 'cptr.contents')  # NULL pointer access
        res = ctypes2lltype(lltype.Ptr(S), cptr)
        assert res == p
        assert not ALLOCATED  # detects memory leaks in the test