Commit e459ff9b authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Complete exhaustive generation of SEQUENCE type

parent def89c60
......@@ -20,6 +20,7 @@ __all__ = ['compute_random_value', 'compute_combinations']
import random
import itertools
import opengeode
from functools import partial
# Set of functions used by the simulator to compute a random value for
......@@ -157,7 +158,7 @@ def myproduct(*iterables):
has to consume the iterables completely before making the product,
meaning it is useless with large iterable due to memory usage '''
if len(iterables) == 0:
raise StopIteration
yield ()
for item in iterables[0]():
for items in myproduct(*iterables[1:]):
......@@ -166,15 +167,11 @@ def myproduct(*iterables):
def compute_sequence_combinations(asn1_ty, pool):
''' Generator returning all combinations of SEQUENCE types '''
# Prepare generators to compute combinations of each field
elems = [lambda: compute_combinations(sort, pool)
# Using partial (closure) here allows to have "resettable" generators
elems = [partial(compute_combinations, sort, pool)
for sort in asn1_ty.Children.viewvalues()]
# Combine all field generators to get the complete set of values
elems_lambda = [lambda: elem() for elem in elems]
# This is not quite right. XXX
#for each in itertools.product(*elems):
for each in myproduct(*elems_lambda):
# "each" contains a generator per field in the SEQUENCE
for each in myproduct(*elems):
# each is a tuple with values for the sequence, join with fieldnames
pairs = itertools.izip(asn1_ty.Children.viewkeys(), each)
res = ('{} {}'.format(name, value) for name, value in pairs)
......@@ -26,6 +26,11 @@ Type-SimpleSeq ::= SEQUENCE {
item-d Type-SingleString
Type-TinySeq ::= SEQUENCE {
a INTEGER(1..2),
Type-SingleSeqOf ::= SEQUENCE (SIZE (0..5)) OF INTEGER (0..255)
Type-TinySeqOf2::= SEQUENCE (SIZE (0..2)) OF INTEGER (0..5)
#!/usr/bin/env python
import sys
from os.path import dirname
sys.path.insert(0, '../..')
from asn1_value_editor import standalone_editor as editor
......@@ -12,7 +13,7 @@ Use py.test-2.7 to run these tests, and make sure you have installed pytest-qt
pip install --user pytest-qt
TEST = editor.SingleValueEditor('data/dv1.asn')
TEST = editor.SingleValueEditor(dirname(__file__) + '/data/dv1.asn')
def common(typeName, defValue):
''' Set up test case: create asn1 editor and fill with default value '''
......@@ -109,6 +109,17 @@ def test_random_simpleseq():
'item-a' in result and 'item-b' in result and 'item-C' \
in result and 'item-d' in result
def test_exhaustive_tinysequence():
''' Test more complex sequence '''
typeName = "Type-TinySeq"
expected = [{'a': 1, 'b': True}, {'a': 1, 'b': False},
{'a': 2, 'b': True}, {'a': 2, 'b': False}]
for each in compute_combinations(pool[typeName], pool):
value = parse_gser('result', each)['result']
assert value in expected
assert not expected
def test_random_simplechoice():
''' Test CHOICE. '''
typeName = "Type-SingleChoice"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment