forked from i25959341/orderbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderqueue_test.go
122 lines (98 loc) · 2.65 KB
/
orderqueue_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
package orderbook
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/shopspring/decimal"
)
func TestOrderQueue(t *testing.T) {
price := decimal.New(100, 0)
oq := NewOrderQueue(price)
o1 := NewOrder(
"order-1",
Buy,
decimal.New(100, 0),
decimal.New(100, 0),
time.Now().UTC(),
)
o2 := NewOrder(
"order-2",
Buy,
decimal.New(100, 0),
decimal.New(100, 0),
time.Now().UTC(),
)
head := oq.Append(o1)
tail := oq.Append(o2)
if head == nil || tail == nil {
t.Fatal("Could not append order to the OrderQueue")
}
if !oq.Volume().Equal(decimal.New(200, 0)) {
t.Fatalf("Invalid order volume (have: %s, want: 200", oq.Volume())
}
if head.Value.(*Order) != o1 || tail.Value.(*Order) != o2 {
t.Fatal("Invalid element value")
}
if oq.Head() != head || oq.Tail() != tail {
t.Fatal("Invalid element position")
}
if oq.Head().Next() != oq.Tail() || oq.Tail().Prev() != head ||
oq.Head().Prev() != nil || oq.Tail().Next() != nil {
t.Fatal("Invalid element link")
}
o1 = NewOrder(
"order-3",
Buy,
decimal.New(200, 0),
decimal.New(200, 0),
time.Now().UTC(),
)
oq.Update(head, o1)
if !oq.Volume().Equal(decimal.New(300, 0)) {
t.Fatalf("Invalid order volume (have: %s, want: 300", oq.Volume())
}
if o := oq.Remove(head); o != o1 {
t.Fatal("Invalid element value")
}
if !oq.Volume().Equal(decimal.New(100, 0)) {
t.Fatalf("Invalid order volume (have: %s, want: 100", oq.Volume())
}
t.Log(oq)
}
func TestOrderQueueJSON(t *testing.T) {
data := NewOrderQueue(decimal.New(111, 0))
data.Append(NewOrder("one", Buy, decimal.New(11, -1), decimal.New(11, 1), time.Now().UTC()))
data.Append(NewOrder("two", Buy, decimal.New(22, -1), decimal.New(22, 1), time.Now().UTC()))
data.Append(NewOrder("three", Sell, decimal.New(33, -1), decimal.New(33, 1), time.Now().UTC()))
data.Append(NewOrder("four", Sell, decimal.New(44, -1), decimal.New(44, 1), time.Now().UTC()))
result, _ := json.Marshal(data)
t.Log(string(result))
data = NewOrderQueue(decimal.Zero)
if err := json.Unmarshal(result, data); err != nil {
t.Fatal(err)
}
t.Log(data)
err := json.Unmarshal([]byte(`[{"side":"fake"}]`), &data)
if err == nil {
t.Fatal("can unmarshal unsupported value")
}
}
func BenchmarkOrderQueue(b *testing.B) {
price := decimal.New(100, 0)
orderQueue := NewOrderQueue(price)
stopwatch := time.Now()
var o *Order
for i := 0; i < b.N; i++ {
o = NewOrder(
fmt.Sprintf("order-%d", i),
Buy,
decimal.New(100, 0),
decimal.New(int64(i), 0),
stopwatch,
)
orderQueue.Append(o)
}
elapsed := time.Since(stopwatch)
fmt.Printf("\n\nElapsed: %s\nTransactions per second: %f\n", elapsed, float64(b.N)/elapsed.Seconds())
}