Test Failed
Pull Request — master (#187)
by Jan
02:35
created

oval_graph.oval_tree.evaluate.one_is_true()   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.512

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 13
ccs 1
cts 5
cp 0.2
crap 1.512
rs 10
c 0
b 0
f 0
1
"""
2
    Function for evaluate OVAL operators.
3
"""
4
5
6 1
def oval_operator_and(result):
7
    """The AND operator produces a true result if every argument is true. If one or more arguments
8
       are false, the result of the AND is false. If one or more of the arguments are unknown, and
9
       if none of the arguments are false, then the AND operator produces a result of unknown.
10
11
    Args:
12
            result (dict): Dictionary storing occurrence of given values
13
14
    Returns:
15
            str. return values::
16
17
                true
18
                false
19
                error
20
                unknown
21
                noteval
22
    """
23
    out_result = None
24
    eval_eq_zero = eq_zero(result, 'false_cnt')
25
    eval_greater_zero = greater_zero(result, 'true_cnt')
26
    if eval_eq_zero and eval_greater_zero and error_unknown_noteval_eq_zero(result):
27
        out_result = 'true'
28
    elif greater_zero(result, 'false_cnt'):
29
        out_result = 'false'
30
    else:
31
        out_result = error_unknown_noteval_for_operators_and_or(result, 'and')
32
    return out_result
33
34
35 1
def oval_operator_one(result):
36
    """The ONE operator produces a true result if one and only one argument is true. If there are
37
       more than argument is true (or if there are no true arguments), the result of the ONE
38
       is false. If one or more of the arguments are unknown, then the ONE operator produces
39
       a result of unknown.
40
41
    Args:
42
            result (dict): Dictionary storing occurrence of given values
43
44
    Returns:
45
            str. return values::
46
47
                true
48
                false
49
                error
50
                unknown
51
                noteval
52
    """
53
    out_result = None
54
    if one_is_true(result):
55
        out_result = 'true'
56
    elif one_is_false(result):
57
        out_result = 'false'
58
    elif one_is_error(result):
59
        out_result = 'error'
60
    elif one_is_unknown(result):
61
        out_result = 'unknown'
62
    elif one_is_noteval(result):
63
        out_result = 'noteval'
64
    return out_result
65
66
67 1
def oval_operator_or(result):
68
    """The OR operator produces a true result if one or more arguments is true. If every argument
69
       is false, the result of the OR is false. If one or more of the arguments are unknown and
70
       if none of arguments are true, then the OR operator produces a result of unknown.
71
72
    Args:
73
            result (dict): Dictionary storing occurrence of given values
74
75
    Returns:
76
            str. return values::
77
78
                true
79
                false
80
                error
81
                unknown
82
                noteval
83
    """
84
    out_result = None
85
    eval_eq_zero = eq_zero(result, 'true_cnt')
86
    eval_greater_zero = greater_zero(result, 'false_cnt')
87
    if greater_zero(result, 'true_cnt'):
88
        out_result = 'true'
89
    elif eval_eq_zero and eval_greater_zero and error_unknown_noteval_eq_zero(result):
90
        out_result = 'false'
91
    else:
92
        out_result = error_unknown_noteval_for_operators_and_or(result, 'or')
93
    return out_result
94
95
96 1
def oval_operator_xor(result):
97
    """XOR is defined to be true if an odd number of its arguments are true, and false otherwise.
98
       If any of the arguments are unknown, then the XOR operator produces a result of unknown.
99
100
    Args:
101
            result (dict): Dictionary storing occurrence of given values
102
103
    Returns:
104
            str. return values::
105
106
                true
107
                false
108
                error
109
                unknown
110
                noteval
111
    """
112
    out_result = None
113
    eval_error_cnt = eq_zero(result, 'error_cnt')
114
    eval_unknown_cnt = eq_zero(result, 'unknown_cnt')
115
    if (result['true_cnt'] % 2) == 1 and eq_zero_unknown_noteval_notappl(result):
116
        out_result = 'true'
117
    elif (result['true_cnt'] % 2) == 0 and eq_zero_unknown_noteval_notappl(result):
118
        out_result = 'false'
119
    elif greater_zero(result, 'error_cnt'):
120
        out_result = 'error'
121
    elif eq_zero(result, 'error_cnt') and greater_zero(result, 'unknown_cnt'):
122
        out_result = 'unknown'
123
    elif eval_error_cnt and eval_unknown_cnt and greater_zero(result, 'noteval_cnt'):
124
        out_result = 'noteval'
125
    return out_result
126
127
128 1
def error_unknown_noteval_for_operators_and_or(result, operator):
129
    """Evaluates if the result match the values for error, unknown, noteval.
130
131
    Args:
132
            result (dict): Dictionary storing occurrence of given values
133
            operator (str): Specifies for which operator is used
134
135
    Returns:
136
            str or None. return values::
137
138
                error
139
                unknown
140
                notappl
141
    """
142
    out_result = None
143
    value_for_check = 'false_cnt' if operator == 'and' else 'true_cnt'
144
    if eq_zero(result, value_for_check) and greater_zero(result, 'error_cnt'):
145
        out_result = 'error'
146
    elif eq_zero(result, value_for_check) and error_unknown_eq_zero(result):
147
        out_result = 'unknown'
148
    elif eq_zero(result, value_for_check) and error_unknown_eq_noteval_greater_zero(result):
149
        out_result = 'noteval'
150
    return out_result
151
152
153 1
def eq_zero(result, cnt):
154
    return result[cnt] == 0
155
156
157 1
def eq_zero_duo(result, cnt0, cnt1):
158
    return result[cnt0] == 0 and result[cnt1] == 0
159
160
161 1
def greater_zero(result, cnt):
162
    return result[cnt] > 0
163
164
165 1
def eq_or_greater_zero(result, cnt):
166
    return result[cnt] >= 0
167
168
169 1
def eq_or_greater_zero_duo(result, cnt0, cnt1):
170
    return result[cnt0] >= 0 and result[cnt1] >= 0
171
172
173 1
def smaller_than_two(result, cnt):
174
    return result[cnt] < 2
175
176
177 1
def one_is_noteval(result):
178
    """Evaluates if the result match noteval for one operator.
179
180
    Args:
181
            result (dict): Dictionary storing occurrence of given values
182
183
    Returns:
184
            bool.
185
    """
186
    true_cnt = smaller_than_two(result, 'true_cnt')
187
    false_cnt_notappl_cnt = eq_or_greater_zero_duo(result, 'false_cnt', 'notappl_cnt')
188
    error_cnt_unknown_cnt = eq_zero_duo(result, 'error_cnt', 'unknown_cnt')
189
    noteval_cnt = greater_zero(result, 'noteval_cnt')
190
    return true_cnt and false_cnt_notappl_cnt and error_cnt_unknown_cnt and noteval_cnt
191
192
193 1
def one_is_unknown(result):
194
    """Evaluates if the result match unknown for one operator.
195
196
    Args:
197
            result (dict): Dictionary storing occurrence of given values
198
199
    Returns:
200
            bool.
201
    """
202
    true_cnt = smaller_than_two(result, 'true_cnt')
203
    false_cnt = eq_or_greater_zero(result, 'false_cnt')
204
    error_cnt = eq_zero(result, 'error_cnt')
205
    unknown_cnt = result['unknown_cnt'] >= 1
206
    noteval_cnt_notappl_cnt = eq_or_greater_zero_duo(result, 'noteval_cnt', 'notappl_cnt')
207
    return true_cnt and false_cnt and error_cnt and unknown_cnt and noteval_cnt_notappl_cnt
208
209
210 1
def one_is_error(result):
211
    """Evaluates if the result match unknown for one operator.
212
213
    Args:
214
            result (dict): Dictionary storing occurrence of given values
215
216
    Returns:
217
            bool.
218
    """
219
    true_cnt = smaller_than_two(result, 'true_cnt')
220
    false_cnt = eq_or_greater_zero(result, 'false_cnt')
221
    error_cnt = greater_zero(result, 'error_cnt')
222
    unknown_noteval_notappl = eq_or_greater_zero_unknown_noteval_notappl(result)
223
    return true_cnt and false_cnt and error_cnt and unknown_noteval_notappl
224
225
226 1
def one_is_false(result):
227
    """Evaluates if the result match false for one operator.
228
       Operator ONE has two cases of false state.
229
230
    Args:
231
            result (dict): Dictionary storing occurrence of given values
232
233
    Returns:
234
            bool.
235
    """
236
    # The first case for false
237
    true_cnt = eq_zero(result, 'true_cnt')
238
    false_cnt = eq_or_greater_zero(result, 'false_cnt')
239
    unknown_noteval_cnt = error_unknown_noteval_eq_zero(result)
240
    notappl_cnt = result['notappl_cnt'] >= 0
241
242
    first_case = true_cnt and false_cnt and unknown_noteval_cnt and notappl_cnt
243
244
    # The second case for false
245
    true_cnt = result['true_cnt'] >= 2
246
    false_error_cnt = eq_or_greater_zero_duo(result, 'false_cnt', 'error_cnt')
247
    unknown_noteval_notappl = eq_or_greater_zero_unknown_noteval_notappl(result)
248
    second_case = true_cnt and false_error_cnt and unknown_noteval_notappl
249
250
    return first_case or second_case
251
252
253 1
def one_is_true(result):
254
    """Evaluates if the result match true for one operator.
255
256
    Args:
257
            result (dict): Dictionary storing occurrence of given values
258
259
    Returns:
260
            bool.
261
    """
262
    true_cnt = result['true_cnt'] == 1
263
    false_notappl_cnt = eq_or_greater_zero_duo(result, 'false_cnt', 'notappl_cnt')
264
    unknown_noteval_cnt = error_unknown_noteval_eq_zero(result)
265
    return true_cnt and false_notappl_cnt and unknown_noteval_cnt
266
267
268 1
def eq_or_greater_zero_unknown_noteval_notappl(result):
269
    unknown_cnt = eq_or_greater_zero(result, 'unknown_cnt')
270
    noteval_cnt = eq_or_greater_zero(result, 'noteval_cnt')
271
    notappl_cnt = eq_or_greater_zero(result, 'notappl_cnt')
272
    return unknown_cnt and notappl_cnt and noteval_cnt
273
274
275 1
def eq_zero_unknown_noteval_notappl(result):
276
    error_cnt = eq_zero(result, 'error_cnt')
277
    unknown_cnt = eq_zero(result, 'unknown_cnt')
278
    noteval_cnt = eq_zero(result, 'noteval_cnt')
279
    return error_cnt and unknown_cnt and noteval_cnt
280
281
282 1
def error_unknown_noteval_eq_zero(result):
283
    error_cnt = eq_zero(result, 'error_cnt')
284
    unknown_cnt = eq_zero(result, 'unknown_cnt')
285
    noteval_cnt = eq_zero(result, 'noteval_cnt')
286
    return error_cnt and unknown_cnt and noteval_cnt
287
288
289 1
def error_unknown_eq_noteval_greater_zero(result):
290
    error_cnt = eq_zero(result, 'error_cnt')
291
    unknown_cnt = eq_zero(result, 'unknown_cnt')
292
    noteval_cnt = greater_zero(result, 'noteval_cnt')
293
    return error_cnt and unknown_cnt and noteval_cnt
294
295
296 1
def error_unknown_eq_zero(result):
297
    return eq_zero(result, 'error_cnt') and greater_zero(result, 'unknown_cnt')
298
299
300 1
def is_notapp_result(result):
301
    """Evaluates if the counts of values in
302
       the result matches the notapp result.
303
304
    Args:
305
            result (dict): Dictionary storing occurrence of given values
306
307
    Returns:
308
            bool.
309
     """
310
    notappl_cnt = result['notappl_cnt'] > 0
311
    false_cnt = eq_zero(result, 'false_cnt')
312
    unknown_noteval_cnt = error_unknown_noteval_eq_zero(result)
313
    true_cnt = eq_zero(result, 'true_cnt')
314
    return notappl_cnt and false_cnt and unknown_noteval_cnt and true_cnt
315