File: test_strstorage.py

package info (click to toggle)
pypy 5.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (96 lines) | stat: -rw-r--r-- 3,293 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import py
import sys
import struct
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.rlib.strstorage import str_storage_getitem
from rpython.rlib.rarithmetic import r_singlefloat
from rpython.rtyper.test.tool import BaseRtypingTest

IS_32BIT = (sys.maxint == 2147483647)

class BaseStrStorageTest:

    ## def test_str_getitem_supported(self):
    ##     if IS_32BIT:
    ##         expected = False
    ##     else:
    ##         expected = True
    ##     #
    ##     assert self.str_storage_supported(rffi.LONGLONG) == expected
    ##     assert self.str_storage_supported(rffi.DOUBLE) == expected

    def test_signed(self):
        buf = struct.pack('@ll', 42, 43)
        size = struct.calcsize('@l')
        assert self.str_storage_getitem(lltype.Signed, buf, 0) == 42
        assert self.str_storage_getitem(lltype.Signed, buf, size) == 43

    def test_short(self):
        buf = struct.pack('@hh', 42, 43)
        size = struct.calcsize('@h')
        x = self.str_storage_getitem(rffi.SHORT, buf, 0)
        assert int(x) == 42
        x = self.str_storage_getitem(rffi.SHORT, buf, size)
        assert int(x) == 43

    def test_float(self):
        ## if not str_storage_supported(lltype.Float):
        ##     py.test.skip('str_storage_getitem(lltype.Float) not supported on this machine')
        buf = struct.pack('@dd', 12.3, 45.6)
        size = struct.calcsize('@d')
        assert self.str_storage_getitem(lltype.Float, buf, 0) == 12.3
        assert self.str_storage_getitem(lltype.Float, buf, size) == 45.6

    def test_singlefloat(self):
        buf = struct.pack('@ff', 12.3, 45.6)
        size = struct.calcsize('@f')
        x = self.str_storage_getitem(lltype.SingleFloat, buf, 0)
        assert x == r_singlefloat(12.3)
        x = self.str_storage_getitem(lltype.SingleFloat, buf, size)
        assert x == r_singlefloat(45.6)


class TestDirect(BaseStrStorageTest):

    ## def str_storage_supported(self, TYPE):
    ##     return str_storage_supported(TYPE)

    def str_storage_getitem(self, TYPE, buf, offset):
        return str_storage_getitem(TYPE, buf, offset)

class TestRTyping(BaseStrStorageTest, BaseRtypingTest):

    ## def str_storage_supported(self, TYPE):
    ##     def fn():
    ##         return str_storage_supported(TYPE)
    ##     return self.interpret(fn, [])

    def str_storage_getitem(self, TYPE, buf, offset):
        def fn(offset):
            return str_storage_getitem(TYPE, buf, offset)
        return self.interpret(fn, [offset])


class TestCompiled(BaseStrStorageTest):
    cache = {}

    def str_storage_getitem(self, TYPE, buf, offset):
        if TYPE not in self.cache:
            from rpython.translator.c.test.test_genc import compile

            assert isinstance(TYPE, lltype.Primitive)
            if TYPE in (lltype.Float, lltype.SingleFloat):
                TARGET_TYPE = lltype.Float
            else:
                TARGET_TYPE = lltype.Signed

            def llf(buf, offset):
                x = str_storage_getitem(TYPE, buf, offset)
                return lltype.cast_primitive(TARGET_TYPE, x)

            fn = compile(llf, [str, int])
            self.cache[TYPE] = fn
        #
        fn = self.cache[TYPE]
        x = fn(buf, offset)
        return lltype.cast_primitive(TYPE, x)