generated from roman-mazur/architecture-lab-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
implementation_test.go
163 lines (152 loc) · 3.15 KB
/
implementation_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
155
156
157
158
159
160
161
162
163
package lab2
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPrefixToPostfix(t *testing.T) {
assert := assert.New(t)
tests := []struct {
name string
input string
expected string
hasError bool
err string
}{
{
name: "Base",
input: "- * / 15 - 7 + 1 1 3 + 2 + 1 1",
expected: "(15 / (7 - 1 + 1)) * 3 - 2 + 1 + 1",
hasError: false,
},
{
name: "Base With Error",
input: "- * / 15 - 7 + 1 1 3 + 2 + 1 ",
expected: "",
hasError: true,
err: "wrong argument(s) in operation: 2 + +",
},
{
name: "Two Operands",
input: "^ 17 19",
expected: "17 ^ 19",
hasError: false,
},
{
name: "Three Operands",
input: "+ 1 ^ 2 3",
expected: "1 + 2 ^ 3",
hasError: false,
},
{
name: "Seven Operands",
input: "+ / - ^ 20 2 34 / 15 - 4 1 56",
expected: "(20 ^ 2 - 34) / (15 / (4 - 1)) + 56",
hasError: false,
},
{
name: "Ten Operands",
input: "+ 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 3",
expected: "3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3",
hasError: false,
},
{
name: "Empty",
input: "",
expected: "",
hasError: true,
err: "empty input",
},
{
name: "Not Valid Symbols",
input: "$ 3 & a b . : ;",
expected: "",
hasError: true,
err: "wrong input: $ 3 & a b . : ;",
},
{
name: "Symbols",
input: "+ b c",
expected: "",
hasError: true,
err: "wrong input: + b c",
},
{
name: "One symbol",
input: "b",
expected: "",
hasError: true,
err: "wrong input: b",
},
{
name: "Spacing",
input: " ",
expected: "",
hasError: true,
err: "empty input",
},
{
name: "One digit",
input: "3",
expected: "3",
hasError: false,
},
{
name: "One operator",
input: "+",
expected: "",
hasError: true,
err: "wrong input: +",
},
{
name: "Only operators",
input: "+ - / * ^ *",
expected: "",
hasError: true,
err: "wrong argument(s) in operation: / - (^) * (*)",
},
{
name: "Only digits",
input: "1 2 3 4",
expected: "",
hasError: true,
err: "wrong operators in operation: 2 1 3",
},
{
name: "wrong argument amount",
input: "3 - 2 2",
expected: "",
hasError: true,
err: "wrong argument amount: 3 - 2 2",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, err := PrefixToInfix(test.input)
assert.Equal(test.expected, actual, test.name)
if test.hasError {
assert.NotNil(err, test.name)
assert.EqualError(err, test.err)
} else {
assert.Nil(err, test.name)
}
})
}
}
func ExamplePrefixToInfix() {
res, _ := PrefixToInfix("- * / 15 - 7 + 1 1 3 + 2 + 1 1")
fmt.Println(res)
// Output:
// (15 / (7 - 1 + 1)) * 3 - 2 + 1 + 1
}
var res string
func BenchmarkPrefixToInfix(b *testing.B) {
str := "- * / 15 - 7 + 1 1 3 + 2 + 1 + 9 "
for i := 1; i <= 20; i++ {
testStr := strings.Repeat(str, i*i) + "1"
b.Run(fmt.Sprintf("%d-operators", i*i*8), func(b *testing.B) {
res, _ = PrefixToInfix(testStr)
})
}
}