Passed
Push — master ( ee9159...d47bb7 )
by Matěj
02:05 queued 11s
created

oval_graph.evaluate.eq_or_greater_zero_duo()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 3
dl 0
loc 2
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
"""
2
    Function for evaluate OVAL operators.
3
"""
4
5
6 1
def oval_operator_and(result):
7
    """
8
        The AND operator produces a true result if every argument is true. If one or more arguments
9
        are false, the result of the AND is false. If one or more of the arguments are unknown, and
10
        if none of the arguments are false, then the AND operator produces a result of unknown.
11
    """
12 1
    out_result = None
13 1
    if eq_zero(result, 'false_cnt')\
14
            and greater_zero(result, 'true_cnt')\
15
            and error_unknown_noteval_eq_zero(result):
16 1
        out_result = 'true'
17 1
    elif greater_zero(result, 'false_cnt'):
18 1
        out_result = 'false'
19
    else:
20 1
        out_result = error_unknown_noteval_for_operators_and_or(result, 'and')
21 1
    return out_result
22
23
24 1
def oval_operator_one(result):
25
    """
26
        The ONE operator produces a true result if one and only one argument is true. If there are
27
        more than argument is true (or if there are no true arguments), the result of the ONE
28
        is false. If one or more of the arguments are unknown, then the ONE operator produces
29
        a result of unknown.
30
    """
31 1
    out_result = None
32 1
    if one_is_true(result):
33 1
        out_result = 'true'
34 1
    elif one_is_false(result) or one_is_false1(result):
35 1
        out_result = 'false'
36 1
    elif one_is_error(result):
37 1
        out_result = 'error'
38 1
    elif one_is_unknown(result):
39 1
        out_result = 'unknown'
40 1
    elif one_is_noteval(result):
41 1
        out_result = 'noteval'
42
    else:
43 1
        out_result = None
44 1
    return out_result
45
46
47 1
def oval_operator_or(result):
48
    """
49
        The OR operator produces a true result if one or more arguments is true. If every argument
50
        is false, the result of the OR is false. If one or more of the arguments are unknown and
51
        if none of arguments are true, then the OR operator produces a result of unknown.
52
    """
53 1
    out_result = None
54 1
    if greater_zero(result, 'true_cnt'):
55 1
        out_result = 'true'
56 1
    elif eq_zero(result, 'true_cnt')\
57
            and greater_zero(result, 'false_cnt')\
58
            and error_unknown_noteval_eq_zero(result):
59 1
        out_result = 'false'
60
    else:
61 1
        out_result = error_unknown_noteval_for_operators_and_or(result, 'or')
62 1
    return out_result
63
64
65 1
def oval_operator_xor(result):
66
    """
67
        XOR is defined to be true if an odd number of its arguments are true, and false otherwise.
68
        If any of the arguments are unknown, then the XOR operator produces a result of unknown.
69
    """
70 1
    out_result = None
71 1
    if (result['true_cnt'] % 2) == 1\
72
            and eq_zero_unknown_noteval_notappl(result):
73 1
        out_result = 'true'
74 1
    elif (result['true_cnt'] % 2) == 0\
75
            and eq_zero_unknown_noteval_notappl(result):
76 1
        out_result = 'false'
77 1
    elif greater_zero(result, 'error_cnt'):
78 1
        out_result = 'error'
79 1
    elif eq_zero(result, 'error_cnt')\
80
            and greater_zero(result, 'unknown_cnt'):
81 1
        out_result = 'unknown'
82 1
    elif eq_zero(result, 'error_cnt')\
83
            and eq_zero(result, 'unknown_cnt')\
84
            and greater_zero(result, 'noteval_cnt'):
85 1
        out_result = 'noteval'
86
    else:
87 1
        out_result = None
88 1
    return out_result
89
90
91 1
def error_unknown_noteval_for_operators_and_or(result, operator):
92 1
    out_result = None
93 1
    value_for_check = 'false_cnt' if operator == 'and' else 'true_cnt'
94 1
    if eq_zero(result, value_for_check)\
95
            and greater_zero(result, 'error_cnt'):
96 1
        out_result = 'error'
97 1
    elif eq_zero(result, value_for_check)\
98
            and error_unknown_eq_zero(result):
99 1
        out_result = 'unknown'
100 1
    elif eq_zero(result, value_for_check)\
101
            and error_unknown_eq_noteval_greater_zero(result):
102 1
        out_result = 'noteval'
103
    else:
104 1
        out_result = None
105 1
    return out_result
106
107
108 1
def eq_zero(result, cnt):
109 1
    return result[cnt] == 0
110
111
112 1
def eq_zero_duo(result, cnt0, cnt1):
113 1
    return result[cnt0] == 0 and result[cnt1] == 0
114
115
116 1
def greater_zero(result, cnt):
117 1
    return result[cnt] > 0
118
119
120 1
def eq_or_greater_zero(result, cnt):
121 1
    return result[cnt] >= 0
122
123
124 1
def eq_or_greater_zero_duo(result, cnt0, cnt1):
125 1
    return result[cnt0] >= 0 and result[cnt1] >= 0
126
127
128 1
def smaller_than_two(result, cnt):
129 1
    return result[cnt] < 2
130
131
132 1
def one_is_noteval(result):
133 1
    return (smaller_than_two(result, 'true_cnt')
134
            and eq_or_greater_zero_duo(result, 'false_cnt', 'notappl_cnt')
135
            and eq_zero_duo(result, 'error_cnt', 'unknown_cnt')
136
            and greater_zero(result, 'noteval_cnt'))
137
138
139 1
def one_is_unknown(result):
140 1
    return (smaller_than_two(result, 'true_cnt')
141
            and eq_or_greater_zero(result, 'false_cnt')
142
            and eq_zero(result, 'error_cnt')
143
            and result['unknown_cnt'] >= 1
144
            and eq_or_greater_zero_duo(result, 'noteval_cnt', 'notappl_cnt'))
145
146
147 1
def one_is_error(result):
148 1
    return (smaller_than_two(result, 'true_cnt')
149
            and eq_or_greater_zero(result, 'false_cnt')
150
            and greater_zero(result, 'error_cnt')
151
            and eq_or_greater_zero_unknown_noteval_notappl(result))
152
153
154 1
def one_is_false(result):
155 1
    return (eq_zero(result, 'true_cnt')
156
            and eq_or_greater_zero(result, 'false_cnt')
157
            and error_unknown_noteval_eq_zero(result)
158
            and result['notappl_cnt'] >= 0)
159
160
161 1
def one_is_false1(result):
162 1
    return (result['true_cnt'] >= 2
163
            and eq_or_greater_zero_duo(result, 'false_cnt', 'error_cnt')
164
            and eq_or_greater_zero_unknown_noteval_notappl(result))
165
166
167 1
def one_is_true(result):
168 1
    return (result['true_cnt'] == 1
169
            and eq_or_greater_zero_duo(result, 'false_cnt', 'notappl_cnt')
170
            and error_unknown_noteval_eq_zero(result))
171
172
173 1
def eq_or_greater_zero_unknown_noteval_notappl(result):
174 1
    return (eq_or_greater_zero(result, 'unknown_cnt')
175
            and eq_or_greater_zero(result, 'noteval_cnt')
176
            and eq_or_greater_zero(result, 'notappl_cnt'))
177
178
179 1
def eq_zero_unknown_noteval_notappl(result):
180 1
    return (eq_zero(result, 'error_cnt')
181
            and eq_zero(result, 'unknown_cnt')
182
            and eq_zero(result, 'noteval_cnt'))
183
184
185 1
def error_unknown_noteval_eq_zero(result):
186 1
    return (eq_zero(result, 'error_cnt')
187
            and eq_zero(result, 'unknown_cnt')
188
            and eq_zero(result, 'noteval_cnt'))
189
190
191 1
def error_unknown_eq_noteval_greater_zero(result):
192 1
    return (eq_zero(result, 'error_cnt')
193
            and eq_zero(result, 'unknown_cnt')
194
            and greater_zero(result, 'noteval_cnt'))
195
196
197 1
def error_unknown_eq_zero(result):
198 1
    return (eq_zero(result, 'error_cnt')
199
            and greater_zero(result, 'unknown_cnt'))
200
201
202 1
def is_notapp_result(result):
203 1
    return (result['notappl_cnt'] > 0
204
            and eq_zero(result, 'false_cnt')
205
            and error_unknown_noteval_eq_zero(result)
206
            and eq_zero(result, 'true_cnt'))
207