-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProofTest.java
executable file
·416 lines (387 loc) · 15.4 KB
/
ProofTest.java
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import junit.framework.TestCase;
public class ProofTest extends TestCase {
//testProof1 is our basic proof and tests that a simple case can be completed.
public void testProof1() throws IllegalLineException, IllegalInferenceException{
Proof p = new Proof(new TheoremSet());
p.extendProof("show (q=>q)");
p.extendProof("assume q");
p.extendProof("ic 2 (q=>q)");
assertTrue(p.isComplete());
}
//testProof2 tests going in and out of subproofs correctly using nextLineNumber()
public void testProof2() throws IllegalLineException, IllegalInferenceException{
Proof p = new Proof(new TheoremSet());
p.extendProof("show (p=>(~p=>q))");
p.extendProof("assume p");
p.extendProof("show (~p=>q)");
assertEquals("3.1", p.nextLineNumber()); //goes into subproof correctly
p.extendProof("assume ~p");
p.extendProof("co 2 3.1 (~p=>q)");
assertEquals("4", p.nextLineNumber()); //exits subproof correctly
p.extendProof("ic 3 (p=>(~p=>q))");
assertTrue(p.isComplete());
}
//testProof3 tests exiting multiple subproofs in a row. We initially had an issue with this because the most recent
//"show" statement had not been popped off before nextLineNumber was called, so this tests it specifically.
//This also tests "mt" and "co".
public void testProof3() throws IllegalLineException, IllegalInferenceException{
Proof p = new Proof(new TheoremSet());
p.extendProof("show (((p=>q)=>q)=>((q=>p)=>p))");
p.extendProof("assume ((p=>q)=>q)");
p.extendProof("show ((q=>p)=>p)");
p.extendProof("assume (q=>p)");
p.extendProof("show p");
p.extendProof("assume ~p");
p.extendProof("mt 3.2.1 3.1 ~q");
p.extendProof("mt 2 3.2.2 ~(p=>q)");
p.extendProof("show (p=>q)");
p.extendProof("assume p");
p.extendProof("co 3.2.4.1 3.2.1 (p=>q)");
//testing exiting multiple subproofs in a row
assertEquals("3.2.5", p.nextLineNumber());
p.extendProof("co 3.2.4 3.2.3 p");
assertEquals("3.3", p.nextLineNumber());
p.extendProof("ic 3.2 ((q=>p)=>p)");
assertEquals("4", p.nextLineNumber());
p.extendProof("ic 3 (((p=>q)=>q)=>((q=>p)=>p))");
assertTrue(p.isComplete());
}
//This tests the repeat function. It also tests if you try to finish a subproof but it throws an exception, the "show"
//statement does not get popped off prematurely. This tests "ic" as well.
public void testRepeat() throws IllegalLineException, IllegalInferenceException{
Proof p = new Proof(new TheoremSet());
p.extendProof("show (p=>p)");
p.extendProof("show (p=>p)");
p.extendProof("assume p");
try {
p.extendProof("ic 2 (p=>p)");
assertTrue(false);
} catch (Exception e){
assertTrue(e instanceof IllegalInferenceException);
}
p.extendProof("ic 2.1 (p=>p)");
assertEquals("3", p.nextLineNumber());
p.extendProof("repeat 2 (p=>p)");
assertTrue(p.isComplete());
}
//This tests "mp" specifically and more "ic" statements.
public void testSampleProof6() throws IllegalLineException, IllegalInferenceException{
Proof p = new Proof(new TheoremSet());
p.extendProof("show ((a=>(b=>c))=>((a=>b)=>(a=>c)))"); //1
p.extendProof("assume (a=>(b=>c))"); //2
assertEquals("3", p.nextLineNumber());
p.extendProof("show ((a=>b)=>(a=>c))"); //3
p.extendProof("assume (a=>b)"); //3.1
p.extendProof("show (a=>c)"); //3.2
p.extendProof("assume a"); //3.2.1
p.extendProof("mp 2 3.2.1 (b=>c)"); //3.2.2
p.extendProof("mp 3.2.1 3.1 b"); //3.2.3
assertEquals("3.2.4", p.nextLineNumber());
p.extendProof("mp 3.2.3 3.2.2 c"); //3.2.4
p.extendProof("ic 3.2.4 (a=>c)"); //3.2.5
p.extendProof("ic 3.2 ((a=>b)=>(a=>c))"); //3.3
p.extendProof("ic 3 ((a=>(b=>c))=>((a=>b)=>(a=>c)))"); // 4
assertTrue(p.isComplete());
}
//This proof tests the use of theorems and tries to catch a mixture of different errors
public void testTheorems() throws IllegalLineException, IllegalInferenceException{
TheoremSet s = new TheoremSet();
s.put("not-or-creation", new Expression("(~p=>(~q=>~(p|q)))"));
Proof p = new Proof(s);
assertEquals("1", p.nextLineNumber());
p.extendProof("show ((a=>q)=>((b=>q)=>((a|b)=>q)))");
try {
p.extendProof("assume a=>q"); //Tests line format (lacks parens)
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalLineException);
}
assertEquals("2", p.nextLineNumber());
p.extendProof("assume (a=>q)");
assertEquals("3", p.nextLineNumber());
p.extendProof("show ((b=>q)=>((a|b)=>q))");
try {
p.extendProof("assume b"); //Tests assume (have to assume whole left side of inference)
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalInferenceException);
}
assertEquals("3.1", p.nextLineNumber());
p.extendProof("assume (b=>q)");
assertEquals("3.2", p.nextLineNumber());
p.extendProof("show ((a|b)=>q)");
try {
p.extendProof("assume (a&b)"); //Tests assume (have to assume whole left side of inference)
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalInferenceException);
}
try {
p.extendProof("assume q"); //Tests assume (have to assume whole left side of inference)
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalInferenceException);
}
assertEquals("3.2.1", p.nextLineNumber());
p.extendProof("assume (a|b)");
assertEquals("3.2.2", p.nextLineNumber());
p.extendProof("show q");
try {
p.extendProof("assume (~q)"); //Tests line format (unnecessary parens)
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalLineException);
}
assertEquals("3.2.2.1", p.nextLineNumber());
p.extendProof("assume ~q");
try {
p.extendProof("mt 2.2.2.1 2 a"); //bad line number (line number doesn't exist)
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalInferenceException);
}
try {
p.extendProof("mt 3.2.1 2 a"); //bad inference (should be ~a)
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalInferenceException);
}
try {
p.extendProof("mt 3.2.2.1 2 a"); //bad inference (wrong line number)
assertTrue(false);
} catch (Exception e) {
System.out.println(e);
assertTrue(e instanceof IllegalInferenceException);
}
assertEquals("3.2.2.2", p.nextLineNumber());
p.extendProof("mt 3.2.2.1 2 ~a");
assertEquals("3.2.2.3", p.nextLineNumber());
p.extendProof("mt 3.1 3.2.2.1 ~b");
try {
p.extendProof("not-o-creation (~a=>(~b=>~(a|b)))"); //Tests theorem usage: theorem name is spelled wrong
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalLineException);
}
try {
p.extendProof("not-or-creation (~a=>(~b=>~(b|b)))"); //bad theorem application: p is a theorem variable
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalInferenceException);
}
assertEquals("3.2.2.4", p.nextLineNumber());
p.extendProof("not-or-creation (~a=>(~b=>~(a|b)))");
p.extendProof("mp 3.2.2.4 3.2.2.2 (~b=>~(a|b))");
try {
p.extendProof("mq 3.2.2.3 3.2.2.5 ~(a|b)"); //since reason isn't mp, mt, co, or ic, it must be a theorem name but it does not exist
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalLineException);
}
p.extendProof("mp 3.2.2.3 3.2.2.5 ~(a|b)");
p.extendProof("co 3.2.2.6 3.2.1 ((a=>q)=>((b=>q)=>((a|b)=>q)))");
p.extendProof("co 3.2.2.6 3.2.1 q");
try {
p.extendProof("ic 3.2.2.8 ((a|b)=>q)"); //cannot access this reference line
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalInferenceException);
}
p.extendProof("ic 3.2.2 ((a|b)=>q)");
p.extendProof("ic 3.2 ((b=>q)=>((a|b)=>q))");
p.extendProof("ic 3 ((a=>q)=>((b=>q)=>((a|b)=>q)))");
assertTrue(p.isComplete());
}
//testSampleProof3 tests multiple tildes and makes sure that it does not affect the flow of the proof.
public void testSampleProof3() throws IllegalLineException, IllegalInferenceException {
Proof p = new Proof(new TheoremSet());
p.extendProof("show (~~p=>p)");
p.extendProof("assume ~~p");
assertEquals("3", p.nextLineNumber());
p.extendProof("show p");
p.extendProof("assume ~p");
assertEquals("3.2", p.nextLineNumber());
p.extendProof("co 2 3.1 p");
p.extendProof("ic 3 (~~p=>p)");
assertTrue(p.isComplete());
}
//testSampleProof1 tests that the first line of a proof has to be a "show" statement. It also tests random garbage statements that
//users could mistakenly put in that should not break the code. It also tests that you cannot put an assume statement after a non-
//show statement.
public void testSampleProof1() throws IllegalLineException, IllegalInferenceException {
Proof p = new Proof(new TheoremSet());
try {
p.extendProof("assume p");
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalInferenceException);
}
p.extendProof("show ((a=>b)=>((b=>c)=>(a=>c)))");
p.extendProof("assume (a=>b)");
p.extendProof("show ((b=>c)=>(a=>c))");
try {
p.extendProof("aasdfjas;dlf"); //inserting garbage
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalLineException);
}
p.extendProof("assume (b=>c)");
p.extendProof("show (a=>c)");
p.extendProof("assume a");
p.extendProof("show c");
p.extendProof("assume ~c");
p.extendProof("mt 3.2.2.1 3.1 ~b");
p.extendProof("mt 2 3.2.2.2 ~a");
try {
p.extendProof("assume ~c"); //testing putting assume after a non-show statement
assertTrue(false);
}catch (Exception e) {
assertTrue(e instanceof IllegalInferenceException);
}
try {
p.extendProof("co 3.2.2.3 3.2.1");
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalLineException);
}
p.extendProof("co 3.2.2.3 3.2.1 c");
try {
p.extendProof("ic 3.2.2.4 (a=>c)");
assertTrue(false);
} catch (Exception e) {
assertTrue(e instanceof IllegalInferenceException);
}
p.extendProof("ic 3.2.2 (a=>c)");
p.extendProof("ic 3.2 ((b=>c)=>(a=>c))");
p.extendProof("ic 3 ((a=>b)=>((b=>c)=>(a=>c)))");
assertTrue(p.isComplete());
}
// testing a valid application of a theorem with nested expressions
public void testTheoremChecker1() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("(((a|b)=>c)=>(a|b))");
Expression theorem = new Expression("((a=>c)=>a)");
assertTrue(Proof.isEqualToTheorem(app, theorem));
}
// testing an invalid application of a theorem with nested expressions
public void testTheoremChecker2() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("(((a|b)&(b|c))=>(c|b))");
Expression theorem = new Expression("((a&b)=>a)");
assertFalse(Proof.isEqualToTheorem(app, theorem));
}
// testing an application with less complexity than the theorem
public void testTheoremChecker3() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("((a=>c)=>a)");
Expression theorem = new Expression("(((a|b)=>c)=>(a|b))");
assertFalse(Proof.isEqualToTheorem(app, theorem));
}
// testing mismatched operators in application
public void testTheoremChecker4() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("(((a&b)=>c)=>(a|b))");
Expression theorem = new Expression("((a=>c)=>a)");
assertFalse(Proof.isEqualToTheorem(app, theorem));
}
// testing mismatched operators between the application and the theorem
public void testTheoremChecker5() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("(((a|b)=>c)=>(a|b))");
Expression theorem = new Expression("((a&c)=>a)");
assertFalse(Proof.isEqualToTheorem(app, theorem));
}
// testing trivial edge case
public void testTheoremChecker6() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("c");
Expression theorem = new Expression("a");
assertTrue(Proof.isEqualToTheorem(app, theorem));
}
// testing trivial expressions
public void testTheoremChecker7() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("(a=>b)");
Expression theorem = new Expression("(b=>c)");
assertTrue(Proof.isEqualToTheorem(app, theorem));
}
// testing top level operator mismatch
public void testTheoremChecker8() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("(a=>b)");
Expression theorem = new Expression("(a&c)");
assertFalse(Proof.isEqualToTheorem(app, theorem));
}
// testing expressions with a not operator
public void testTheoremChecker9() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("(((a|b)=>c)=>~(a|b))");
Expression theorem = new Expression("((a=>c)=>~a)");
assertTrue(Proof.isEqualToTheorem(app, theorem));
}
// testing expressions with double not operators
public void testTheoremChecker10() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("(~~(a|b)=>(a|b))");
Expression theorem = new Expression("(~~a=>a)");
assertTrue(Proof.isEqualToTheorem(app, theorem));
}
// testing expressions with double not operators
public void testTheoremChecker11() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("(~~~(a|b)=>~(a|b))");
Expression theorem = new Expression("(~~~a=>~a)");
assertTrue(Proof.isEqualToTheorem(app, theorem));
}
// testing invalid application of not operators
public void testTheoremChecker12() throws IllegalLineException,
IllegalInferenceException {
Expression app = new Expression("(~~(a|b)=>~(a|b))");
Expression theorem = new Expression("(~~a=>a)");
assertFalse(Proof.isEqualToTheorem(app, theorem));
}
public void testIsValidExpr1(){
String s = "()";
assertFalse(Proof.isValidExpr(s));//empty expression
}
public void testIsValidExpr2(){
String s = "p";
assertTrue(Proof.isValidExpr(s));//single element expression
}
public void testIsValidExpr3(){
String s = "(p=>q)";
assertTrue(Proof.isValidExpr(s));//basic 2 element expression with on operator
}
public void testIsValidExpr4(){
String s = "(r|s)";
assertTrue(Proof.isValidExpr(s));
}
public void testIsValidExpr5(){
String s = "((p&q)=>p)";//nested expression, 2 operators
assertTrue(Proof.isValidExpr(s));
}
public void testIsValidExpr6(){
String s = "(((p=>q)=>q)=>((q=>p)=>p))";//nested expressions on both sides of main operator
assertTrue(Proof.isValidExpr(s));
}
public void testIsValidExpr7(){
String s = "(()=>";
assertFalse(Proof.isValidExpr(s));// illegal number of parenthesis, operator on outside, no expression variables
}
public void testIsValidExpr8(){
String s ="(())=>";
assertFalse(Proof.isValidExpr(s));//legal number of parethesis, operator on outside, no expression variables
}
public void testisValidExpr9(){
String s = "(~~~p=>q|(p&q))";
assertFalse(Proof.isValidExpr(s));// does not match legal number of parenthesis per operator
}
public void testIsValidExpr10(){
String s = "(~~~p=>(q|(p&q)))";
assertTrue(Proof.isValidExpr(s));//upper parenthesis mistake corrected, also tests multiple "~"(nots)
}
public void testIsValidExpr11(){
String s = "((~~~p=>q)|(p&(q|zt)))";//correct number of operators, one extra expression variable in nested expr: "zt"
assertFalse(Proof.isValidExpr(s));
}
}