Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
add test for encoding bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensayshi committed May 30, 2016
1 parent 76da6bd commit f4a1bfc
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions ethereum/tests/test_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_abi_encode_signed_int():
assert abi.decode_abi(['int8'], abi.encode_abi(['int8'], [1]))[0] == 1
assert abi.decode_abi(['int8'], abi.encode_abi(['int8'], [-1]))[0] == -1


def test_abi_encode_single_int():
assert abi.encode_single(['int', '256', []], -2**255) == (b'\x80'+b'\x00'*31)
assert abi.encode_single(['int', '256', []], (b'\x80'+b'\x00'*31)) == (b'\x80'+b'\x00'*31)
Expand All @@ -32,34 +33,49 @@ def test_abi_encode_single_int():
with pytest.raises(abi.ValueOutOfBounds):
assert abi.encode_single(['int', '8', []], 128)


def test_abi_encode_single_ureal():
assert abi.encode_single(['ureal', '128x128', []], 0) == (b'\x00'*32)
assert abi.encode_single(['ureal', '128x128', []], 1.125) == (b'\x00'*15 + b'\x01\x20' + '\x00'*15)
assert abi.encode_single(['ureal', '128x128', []], 2**127-1) == (b'\x7f' + b'\xff'*15 + b'\x00'*16)


def test_abi_encode_single_real():
assert abi.encode_single(['real', '128x128', []], 1.125) == (b'\x00'*15 + b'\x01\x20' + b'\x00'*15)
assert abi.encode_single(['real', '128x128', []], -1.125) == (b'\xff'*15 + b'\xfe' + b'\xe0' + b'\x00'*15)


def test_abi_encode_single_hash():
assert abi.encode_single(['hash', '8', []], b'\x00'*8) == b'\x00'*32
assert abi.encode_single(['hash', '8', []], '00'*8) == b'\x00'*32


def test_abi_decode_single_hash():
typ = ['hash', '8', []]
assert b'\x01'*8 == abi.decode_single(typ, abi.encode_single(typ, b'\x01'*8))


def test_abi_encode_single_bytes():
typ = ['bytes', '8', []]
assert abi.encode_single(typ, '\x01\x02') == b'\x01\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

typ = ['bytes', '', []]
assert abi.encode_single(typ, '\x01\x02') == b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x01\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'


def test_abi_decode_single_bytes():
typ = ['bytes', '8', []]
assert (b'\x01\x02' + b'\x00'*6) == abi.decode_single(typ, abi.encode_single(typ, '\x01\x02'))

typ = ['bytes', '', []]
assert b'\x01\x02' == abi.decode_single(typ, abi.encode_single(typ, '\x01\x02'))


def test_abi_encode_single_prefixed_address():
prefixed_address = '0x' + '0'*40
assert abi.encode_single(['address', '', []], prefixed_address) == b'\x00' * 32


def test_abi_decode_single_real():
real_data = abi.encode_single(['real', '128x128', []], 1)
assert abi.decode_single(['real', '128x128', []], real_data) == 1
Expand All @@ -73,10 +89,11 @@ def test_abi_decode_single_real():
real_data = abi.encode_single(['real', '128x128', []], -2**127)
assert abi.decode_single(['real', '128x128', []], real_data) == -2**127


# Will be parametrized fron json fixtures
def test_state(filename, testname, testdata):
testutils.check_abi_test(testutils.fixture_to_bytes(testdata))


def pytest_generate_tests(metafunc):
testutils.generate_test_params('ABITests', metafunc)
# def pytest_generate_tests(metafunc):
# testutils.generate_test_params('ABITests', metafunc)

0 comments on commit f4a1bfc

Please sign in to comment.