-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
310 lines (263 loc) · 14.3 KB
/
tests.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# -*- coding: utf-8 -*-
# pylint: disable=missing-docstring
from __future__ import unicode_literals
from forseti.formula import Symbol, Not, And, Or, If, Iff, Predicate
from nose import runmodule
from nose.tools import assert_equal, raises
from parameterized import parameterized
import sys
import truthtables
PYTHON_2 = sys.version_info[0] == 2
def test_symbol():
table = truthtables.runner("A")
assert_equal([[[True]], [[False]]], table.broken_evaluation)
assert_equal([[Symbol("A")]], table.broken_formulas)
assert_equal([[True], [False]], table.combinations)
assert_equal([[[]], [[]]], table.connective_evaluation)
assert_equal([[True], [False]], table.evaluation)
assert_equal([Symbol("A")], table.formulas)
assert_equal([[]], table.main_connective_index)
assert_equal([Symbol("A")], table.symbols)
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is TT-Possible", table.get_table_assessment()[0])
def test_not():
table = truthtables.runner("not(A)")
assert_equal([[[False, True]], [[True, False]]], table.broken_evaluation)
assert_equal([[Not(Symbol("A")), Symbol("A")]], table.broken_formulas)
assert_equal([[True], [False]], table.combinations)
assert_equal([[[False]], [[True]]], table.connective_evaluation)
assert_equal([[False], [True]], table.evaluation)
assert_equal([Not(Symbol("A"))], table.formulas)
assert_equal([[0, 0]], table.main_connective_index)
assert_equal([Symbol("A")], table.symbols)
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is TT-Possible", table.get_table_assessment()[0])
def test_and():
table = truthtables.runner("and(A, B)")
assert_equal([[[True, True, True]], [[True, False, False]], [[False, False, True]],
[[False, False, False]]], table.broken_evaluation)
assert_equal([[Symbol("A"), And(Symbol("A"), Symbol("B")), Symbol("B")]],
table.broken_formulas)
assert_equal([[True, True], [True, False], [False, True], [False, False]], table.combinations)
assert_equal([[[True]], [[False]], [[False]], [[False]]], table.connective_evaluation)
assert_equal([[True], [False], [False], [False]], table.evaluation)
assert_equal([And(Symbol("A"), Symbol("B"))], table.formulas)
assert_equal([[1, 0]], table.main_connective_index)
assert_equal([Symbol("A"), Symbol("B")], table.symbols)
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is TT-Possible", table.get_table_assessment()[0])
def test_or():
table = truthtables.runner(str("or(A, B)"))
assert_equal([[[True, True, True]], [[True, True, False]], [[False, True, True]],
[[False, False, False]]], table.broken_evaluation)
assert_equal([[Symbol("A"), Or(Symbol("A"), Symbol("B")), Symbol("B")]], table.broken_formulas)
assert_equal([[True, True], [True, False], [False, True], [False, False]], table.combinations)
assert_equal([[[True]], [[True]], [[True]], [[False]]], table.connective_evaluation)
assert_equal([[True], [True], [True], [False]], table.evaluation)
assert_equal([Or(Symbol("A"), Symbol("B"))], table.formulas)
assert_equal([[1, 0]], table.main_connective_index)
assert_equal([Symbol("A"), Symbol("B")], table.symbols)
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is TT-Possible", table.get_table_assessment()[0])
def test_if():
table = truthtables.runner(str("if(A, B)"))
assert_equal([[[True, True, True]], [[True, False, False]], [[False, True, True]],
[[False, True, False]]], table.broken_evaluation)
assert_equal([[Symbol("A"), If(Symbol("A"), Symbol("B")), Symbol("B")]], table.broken_formulas)
assert_equal([[True, True], [True, False], [False, True], [False, False]], table.combinations)
assert_equal([[[True]], [[False]], [[True]], [[True]]], table.connective_evaluation)
assert_equal([[True], [False], [True], [True]], table.evaluation)
assert_equal([If(Symbol("A"), Symbol("B"))], table.formulas)
assert_equal([[1, 0]], table.main_connective_index)
assert_equal([Symbol("A"), Symbol("B")], table.symbols)
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is TT-Possible", table.get_table_assessment()[0])
def test_iff():
table = truthtables.runner(str("iff(A, B)"))
assert_equal([[[True, True, True]], [[True, False, False]], [[False, False, True]],
[[False, True, False]]], table.broken_evaluation)
assert_equal([[Symbol("A"), Iff(Symbol("A"), Symbol("B")), Symbol("B")]],
table.broken_formulas)
assert_equal([[True, True], [True, False], [False, True], [False, False]], table.combinations)
assert_equal([[[True]], [[False]], [[False]], [[True]]], table.connective_evaluation)
assert_equal([[True], [False], [False], [True]], table.evaluation)
assert_equal([Iff(Symbol("A"), Symbol("B"))], table.formulas)
assert_equal([[1, 0]], table.main_connective_index)
assert_equal([Symbol("A"), Symbol("B")], table.symbols)
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is TT-Possible", table.get_table_assessment()[0])
def test_not_and():
table = truthtables.runner(str("not(and(A, B))"))
assert_equal([[[False, True, True, True]], [[True, True, False, False]],
[[True, False, False, True]], [[True, False, False, False]]],
table.broken_evaluation)
assert_equal([[Not(And(Symbol("A"), Symbol("B"))), Symbol("A"), And(Symbol("A"), Symbol("B")), Symbol("B")]],
table.broken_formulas)
assert_equal([[True, True], [True, False], [False, True], [False, False]], table.combinations)
assert_equal([[[False, True]], [[True, False]], [[True, False]], [[True, False]]],
table.connective_evaluation)
assert_equal([[False], [True], [True], [True]], table.evaluation)
assert_equal([Not(And(Symbol("A"), Symbol("B")))], table.formulas)
assert_equal([[0, 0]], table.main_connective_index)
assert_equal([Symbol("A"), Symbol("B")], table.symbols)
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is TT-Possible", table.get_table_assessment()[0])
def test_and_or():
table = truthtables.runner("and(or(A, C), or(B,C))")
# A or C and B or C
assert_equal([[[True, True, True, True, True, True, True]],
[[True, True, False, True, True, True, False]],
[[True, True, True, True, False, True, True]],
[[True, True, False, False, False, False, False]],
[[False, True, True, True, True, True, True]],
[[False, False, False, False, True, True, False]],
[[False, True, True, True, False, True, True]],
[[False, False, False, False, False, False, False]]],
table.broken_evaluation)
assert_equal([[Symbol("A"), Or(Symbol("A"), Symbol("C")), Symbol("C"),
And(Or(Symbol("A"), Symbol("C")), Or(Symbol("B"), Symbol("C"))), Symbol("B"),
Or(Symbol("B"), Symbol("C")), Symbol("C")]], table.broken_formulas)
assert_equal([[True, True, True], [True, True, False], [True, False, True],
[True, False, False], [False, True, True], [False, True, False],
[False, False, True], [False, False, False]], table.combinations)
assert_equal([[[True, True, True]], [[True, True, True]], [[True, True, True]],
[[True, False, False]], [[True, True, True]], [[False, False, True]],
[[True, True, True]], [[False, False, False]]],
table.connective_evaluation)
assert_equal([[True], [True], [True], [False], [True], [False], [True], [False]],
table.evaluation)
assert_equal([And(Or(Symbol("A"), Symbol("C")), Or(Symbol("B"), Symbol("C")))], table.formulas)
assert_equal([[3, 1]], table.main_connective_index)
assert_equal([Symbol("A"), Symbol("B"), Symbol("C")], table.symbols)
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is TT-Possible", table.get_table_assessment()[0])
if not PYTHON_2:
assert_equal("A B C | ((A ∨ C) ∧ (B ∨ C))\n" +
"---------------------------\n" +
"T T T | T -T T \n" +
"T T F | T -T T \n" +
"T F T | T -T T \n" +
"T F F | T -F F \n" +
"F T T | T -T T \n" +
"F T F | F -F T \n" +
"F F T | T -T T \n" +
"F F F | F -F F \n", table.generate_table())
def test_and_not_or():
table = truthtables.runner(str("and(not(or(A, C)), B)"), display_connectives=False)
assert_equal([[[False, True, True, True, False, True]],
[[False, True, True, False, False, True]],
[[False, True, True, True, False, False]],
[[False, True, True, False, False, False]],
[[False, False, True, True, False, True]],
[[True, False, False, False, True, True]],
[[False, False, True, True, False, False]],
[[True, False, False, False, False, False]]],
table.broken_evaluation)
assert_equal([[Not(Or(Symbol("A"), Symbol("C"))), Symbol("A"), Or(Symbol("A"), Symbol("C")),
Symbol("C"), And(Not(Or(Symbol("A"), Symbol("C"))), Symbol("B")), Symbol("B")]],
table.broken_formulas)
assert_equal([[True, True, True], [True, True, False], [True, False, True],
[True, False, False], [False, True, True], [False, True, False],
[False, False, True], [False, False, False]], table.combinations)
assert_equal([[[False, True, False]], [[False, True, False]], [[False, True, False]],
[[False, True, False]], [[False, True, False]], [[True, False, True]],
[[False, True, False]], [[True, False, False]]],
table.connective_evaluation)
assert_equal([[False], [False], [False], [False], [False], [True], [False], [False]],
table.evaluation)
assert_equal([And(Not(Or(Symbol("A"), Symbol("C"))), Symbol("B"))], table.formulas)
assert_equal([[4, 2]], table.main_connective_index)
assert_equal([Symbol("A"), Symbol("B"), Symbol("C")], table.symbols)
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is TT-Possible", table.get_table_assessment()[0])
if not PYTHON_2:
assert_equal("A B C | (¬(A ∨ C) ∧ B)\n" +
"----------------------\n" +
"T T T | -F \n" +
"T T F | -F \n" +
"T F T | -F \n" +
"T F F | -F \n" +
"F T T | -F \n" +
"F T F | -T \n" +
"F F T | -F \n" +
"F F F | -F \n", table.generate_table())
def test_tautology():
table = truthtables.runner("or(A,not(A))")
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is a Tautology", table.get_table_assessment()[0])
def test_contradiction():
table = truthtables.runner("and(A,not(A))")
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is a Contradiction", table.get_table_assessment()[0])
def test_empty_statements():
table = truthtables.runner(["A", ''])
assert_equal([[[True]], [[False]]], table.broken_evaluation)
assert_equal([[Symbol("A")]], table.broken_formulas)
assert_equal([[True], [False]], table.combinations)
assert_equal([[[]], [[]]], table.connective_evaluation)
assert_equal([[True], [False]], table.evaluation)
assert_equal([Symbol("A")], table.formulas)
assert_equal([[]], table.main_connective_index)
assert_equal([Symbol("A")], table.symbols)
assert_equal(1, len(table.get_table_assessment()))
assert_equal("Sentence is TT-Possible", table.get_table_assessment()[0])
def test_set_1():
table = truthtables.runner(["A", "A"])
assert_equal([[[True], [True]], [[False], [False]]], table.broken_evaluation)
assert_equal([[Symbol("A")], [Symbol("A")]], table.broken_formulas)
assert_equal([[True], [False]], table.combinations)
assert_equal([[[], []], [[], []]], table.connective_evaluation)
assert_equal([[True, True], [False, False]], table.evaluation)
assert_equal([Symbol("A"), Symbol("A")], table.formulas)
assert_equal([[], []], table.main_connective_index)
assert_equal([Symbol("A")], table.symbols)
assert_equal(4, len(table.get_table_assessment()))
def test_set_2():
table = truthtables.runner(["A", "B"])
assert_equal(1, len(table.get_table_assessment()))
@raises(TypeError)
def test_truthtables_runner_invalid_type():
truthtables.runner(1)
@raises(TypeError)
def test_truthtables_invalid_type():
truthtables.TruthTable(1)
@raises(TypeError)
def test_truthtables_invalid_type_list():
truthtables.TruthTable([1])
@parameterized([
(Symbol('a'), 'a'),
(Not(Symbol('a')), '¬a'),
(And(Symbol('a'), Symbol('b')), '(a ∧ b)'),
(Or(Symbol('a'), Symbol('b')), '(a ∨ b)'),
(If(Symbol('a'), Symbol('b')), '(a → b)'),
(Iff(Symbol('a'), Symbol('b')), '(a ↔ b)'),
(Not(And(
Symbol('a'),
If(Symbol('c'), Iff(Or(Symbol('d'), Symbol('a')), Symbol('c')))
)), '¬(a ∧ (c → ((d ∨ a) ↔ c)))')
])
def test_pretty_print(formula, expected):
assert_equal(expected, truthtables.pretty_print(formula))
@raises(TypeError)
def test_pretty_print_raises():
class Foo(Predicate):
name = "not"
arity = 1
truthtables.pretty_print(Foo)
@parameterized([
([[True], [False]], 1),
([[True, True], [True, False], [False, True], [False, False]], 2),
(
[
[True, True, True], [True, True, False], [True, False, True],
[True, False, False], [False, True, True], [False, True, False],
[False, False, True], [False, False, False]
],
3
)
])
def test_combinations(expected, num):
actual = truthtables.get_combinations(num)
assert_equal(expected, actual)
if __name__ == "__main__":
runmodule()