Kirk Rader  1.0-SNAPSHOT
BiconditionalTest.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 import static org.junit.Assert.fail;
23 
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Map;
27 
28 import org.junit.Test;
29 
31 import us.rader.tt.formula.Formula;
32 import us.rader.tt.formula.Variable;
33 
34 /**
35  * Unit tests for us.rader.tt.formula.Biconditional
36  */
37 public class BiconditionalTest {
38 
39  /**
40  * Biconditional Unit test for Biconditional.evaluate(Map).
41  *
42  * @throws Exception
43  * Thrown if an unexpected error occurs.
44  */
45  @Test
46  public void evaluateFalseFalseTest() throws Exception {
47 
48  final Formula formula = Formula.parseFormula("(P<->Q)");
49  final Formula operand1 = Formula.parseFormula("P");
50  final Formula operand2 = Formula.parseFormula("Q");
51  final Map<Expression, Boolean> values = new HashMap<>();
52  values.put(operand1, false);
53  values.put(operand2, false);
54  assertTrue(formula.evaluate(values));
55 
56  }
57 
58  /**
59  * Unit test for Biconditional.evaluate(Map).
60  *
61  * @throws Exception
62  * Thrown if an unexpected error occurs.
63  */
64  @Test
65  public void evaluateFalseTrueTest() throws Exception {
66 
67  final Formula formula = Formula.parseFormula("(P<->Q)");
68  final Formula operand1 = Formula.parseFormula("P");
69  final Formula operand2 = Formula.parseFormula("Q");
70  final Map<Expression, Boolean> values = new HashMap<>();
71  values.put(operand1, false);
72  values.put(operand2, true);
73  assertFalse(formula.evaluate(values));
74 
75  }
76 
77  /**
78  * Unit test for Biconditional.evaluate(Map).
79  *
80  * @throws Exception
81  * Thrown if an unexpected error occurs.
82  */
83  @Test
84  public void evaluateTrueFalseTest() throws Exception {
85 
86  final Formula formula = Formula.parseFormula("(P<->Q)");
87  final Formula operand1 = Formula.parseFormula("P");
88  final Formula operand2 = Formula.parseFormula("Q");
89  final Map<Expression, Boolean> values = new HashMap<>();
90  values.put(operand1, true);
91  values.put(operand2, false);
92  assertFalse(formula.evaluate(values));
93 
94  }
95 
96  /**
97  * Unit test for Biconditional.evaluate(Map).
98  *
99  * @throws Exception
100  * Thrown if an unexpected error occurs.
101  */
102  @Test
103  public void evaluateTrueTrueTest() throws Exception {
104 
105  final Formula formula = Formula.parseFormula("(P<->Q)");
106  final Formula operand1 = Formula.parseFormula("P");
107  final Formula operand2 = Formula.parseFormula("Q");
108  final Map<Expression, Boolean> values = new HashMap<>();
109  values.put(operand1, true);
110  values.put(operand2, true);
111  assertTrue(formula.evaluate(values));
112 
113  }
114 
115  /**
116  * Unit test for {@link Formula#parseFormula}.
117  */
118  @Test
119  public void parseUnexpectedEndTest() {
120 
121  try {
122 
123  Formula.parseFormula("(P<-");
124  fail("IOException e");
125 
126  } catch (@SuppressWarnings("unused") final IOException e) {
127 
128  // success
129 
130  }
131  }
132 
133  /**
134  * Unit test for {@link Formula#parseFormula}.
135  */
136  @Test
138 
139  try {
140 
141  Formula.parseFormula("(P<-:Q)");
142  fail("IOException e");
143 
144  } catch (@SuppressWarnings("unused") final IOException e) {
145 
146  // success
147 
148  }
149  }
150 
151  /**
152  * Unit test for {@link Formula#parseFormula}.
153  *
154  * @throws Exception
155  * Thrown if an unexpected error occurs.
156  */
157  @Test
158  public void parseWellFormedTest() throws Exception {
159 
160  final Formula formula = Formula.parseFormula(" (P <-> Q ) ");
161  assertEquals("(P<->Q)", formula.toString());
162 
163  }
164 
165  /**
166  * Unit test for Biconditional.replaceVariable(Variable, Expression).
167  *
168  * @throws Exception
169  * Thrown if an unexpected error occurs.
170  */
171  @Test
172  public void replaceVariableTest() throws Exception {
173 
174  final Formula biconditional1 = Formula.parseFormula("(Fx<->Gx)");
175  final Variable boundVariable = new Variable("x");
176  final Variable freeVariable = new Variable("a");
177  final Expression biconditional2 = biconditional1.replaceVariable(boundVariable, freeVariable);
178  assertEquals("(Fa<->Ga)", biconditional2.toString());
179 
180  }
181 
182  /**
183  * Unit test for {@link Formula#toDigraph}.
184  *
185  * @throws Exception
186  * Thrown if an unexpected error occurs.
187  */
188  @Test
189  public void toDigraphTest() throws Exception {
190 
191  final Formula formula = Formula.parseFormula("(P<->Q)");
192  final String digraph = formula.toDigraph(0);
193  assertTrue(digraph.startsWith("digraph"));
194 
195  }
196 
197  /**
198  * Unit test for {@link Formula#toTruthTable}.
199  *
200  * @throws Exception
201  * Thrown if an unexpected error occurs.
202  */
203  @Test
204  public void toTruthTableTest() throws Exception {
205 
206  final Formula formula = Formula.parseFormula("(P<->Q)");
207  final String truthTable = formula.toTruthTable(0);
208  assertTrue(truthTable.startsWith("\\begin{array}"));
209 
210  }
211 
212 }
void evaluateFalseFalseTest()
Biconditional Unit test for Biconditional.evaluate(Map).
void evaluateTrueFalseTest()
Unit test for Biconditional.evaluate(Map).
Demonstrate object-oriented design and implementation using a simple Java class library.
void evaluateFalseTrueTest()
Unit test for Biconditional.evaluate(Map).
void parseUnexpectedEndTest()
Unit test for Formula#parseFormula.
void evaluateTrueTrueTest()
Unit test for Biconditional.evaluate(Map).
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
void replaceVariableTest()
Unit test for Biconditional.replaceVariable(Variable, Expression).
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...
void parseUnexpectedTerminatorTest()
Unit test for Formula#parseFormula.
void toTruthTableTest()
Unit test for Formula#toTruthTable.
Processor for well-formed formulas of the monadic predicate calculus.
Unit tests for us.rader.tt.formula.Biconditional.
void parseWellFormedTest()
Unit test for Formula#parseFormula.
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.
void toDigraphTest()
Unit test for Formula#toDigraph.
A formula of the monadic predicate calculus.
Definition: Formula.java:51