-
Notifications
You must be signed in to change notification settings - Fork 58
/
ordersqueue_test.go
78 lines (68 loc) · 1.36 KB
/
ordersqueue_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
package hftorderbook
import (
"testing"
)
func TestOrdersQueueEmpty(t *testing.T) {
q := NewOrdersQueue()
if !q.IsEmpty() || q.Size() != 0 {
t.Errorf("a queue should be initialized as empty")
}
}
func TestOrdersQueueEnqueDequeue(t *testing.T) {
q := NewOrdersQueue()
o := &Order{
Id: 1,
}
q.Enqueue(o)
if q.Size() != 1 || q.IsEmpty() {
t.Errorf("a queue size should be non-zero")
}
r := q.Dequeue()
if q.Size() != 0 || !q.IsEmpty() {
t.Errorf("a queue should be empty now")
}
r2 := q.Dequeue()
if r2 != nil {
t.Errorf("no elements should remain in the queue")
}
if o != r {
t.Errorf("consistency failed")
}
q.Enqueue(o)
q.Dequeue()
if q.Size() != 0 || !q.IsEmpty() {
t.Errorf("a queue should be empty now")
}
}
func TestOrdersQueueMultiple(t *testing.T) {
q := NewOrdersQueue()
n := 100
for i := 0; i < n; i += 1 {
q.Enqueue(&Order{ Id: i })
}
if q.Size() != n {
t.Errorf("queue size should be %d", n)
}
for i := 0; i < n; i += 1 {
o := q.Dequeue()
if o.Id != i {
t.Errorf("order invariant failed")
break
}
if q.Size() != n-i-1 {
t.Errorf("invalid size counter")
break
}
}
}
func TestOrdersQueueMixed(t *testing.T) {
q := NewOrdersQueue()
n := 100
for i := 0; i < n; i += 1 {
q.Enqueue(&Order{ Id: i })
q.Dequeue()
}
if q.Size() != 0 || !q.IsEmpty() {
t.Errorf("a queue should be empty now")
}
}