-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathinfix_to_postfix_test.go
41 lines (34 loc) · 1.4 KB
/
infix_to_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
package stack
import (
"slices"
"testing"
)
/*
TestInfixToPostfix tests solution(s) with the following signature and problem description:
func InfixToPostfix(infix []string) []string
Given an infix expression convert it to a postfix expression supporting the four basic arithmetic
operations and parentheses.
Infix expression is how humans typically write arithmetic expressions like 1*2+3+4*5 which
is equivalent of (1*2) + 3 + (4*5).
For example given 1*2+3+4*5, return 1 2 * 3 + 4 5 * which both evaluate to 25.
*/
func TestInfixToPostfix(t *testing.T) {
tests := []struct {
infix []string
postfix []string
}{
{[]string{""}, []string{""}},
{[]string{"a", "+", "b"}, []string{"a", "b", "+"}},
{[]string{"a", "-", "b", "+", "c"}, []string{"a", "b", "c", "+", "-"}},
{[]string{"a", "-", "(", "b", "+", "c", ")"}, []string{"a", "b", "c", "+", "-"}},
{[]string{"a", "+", "b", "-", "c"}, []string{"a", "b", "c", "-", "+"}},
{[]string{"a", "/", "b"}, []string{"a", "b", "/"}},
{[]string{"1", "*", "2", "+", "3", "+", "4", "*", "5"}, []string{"1", "2", "3", "4", "5", "*", "+", "+", "*"}},
{[]string{"1", "*", "(", "2", "+", "3", ")", "+", "4", "*", "5"}, []string{"1", "2", "3", "+", "4", "5", "*", "+", "*"}},
}
for i, test := range tests {
if got := InfixToPostfix(test.infix); !slices.Equal(got, test.postfix) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.postfix, got)
}
}
}