diff options
author | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2021-06-11 18:22:23 +0200 |
---|---|---|
committer | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2021-06-11 18:22:23 +0200 |
commit | 3baa44f1c8459713a96f4db994ee9905cc4e8df1 (patch) | |
tree | 1f259a1f7441b75b0385a16d649b2285f38a38fb /calculator-java | |
parent | 98ddff97ec1d092b18ef2d176e83bd92f9671e03 (diff) |
[~] Updated the unit tests to take in account the variables
Diffstat (limited to 'calculator-java')
-rw-r--r-- | calculator-java/src/test/java/ExpressionParserTest.java | 115 | ||||
-rw-r--r-- | calculator-java/src/test/java/StatementParserTest.java | 36 |
2 files changed, 35 insertions, 116 deletions
diff --git a/calculator-java/src/test/java/ExpressionParserTest.java b/calculator-java/src/test/java/ExpressionParserTest.java deleted file mode 100644 index b15e844..0000000 --- a/calculator-java/src/test/java/ExpressionParserTest.java +++ /dev/null @@ -1,115 +0,0 @@ -import ch.bfh.CalculatorLexer; -import ch.bfh.parser.ExpressionParser; -import org.junit.Test; -import static org.junit.Assert.assertEquals; - -public class ExpressionParserTest { - - // Only testing correct expressions first - @Test - public void emptyExpression() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer(""); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(0.0, ep.getValue(), 0.0); - } - - @Test - public void oneFactor() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("10"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(10.0, ep.getValue(), 0.0); - } - - @Test - public void oneParenthesisedFactor() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("(10)"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(10.0, ep.getValue(), 0.0); - } - - @Test - public void oneNegativeFactor() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("-10"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(-10.0, ep.getValue(), 0.0); - } - - @Test - public void oneNegativeParenthesisedFactor() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("-(10)"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(-10.0, ep.getValue(), 0.0); - } - - @Test - public void oneNegativeParenthesisedNegativeFactor() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("-(-10)"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(10.0, ep.getValue(), 0.0); - } - - @Test - public void twoFactorSum() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("2+3"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(5.0, ep.getValue(), 0.0); - } - - @Test - public void twoFactorSub() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("2-3"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(-1.0, ep.getValue(), 0.0); - } - - @Test - public void twoFactorSumWithSub() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("2--3"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(5.0, ep.getValue(), 0.0); - } - - @Test - public void twoFactorSumWithSubParenthesised() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("2-(-3)"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(5.0, ep.getValue(), 0.0); - } - - @Test - public void twoFactorMul() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("2*3"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(6.0, ep.getValue(), 0.0); - } - - @Test - public void twoFactorDiv() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("11/4"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(2.75, ep.getValue(), 0.0); - } - - @Test - public void completeArithmeticOperationWithPriorities() { - CalculatorLexer cl = new CalculatorLexer(); - cl.initLexer("(4+5)*3/4"); - ExpressionParser ep = new ExpressionParser(cl); - assertEquals(6.75, ep.getValue(), 0.0); - } - - //TODO - test error detection - -} diff --git a/calculator-java/src/test/java/StatementParserTest.java b/calculator-java/src/test/java/StatementParserTest.java index 4b2e635..8c2db59 100644 --- a/calculator-java/src/test/java/StatementParserTest.java +++ b/calculator-java/src/test/java/StatementParserTest.java @@ -1,4 +1,3 @@ -import ch.bfh.CalculatorLexer; import ch.bfh.parser.StatementParser; import org.junit.Test; @@ -86,6 +85,41 @@ public class StatementParserTest { assertEquals(6.75, sp.getValue(), 0.0); } + @Test + public void variableTest() { + sp.parseStatement("let a = 5"); + assertEquals(5.0, sp.getValue(), 0.0); + + sp.parseStatement("a"); + assertEquals(5.0, sp.getValue(), 0.0); + + sp.parseStatement("(a)"); + assertEquals(5.0, sp.getValue(), 0.0); + + sp.parseStatement("-a"); + assertEquals(-5.0, sp.getValue(), 0.0); + + sp.parseStatement("-(a)"); + assertEquals(-5.0, sp.getValue(), 0.0); + + sp.parseStatement("-(-a)"); + assertEquals(5.0, sp.getValue(), 0.0); + + sp.parseStatement("let b=5*50"); + assertEquals(250.0, sp.getValue(), 0.0); + + sp.parseStatement("b/a"); + assertEquals(50.0, sp.getValue(), 0.0); + + sp.parseStatement("b+a/2"); + assertEquals(252.5, sp.getValue(), 0.0); + + sp.parseStatement("let z = b+a/2"); + assertEquals(252.5, sp.getValue(), 0.0); + + sp.parseStatement("let a = b+a"); + assertEquals(255.0, sp.getValue(), 0.0); + } //TODO - test error detection } |