-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackmachine_test.go
More file actions
291 lines (228 loc) · 8.64 KB
/
stackmachine_test.go
File metadata and controls
291 lines (228 loc) · 8.64 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
// "errors"
"fmt"
"testing"
)
func TestStack_Pop(t *testing.T) {
stack := NewStackFromNumbers([]int{1, 2, 3})
poppedValue, err := stack.Pop()
if err != nil {
t.Fatalf("expected no error, but got %v", err)
}
if poppedValue != 3 {
t.Errorf("expected popped value to be 3, but got %d", poppedValue)
}
if stack.len() != 2 {
t.Errorf("expected stack size to be 2 after pop, but got %d", len(stack.StackNumbers))
}
emptyStack := NewStack()
_, err = emptyStack.Pop()
if err == nil {
t.Error("expected an error when popping from an empty stack, but got none")
}
}
func TestStack_Push(t *testing.T) {
stack := NewStack()
stack.Push(5)
if stack.len() != 1 {
t.Errorf("expected stack size to be 1 after pushing, but got %d", len(stack.StackNumbers))
}
top := stack.Peek()
if top != 5 {
t.Errorf("expected top element to be 5 after pushing, but got %d", stack.StackNumbers[0])
}
stack.Push(10)
stack.Push(15)
if stack.len() != 3 {
t.Errorf("expected stack size to be 3 after pushing multiple elements, but got %d", len(stack.StackNumbers))
}
top = stack.Peek()
if top != 15 {
t.Errorf("expected top element to be 15 after pushing, but got %d", stack.StackNumbers[2])
}
stackWithNumbers := NewStackFromNumbers([]int{1, 2, 3})
stackWithNumbers.Push(4)
if stackWithNumbers.len() != 4 {
t.Errorf("expected stack size to be 4 after pushing, but got %d", len(stackWithNumbers.StackNumbers))
}
if stackWithNumbers.StackNumbers[3] != 4 {
t.Errorf("expected top element to be 4 after pushing, but got %d", stackWithNumbers.StackNumbers[3])
}
stackWithBigNumbers := NewStackFromNumbers([]int{20001, 30000})
err := stackWithBigNumbers.Plus()
if err == nil {
t.Errorf("expected an error when summing 20001 and 30000 but did not receive one.")
}
}
func TestStack_Duplicate(t *testing.T) {
stack := NewStackFromNumbers([]int{1, 2, 3})
stack.Duplicate()
if stack.len() != 4 {
t.Errorf("expected stack size to be 4 after DUP, but got %d", len(stack.StackNumbers))
}
top := stack.Peek()
if top != 3 {
t.Errorf("expected top element to be 3 after DUP, but got %d", stack.StackNumbers[3])
}
emptyStack := NewStack()
if emptyStack.len() != 0 {
t.Errorf("expected stack size to remain 0 after DUP on empty stack, but got %d", emptyStack.len())
}
}
func TestStack_Plus(t *testing.T) {
stack := NewStackFromNumbers([]int{10, 20})
err := stack.Plus()
if err != nil {
t.Fatalf("expected no error, but got %v", err)
}
if stack.len() != 1 {
t.Errorf("expected stack size to be 1 after addition, but got %d", len(stack.StackNumbers))
}
top := stack.Peek()
if top != 30 {
t.Errorf("expected top element to be 30 after addition, but got %d", stack.StackNumbers[0])
}
stackWithOverflow := NewStackFromNumbers([]int{25000, 26000})
err = stackWithOverflow.Plus()
if err == nil {
t.Fatal("expected an overflow error, but got none")
}
stackWithOneElement := NewStackFromNumbers([]int{10})
err = stackWithOneElement.Plus()
if err == nil {
t.Fatal("expected an error due to insufficient elements, but got none")
}
}
func TestStack_Minus(t *testing.T) {
stack := NewStackFromNumbers([]int{10, 20})
err := stack.Minus()
if err != nil {
t.Fatalf("expected no error, but got %v", err)
}
if stack.len() != 1 {
t.Errorf("expected stack size to be 1 after subtraction, but got %d", len(stack.StackNumbers))
}
top := stack.Peek()
if top != 10 {
t.Errorf("expected top element to be 10 after subtraction, but got %d", stack.StackNumbers[0])
}
stackWithNegativeResult := NewStackFromNumbers([]int{20, 10})
err = stackWithNegativeResult.Minus()
if err == nil {
t.Fatal("expected a result below zero error, but got none")
}
stackWithOneElement := NewStackFromNumbers([]int{10})
err = stackWithOneElement.Minus()
if err == nil {
t.Fatal("expected an error due to insufficient elements, but got none")
}
}
func TestStack_Multiply(t *testing.T) {
stack := NewStackFromNumbers([]int{10, 5})
err := stack.Multiply()
if err != nil {
t.Fatalf("expected no error, but got %v", err)
}
if stack.len() != 1 {
t.Errorf("expected stack size to be 1 after multiplication, but got %d", len(stack.StackNumbers))
}
top := stack.Peek()
if top != 50 {
t.Errorf("expected top element to be 50 after multiplication, but got %d", stack.StackNumbers[0])
}
stackWithOneElement := NewStackFromNumbers([]int{10})
err = stackWithOneElement.Multiply()
if err == nil {
t.Fatal("expected an error due to insufficient elements, but got none")
}
stackWithLargeNumbers := NewStackFromNumbers([]int{1000, 2000})
err = stackWithLargeNumbers.Multiply()
if err == nil {
t.Error("expected error when multiplying two large numbers but did not receieve one.")
}
}
func TestStack_Sum(t *testing.T) {
stack := NewStackFromNumbers([]int{10, 20, 30})
err := stack.Sum()
if err != nil {
t.Fatalf("expected no error, but got %v", err)
}
if stack.len() != 1 {
t.Errorf("expected stack size to be 1 after SUM, but got %d", len(stack.StackNumbers))
}
top := stack.Peek()
if top != 60 {
t.Errorf("expected top element to be 60 after SUM, but got %d", stack.StackNumbers[0])
}
emptyStack := NewStack()
err = emptyStack.Sum()
if err == nil {
t.Fatal("expected an error due to empty stack, but got none")
}
stackWithOneElement := NewStackFromNumbers([]int{42})
err = stackWithOneElement.Sum()
if err != nil {
t.Fatalf("expected no error, but got %v", err)
}
if stack.len() != 1 {
t.Errorf("expected stack size to be 1 after SUM, but got %d", len(stackWithOneElement.StackNumbers))
}
top = stackWithOneElement.Peek()
if top != 42 {
t.Errorf("expected top element to be 42 after SUM, but got %d", top)
}
}
/*
All these tests must pass for completion
*/
func TestAcceptanceTests(t *testing.T) {
tests := []struct {
name string
commands string
expected int
shouldError bool
}{
{name: "empty error", commands: "", expected: 0, shouldError: true},
{name: "add overflow", commands: "50000 DUP +", expected: 0, shouldError: true},
{name: "too few add", commands: "99 +", expected: 0, shouldError: true},
{name: "too few minus", commands: "99 -", expected: 0, shouldError: true},
{name: "too few multiply", commands: "99 *", expected: 0, shouldError: true},
{name: "empty stack", commands: "99 CLEAR", expected: 0, shouldError: true},
{name: "sum single value", commands: "99 SUM", expected: 99, shouldError: false},
{name: "sum empty", commands: "SUM", expected: 0, shouldError: true},
{name: "normal +*", commands: "5 6 + 2 *", expected: 22, shouldError: false},
{name: "clear too few", commands: "1 2 3 4 + CLEAR 12 +", expected: 0, shouldError: true},
{name: "normal after clear", commands: "1 CLEAR 2 3 +", expected: 5, shouldError: false},
{name: "single integer", commands: "9876", expected: 9876, shouldError: false},
{name: "invalid command", commands: "DOGBANANA", expected: 0, shouldError: true},
{name: "normal +-*", commands: "5 9 DUP + + 43 - 3 *", expected: 60, shouldError: false},
{name: "minus", commands: "2 5 -", expected: 3, shouldError: false},
{name: "underflow minus", commands: "5 2 -", expected: 0, shouldError: true},
{name: "at overflow limit", commands: "25000 DUP +", expected: 50000, shouldError: false},
{name: "at overflow limit single value", commands: "50000 0 +", expected: 50000, shouldError: false},
{name: "overflow plus", commands: "50000 1 +", expected: 0, shouldError: true},
{name: "overflow single value", commands: "50001", expected: 0, shouldError: true},
{name: "times zero at overflow limit", commands: "50000 0 *", expected: 0, shouldError: false},
{name: "too few at first", commands: "1 2 3 4 5 + + + + * 999", expected: 0, shouldError: true},
{name: "normal simple", commands: "1 2 - 99 +", expected: 100, shouldError: false},
{name: "at overflow minus to zero", commands: "50000 50000 -", expected: 0, shouldError: false},
{name: "clear empties stack", commands: "CLEAR", expected: 0, shouldError: true},
{name: "normal sum", commands: "3 4 3 5 5 1 1 1 SUM", expected: 23, shouldError: false},
{name: "sum after clear stack", commands: "3 4 3 5 CLEAR 5 1 1 1 SUM", expected: 8, shouldError: false},
{name: "sum then too few", commands: "3 4 3 5 5 1 1 1 SUM -", expected: 0, shouldError: true},
{name: "fibonacci", commands: "1 2 3 4 5 * * * *", expected: 120, shouldError: false},
}
for _, test := range tests {
fmt.Println(test.name)
got, err := StackMachine(test.commands)
fmt.Println(got)
if test.shouldError {
if err == nil {
t.Errorf("%s (%s) Expected error, but received nil", test.name, test.commands)
}
} else if got != test.expected {
t.Errorf("%s (%s) got %v, want %v", test.name, test.commands, got, test.expected)
}
}
}