File: test_mutbuffer.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 (44 lines) | stat: -rw-r--r-- 1,392 bytes parent folder | download | duplicates (7)
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
import pytest
import struct
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.rlib.mutbuffer import MutableStringBuffer

class TestMutableStringBuffer(object):

    def test_finish(self):
        buf = MutableStringBuffer(4)
        buf.setzeros(0, 4)
        pytest.raises(ValueError, "buf.as_str()")
        s = buf.finish()
        assert s == '\x00' * 4
        pytest.raises(ValueError, "buf.finish()")

    def test_setitem(self):
        buf = MutableStringBuffer(4)
        buf.setitem(0, 'A')
        buf.setitem(1, 'B')
        buf.setitem(2, 'C')
        buf.setitem(3, 'D')
        assert buf.finish() == 'ABCD'

    def test_setslice(self):
        buf = MutableStringBuffer(6)
        buf.setzeros(0, 6)
        buf.setslice(2, 'ABCD')
        assert buf.finish() == '\x00\x00ABCD'

    def test_setzeros(self):
        buf = MutableStringBuffer(8)
        buf.setslice(0, 'ABCDEFGH')
        buf.setzeros(2, 3)
        assert buf.finish() == 'AB\x00\x00\x00FGH'

    def test_typed_write(self):
        expected = struct.pack('ifqd', 0x1234, 123.456, 0x12345678, 789.123)
        buf = MutableStringBuffer(24)
        buf.typed_write(rffi.INT, 0, 0x1234)
        buf.typed_write(rffi.FLOAT, 4, 123.456)
        buf.typed_write(rffi.LONGLONG, 8, 0x12345678)
        buf.typed_write(rffi.DOUBLE, 16, 789.123)
        s = buf.finish()
        assert s == expected