-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_test.go
More file actions
82 lines (74 loc) · 1.57 KB
/
Copy pathstack_test.go
File metadata and controls
82 lines (74 loc) · 1.57 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
package stack
import "testing"
func TestEmpty(t *testing.T) {
s := New()
if s.Empty() == false {
t.Error("Stack should be empty")
}
s.Push(10)
if s.Empty() == true {
t.Error("Stack should not be empty")
}
s.Pop()
if s.Empty() == false {
t.Error("Stack should be empty")
}
}
func TestLen(t *testing.T) {
s := New()
// Test that s.Len() is incremented as items pushed
for i := 0; i < 10; i++ {
if s.Len() != i {
t.Errorf("Stack length is %d instead of %d", i, s.Len())
}
s.Push(1)
}
// Test that s.Len() is decremented as items popped
for i := 10; i >= 0; i-- {
if s.Len() != i {
t.Errorf("Stack length is %d instead of %d", i, s.Len())
}
s.Pop()
}
// Test that s.Len() doesn't become negative when nothing to pop
s.Pop()
if s.Len() != 0 {
t.Errorf("Stack length is %d instead of 0", s.Len())
}
}
func TestPush(t *testing.T) {
s := New()
err := s.Push(10)
if err != nil {
t.Errorf("Stack Push error: %s", err)
}
err = s.Push(11)
if err != nil {
t.Errorf("Stack Push error: %s", err)
}
}
func TestPop(t *testing.T) {
s := New()
v1 := 10
v2 := 11
_, err := s.Pop()
if err == nil {
t.Errorf("stack.Pop() should have returned Empty Stack error")
}
s.Push(v1)
s.Push(v2)
val, err := s.Pop()
if err != nil {
t.Errorf("stack.Pop() returned error: %s", err)
}
if val != v2 {
t.Error("stack.Pop() returned %d but should have returned %d", val, v2)
}
val, err = s.Pop()
if err != nil {
t.Errorf("stack.Pop() returned error: %s", err)
}
if val != v1 {
t.Error("stack.Pop() returned %d but should have returned %d", val, v1)
}
}