-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstack_test.go
127 lines (99 loc) · 2.42 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package winman_test
import (
"strings"
"testing"
"github.com/epiclabs-io/winman"
)
func dump(s winman.Stack) string {
var b strings.Builder
for _, e := range s {
b.WriteRune(e.(rune))
}
return b.String()
}
func checkDump(t *testing.T, s winman.Stack, expected string) {
actual := dump(s)
if actual != expected {
t.Fatalf("Expected stack to contain %q, got %q", expected, actual)
}
}
func TestStack(t *testing.T) {
var s winman.Stack
e := s.Pop()
if e != nil {
t.Fatalf("Expected .Pop to return nil when the stack is empty, got %v", e)
}
// add an element
s.Push('A')
if len(s) != 1 {
t.Fatalf("Expected the length of the stack to be 1 after adding the first element, got %d", len(s))
}
// add the same element
s.Push('A')
if len(s) != 1 {
t.Fatalf("Expected the length of the stack to be 1 after adding the same element, got %d", len(s))
}
// expect panic if a nil item is added.
assertPanic(t, func() {
s.Push(nil)
})
s.Push('B')
if len(s) != 2 {
t.Fatalf("Expected the length of the stack to be 2 after adding a unique element, got %d", len(s))
}
e = s.Pop()
if e != 'B' {
t.Fatalf("Expected Pop to return the last pushed element, got %v", e)
}
if len(s) != 1 {
t.Fatalf("Expected the length of the stack to be 1 after removing the second element, got %d", len(s))
}
s = nil // clear the stack
items := []rune{'A', 'B', 'C', 'D', 'E', 'F', 'G'}
// add a bunch of items
for _, e := range items {
s.Push(e)
}
checkDump(t, s, "ABCDEFG")
// remove the first item
s.Remove('A')
checkDump(t, s, "BCDEFG")
// remove the last item
s.Remove('G')
checkDump(t, s, "BCDEF")
// remove some intermediate item
s.Remove('E')
checkDump(t, s, "BCDF")
// move first item to the last position
s.Move('B', -1)
checkDump(t, s, "CDFB")
// move last item to first position
s.Move('B', 0)
checkDump(t, s, "BCDF")
// move intermediate item to first position
s.Move('D', 0)
checkDump(t, s, "DBCF")
// move intermediate item to last position
s.Move('C', 10000)
checkDump(t, s, "DBFC")
// test find
found := s.Find(func(item interface{}) bool {
r := item.(rune)
return r > 'C'
})
if found.(rune) != 'F' {
t.Fatalf("Expected Find to find 'F', found %c", found.(rune))
}
indices := map[rune]int{
'D': 0,
'B': 1,
'F': 2,
'C': 3,
}
for r, i := range indices {
index := s.IndexOf(r)
if index != i {
t.Fatalf("Expected to find %c at index %d, got %d", r, i, index)
}
}
}