-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathqueue_using_stacks_test.go
48 lines (42 loc) · 1.12 KB
/
queue_using_stacks_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
package queue
import "testing"
/*
TestQueueUsingStacks tests solution(s) with the following signature and problem description:
type UsingStacks struct {
stack1 Stack
stack2 Stack
}
(usingStacks *UsingStacks) enqueue(n int)
(usingStacks *UsingStacks) dequeue() int
Implement a queue of integers using stacks.
*/
func TestQueueUsingStacks(t *testing.T) {
tests := []struct {
enqueue []int
dequeueTimes int
expectedLastDequeuedItem int
expectErr bool
}{
{[]int{1, 2, 3, 4}, 1, 1, false},
{[]int{1, 2, 3, 4}, 2, 2, false},
{[]int{1, 2, 3, 4}, 3, 3, false},
{[]int{1, 2, 2, 3}, 2, 2, false},
}
for i, test := range tests {
queue := new(UsingStacks)
for _, n := range test.enqueue {
queue.enqueue(n)
}
var n int
var err error
for j := 1; j <= test.dequeueTimes; j++ {
n = queue.dequeue()
if !test.expectErr && err != nil {
t.Fatalf("Failed test case #%d. Did not expect an error. Error %s", i, err)
}
}
if n != test.expectedLastDequeuedItem {
t.Fatalf("Failed test case #%d. Want %d got %d", i, test.expectedLastDequeuedItem, n)
}
}
}