Kirk Rader  1.0-SNAPSHOT
NegationTest.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.Negation
34  */
35 public class NegationTest {
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 evaluateFalseTest() throws Exception {
45 
46  final Formula formula = Formula.parseFormula("~P");
47  final Formula operand = Formula.parseFormula("P");
48  final Map<Expression, Boolean> values = new HashMap<>();
49  values.put(operand, false);
50  assertTrue(formula.evaluate(values));
51 
52  }
53 
54  /**
55  * Unit test for {@link Formula#evaluate}.
56  *
57  * @throws Exception
58  * Thrown if an unexpected error occurs.
59  */
60  @Test
61  public void evaluateTrueTest() throws Exception {
62 
63  final Formula formula = Formula.parseFormula("~P");
64  final Formula operand = Formula.parseFormula("P");
65  final Map<Expression, Boolean> values = new HashMap<>();
66  values.put(operand, true);
67  assertFalse(formula.evaluate(values));
68 
69  }
70 
71  /**
72  * Unit test for {@link Expression#replaceVariable}.
73  *
74  * @throws Exception
75  * Thrown if an unexpected error occurs.
76  */
77  @Test
78  public void replaceVariableTest() throws Exception {
79 
80  final Formula conditional1 = Formula.parseFormula("~Fx");
81  final Variable boundVariable = new Variable("x");
82  final Variable freeVariable = new Variable("a");
83  final Expression conditional2 = conditional1.replaceVariable(boundVariable, freeVariable);
84  assertEquals("~Fa", conditional2.toString());
85 
86  }
87 
88  /**
89  * Unit test for {@link Formula#toDigraph}.
90  *
91  * @throws Exception
92  * Thrown if an unexpected error occurs.
93  */
94  @Test
95  public void toDigraphTest() throws Exception {
96 
97  final Formula formula = Formula.parseFormula("~P");
98  final String digraph = formula.toDigraph(0);
99  assertTrue(digraph.startsWith("digraph"));
100 
101  }
102 
103  /**
104  * Unit test for {@link Formula#toTruthTable}.
105  *
106  * @throws Exception
107  * Thrown if an unexpected error occurs.
108  */
109  @Test
110  public void toTruthTableTest() throws Exception {
111 
112  final Formula formula = Formula.parseFormula("~P");
113  final String truthTable = formula.toTruthTable(0);
114  assertTrue(truthTable.startsWith("\\begin{array}"));
115 
116  }
117 
118 }
Demonstrate object-oriented design and implementation using a simple Java class library.
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 evaluateFalseTest()
Unit test for Formula#evaluate.
void toTruthTableTest()
Unit test for Formula#toTruthTable.
static Formula parseFormula(final BufferedReader reader)
Parse the next Formula from the given buffered input stream.
Definition: Formula.java:112
void evaluateTrueTest()
Unit test for Formula#evaluate.
void toDigraphTest()
Unit test for Formula#toDigraph.
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.
Unit tests for us.rader.tt.formula.Negation.
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.
A formula of the monadic predicate calculus.
Definition: Formula.java:51
void replaceVariableTest()
Unit test for Expression#replaceVariable.