File: test_rust_io.py

package info (click to toggle)
python-cramjam 2.7.0.1%2Bds1-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 3,048 kB
  • sloc: python: 622; makefile: 41
file content (57 lines) | stat: -rw-r--r-- 1,464 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
import pytest

from cramjam import File, Buffer


@pytest.mark.parametrize("Obj", (File, Buffer))
def test_obj_api(tmpdir, Obj):
    if isinstance(Obj, File):
        buf = File(str(tmpdir.join("file.txt")))
    else:
        buf = Buffer()

    assert buf.write(b"bytes") == 5
    assert buf.tell() == 5
    assert buf.seek(0) == 0
    assert buf.read() == b"bytes"
    assert buf.seek(-1, 2) == 4  # set one byte backwards from end; position 4
    assert buf.read() == b"s"
    assert buf.seek(-2, whence=1) == 3  # set two bytes from current (end): position 3
    assert buf.read() == b"es"

    with pytest.raises(ValueError):
        buf.seek(1, 3)  # only 0, 1, 2 are valid seek from positions

    for out in (
        b"12345",
        bytearray(b"12345"),
        File(str(tmpdir.join("test.txt"))),
        Buffer(),
    ):
        buf.seek(0)

        expected = b"bytes"

        buf.readinto(out)

        # Will update the output buffer
        if isinstance(out, (File, Buffer)):
            out.seek(0)
            assert out.read() == expected
        elif isinstance(out, bytearray):
            assert out == bytearray(expected)
        else:
            assert out == expected

    # Set the length
    buf.set_len(2)
    buf.seek(0)
    assert buf.read() == b"by"
    buf.set_len(10)
    buf.seek(0)
    assert buf.read() == b"by\x00\x00\x00\x00\x00\x00\x00\x00"

    # truncate
    buf.truncate()
    buf.seek(0)
    assert buf.read() == b""