Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
pipermerriam committed Apr 11, 2019
1 parent fa2f35b commit c2db9fe
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 168 deletions.
62 changes: 56 additions & 6 deletions ssz/basic_types.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
from random import Random
import itertools
import random

import ssz
from ssz.sedes import (
boolean,
UInt,
)

from _utils import seed
from renderers import (
render_test_case,
render_test,
)


random = Random(0)

UINT_SIZES = tuple(8 * 2**exponent for exponent in range(0, 6)) # 8, 16, ..., 256
NUM_RANDOM_UINT_VALUES = 16

Expand Down Expand Up @@ -43,6 +42,7 @@ def generate_uint_test_cases():
)


@seed
def generate_uint_test():
return render_test(
title="UInt",
Expand All @@ -52,7 +52,7 @@ def generate_uint_test():
)


def generate_bool_test_cases():
def generate_bool_true_and_false_test_cases():
for value in (True, False):
yield render_test_case(
sedes=boolean,
Expand All @@ -63,10 +63,60 @@ def generate_bool_test_cases():
)


WRONG_LENGTH_SERIALS = (
b'',
b'\x00' * 2,
b'\x01' * 2,
b'\xFF' * 2,
b'\x00' * 3,
b'\x01' * 3,
b'\xFF' * 3,
b'\x00' * 5,
b'\x01' * 5,
b'\xFF' * 5,
b'\x00\x01',
b'\x01\x00',
b'\x00\xFF',
b'\x01\xFF',
)


def generate_bool_wrong_length_test_cases():
tags = ("atomic", "bool", "wrong_length")
sedes = boolean

for serial in WRONG_LENGTH_SERIALS:
yield render_test_case(
sedes=sedes,
valid=False,
serial=serial,
tags=tags,
)


def generate_bool_invalid_byte_test_cases():
tags = ("atomic", "bool", "invalid")
sedes = boolean

for i in range(2, 255):
serial = bytes((i,))
yield render_test_case(
sedes=sedes,
valid=False,
serial=serial,
tags=tags,
)


@seed
def generate_bool_test():
return render_test(
title="Bool",
summary="Tests for the two bool values",
version="0.1",
test_cases=generate_bool_test_cases(),
test_cases=itertools.chain(
generate_bool_true_and_false_test_cases(),
generate_bool_wrong_length_test_cases(),
generate_bool_invalid_byte_test_cases(),
),
)
122 changes: 0 additions & 122 deletions ssz/bool_test_generators.py

This file was deleted.

45 changes: 20 additions & 25 deletions ssz/flat_composite_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from itertools import chain
from random import Random
import itertools
import random

import ssz
from ssz.sedes import (
Expand All @@ -11,14 +11,13 @@
Vector,
)

from _utils import seed
from renderers import (
render_test_case,
render_test,
)


random = Random(0)

UINT_SIZES = tuple(8 * 2**exponent for exponent in range(0, 6)) # 8, 16, ..., 256
NUM_RANDOM_LENGTHS = 16
MAX_RANDOM_LIST_LENGTH = 512
Expand Down Expand Up @@ -68,15 +67,12 @@ def generate_flat_homogenous_container_test_cases():
for _ in range(NUM_RANDOM_LENGTHS)
)
for length in lengths:
field_names = tuple(f"field{field_index}" for field_index in range(length))
sedes = Container(tuple(
(field_name, element_sedes)
for field_name in field_names
))
value = {
field_name: get_random_basic_value(element_sedes)
for field_name in field_names
}
fields = tuple(itertools.repeat(element_sedes, length))
sedes = Container(fields)
value = tuple(
get_random_basic_value(element_sedes)
for element_sedes in fields
)
yield render_test_case(
sedes=sedes,
valid=True,
Expand All @@ -97,16 +93,12 @@ def generate_flat_heterogenous_container_test_cases():
UInt(bit_size) for bit_size in UINT_SIZES
)
for length in lengths:
field_names = tuple(f"field{field_index}" for field_index in range(length))
field_sedes = tuple(random.choice(element_sedes_choices) for _ in range(length))
sedes = Container(tuple(
(field_name, field_sedes)
for field_name, field_sedes in zip(field_names, field_sedes)
))
value = {
field_name: get_random_basic_value(sedes)
for field_name, sedes in zip(field_names, field_sedes)
}
fields = tuple(random.choice(element_sedes_choices) for _ in range(length))
sedes = Container(fields)
value = tuple(
get_random_basic_value(field)
for field in fields
)

yield render_test_case(
sedes=sedes,
Expand All @@ -127,7 +119,7 @@ def generate_flat_vector_test_cases():
for _ in range(NUM_RANDOM_LENGTHS)
)
for length in lengths:
sedes = Vector(length, element_sedes)
sedes = Vector(element_sedes, length)
value = tuple(get_random_basic_value(element_sedes) for _ in range(length))
yield render_test_case(
sedes=sedes,
Expand All @@ -138,6 +130,7 @@ def generate_flat_vector_test_cases():
)


@seed
def generate_flat_list_test():
return render_test(
title="Flat List",
Expand All @@ -147,18 +140,20 @@ def generate_flat_list_test():
)


@seed
def generate_flat_container_test():
return render_test(
title="Flat Container",
summary="Tests for containers consisting of only basic types",
version="0.1",
test_cases=chain(
test_cases=itertools.chain(
generate_flat_homogenous_container_test_cases(),
generate_flat_heterogenous_container_test_cases(),
)
)


@seed
def generate_flat_vector_test():
return render_test(
title="Flat Vector",
Expand Down
17 changes: 10 additions & 7 deletions ssz/nested_composite_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Vector,
)

from _utils import seed
from renderers import (
render_test_case,
render_test,
Expand Down Expand Up @@ -56,7 +57,7 @@ def get_random_value(sedes, max_list_length):
elif isinstance(sedes, Vector):
return tuple(
get_random_value(sedes.element_sedes, max_list_length)
for _ in range(sedes.number_of_elements)
for _ in range(sedes.length)
)
elif isinstance(sedes, List):
length = random.randint(0, max_list_length)
Expand All @@ -65,10 +66,10 @@ def get_random_value(sedes, max_list_length):
for _ in range(length)
)
elif isinstance(sedes, Container):
return {
field_name: get_random_value(field_sedes, max_list_length)
for field_name, field_sedes in sedes.fields
}
return tuple(
get_random_value(field, max_list_length)
for field in sedes.field_sedes
)
else:
raise ValueError(f"Cannot generate random value for sedes {sedes}")

Expand All @@ -81,13 +82,13 @@ def get_random_list_sedes(element_sedes_factory):
def get_random_vector_sedes(element_sedes_factory, max_length):
length = random.randint(0, max_length)
element_sedes = element_sedes_factory()
return Vector(length, element_sedes)
return Vector(element_sedes, length)


def get_random_container_sedes(element_sedes_factory, max_length):
length = random.randint(0, max_length)
fields = tuple(
(f"field{index}", element_sedes_factory())
element_sedes_factory()
for index in range(length)
)
return Container(fields)
Expand Down Expand Up @@ -149,6 +150,7 @@ def generate_deeply_nested_composite_test_cases():
)


@seed
def generate_two_layer_composite_test():
return render_test(
title="Nested composite types",
Expand All @@ -158,6 +160,7 @@ def generate_two_layer_composite_test():
)


@seed
def generate_deeply_nested_composite_test():
return render_test(
title="Deeply nested composite types",
Expand Down
Loading

0 comments on commit c2db9fe

Please sign in to comment.