-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_test.go
54 lines (48 loc) · 1.04 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
package util
import "testing"
func Test_Stack(t *testing.T) {
var s = Stack{}
if s.Size() != 0 {
t.Error("A new Stack should have a size of zero")
}
if !s.Empty() {
t.Error("Empty should be true with an empty stack")
}
var n = 5
for i := 0; i < n; i++ {
s.Push(i)
}
if s.Size() != n {
t.Errorf("Size is wrong: expected %d but got %d", n, s.Size())
}
if s.Empty() {
t.Error("Empty should be false with a non-empty stack")
}
var x = "test"
s.Push(x)
n++
if s.Size() != n {
t.Error("Push should increase the stack size")
}
var y, err = s.Peek()
if err != nil || y == nil {
t.Fatal("Peek should not fail on a non-empty stack")
}
if x != y {
t.Error("Peek should return the last pushed element")
}
if s.Size() != n {
t.Error("Peek should not alter the stack size")
}
y, err = s.Pop()
n--
if err != nil || y == nil {
t.Fatal("Pop should not fail on a non-empty stack")
}
if x != y {
t.Error("Pop should return the last pushed element")
}
if s.Size() != n {
t.Error("Pop should decrease the stack size")
}
}