Kirk Rader  1.0-SNAPSHOT
UniversalGeneralizationTest.java
Go to the documentation of this file.
1 /*
2  * Copyright 2016 Kirk Rader
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package us.rader.tt.formula.test;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 
23 import java.util.HashMap;
24 import java.util.Map;
25 
26 import org.junit.Test;
27 
29 import us.rader.tt.formula.Formula;
30 import us.rader.tt.formula.Variable;
32 
33 /**
34  * Unit tests for us.rader.tt.formula.UniversalGeneralization
35  */
37 
38  /**
39  * Unit test for {@link VariableBindingOperator#evaluate}.
40  *
41  * @throws Exception
42  * Thrown if an unexpected error occurs.
43  */
44  @Test
45  public void evaluateTest() throws Exception {
46 
47  try {
48 
49  final Formula formula = Formula.parseFormula("{x}Fx");
50  final Formula operand = Formula.parseFormula("Fx");
51  final Map<Expression, Boolean> values = new HashMap<>();
52  values.put(operand, false);
53  formula.evaluate(values);
54  fail("IllegalArgumentException e");
55 
56  } catch (@SuppressWarnings("unused") final IllegalArgumentException e) {
57 
58  // success
59 
60  }
61  }
62 
63  /**
64  * Unit test for {@link VariableBindingOperator#instantiate}.
65  *
66  * @throws Exception
67  * Thrown if an unexpected error occurs.
68  */
69  @Test
70  public void instantiateTest() throws Exception {
71 
72  final VariableBindingOperator formula1 = (VariableBindingOperator) Formula.parseFormula("{x}(Fx->Gx)");
73  final Variable freeVariable = new Variable("a");
74  final Formula formula2 = formula1.instantiate(freeVariable);
75  assertEquals("(Fa->Ga)", formula2.toString());
76 
77  }
78 
79  /**
80  * Unit test for {@link Expression#replaceVariable}.
81  *
82  * @throws Exception
83  * Thrown if an unexpected error occurs.
84  */
85  @Test
86  public void replaceVariableTest() throws Exception {
87 
88  final VariableBindingOperator formula1 = (VariableBindingOperator) Formula.parseFormula("{x}(Fx->Gy)");
89  final Variable boundVariable = new Variable("y");
90  final Variable freeVariable = new Variable("a");
91  final Expression formula2 = formula1.replaceVariable(boundVariable, freeVariable);
92  assertEquals("{x}(Fx->Ga)", formula2.toString());
93 
94  }
95 
96  /**
97  * Unit test for {@link Formula#toDigraph}.
98  *
99  * @throws Exception
100  * Thrown if an unexpected error occurs.
101  */
102  @Test
103  public void toDigraphTest() throws Exception {
104 
105  final Formula formula = Formula.parseFormula("{x}Fx");
106  final String digraph = formula.toDigraph(0);
107  assertTrue(digraph.startsWith("digraph"));
108 
109  }
110 
111  /**
112  * Unit test for {@link Formula#toTruthTable}.
113  *
114  * @throws Exception
115  * Thrown if an unexpected error occurs.
116  */
117  @Test
118  public void toTruthTableTest() throws Exception {
119 
120  try {
121 
122  final Formula formula = Formula.parseFormula("{x}Fx");
123  formula.toTruthTable(0);
124  fail("IllegalArgumentException e");
125 
126  } catch (@SuppressWarnings("unused") final IllegalArgumentException e) {
127 
128  // success
129 
130  }
131  }
132 
133 }
Demonstrate object-oriented design and implementation using a simple Java class library.
void replaceVariableTest()
Unit test for Expression#replaceVariable.
void evaluateTest()
Unit test for VariableBindingOperator#evaluate.
void toTruthTableTest()
Unit test for Formula#toTruthTable.
A variable (lower-case alphabetic character) of the monadic predicate calculus.
Definition: Variable.java:27
final String toDigraph(final int indent)
Definition: Formula.java:505
final String toTruthTable(final int indent)
Definition: Formula.java:550
static Formula parseFormula(final BufferedReader reader)
Parse the next Formula from the given buffered input stream.
Definition: Formula.java:112
abstract boolean evaluate(Map< Expression, Boolean > values)
Evaluate this formula according to its truth table assuming the given values for its primitive terms...
Processor for well-formed formulas of the monadic predicate calculus.
Objects with unique Graphviz node names.
Definition: Expression.java:32
final Formula instantiate(final Expression factor)
Return the Formula that results from instantiating this operator.
abstract Expression replaceVariable(Variable boundVariable, Expression factor)
Replace boundVariable with factor everywhere in this Expression.
Unit tests for us.rader.tt.formula.UniversalGeneralization.
A formula of the monadic predicate calculus.
Definition: Formula.java:51
void instantiateTest()
Unit test for VariableBindingOperator#instantiate.