-
Notifications
You must be signed in to change notification settings - Fork 57
/
queue_test.go
100 lines (85 loc) · 1.97 KB
/
queue_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
// Copyright 2019 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zmq4
import (
"reflect"
"testing"
)
func makeMsg(i int) Msg {
return NewMsgString(string(rune(i)))
}
func TestQueue(t *testing.T) {
q := NewQueue()
if q.Len() != 0 {
t.Fatal("queue should be empty")
}
if _, exists := q.Peek(); exists {
t.Fatal("Queue should be empty")
}
q.Push(makeMsg(1))
if q.Len() != 1 {
t.Fatal("queue should contain 1 element")
}
msg, ok := q.Peek()
if !ok || !reflect.DeepEqual(msg, makeMsg(1)) {
t.Fatal("unexpected value in queue")
}
q.Push(makeMsg(2))
if q.Len() != 2 {
t.Fatal("queue should contain 2 elements")
}
msg, ok = q.Peek()
if !ok || !reflect.DeepEqual(msg, makeMsg(1)) {
t.Fatal("unexpected value in queue")
}
q.Pop()
if q.Len() != 1 {
t.Fatal("queue should contain 1 element")
}
msg, ok = q.Peek()
if !ok || !reflect.DeepEqual(msg, makeMsg(2)) {
t.Fatal("unexpected value in queue")
}
q.Pop()
if q.Len() != 0 {
t.Fatal("queue should be empty")
}
q.Push(makeMsg(1))
q.Push(makeMsg(2))
q.Init()
if q.Len() != 0 {
t.Fatal("queue should be empty")
}
}
func TestQueueNewInnerList(t *testing.T) {
q := NewQueue()
for i := 1; i <= innerCap; i++ {
q.Push(makeMsg(i))
}
if q.Len() != innerCap {
t.Fatalf("queue should contain %d elements", innerCap)
}
// next push will create a new inner slice
q.Push(makeMsg(innerCap + 1))
if q.Len() != innerCap+1 {
t.Fatalf("queue should contain %d elements", innerCap+1)
}
msg, ok := q.Peek()
if !ok || !reflect.DeepEqual(msg, makeMsg(1)) {
t.Fatal("unexpected value in queue")
}
q.Pop()
if q.Len() != innerCap {
t.Fatalf("queue should contain %d elements", innerCap)
}
msg, ok = q.Peek()
if !ok || !reflect.DeepEqual(msg, makeMsg(2)) {
t.Fatal("unexpected value in queue")
}
q.Push(makeMsg(innerCap + 1))
q.Init()
if q.Len() != 0 {
t.Fatal("queue should be empty")
}
}