2019-07-27 21:57:07 +00:00
|
|
|
from ssz_test_case import invalid_test_case, valid_test_case
|
2020-03-11 22:18:06 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_typing import BasicView, uint8, uint16, uint32, uint64, uint128, uint256
|
2019-07-27 14:45:30 +00:00
|
|
|
from random import Random
|
2020-03-11 22:18:06 +00:00
|
|
|
from typing import Type
|
2019-07-27 14:45:30 +00:00
|
|
|
from eth2spec.debug.random_value import RandomizationMode, get_random_ssz_object
|
|
|
|
|
|
|
|
|
2020-03-11 22:18:06 +00:00
|
|
|
def uint_case_fn(rng: Random, mode: RandomizationMode, typ: Type[BasicView]):
|
2019-07-27 14:45:30 +00:00
|
|
|
return get_random_ssz_object(rng, typ,
|
2020-03-11 22:18:06 +00:00
|
|
|
max_bytes_length=typ.type_byte_length(),
|
2019-07-27 14:45:30 +00:00
|
|
|
max_list_length=1,
|
2023-08-01 19:46:16 +00:00
|
|
|
mode=mode, chaos=False)
|
2019-07-27 14:45:30 +00:00
|
|
|
|
|
|
|
|
2019-07-27 20:31:04 +00:00
|
|
|
UINT_TYPES = [uint8, uint16, uint32, uint64, uint128, uint256]
|
|
|
|
|
|
|
|
|
2019-07-27 14:45:30 +00:00
|
|
|
def valid_cases():
|
|
|
|
rng = Random(1234)
|
2019-07-27 20:31:04 +00:00
|
|
|
for uint_type in UINT_TYPES:
|
2023-08-01 19:47:08 +00:00
|
|
|
mode = RandomizationMode.mode_random
|
2020-03-11 22:18:06 +00:00
|
|
|
byte_len = uint_type.type_byte_length()
|
|
|
|
yield f'uint_{byte_len * 8}_last_byte_empty', \
|
2024-08-21 18:15:57 +00:00
|
|
|
valid_test_case(lambda: uint_type((2 ** ((byte_len - 1) * 8)) - 1))
|
2019-07-27 14:45:30 +00:00
|
|
|
for variation in range(5):
|
2023-08-01 20:19:48 +00:00
|
|
|
yield f'uint_{byte_len * 8}_{mode.to_name()}_{variation}', \
|
2023-08-01 19:47:08 +00:00
|
|
|
valid_test_case(lambda: uint_case_fn(rng, mode, uint_type))
|
|
|
|
for mode in [RandomizationMode.mode_zero, RandomizationMode.mode_max]:
|
2023-08-01 20:19:48 +00:00
|
|
|
yield f'uint_{byte_len * 8}_{mode.to_name()}', \
|
2023-08-01 19:47:08 +00:00
|
|
|
valid_test_case(lambda: uint_case_fn(rng, mode, uint_type))
|
2019-07-27 14:45:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def invalid_cases():
|
2019-07-27 20:31:04 +00:00
|
|
|
for uint_type in UINT_TYPES:
|
2020-03-11 22:18:06 +00:00
|
|
|
byte_len = uint_type.type_byte_length()
|
|
|
|
yield f'uint_{byte_len * 8}_one_too_high', \
|
|
|
|
invalid_test_case(lambda: (2 ** (byte_len * 8)).to_bytes(byte_len + 1, 'little'))
|
2019-07-27 14:45:30 +00:00
|
|
|
for uint_type in [uint8, uint16, uint32, uint64, uint128, uint256]:
|
2020-03-11 22:18:06 +00:00
|
|
|
byte_len = uint_type.type_byte_length()
|
|
|
|
yield f'uint_{byte_len * 8}_one_byte_longer', \
|
2024-08-21 18:15:57 +00:00
|
|
|
invalid_test_case(lambda: (2 ** (byte_len * 8) - 1).to_bytes(byte_len + 1, 'little'))
|
2019-07-27 14:45:30 +00:00
|
|
|
for uint_type in [uint8, uint16, uint32, uint64, uint128, uint256]:
|
2020-03-11 22:18:06 +00:00
|
|
|
byte_len = uint_type.type_byte_length()
|
|
|
|
yield f'uint_{byte_len * 8}_one_byte_shorter', \
|
2024-08-21 18:15:57 +00:00
|
|
|
invalid_test_case(lambda: (2 ** ((byte_len - 1) * 8) - 1).to_bytes(byte_len - 1, 'little'))
|