forked from npillmayer/uax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prioq_test.go
152 lines (141 loc) · 3.57 KB
/
prioq_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package uax
import "testing"
// --- ad hoc type for testing purposes----------------------------------
type item struct { // will implement RuneSubscriber
done bool
}
func (it *item) Done() bool {
return it.done
}
func (it *item) Unsubscribed() {}
func (it *item) RuneEvent(r rune, codePointClass int) []int { return nil }
func (it *item) MatchLength() int { return 1 }
// ----------------------------------------------------------------------
func TestQueue1(t *testing.T) {
pq := &DefaultRunePublisher{}
if pq.PopDone() != nil {
t.Error("Should not be able to PopDone() on empty Q")
}
pq.Push(&item{done: true})
if pq.Len() != 1 {
t.Error("Len() should be 1 for Q with 1 item")
}
if !pq.Top().Done() {
t.Error("single item in Q is done; access by Top() does not reflect this")
}
if pq.gap != 0 {
t.Errorf("gap calculation after Push() to Q is wrong, should be 0, is %d", pq.gap)
}
pq.Fix(0)
if pq.gap != 0 {
t.Errorf("gap calculation in Q after Fix() is wrong, should be 0, is %d", pq.gap)
}
}
func TestQueue2(t *testing.T) {
pq := &DefaultRunePublisher{}
it := &item{done: true}
pq.Push(it)
pq.Push(&item{done: false})
if pq.Len() != 2 {
t.Error("Len() should be 2 for Q with 2 item")
}
if !pq.Top().Done() {
t.Error("top item in Q is not done; should be")
}
if pq.gap != 1 {
t.Errorf("gap calculation after Push()+Push() is wrong, should be 1, is %d", pq.gap)
}
it.done = false
pq.Fix(1)
if pq.Top().Done() {
t.Error("top item in Q is done; should not be any more")
}
if pq.gap != 2 {
t.Errorf("gap calculation after Fix() in Q with length 2 is wrong: %d", pq.gap)
}
}
func TestQueue3(t *testing.T) {
pq := &DefaultRunePublisher{}
it := &item{done: false}
pq.Push(it)
pq.Push(&item{done: true})
pq.Push(&item{done: false})
if pq.Len() != 3 {
t.Error("Len() should be 3 for Q with 3 item")
}
if !pq.Top().Done() {
t.Error("top item in Q is not done; should be")
}
if pq.gap != 2 {
t.Errorf("gap calculation after 3 x Push() is wrong: %d", pq.gap)
}
it.done = true
pq.Fix(0)
if pq.gap != 1 {
t.Errorf("gap calculation after Fix() in Q with length 3 is wrong: %d", pq.gap)
}
}
func TestQueue4(t *testing.T) {
pq := &DefaultRunePublisher{}
it1 := &item{done: false}
pq.Push(it1)
it2 := &item{done: false}
pq.Push(it2)
it3 := &item{done: false}
pq.Push(it3)
it4 := &item{done: false}
pq.Push(it4)
if pq.gap != 4 {
t.Errorf("gap calculation after 4 x Push() is wrong: %d", pq.gap)
}
it3.done = true
pq.Fix(2)
it2.done = true
pq.Fix(1)
it1.done = true
pq.Fix(0)
if pq.gap != 1 {
t.Errorf("gap calculation after Fix() is wrong: %d", pq.gap)
}
for j := 0; j < 3; j++ {
if s := pq.PopDone(); s == nil {
t.Error("top 3 items should have been done")
}
}
if pq.Top().Done() {
t.Error("top/only item in Q is done; should not be")
}
if pq.Len() != 1 {
t.Error("Len() should be 1 after 3 pops")
}
}
func TestQueue5(t *testing.T) {
pq := &DefaultRunePublisher{}
it1 := &item{done: false}
pq.Push(it1)
it2 := &item{done: false}
pq.Push(it2)
it3 := &item{done: false}
pq.Push(it3)
if pq.gap != 3 {
t.Errorf("gap calculation after 3 x Push() is wrong: %d", pq.gap)
}
it2.done = true
pq.Fix(1)
if s := pq.PopDone(); s == nil {
t.Error("top item should have been done")
}
if pq.Len() != 2 {
t.Error("Len() should be 2 after 1 pop")
}
it4 := &item{done: false}
pq.Push(it4)
if s := pq.PopDone(); s != nil {
t.Error("top item should not have been done")
}
it1.done = true
pq.Fix(0)
if s := pq.PopDone(); s == nil {
t.Error("new top item should have been done")
}
}