forked from spring1843/go-dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_postfix_test.go
42 lines (36 loc) · 1.22 KB
/
evaluate_postfix_test.go
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
package stack
import "testing"
/*
TestEvaluatePostfix tests solution(s) with the following signature and problem description:
func EvaluatePostfixExpression(expression []string) (float64, error)
Given a postfix expression like 1 2 3 + *, calculate the expression e.g. 5.
*/
func TestEvaluatePostfix(t *testing.T) {
tests := []struct {
postfix []string
expectErr bool
outcome float64
}{
{[]string{""}, true, -1},
{[]string{"+"}, true, -1},
{[]string{"A", "B", "+"}, true, -1},
{[]string{"1", "B", "+"}, true, -1},
{[]string{"1", "2", "+"}, false, 3},
{[]string{"1", "2", "3", "+", "*"}, false, 5},
{[]string{"1", "2", "3", "+", "+"}, false, 6},
{[]string{"1", "3", "2", "-", "+"}, false, 2},
{[]string{"9", "3", "/"}, false, 3},
{[]string{"3", "9", "3", "/", "-"}, false, 0},
{[]string{"1", "2", "3", "4", "5", "*", "+", "+", "*"}, false, 25},
{[]string{"1", "2", "3", "+", "4", "5", "*", "+", "*"}, false, 25},
}
for i, test := range tests {
got, err := EvaluatePostfixExpression(test.postfix)
if err != nil && !test.expectErr {
t.Fatalf("Did not expect an error. Error : %s", err)
}
if got != test.outcome {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.outcome, got)
}
}
}