File: test_struct.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (64 lines) | stat: -rw-r--r-- 2,173 bytes parent folder | download | duplicates (4)
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
"""
Tests for the struct module implemented at interp-level in pypy/module/struct.
"""

class AppTestFastPath(object):
    spaceconfig = dict(usemodules=['array', 'struct', '__pypy__'])

    def setup_class(cls):
        from rpython.rlib.rstruct import standardfmttable
        standardfmttable.ALLOW_SLOWPATH = False
        #
        cls.w_struct = cls.space.appexec([], """():
            import struct
            return struct
        """)
        cls.w_bytebuffer = cls.space.appexec([], """():
            import __pypy__
            return __pypy__.bytebuffer
        """)

    def teardown_class(cls):
        from rpython.rlib.rstruct import standardfmttable
        standardfmttable.ALLOW_SLOWPATH = True

    def test_unpack_simple(self):
        buf = self.struct.pack("iii", 0, 42, 43)
        assert self.struct.unpack("iii", buf) == (0, 42, 43)

    def test_unpack_from(self):
        buf = self.struct.pack("iii", 0, 42, 43)
        offset = self.struct.calcsize("i")
        assert self.struct.unpack_from("ii", buf, offset) == (42, 43)

    def test_unpack_bytearray(self):
        data = self.struct.pack("iii", 0, 42, 43)
        buf = bytearray(data)
        assert self.struct.unpack("iii", buf) == (0, 42, 43)

    def test_unpack_array(self):
        import array
        data = self.struct.pack("iii", 0, 42, 43)
        buf = array.array('B', data)
        assert self.struct.unpack("iii", buf) == (0, 42, 43)

    def test_pack_into_bytearray(self):
        expected = self.struct.pack("ii", 42, 43)
        buf = bytearray(len(expected))
        self.struct.pack_into("ii", buf, 0, 42, 43)
        assert buf == expected

    def test_pack_into_bytearray_padding(self):
        expected = self.struct.pack("xxi", 42)
        buf = bytearray(len(expected))
        self.struct.pack_into("xxi", buf, 0, 42)
        assert buf == expected

    def test_pack_into_bytearray_delete(self):
        expected = self.struct.pack("i", 42)
        # force W_BytearrayObject._delete_from_start
        buf = bytearray(64)
        del buf[:8]
        self.struct.pack_into("i", buf, 0, 42)
        buf = buf[:len(expected)]
        assert buf == expected