forked from bjssacademy/stackmachine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackmachine_test.go
More file actions
216 lines (181 loc) · 4.97 KB
/
Copy pathstackmachine_test.go
File metadata and controls
216 lines (181 loc) · 4.97 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"testing"
)
func TestStartsWithEmptyStack(t *testing.T) {
_, err := StackMachine("")
if err == nil {
t.Error("expected error due to no results")
}
}
// Write your own TDD tests here as you develop
func TestCommandNotValid(t *testing.T) {
commands := []string{
"12+",
"INVALID",
}
for _, command := range commands {
_, err := StackMachine(command)
if err == nil {
t.Error("expected error for invalid commands")
}
}
}
func TestHasNumberOutOfBounds(t *testing.T) {
commands := []string{
"50001",
"-34",
}
for _, command := range commands {
_, err := StackMachine(command)
if err == nil {
t.Error("expected error for integer out of bounds")
}
}
}
func TestInvalidCommandsForAdd(t *testing.T) {
commands := []string{
"+",
"12 +",
"1 2 + +",
"1 2 + 4 + +",
}
for _, command := range commands {
_, err := StackMachine(command)
if err == nil {
t.Error("expected error for add operation")
}
}
}
func TestInvalidCommandsForDUP(t *testing.T) {
commands := []string{
"DUP",
}
for _, command := range commands {
_, err := StackMachine(command)
if err == nil {
t.Error("expected error for DUP operation")
}
}
}
func TestInvalidCommandsForPOP(t *testing.T) {
commands := []string{
"POP",
}
for _, command := range commands {
_, err := StackMachine(command)
if err == nil {
t.Error("expected error for POP operation")
}
}
}
func TestInvalidCommandsForMinus(t *testing.T) {
commands := []string{
"12 -",
"-",
}
for _, command := range commands {
_, err := StackMachine(command)
if err == nil {
t.Error("expected error for - operation")
}
}
}
func TestInvalidCommandsForMultiply(t *testing.T) {
commands := []string{
"12 *",
"*",
}
for _, command := range commands {
_, err := StackMachine(command)
if err == nil {
t.Error("expected error for * operation")
}
}
}
func TestClearCommandError(t *testing.T) {
commands := []string{
"12 CLEAR",
"12 23 CLEAR",
}
for _, command := range commands {
got, _ := StackMachine(command)
if got != 0 {
t.Errorf("expected %d, got %d", 0, got)
}
}
}
func TestSumCommandError(t *testing.T) {
commands := []string{
"SUM",
"50000 45 SUM",
}
for _, command := range commands {
got, err := StackMachine(command)
if err == nil {
t.Errorf("expected error for SUM operation got %d", got)
}
}
}
func TestCommandReturnsExpectedOutput(t *testing.T) {
commands := []string{
"501",
}
for _, command := range commands {
got, _ := StackMachine(command)
expected := 501
if got != expected {
t.Errorf("expected %d, but got %d", expected, got)
}
}
}
/*
All these tests must pass for completion
*/
func TestAcceptanceTests(t *testing.T) {
tests := []struct {
name string
commands string
expected int
expectedErr error
}{
{name: "empty error", commands: "", expected: 0},
{name: "add overflow", commands: "50000 DUP +", expected: 0},
{name: "too few add", commands: "99 +", expected: 0},
{name: "too few minus", commands: "99 -", expected: 0},
{name: "too few multiply", commands: "99 *", expected: 0},
{name: "empty stack", commands: "99 CLEAR", expected: 0},
{name: "sum single value", commands: "99 SUM", expected: 99},
{name: "sum empty", commands: "SUM", expected: 0},
{name: "normal +*", commands: "5 6 + 2 *", expected: 22},
{name: "clear too few", commands: "1 2 3 4 + CLEAR 12 +", expected: 0},
{name: "normal after clear", commands: "1 CLEAR 2 3 +", expected: 5},
{name: "single integer", commands: "9876", expected: 9876},
{name: "invalid command", commands: "DOGBANANA", expected: 0},
{name: "normal +-*", commands: "5 9 DUP + + 43 - 3 *", expected: 60},
{name: "minus", commands: "2 5 -", expected: 3},
{name: "underflow minus", commands: "5 2 -", expected: 0},
{name: "at overflow limit", commands: "25000 DUP +", expected: 50000},
{name: "at overflow limit single value", commands: "50000 0 +", expected: 50000},
{name: "overflow plus", commands: "50000 1 +", expected: 0},
{name: "overflow single value", commands: "50001", expected: 0},
{name: "times zero at overflow limit", commands: "50000 0 *", expected: 0},
{name: "too few at first", commands: "1 2 3 4 5 + + + + * 999", expected: 0},
{name: "normal simple", commands: "1 2 - 99 +", expected: 100},
{name: "at overflow minus to zero", commands: "50000 50000 -", expected: 0},
{name: "clear empties stack", commands: "CLEAR", expected: 0},
{name: "normal sum", commands: "3 4 3 5 5 1 1 1 SUM", expected: 23},
{name: "sum after clear stack", commands: "3 4 3 5 CLEAR 5 1 1 1 SUM", expected: 8},
{name: "sum then too few", commands: "3 4 3 5 5 1 1 1 SUM -", expected: 0},
{name: "fibonacci", commands: "1 2 3 4 5 * * * *", expected: 120},
}
for _, test := range tests {
got, err := StackMachine(test.commands)
if got > 0 && err != nil {
t.Errorf("expected no error, got %s", err.Error())
}
if got != test.expected {
t.Errorf("(%s) got %v, want %v", test.commands, got, test.expected)
}
}
}