Kirk Rader  1.0-SNAPSHOT
ConditionalTest.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.assertFalse;
21 import static org.junit.Assert.assertTrue;
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;
31 
32 /**
33  * Unit tests for us.rader.tt.formula.Conditional
34  */
35 public class ConditionalTest {
36 
37  /**
38  * Unit test for {@link Formula#evaluate}.
39  *
40  * @throws Exception
41  * Thrown if an unexpected error occurs.
42  */
43  @Test
44  public void evaluateFalseFalseTest() throws Exception {
45 
46  final Formula formula = Formula.parseFormula("(P->Q)");
47  final Formula operand1 = Formula.parseFormula("P");
48  final Formula operand2 = Formula.parseFormula("Q");
49  final Map<Expression, Boolean> values = new HashMap<>();
50  values.put(operand1, false);
51  values.put(operand2, false);
52  assertTrue(formula.evaluate(values));
53 
54  }
55 
56  /**
57  * Unit test for {@link Formula#evaluate}.
58  *
59  * @throws Exception
60  * Thrown if an unexpected error occurs.
61  */
62  @Test
63  public void evaluateFalseTrueTest() throws Exception {
64 
65  final Formula formula = Formula.parseFormula("(P->Q)");
66  final Formula operand1 = Formula.parseFormula("P");
67  final Formula operand2 = Formula.parseFormula("Q");
68  final Map<Expression, Boolean> values = new HashMap<>();
69  values.put(operand1, false);
70  values.put(operand2, true);
71  assertTrue(formula.evaluate(values));
72 
73  }
74 
75  /**
76  * Unit test for {@link Formula#evaluate}.
77  *
78  * @throws Exception
79  * Thrown if an unexpected error occurs.
80  */
81  @Test
82  public void evaluateTrueFalseTest() throws Exception {
83 
84  final Formula formula = Formula.parseFormula("(P->Q)");
85  final Formula operand1 = Formula.parseFormula("P");
86  final Formula operand2 = Formula.parseFormula("Q");
87  final Map<Expression, Boolean> values = new HashMap<>();
88  values.put(operand1, true);
89  values.put(operand2, false);
90  assertFalse(formula.evaluate(values));
91 
92  }
93 
94  /**
95  * Unit test for {@link Formula#evaluate}.
96  *
97  * @throws Exception
98  * Thrown if an unexpected error occurs.
99  */
100  @Test
101  public void evaluateTrueTrueTest() throws Exception {
102 
103  final Formula formula = Formula.parseFormula("(P->Q)");
104  final Formula operand1 = Formula.parseFormula("P");
105  final Formula operand2 = Formula.parseFormula("Q");
106  final Map<Expression, Boolean> values = new HashMap<>();
107  values.put(operand1, true);
108  values.put(operand2, true);
109  assertTrue(formula.evaluate(values));
110 
111  }
112 
113  /**
114  * Unit test for {@link Formula#replaceVariable}.
115  *
116  * @throws Exception
117  * Thrown if an unexpected error occurs.
118  */
119  @Test
120  public void replaceVariableTest() throws Exception {
121 
122  final Formula conditional1 = Formula.parseFormula("(Fx->Gx)");
123  final Variable boundVariable = new Variable("x");
124  final Variable freeVariable = new Variable("a");
125  final Expression conditional2 = conditional1.replaceVariable(boundVariable, freeVariable);
126  assertEquals("(Fa->Ga)", conditional2.toString());
127 
128  }
129 
130  /**
131  * Unit test for {@link Formula#toDigraph}.
132  *
133  * @throws Exception
134  * Thrown if an unexpected error occurs.
135  */
136  @Test
137  public void toDigraphTest() throws Exception {
138 
139  final Formula formula = Formula.parseFormula("(P->Q)");
140  final String digraph = formula.toDigraph(0);
141  assertTrue(digraph.startsWith("digraph"));
142 
143  }
144 
145  /**
146  * Unit test for {@link Formula#toTruthTable}.
147  *
148  * @throws Exception
149  * Thrown if an unexpected error occurs.
150  */
151  @Test
152  public void toTruthTableTest() throws Exception {
153 
154  final Formula formula = Formula.parseFormula("(P->Q)");
155  final String truthTable = formula.toTruthTable(0);
156  assertTrue(truthTable.startsWith("\\begin{array}"));
157 
158  }
159 
160 }
Demonstrate object-oriented design and implementation using a simple Java class library.
void evaluateTrueTrueTest()
Unit test for Formula#evaluate.
void evaluateTrueFalseTest()
Unit test for Formula#evaluate.
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
void toTruthTableTest()
Unit test for Formula#toTruthTable.
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
abstract Expression replaceVariable(Variable boundVariable, Expression factor)
Replace boundVariable with factor everywhere in this Expression.
Unit tests for us.rader.tt.formula.Conditional.
void evaluateFalseFalseTest()
Unit test for Formula#evaluate.
void evaluateFalseTrueTest()
Unit test for Formula#evaluate.
void replaceVariableTest()
Unit test for Formula#replaceVariable.
A formula of the monadic predicate calculus.
Definition: Formula.java:51
void toDigraphTest()
Unit test for Formula#toDigraph.