-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator_test.go
110 lines (101 loc) · 3.38 KB
/
calculator_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
package calculator
import (
"math"
"reflect"
"testing"
)
func TestCmdLineInputParsing(t *testing.T) {
tables := []struct {
i string
o []string
}{
{"sin(0)", []string{"sin", "(", "0", ")"}},
{"1 + 3*2", []string{"1", "+", "3", "*", "2"}},
{"1 + 3 *2", []string{"1", "+", "3", "*", "2"}},
{"1+3*2", []string{"1", "+", "3", "*", "2"}},
{"11+3*2", []string{"11", "+", "3", "*", "2"}},
{"1+32*2", []string{"1", "+", "32", "*", "2"}},
{"1+3*203", []string{"1", "+", "3", "*", "203"}},
{"1.1+3*2", []string{"1.1", "+", "3", "*", "2"}},
{"1.1+(3*2)", []string{"1.1", "+", "(", "3", "*", "2", ")"}},
{"(1+1)*5+3", []string{"(", "1", "+", "1", ")", "*", "5", "+", "3"}},
{"3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3", []string{"3", "+", "4", "*", "2", "/", "(", "1", "-", "5", ")", "^", "2", "^", "3"}},
}
for _, table := range tables {
total := CmdLineInputParsing(table.i)
if reflect.DeepEqual(total, table.o) == false {
t.Errorf("Parsing of %s was incorrect, got: %s, want: %s.", table.i, total, table.o)
}
}
}
func TestShuntingYardAlgorithm(t *testing.T) {
tables := []struct {
i []string
o []string
}{
{[]string{"sin", "(", "0", ")"}, []string{"0", "sin"}},
{[]string{"1", "+", "3", "+", "2"}, []string{"1", "3", "+", "2", "+"}},
{[]string{"1", "+", "3", "*", "2"}, []string{"1", "3", "2", "*", "+"}},
{[]string{"11", "+", "3", "*", "2"}, []string{"11", "3", "2", "*", "+"}},
{[]string{"1", "+", "32", "*", "2"}, []string{"1", "32", "2", "*", "+"}},
{[]string{"1", "+", "3", "*", "203"}, []string{"1", "3", "203", "*", "+"}},
{[]string{"1.1", "+", "3", "*", "2"}, []string{"1.1", "3", "2", "*", "+"}},
{[]string{"3", "+", "4", "*", "2", "/", "(", "1", "-", "5", ")", "^", "2", "^", "3"}, []string{"3", "4", "2", "*", "1", "5", "-", "2", "3", "^", "^", "/", "+"}},
}
for _, table := range tables {
rpn := ShuntingYardAlgorithm(table.i)
if reflect.DeepEqual(rpn, table.o) == false {
t.Errorf("Parsing of %s was incorrect, got: %s, want: %s.", table.i, rpn, table.o)
}
}
}
func TestComputeResult(t *testing.T) {
tables := []struct {
i []string
o float64
}{
{[]string{"0", "sin"}, 0},
{[]string{"1", "2", "-"}, -1},
{[]string{"1", "3", "+", "2", "+"}, 6},
{[]string{"1", "3", "2", "*", "+"}, 7},
{[]string{"11", "3", "2", "*", "+"}, 17},
{[]string{"1", "32", "2", "*", "+"}, 65},
{[]string{"1", "3", "203", "*", "+"}, 610},
{[]string{"1.1", "3", "2", "*", "+"}, 7.1},
{[]string{"2", "3", "^"}, 8},
{[]string{"3", "4", "2", "*", "1", "5", "-", "2", "3", "^", "^", "/", "+"}, 3.0001220703125},
}
for _, table := range tables {
result := ComputeResult(table.i)
if result != table.o {
t.Errorf("Parsing of %s was incorrect, got: %f, want: %f.", table.i, result, table.o)
}
}
}
func TestCalculate(t *testing.T) {
tables := []struct {
i string
o float64
}{
{"sin(1)", math.Sin(1)},
{"sin(pi)", math.Sin(math.Pi)},
{"cos(0)", math.Cos(0)},
{"1+sin(1)", 1 + math.Sin(1)},
{"1+sin(1+1)", 1 + math.Sin(1+1)},
{"1+sin(sin(1+1))", 1 + math.Sin(math.Sin(1+1))},
{"1+2*sin(1)", 1 + 2*math.Sin(1)},
{"1 + 3*2", 7},
{"1 - 2", -1},
{"1+32*2", 65},
{"1+3*203", 610},
{"1.1+(3*2)", 7.1},
{"2^3", 8},
{"3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3", 3.0001220703125},
}
for _, table := range tables {
result := Calculate(table.i)
if result != table.o {
t.Errorf("Parsing of %s was incorrect, got: %f, want: %f.", table.i, result, table.o)
}
}
}