Commit e459ff9b by Maxime Perrotin

### Complete exhaustive generation of SEQUENCE type

parent def89c60
 ... @@ -20,6 +20,7 @@ __all__ = ['compute_random_value', 'compute_combinations'] ... @@ -20,6 +20,7 @@ __all__ = ['compute_random_value', 'compute_combinations'] import random import random import itertools import itertools import opengeode import opengeode from functools import partial # Set of functions used by the simulator to compute a random value for # Set of functions used by the simulator to compute a random value for ... @@ -157,7 +158,7 @@ def myproduct(*iterables): ... @@ -157,7 +158,7 @@ def myproduct(*iterables): has to consume the iterables completely before making the product, has to consume the iterables completely before making the product, meaning it is useless with large iterable due to memory usage ''' meaning it is useless with large iterable due to memory usage ''' if len(iterables) == 0: if len(iterables) == 0: raise StopIteration yield () else: else: for item in iterables[0](): for item in iterables[0](): for items in myproduct(*iterables[1:]): for items in myproduct(*iterables[1:]): ... @@ -166,15 +167,11 @@ def myproduct(*iterables): ... @@ -166,15 +167,11 @@ def myproduct(*iterables): def compute_sequence_combinations(asn1_ty, pool): def compute_sequence_combinations(asn1_ty, pool): ''' Generator returning all combinations of SEQUENCE types ''' ''' Generator returning all combinations of SEQUENCE types ''' # Prepare generators to compute combinations of each field # 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()] 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): for each in myproduct(*elems_lambda): # "each" contains a generator per field in the SEQUENCE # each is a tuple with values for the sequence, join with fieldnames # each is a tuple with values for the sequence, join with fieldnames pairs = itertools.izip(asn1_ty.Children.viewkeys(), each) pairs = itertools.izip(asn1_ty.Children.viewkeys(), each) res = ('{} {}'.format(name, value) for name, value in pairs) res = ('{} {}'.format(name, value) for name, value in pairs) ... ...
 ... @@ -26,6 +26,11 @@ Type-SimpleSeq ::= SEQUENCE { ... @@ -26,6 +26,11 @@ Type-SimpleSeq ::= SEQUENCE { item-d Type-SingleString item-d Type-SingleString } } Type-TinySeq ::= SEQUENCE { a INTEGER(1..2), b BOOLEAN } Type-SingleSeqOf ::= SEQUENCE (SIZE (0..5)) OF INTEGER (0..255) Type-SingleSeqOf ::= SEQUENCE (SIZE (0..5)) OF INTEGER (0..255) Type-TinySeqOf ::= SEQUENCE (SIZE (2)) OF BOOLEAN Type-TinySeqOf ::= SEQUENCE (SIZE (2)) OF BOOLEAN Type-TinySeqOf2::= SEQUENCE (SIZE (0..2)) OF INTEGER (0..5) Type-TinySeqOf2::= SEQUENCE (SIZE (0..2)) OF INTEGER (0..5) ... ...
 #!/usr/bin/env python #!/usr/bin/env python import sys import sys from os.path import dirname sys.path.insert(0, '../..') sys.path.insert(0, '../..') from asn1_value_editor import standalone_editor as editor 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 ... @@ -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 pip install --user pytest-qt ''' ''' TEST = editor.SingleValueEditor('data/dv1.asn') TEST = editor.SingleValueEditor(dirname(__file__) + '/data/dv1.asn') def common(typeName, defValue): def common(typeName, defValue): ''' Set up test case: create asn1 editor and fill with default value ''' ''' Set up test case: create asn1 editor and fill with default value ''' ... ...
 ... @@ -109,6 +109,17 @@ def test_random_simpleseq(): ... @@ -109,6 +109,17 @@ def test_random_simpleseq(): 'item-a' in result and 'item-b' in result and 'item-C' \ 'item-a' in result and 'item-b' in result and 'item-C' \ in result and 'item-d' in result 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 expected.remove(value) assert not expected def test_random_simplechoice(): def test_random_simplechoice(): ''' Test CHOICE. ''' ''' Test CHOICE. ''' typeName = "Type-SingleChoice" 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!