aboutsummaryrefslogtreecommitdiff
path: root/calculator-java/src/test/java/StatementParserTest.java
blob: ee4ab6b84df3afdecfcdf636f703421e923849c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import ch.bfh.parser.StatementParser;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class StatementParserTest {

    StatementParser sp = new StatementParser();
    // Only testing correct expressions first
    @Test
    public void emptyExpression() {
        sp.parseStatement("");
        assertEquals(0.0, sp.getValue(), 0.0);
    }

    @Test
    public void oneFactor() {
        sp.parseStatement("10");
        assertEquals(10.0, sp.getValue(), 0.0);
    }

    @Test
    public void oneParenthesisedFactor() {
        sp.parseStatement("(10)");
        assertEquals(10.0, sp.getValue(), 0.0);
    }

    @Test
    public void oneNegativeFactor() {
        sp.parseStatement("-10");
        assertEquals(-10.0, sp.getValue(), 0.0);
    }

    @Test
    public void oneNegativeParenthesisedFactor() {
        sp.parseStatement("-(10)");
        assertEquals(-10.0, sp.getValue(), 0.0);
    }

    @Test
    public void oneNegativeParenthesisedNegativeFactor() {
        sp.parseStatement("-(-10)");
        assertEquals(10.0, sp.getValue(), 0.0);
    }

    @Test
    public void twoFactorSum() {
        sp.parseStatement("2+3");
        assertEquals(5.0, sp.getValue(), 0.0);
    }

    @Test
    public void twoFactorSub() {
        sp.parseStatement("2-3");
        assertEquals(-1.0, sp.getValue(), 0.0);
    }

    @Test
    public void twoFactorSumWithSub() {
        sp.parseStatement("2--3");
        assertEquals(5.0, sp.getValue(), 0.0);
    }

    @Test
    public void twoFactorSumWithSubParenthesised() {
        sp.parseStatement("2-(-3)");
        assertEquals(5.0, sp.getValue(), 0.0);
    }

    @Test
    public void twoFactorMul() {
        sp.parseStatement("2*3");
        assertEquals(6.0, sp.getValue(), 0.0);
    }

    @Test
    public void twoFactorDiv() {
        sp.parseStatement("11/4");
        assertEquals(2.75, sp.getValue(), 0.0);
    }

    @Test
    public void completeArithmeticOperationWithPriorities() {
        sp.parseStatement("(4+5)*3/4");
        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");
        assertEquals(255.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

}