Esempio n. 1
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)
Esempio n. 2
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)
Esempio n. 3
0
def test_list():
    typ = List[uint64, 128]
    assert issubclass(typ, List)
    assert issubclass(typ, View)
    assert isinstance(typ, TypeDef)

    assert not typ.is_fixed_byte_length()

    assert len(typ()) == 0  # empty
    assert len(typ(uint64(0))) == 1  # single arg
    assert len(typ(uint64(i) for i in range(10))) == 10  # generator
    assert len(typ(uint64(0), uint64(1), uint64(2))) == 3  # args
    assert isinstance(typ(1, 2, 3, 4, 5)[4], uint64)  # coercion
    assert isinstance(typ(i for i in range(10))[9],
                      uint64)  # coercion in generator

    v = typ(uint64(2), uint64(1))
    v[0] = uint64(123)
    assert v[0] == 123
    assert isinstance(v[0], uint64)

    assert isinstance(v, List)
    assert isinstance(v, View)

    assert len(typ([i for i in range(10)])) == 10  # cast py list to SSZ list

    foo = List[uint32, 128](0 for i in range(128))
    foo[0] = 123
    foo[1] = 654
    foo[127] = 222
    assert sum(foo) == 999
    try:
        foo[3] = 2**32  # out of bounds
    except ValueError:
        pass

    for i in range(128):
        foo.pop()
        assert len(foo) == 128 - 1 - i
    for i in range(128):
        foo.append(uint32(i))
        assert len(foo) == i + 1
        assert foo[i] == i

    try:
        foo[3] = uint64(2**32 - 1)  # within bounds, wrong type
        assert False
    except ValueError:
        pass

    try:
        foo[128] = 100
        assert False
    except IndexError:
        pass

    try:
        foo[-1] = 100  # valid in normal python lists
        assert False
    except IndexError:
        pass

    try:
        foo[128] = 100  # out of bounds
        assert False
    except IndexError:
        pass
Esempio n. 4
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,
      'B': 0x0123
  }),
 ("small [4567, 0123]::2", Vector[uint16,
                                  2], Vector[uint16,
                                             2](uint16(0x4567),
                                                uint16(0x0123)), "67452301",
  chunk("67452301"), (0x4567, 0x0123)),
 ("uint32 01234567", uint32, uint32(0x01234567), "67452301",
  chunk("67452301"), 0x01234567),
 ("uint64 0000000000000000", uint64, uint64(0x00000000), "0000000000000000",
  chunk("0000000000000000"), 0),
 ("uint64 0123456789abcdef", uint64, uint64(0x0123456789abcdef),
  "efcdab8967452301", chunk("efcdab8967452301"), 0x0123456789abcdef),
 ("uint128 00000000000000000000000000000000", uint128, uint128(0),
  "00000000000000000000000000000000",
  chunk("00000000000000000000000000000000"),
  '0x00000000000000000000000000000000'),
 ("uint128 11223344556677880123456789abcdef", uint128,
  uint128(0x11223344556677880123456789abcdef),
  "efcdab89674523018877665544332211",
  chunk("efcdab89674523018877665544332211"),
  '0xefcdab89674523018877665544332211'),
 ("bytes48", Vector[byte, 48], Vector[byte, 48](*range(48)),
  "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f",
  h("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",