This repository was archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathstack_test.go
102 lines (73 loc) · 1.8 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package queuestack
import "testing"
func TestStackIsInitiallyEmpty(t *testing.T) {
s := NewStack()
if !s.Empty() {
t.Error("newly created stack should contain no elements!")
}
}
func TestStackIsNonEmptyAfterPush(t *testing.T) {
s := NewStack()
s.Push(1)
if s.Empty() {
t.Error("stack should not be empty after pushing any elements!")
}
}
func TestStackIsEmptyAfterPushAndPop(t *testing.T) {
s := NewStack()
s.Push(1)
v, err := s.Pop()
if !s.Empty() {
t.Error("stack should be empty after pushing and popping an element!")
}
if err != nil {
t.Error("there should not have been anything erroneous occurring after pushing and popping an element:", err)
}
if v != 1 {
t.Error("value popped should be the same as what was pushed! got:", v, "want:", 1)
}
}
func TestPoppingAnEmptyStackErrors(t *testing.T) {
s := NewStack()
_, err := s.Pop()
if err == nil {
t.Error("stack should have errored when it was popped while empty!")
}
}
func TestPeekingAnEmptyStackErrors(t *testing.T) {
s := NewStack()
_, err := s.Peek()
if err == nil {
t.Error("stack should have errored when it was peeked while empty!")
}
}
func TestPeekReturnsTopOfStack(t *testing.T) {
s := NewStack()
s.Push(1)
s.Push(2)
v, err := s.Peek()
if err != nil {
t.Error("there should have been nothing erroneous that occurred:", err)
}
if v != 2 {
t.Error("stack should have returned the top value on the stack! got:", v, "want:", 2)
}
}
func TestStackIsLIFOCompliant(t *testing.T) {
s := NewStack()
values := []interface{}{0, 1, 2, 3}
for _, v := range values {
s.Push(v)
}
offset := len(values) - 1
for !s.Empty() {
val, err := s.Pop()
if err != nil {
t.Fatal("encountered unexpected error:", err)
}
if val != values[offset] {
t.Error("stack is not LIFO compliant")
}
offset--
}
}