test_valuegenerator.py 6.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python

import sys
import re

sys.path.insert(0, '../..')
from asn1_value_editor.ValueGenerator import (compute_random_value,
                                              compute_combinations)

from asn1_value_editor.vn import fromValueNotationToPySide as parse_gser

from opengeode import Asn1scc as asn1scc

'''
Use py.test-2.7 to run these tests
'''

TEST = ['data/dv1.asn']

dataview = asn1scc.parse_asn1(TEST,
                              rename_policy=asn1scc.ASN1.RenameOnlyConflicting,
                              ast_version=asn1scc.ASN1.UniqueEnumeratedNames,
                              flags=[asn1scc.ASN1.AstOnly])
pool = dataview.types


27
def test_random_bool():
28
29
30
31
32
33
34
35
36
    ''' Test boolean values '''
    typeName = "Type-SingleBool"
    results = []
    for i in range(100):
        result = compute_random_value(pool[typeName], pool)
        assert result in ('TRUE', 'FALSE')
        results.append(result)
    assert 'TRUE' in results and 'FALSE' in results

37
38
39
40
41
42
43
def test_exhaustive_bool():
    ''' Test boolean values '''
    typeName = "Type-SingleBool"
    result = list(compute_combinations(pool[typeName], pool))
    assert result == ['TRUE', 'FALSE']

def test_random_int():
44
45
46
47
48
49
    ''' Test integer value '''
    typeName = "Type-SingleInt"
    for i in range(1000):
        result = compute_random_value(pool[typeName], pool)
        assert 0 <= int(result) <= 255

50
51
52
53
54
def test_exhaustive_int():
    ''' Test integer value '''
    typeName = "Type-SingleInt"
    result = list(compute_combinations(pool[typeName], pool))
    assert result == [str(i) for i in range(256)]
55

56
def test_random_real():
57
58
59
60
61
62
    ''' Test float value '''
    typeName = "Type-SingleReal"
    for i in range(1000):
        result = compute_random_value(pool[typeName], pool)
        assert -5.0 <= float(result) <= 5.0

63
64
65
66
67
68
69
70
def test_exhaustive_real():
    ''' Test float value '''
    typeName = "Type-SingleReal"
    result = list(compute_combinations(pool[typeName], pool))
    asFloat = [float(i) for i in result]
    assert asFloat == [-5.0, 0.0, 5.0]

def test_random_enum():
71
72
73
74
75
76
77
78
79
    ''' Test enumerated value '''
    typeName = "Type-SingleEnum"
    results = []
    for i in range(100):
        result = compute_random_value(pool[typeName], pool)
        assert result in ('enum-one', 'enum-two')
        results.append(result)
    assert 'enum-one' in results and 'enum-two' in results

80
81
82
83
84
85
86
def test_exhaustive_enum():
    ''' Test enumerated value '''
    typeName = "Type-SingleEnum"
    result = list(compute_combinations(pool[typeName], pool))
    assert result == ['enum-one', 'enum-two']

def test_random_string():
87
88
89
90
91
92
    ''' Test octet string value '''
    typeName = "Type-SingleString"
    for i in range(1000):
        result = compute_random_value(pool[typeName], pool)
        assert re.sub('[X\"]', '', result) == '' and 2 <= len(result) <= 22

93
94
95
96
97
98
99
100
101
102
103
def test_exhaustive_string():
    ''' Test octet string value '''
    typeName = "Type-SingleString"
    result = list(compute_combinations(pool[typeName], pool))
    expected_len = [i+2 for i in range(21)]
    assert result[0] == '""'
    assert result[1] == '"X"'
    for idx, each in enumerate(result):
        assert len(each) == expected_len[idx]

def test_random_simpleseq():
104
105
106
107
108
109
110
111
    ''' Test SEQUENCE '''
    typeName = "Type-SimpleSeq"
    for i in range(1000):
        result = compute_random_value(pool[typeName], pool)
        assert result.startswith("{") and result.endswith("}") and \
               'item-a' in result and 'item-b' in result and 'item-C' \
               in result and 'item-d' in result

112
def test_random_simplechoice():
113
114
    ''' Test CHOICE. '''
    typeName = "Type-SingleChoice"
115
    for i in range(100):
116
117
118
119
120
121
122
123
124
125
126
        result = compute_random_value(pool[typeName], pool)
        if 'choice-B' in result:
            assert 'TRUE' in result or 'FALSE' in result
        elif 'choice-C' in result:
            assert 'enum-ONE' in result or 'enum-TWO' in result or \
                    'enum-THREE' in result
        elif 'choice-A' in result:
            pass
        else:
            assert False, 'No valid choice in result'

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def test_exhaustive_simplechoice():
    ''' Test simple choice (only made of basic types, not sequences '''
    typeName = "Type-SingleChoice"
    result = compute_combinations(pool[typeName], pool)
    choiceA = choiceB = choiceC = 0
    expected = {'choice-A': range(256),
                'choice-B': [True, False],
                'choice-C': [{'Enum': 'enum-ONE'},
                             {'Enum': 'enum-TWO'},
                             {'Enum': 'enum-THREE'}]}
    for each in result:
        value = parse_gser('result', each)['result']
        choiceDet = value['Choice']
        assert value[choiceDet] in expected[choiceDet]
        expected[choiceDet].remove(value[choiceDet])
    assert not any(value for value in expected.viewvalues())

def test_random_simpleseqof():
145
146
    ''' Test SEQOF '''
    typeName = "Type-SingleSeqOf"
147
    for i in range(100):
148
149
150
151
152
153
        result = compute_random_value(pool[typeName], pool)
        value = parse_gser('result', result)['result']
        assert 0 <= len(value) <= 5
        for each in value:
            assert type(each) is int and 0 <= each <= 255

154
155
156
157
158
159
160
161
162
163
164
165
def test_exhaustive_tinyseqof():
    ''' Test SEQOF '''
    typeName = "Type-TinySeqOf"  # Size-2 seq of boolean -> 00 01 10 11
    result = compute_combinations(pool[typeName], pool)
    expected = [[False, False], [False, True], [True, False], [True, True]]
    for each in result:
        value = parse_gser('result', each)['result']
        assert value in expected
        expected.remove(value)
    assert not expected

def test_random_mydata():
166
167
    ''' Test more complex sequence '''
    typeName = "MyData"
168
    for i in range(10):
169
170
171
172
173
174
175
176
177
178
        result = compute_random_value(pool[typeName], pool)
        value = parse_gser('result', result)['result']
        assert type(value) is list and 1 <= len(value) <= 10
        for item in value:
            assert 'a' in item and 'b' in item
            assert type(item['a']) is list and len(item['a']) == 10
            for each in item['a']:
                assert type(each) is int and 0 <= each <= 255
            assert item['b']['Enum'] in ('enum-one', 'enum-two')

179
def test_random_seq2():
180
181
    ''' Test more complex sequence '''
    typeName = "Type-Seq"
182
    for i in range(10):
183
184
185
186
187
188
189
190
191
192
193
        result = compute_random_value(pool[typeName], pool)
        value = parse_gser('result', result)['result']
        assert 'item-a' in value and 'item-b' in value and 'item-C' in value \
                and 'item-d' in value
        assert type(value['item-a']) is float and -5.0 <= value['item-a'] <= 5.0
        assert value['item-b']['Choice'] in ('choice-A', 'choice-B', 'choice-C')

if __name__ == '__main__':
    print('You must run py.test-2.7 to execute this test script')
    print('Make sure you have pytest-qt (pip install --user pytest-qt)')
    sys.exit(1)