-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie_queue_internal_test.go
114 lines (88 loc) · 2.16 KB
/
trie_queue_internal_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
package gostr
import "testing"
func TestTrieQueue(t *testing.T) { //nolint:funlen // test functions can be long, damned you!
// this is just dummy data...
var (
n1 = new(Trie)
n2 = new(Trie)
n3 = new(Trie)
n4 = new(Trie)
n5 = new(Trie)
n6 = new(Trie)
n7 = new(Trie)
queue = newTrieQueue(0)
)
if !queue.isEmpty() {
t.Fatal("queue should be empty")
}
if queue.isFull() {
t.Fatal("queue should not be full")
}
queue.enqueue(n1)
if !queue.isFull() {
t.Fatal("queue should be full now")
}
queue.enqueue(n2)
if len(queue.elms) != 2 {
t.Errorf("we expected the queue to have two elements, but it has %d", len(queue.elms))
}
if queue.used != 2 {
t.Errorf("we expected the queue to have two elements, but it has %d", queue.used)
}
if !queue.isFull() {
t.Fatal("queue should be full again (now with cap 2)")
}
queue.enqueue(n3)
if queue.used != 3 {
t.Errorf("The queue should have three elments, but it has %d", queue.used)
}
if len(queue.elms) != 4 {
t.Errorf("expected cap on 5 but it is %d", len(queue.elms))
}
var n *Trie
n = queue.dequeue()
if n != n1 {
t.Errorf("unexpected dequeue: %p", n)
}
if queue.used != 2 {
t.Errorf("The queue should have two elments, but it has %d", queue.used)
}
n = queue.dequeue()
if n != n2 {
t.Errorf("unexpected dequeue: %p", n)
}
if queue.used != 1 {
t.Errorf("The queue should have one elment, but it has %d", queue.used)
}
// push it up to capacity again... cap is 4 and we have used 1, so we need to add 4
queue.enqueue(n4)
queue.enqueue(n5)
queue.enqueue(n6)
queue.enqueue(n7)
n = queue.dequeue()
if n != n3 {
t.Errorf("unexpected dequeue: %p", n)
}
n = queue.dequeue()
if n != n4 {
t.Errorf("unexpected dequeue: %p", n)
}
n = queue.dequeue()
if n != n5 {
t.Errorf("unexpected dequeue: %p", n)
}
n = queue.dequeue()
if n != n6 {
t.Errorf("unexpected dequeue: %p", n)
}
n = queue.dequeue()
if n != n7 {
t.Errorf("unexpected dequeue: %p", n)
}
if queue.used != 0 {
t.Errorf("The queue should have zero elments, but it has %d", queue.used)
}
if !queue.isEmpty() {
t.Error("The queue should be empty, but it isn't.")
}
}