-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_test.go
More file actions
94 lines (87 loc) · 2.09 KB
/
stack_test.go
File metadata and controls
94 lines (87 loc) · 2.09 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
83
84
85
86
87
88
89
90
91
92
93
94
package gdk
import "testing"
type Info struct {
Name string
}
func TestNewStack(t *testing.T) {
t.Run("StackEmpty", func(t *testing.T) {
s := NewStack[*Info]()
if s.IsEmpty() != true {
t.Errorf("got %v, want true", s.IsEmpty())
}
ss := NewStackSize[*Info](-1)
if ss.IsEmpty() != true {
t.Errorf("got %v, want true", ss.IsEmpty())
}
})
t.Run("StackSize", func(t *testing.T) {
s := NewStackSize[*Info](16)
if s.Cap() != 16 {
t.Errorf("got %v, want %v", s.Cap(), 16)
}
})
}
func TestStackPushPop(t *testing.T) {
s := NewStackSize[*Info](16)
s.Push(&Info{Name: "matt"})
if s.Cap() != 15 {
t.Errorf("s.Cap() should return 15, got %v", s.Cap())
}
if s.IsEmpty() != false {
t.Errorf("s.IsEmpty() should return false, got %v", s.IsEmpty())
}
info := s.Pop()
if info == nil {
t.Errorf("info should not be nil, got %v", info)
}
if info.Name != "matt" {
t.Errorf("info.Name should be matt, got %v", info.Name)
}
info = s.Pop()
if info != nil {
t.Errorf("info should be nil, got %v", info)
}
if s.IsEmpty() != true {
t.Errorf("s.IsEmpty() should be true, got %v", s.IsEmpty())
}
}
func TestStackResize(t *testing.T) {
t.Run("Resize", func(t *testing.T) {
s := NewStackSize[*Info](2)
if s.Size() != 0 {
t.Errorf("s.Size() should equal 0, got %v", s.Size())
}
if s.Cap() != 2 {
t.Errorf("s.Cap() should equal 2, got %v", s.Cap())
}
s.Push(&Info{"matt"})
s.Push(&Info{"xml"})
if s.Size() != 2 {
t.Errorf("s.Size() should return 2, got %v", s.Size())
}
if c := s.Cap(); c != 0 {
t.Errorf("s.Cap() should return 0, got %v", c)
}
s.Push(&Info{"max"})
if s := s.Size(); s != 3 {
t.Errorf("s.Size() should return 3, got %v", s)
}
if c := s.Cap(); c != 1 {
t.Errorf("s.Cap() should return 1, got %v", c)
}
})
}
func TestStackCopy(t *testing.T) {
t.Run("Copy", func(t *testing.T) {
s := NewStackSize[*Info](2)
s.Push(&Info{"matt"})
s.Push(&Info{"xl"})
cs := s.Copy()
if cs.Cap() != s.Cap() {
t.Errorf("got %v, want %v", cs.Cap(), s.Cap())
}
if cs.Size() != s.Size() {
t.Errorf("got %v, want %v", cs.Size(), s.Size())
}
})
}