-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpn_test.go
154 lines (142 loc) · 3.91 KB
/
rpn_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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package rpn
import (
"errors"
"fmt"
"math"
"strconv"
"testing"
)
var cases = []struct {
infix string
rpn string
result float64
}{
{"3 / 0", "3 0 /", math.Inf(1)},
{"3 + 2", "3 2 +", 5},
{"3 + -2", "3 0 2 - +", 1},
{"3 - -2", "3 0 2 - -", 5},
{"-3 + -2", "0 3 - 0 2 - +", -5},
{"-3 + +2", "0 3 - 0 2 + +", -1},
{"-3 + -0 + -2", "0 3 - 0 0 - + 0 2 - +", -5},
{"-(3 + -2)*-1", "0 3 0 2 - + 0 1 - * -", 1},
{"3 - - - - 5", "3 0 0 0 5 - - - -", 8},
{" 3 + 2 ", "3 2 +", 5},
{"(3 + 2)", "3 2 +", 5},
{"(3 + (2))", "3 2 +", 5},
{"3 + 2 * 3", "3 2 3 * +", 9},
{"3 + 2 + 3 * 4", "3 2 + 3 4 * +", 17},
{"3 * 2 + 3 + 4", "3 2 * 3 + 4 +", 13},
{"3 + 2 * 3 + 4 * 5", "3 2 3 * + 4 5 * +", 29},
{"(3 + 2) * 3", "3 2 + 3 *", 15},
{"3 * 2 + 3 * (5 + 2)", "3 2 * 3 5 2 + * +", 27},
{"3 * 2 + 3 * (5 + 2 * 3)", "3 2 * 3 5 2 3 * + * +", 39},
{"((3 + 2) * 5)", "3 2 + 5 *", 25},
{"((3 - 1) / 2) * 3 + 5", "3 1 - 2 / 3 * 5 +", 8},
{"((3 - 1) / 2) * (3 + 5)", "3 1 - 2 / 3 5 + *", 8},
{"((((1 * (2 + 3)) - 3) + 4) * 5)", "1 2 3 + * 3 - 4 + 5 *", 30},
{"(1 + 2) * 4 + 3", "1 2 + 4 * 3 +", 15},
{"3 + 4 * 2 / (1 - 5) ^ 2", "3 4 2 * 1 5 - 2 ^ / +", 3.5},
{"9 ^ (1 / 2)", "9 1 2 / ^", 3},
{"9^(1/2)", "9 1 2 / ^", 3},
{"9 ^ ((0 - 1) / 2)", "9 0 1 - 2 / ^", 0.3333333333333333},
{"((15 / (7 - (1 + 1))) * 3) - (2 + (1 + 1))", "15 7 1 1 + - / 3 * 2 1 1 + + -", 5},
}
func TestFromInfix(t *testing.T) {
for i, e := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
r, err := FromInfix(e.infix)
if err != nil {
t.Error("unexpected error:", err)
}
if e.rpn != r {
t.Error("\n\tinfix: ", e.infix,
"\n\texpected rpn:", e.rpn,
"\n\tresult: ", r)
}
})
}
}
func BenchmarkFromInfix(b *testing.B) {
for j := 0; j < b.N; j++ {
FromInfix("((15 / (7 - (1 + 1))) * 3) - (2 + (1 + 1))")
}
}
func TestRpnCalculation(t *testing.T) {
for i, e := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
r, err := Calculate(e.rpn)
if err != nil {
t.Error("unexpected error:", err)
}
if e.result != r {
t.Error("\n\tinfix: ", e.infix,
"\n\trpn: ", e.rpn,
"\n\texpected result:", e.result,
"\n\tresult: ", r)
}
})
}
}
func BenchmarkRpnCalculation(b *testing.B) {
for j := 0; j < b.N; j++ {
Calculate("15 7 1 1 + - / 3 * 2 1 1 + + -")
}
}
var invalidInfixCases = []struct {
infix string
err error
}{
{"3 + 3)", errors.New("Invalid bracket order: 3 + 3). Not enough open bracket")},
{"(3 + 3", errors.New("Invalid bracket order: (3 + 3. Not enough closed bracket")},
{"((((1 * (2 + 3)) - 3) + 4) * 5", errors.New("Invalid bracket order: ((((1 * (2 + 3)) - 3) + 4) * 5. Not enough closed bracket")},
}
func TestInvalidInfixInput(t *testing.T) {
for i, e := range invalidInfixCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
_, err := FromInfix(e.infix)
if e.err.Error() != err.Error() {
t.Error("\n\tinfix: ", e.infix,
"\n\texpected error: ", e.err,
"\n\tresult: ", err)
}
})
}
}
var invalidPostfixCases = []string{
"1 a + 3 *",
"a 1 + 3 *",
"+ 3",
"2 3 ?",
"2 3 + *",
}
func TestInvalidPostfixInput(t *testing.T) {
for i, c := range invalidPostfixCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
_, err := Calculate(c)
exp := fmt.Errorf("Invalid postfix notation: %s", c)
if err == nil || exp.Error() != err.Error() {
t.Error("\n\trpn: ", c,
"\n\texpected error: ", exp,
"\n\tresult: ", err)
}
})
}
}
func TestPostfixValidation(t *testing.T) {
for i, e := range invalidPostfixCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if isValidRpn(e) != false {
t.Errorf("Given rpn: %s is invalid, but it not been determined", e)
}
})
}
}
func TestNotTrimmedPostfixString(t *testing.T) {
r, _ := Calculate(" 3 3 + ")
if r != 6 {
t.Error(
"\n\trpn: 3 3 + ",
"\n\texpected result: 6",
"\n\tresult: ", r)
}
}