-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_test.go
79 lines (62 loc) · 1.73 KB
/
stack_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
package datastruct
import (
"testing"
)
func TestStackIsEmpty(t *testing.T) {
var stack = NewStack()
if !stack.IsEmpty() {
t.Errorf("Stack: IsEmpty error: it should be empty")
}
stack.Push(1)
if stack.IsEmpty() {
t.Errorf("Stack: IsEmpty error: it should not be empty")
}
}
func TestStackPush(t *testing.T) {
var stack = NewStack()
stack.Push(1)
stack.Push(2)
stack.Push(3)
if stack.GetLength() != 3 {
t.Errorf("Stack: Push error: the stack length should be 3 but get length: %d", stack.GetLength())
}
}
func TestStackPop(t *testing.T) {
var stack = NewStack()
if pop := stack.Pop(); pop != nil {
t.Errorf("Stack: Pop error: it should be nil, but get %v", pop)
}
stack.Push(1)
stack.Push(2)
stack.Push(3)
if pop := stack.Pop(); pop != 3 {
t.Errorf("Stack: Pop error: the node should be %d, but is %d", 3, pop)
}
if stack.GetLength() != 2 {
t.Errorf("Stack: Pop error: the stack length should be %d, but is %d", 2, stack.GetLength())
}
}
func TestStackGetTop(t *testing.T) {
var stack = NewStack()
if top := stack.GetTop(); top != nil {
t.Errorf("Stack: GetTop error: it should be nil, but get %v", top)
}
stack.Push(1)
stack.Push(2)
stack.Push(3)
if top := stack.GetTop(); top != 3 {
t.Errorf("Stack: GetTop error: the node should be %d, but is %d", 3, top)
}
if stack.GetLength() != 3 {
t.Errorf("Stack: GetTop error: the stack length should be %d, but is %d", 3, stack.GetLength())
}
}
func TestStackClear(t *testing.T) {
var stack = NewStack()
stack.Push(1)
stack.Push(2)
stack.Push(3)
if stack.Clear(); stack.Head != nil || stack.GetLength() != 0 {
t.Errorf("Stack: Clear error: the stack length should be 0 and the head should be nil but is %d and is %v", stack.GetLength(), stack.Head)
}
}