File: test_py3compat.py

package info (click to toggle)
construct 2.10.68%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,812 kB
  • sloc: python: 11,793; makefile: 135
file content (26 lines) | stat: -rw-r--r-- 798 bytes parent folder | download | duplicates (2)
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
from tests.declarativeunittest import *
from construct.lib.py3compat import *


def test_int_byte():
    assert int2byte(5) == b"\x05"
    assert int2byte(255) == b"\xff"
    assert byte2int(b"\x05") == 5
    assert byte2int(b"\xff") == 255
    assert all(byte2int(int2byte(i)) == i for i in range(256))

def test_str_bytes():
    assert str2bytes("abc") == b"abc"
    assert bytes2str(b"abc") == "abc"
    assert bytes2str(str2bytes("abc123\n")) == "abc123\n"
    assert str2bytes(bytes2str(b"abc123\n")) == b"abc123\n"

def test_bytes():
    assert bytes() == b''
    assert bytes(2) == b'\x00\x00'
    assert bytes([1,2]) == b'\x01\x02'
    assert bytes((1,)) == b'\x01'

def test_bytes_integers():
    assert bytes2integers(b'abc') == [97,98,99]
    assert integers2bytes([97,98,99]) == b'abc'