forked from spring1843/go-dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infix_to_postfix_test.go
36 lines (31 loc) · 1.24 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
package stack
import (
"reflect"
"testing"
)
/*
TestInfixToPostfix tests solution(s) with the following signature and problem description:
func InfixToPostfix(infix []string) []string
Given an infix expression e.g. 1*2+3+4*5, convert it to a postfix expression
like 1 2 * 3 + 4 5 * supporting the four basic arithmetic operations and parentheses.
*/
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); !reflect.DeepEqual(got, test.postfix) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.postfix, got)
}
}
}