Beispiel #1
0
def test_mixed_uint_lshifts():
    assert uint64(0xff) << uint8(8 * 7) == uint64(0xff00_0000_0000_0000)
    assert uint16(0xff) << uint8(8 * 7) == uint16(0)
    assert uint256(1) << uint8(255) == uint256(1 << 255)
    assert uint256(1) << uint16(255) == uint256(1 << 255)
    assert uint256(1) << uint16(256) == uint256(0)
    assert uint8(42) >> uint64(3) == uint8(42 >> 3)
Beispiel #2
0
def test_uint_math():
    assert uint8(0) + uint8(uint32(16)) == uint8(
        16)  # allow explicit casting to make invalid addition valid

    expect_op_error(lambda: uint8(0) - uint8(1), "no underflows allowed")
    expect_op_error(lambda: uint8(1) + uint8(255), "no overflows allowed")
    expect_op_error(lambda: uint8(0) + 256, "no overflows allowed")
    expect_op_error(lambda: uint8(42) + uint32(123), "no mixed types")
    expect_op_error(lambda: uint32(42) + uint8(123), "no mixed types")

    assert type(uint32(1234) + 56) == uint32
Beispiel #3
0
def test_mixed_uint_rshifts():
    assert uint64(0xff00_0000_0000_0000) >> uint8(8 * 7) == uint64(0xff)
    assert uint16(0xff) >> uint8(8) == uint16(0)
    assert uint16(0xff) >> uint8(6) == uint16(3)
    assert uint256(1 << 255) >> uint8(255) == uint256(1)
    assert uint256(1 << 255) >> uint16(255) == uint256(1)
    assert uint8(42) << uint64(3) == uint8((42 << 3) & 0xff)
Beispiel #4
0
def test_container():
    class Foo(Container):
        a: uint8
        b: uint32

    empty = Foo()
    assert empty.a == uint8(0)
    assert empty.b == uint32(0)

    assert issubclass(Foo, Container)
    assert issubclass(Foo, View)

    assert Foo.is_fixed_byte_length()
    x = Foo(a=uint8(123), b=uint32(45))
    assert x.a == 123
    assert x.b == 45
    assert isinstance(x.a, uint8)
    assert isinstance(x.b, uint32)
    assert x.__class__.is_fixed_byte_length()

    class Bar(Container):
        a: uint8
        b: List[uint8, 1024]

    assert not Bar.is_fixed_byte_length()

    y = Bar(a=123, b=List[uint8, 1024](uint8(1), uint8(2)))
    assert y.a == 123
    assert isinstance(y.a, uint8)
    assert len(y.b) == 2
    assert isinstance(y.a, uint8)
    assert not y.__class__.is_fixed_byte_length()
    assert y.b[0] == 1
    v: List = y.b
    assert v.__class__.element_cls() == uint8
    assert v.__class__.limit() == 1024

    field_values = list(y)
    assert field_values == [y.a, y.b]

    f_a, f_b = y
    assert f_a == y.a
    assert f_b == y.b

    y.a = 42
    try:
        y.a = 256  # out of bounds
        assert False
    except ValueError:
        pass

    try:
        y.a = uint16(255)  # within bounds, wrong type
        assert False
    except ValueError:
        pass

    try:
        y.not_here = 5
        assert False
    except AttributeError:
        pass

    try:
        Foo(wrong_field_name=100)
        assert False
    except AttributeError:
        pass
Beispiel #5
0
 ("long bitvector", Bitvector[512], Bitvector[512](1 for i in range(512)),
  "ff" * 64, h("ff" * 32, "ff" * 32), "0x" + ("ff" * 64)),
 ("long bitlist", Bitlist[512], Bitlist[512](1), "03",
  h(h(chunk("01"), chunk("")), chunk("01")), "0x03"),
 ("long bitlist", Bitlist[512], Bitlist[512](1 for i in range(512)),
  "ff" * 64 + "01", h(h("ff" * 32, "ff" * 32),
                      chunk("0002")), "0x" + ("ff" * 64 + "01")),
 ("odd bitvector", Bitvector[513], Bitvector[513](1 for i in range(513)),
  "ff" * 64 + "01", h(h("ff" * 32, "ff" * 32),
                      h(chunk("01"),
                        chunk(""))), "0x" + ("ff" * 64) + "01"),
 ("odd bitlist", Bitlist[513], Bitlist[513](1 for i in range(513)),
  "ff" * 64 + "03",
  h(h(h("ff" * 32, "ff" * 32), h(chunk("01"), chunk(""))),
    chunk("0102")), "0x" + ("ff" * 64) + "03"),
 ("uint8 00", uint8, uint8(0x00), "00", chunk("00"), 0),
 ("uint8 01", uint8, uint8(0x01), "01", chunk("01"), 1),
 ("uint8 ab", uint8, uint8(0xab), "ab", chunk("ab"), 0xab),
 ("byte 00", byte, byte(0x00), "00", chunk("00"), 0),
 ("byte 01", byte, byte(0x01), "01", chunk("01"), 1),
 ("byte ab", byte, byte(0xab), "ab", chunk("ab"), 0xab),
 ("uint16 0000", uint16, uint16(0x0000), "0000", chunk("0000"), 0),
 ("uint16 abcd", uint16, uint16(0xabcd), "cdab", chunk("cdab"), 0xabcd),
 ("uint32 00000000", uint32, uint32(0x00000000), "00000000",
  chunk("00000000"), 0),
 ("uint32 01234567", uint32, uint32(0x01234567), "67452301",
  chunk("67452301"), 0x01234567),
 ("small (4567, 0123)", SmallTestStruct, SmallTestStruct(A=0x4567,
                                                         B=0x0123),
  "67452301", h(chunk("6745"), chunk("2301")), {
      'A': 0x4567,