-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
233 lines (167 loc) · 4.66 KB
/
test.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import bool_wrapper
from lib.test_helpers import run_test
T = bool_wrapper.TRUE
F = bool_wrapper.FALSE
w = bool_wrapper.SYMBOL("w")
x = bool_wrapper.SYMBOL("x")
y = bool_wrapper.SYMBOL("y")
z = bool_wrapper.SYMBOL("z")
def check(expr, expected_str):
if str(expr) != expected_str:
raise AssertionError(f"got {expr} when expecting {expected_str}")
expr.check()
assert expr.RESTRICTS(expr)
@run_test
def associativity():
check(x & (y & z), "x&y&z")
check(x | (y | z), "x|y|z")
check(x & (z & y), "x&y&z")
check(x | (z | y), "x|y|z")
check((x & z) & y, "x&y&z")
check((x | z) | y, "x|y|z")
@run_test
def commutativity():
check(y & x, "x&y")
check(x & y, "x&y")
check(z & x & y, "x&y&z")
check(y | x, "x|y")
check(x | y, "x|y")
check(z | x | y, "x|y|z")
@run_test
def distributivity():
check(x & (z | y), "(x&y)|(x&z)")
check(x | (z & y), "(x|y)&(x|z)")
@run_test
def identity():
check(x & T, "x")
check(x | F, "x")
@run_test
def annihilator():
check(x & F, "F")
check(x | T, "T")
@run_test
def idemptotence():
check(x & x, "x")
check(x | x, "x")
@run_test
def absorption():
check((x | y) & x, "x")
check(x & (x | y), "x")
check(x | (x & y), "x")
check((x & y) | x, "x")
@run_test
def negative_absorption():
check(x & (~x | y), "x&y")
check((~x | y) & x, "x&y")
check(x | (~x & y), "x|y")
check((~x & y) | x, "x|y")
@run_test
def complementation():
check(x & ~x, "F")
check(x | ~x, "T")
check(~x & x, "F")
check(~x | x, "T")
@run_test
def double_negation():
check(~~x, "x")
check(~~~~x, "x")
check(~~~x, "~x")
check(~~~~~x, "~x")
check(~~~~(x | y | z), "x|y|z")
check(~~~~(x & y & z), "x&y&z")
@run_test
def de_morgan():
check(~(x & y), "~x|~y")
check(~(x | y), "~x&~y")
@run_test
def elimination():
check((x & y) | (x & ~y), "x")
check((x | y) & (x | ~y), "x")
# OTHER
@run_test
def negated_vars():
check(~x, "~x")
check(~x & ~x, "~x")
check(~x | ~x, "~x")
@run_test
def restriction():
assert not T.RESTRICTS(x)
assert not x.RESTRICTS(y)
assert not x.RESTRICTS(y & z)
assert not (y & x).RESTRICTS(z)
assert not x.RESTRICTS(~x)
assert not x.RESTRICTS(y | z)
assert not (x & y & z).RESTRICTS(w & x)
assert not (x | w).RESTRICTS(x | y | z)
assert F.RESTRICTS(T)
assert F.RESTRICTS(x)
assert F.RESTRICTS(x & y)
assert x.RESTRICTS(T)
assert (x & y).RESTRICTS(x)
assert (x & y).RESTRICTS(x & y)
assert x.RESTRICTS(x)
assert x.RESTRICTS(x | y)
assert (x | y).RESTRICTS(x | y | z)
assert (x | y).RESTRICTS(x | y)
assert (x & y & z).RESTRICTS(x & y)
assert ((x | y) & z).RESTRICTS(x | y)
assert ((x | y) & (w | z)).RESTRICTS(x | y)
assert (x & y).RESTRICTS((x & y) | (w & z))
assert x.RESTRICTS(x | (z & y))
assert (z & y).RESTRICTS(x | (z & y))
assert (x | (z & y)).RESTRICTS(x | (z & y))
assert T.LOOSENS(F)
assert T.LOOSENS(x)
assert T.LOOSENS(x | y)
assert (x | y).LOOSENS(x)
@run_test
def reductions():
check((x & y) & (x & y & z), "x&y&z")
check((x & y & z) & (x & y), "x&y&z")
check((~w & x & y & z) & (x & y), "~w&x&y&z")
check((x | y) | (x | y | z), "x|y|z")
check((x | y | z) | (x | y), "x|y|z")
check((~w | x | y | z) | (x | y), "~w|x|y|z")
@run_test
def advanced_de_morgan():
check(x | (z & y), "(x|y)&(x|z)")
check(~(x | (y & z)), "(~x&~y)|(~x&~z)")
check(x & (z | y), "(x&y)|(x&z)")
check(~(x & (z | y)), "(~x|~y)&(~x|~z)")
@run_test
def simple_or_clauses():
check(x | y, "x|y")
check(z | y | x, "x|y|z")
@run_test
def negated_or_clauses():
check(x | ~y, "x|~y")
check(~y | x, "x|~y")
check(z | x | ~y, "x|~y|z")
check(z | ~y | x, "x|~y|z")
check(z | x | ~y | w, "w|x|~y|z")
check(z | x | ~y | ~w, "~w|x|~y|z")
check(z | x | ~y | w | z, "w|x|~y|z")
check(z | x | ~y | ~w | z, "~w|x|~y|z")
check((z | x) | (~y | w | z), "w|x|~y|z")
check((z | x) | (~y | ~w | z), "~w|x|~y|z")
check((z | x) | ~x, "T")
check((~z | x) | (y | ~z), "x|y|~z")
check((x | z) | (x | ~z), "T")
@run_test
def negated_and():
check((z & x) & ~x, "F")
@run_test
def simple_and_clauses():
check(x & y, "x&y")
check(x & y & z, "x&y&z")
check(z & x & y, "x&y&z")
check((z & x) & y, "x&y&z")
check((z & x) & (y & w), "w&x&y&z")
check((z & x) & (y & ~w), "~w&x&y&z")
check((z & x) & (y & ~w) & T, "~w&x&y&z")
check((z & x) & (y & ~w) & F, "F")
@run_test
def tricky():
check((x & y) & (~x | ~y), "F")
check((x & y) | (x | y), "x|y")
check(x & ((x) & (y)) | (~(x) | (y)), "~x|y")