-
Notifications
You must be signed in to change notification settings - Fork 5
/
matching_test.go
197 lines (154 loc) · 5.21 KB
/
matching_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"fmt"
"math/rand"
"testing"
)
func TestBasicMatch(t *testing.T) {
LogRed("Testing: basic match")
matchCh := make(chan Match, 1000)
matcher := StartMatcher(nil, matchCh)
matcher.AddOrder(makeOrder(1, "ETH", 1), "USD")
matcher.AddOrder(makeOrder(1, "USD", 1), "ETH")
matcher.FindAllMatches()
match := <-matchCh
LogRed("Got match %v", match)
LogRed("Passed: basic match")
fmt.Println()
}
func TestLargerBuyCleanSplit(t *testing.T) {
LogRed("Testing: larger buy clean split")
matchCh := make(chan Match, 1000)
matcher := StartMatcher(nil, matchCh)
matcher.AddOrder(makeOrder(200, "ETH", 100), "USD")
matcher.AddOrder(makeOrder(4, "USD", 10), "ETH")
matcher.FindAllMatches()
match := <-matchCh
LogRed("Got match %v", match)
assertEqual(t, match.TransferAmt, 10)
ob := matcher.orderbooks["USD"]["ETH"]
assertEqual(t, ob.QuoteQueue.Len(), 1)
assertEqual(t, ob.QuoteQueue.Items[0].order.AmountToBuy, 190)
assertEqual(t, ob.QuoteQueue.Items[0].order.AmountToSell, 95)
assertEqual(t, ob.BaseQueue.Len(), 0)
LogRed("Passed: larger buy clean split")
fmt.Println()
}
func TestLargerSellMessySplit(t *testing.T) {
LogRed("Testing: larger sell messy split")
matchCh := make(chan Match, 1000)
matcher := StartMatcher(nil, matchCh)
matcher.AddOrder(makeOrder(123, "ETH", 71), "USD")
matcher.AddOrder(makeOrder(367, "USD", 767), "ETH")
matcher.FindAllMatches()
match := <-matchCh
LogRed("Got match %v", match)
assertEqual(t, match.TransferAmt, 123)
ob := matcher.orderbooks["USD"]["ETH"]
assertEqual(t, ob.QuoteQueue.Len(), 0)
assertEqual(t, ob.BaseQueue.Len(), 1)
assertEqual(t, ob.BaseQueue.Items[0].order.AmountToBuy, 308)
assertEqual(t, ob.BaseQueue.Items[0].order.AmountToSell, 644)
LogRed("Passed: larger sell messy split")
fmt.Println()
}
func TestVanishingOrders(t *testing.T) {
LogRed("Testing: vanishing orders")
matchCh := make(chan Match, 1000)
matcher := StartMatcher(nil, matchCh)
matcher.AddOrder(makeOrder(1, "ETH", 5), "USD")
matcher.AddOrder(makeOrder(1, "USD", 5), "ETH")
matcher.FindAllMatches()
match := <-matchCh
LogRed("Got match %v", match)
assertEqual(t, match.TransferAmt, 1)
ob := matcher.orderbooks["USD"]["ETH"]
assertEqual(t, ob.QuoteQueue.Len(), 0)
assertEqual(t, ob.BaseQueue.Len(), 0)
LogRed("Passed: vanishing orders")
fmt.Println()
}
func TestSeveralOrders(t *testing.T) {
LogRed("Testing: several orders")
matchCh := make(chan Match, 1000)
matcher := StartMatcher(nil, matchCh)
matcher.AddOrder(makeOrder(100, "ETH", 10), "USD")
matcher.AddOrder(makeOrder(100, "ETH", 20), "USD")
matcher.AddOrder(makeOrder(100, "ETH", 30), "USD")
matcher.AddOrder(makeOrder(10, "USD", 100), "ETH")
matcher.AddOrder(makeOrder(20, "USD", 100), "ETH")
matcher.AddOrder(makeOrder(30, "USD", 100), "ETH")
matcher.FindAllMatches()
match1 := <-matchCh
LogRed("Got match %v", match1)
match2 := <-matchCh
LogRed("Got match %v", match2)
ob := matcher.orderbooks["USD"]["ETH"]
assertEqual(t, ob.QuoteQueue.Len(), 1)
assertEqual(t, ob.QuoteQueue.Items[0].order.AmountToSell, 10)
assertEqual(t, ob.BaseQueue.Len(), 1)
assertEqual(t, ob.BaseQueue.Items[0].order.AmountToBuy, 30)
LogRed("Passed: several orders")
fmt.Println()
}
func TestMultifill(t *testing.T) {
LogRed("Testing: multifill")
matchCh := make(chan Match, 1000)
matcher := StartMatcher(nil, matchCh)
matcher.AddOrder(makeOrder(10, "ETH", 90), "USD")
matcher.AddOrder(makeOrder(100, "ETH", 1000), "USD")
matcher.AddOrder(makeOrder(75, "USD", 25), "ETH")
matcher.AddOrder(makeOrder(150, "USD", 30), "ETH")
matcher.AddOrder(makeOrder(440, "USD", 55), "ETH")
matcher.FindAllMatches()
match1 := <-matchCh
LogRed("Got match %v", match1)
match2 := <-matchCh
LogRed("Got match %v", match2)
match3 := <-matchCh
LogRed("Got match %v", match3)
match4 := <-matchCh
LogRed("Got match %v", match4)
ob := matcher.orderbooks["USD"]["ETH"]
assertEqual(t, ob.QuoteQueue.Len(), 0)
assertEqual(t, ob.BaseQueue.Len(), 0)
LogRed("Passed: multifill")
fmt.Println()
}
func TestCancellation(t *testing.T) {
LogRed("Testing: cancellation")
matchCh := make(chan Match, 1000)
matcher := StartMatcher(nil, matchCh)
buyOrder := makeOrder(1, "ETH", 10)
buySellSymbol := "USD"
sellOrder := makeOrder(1, "USD", 1)
sellSellSymbol := "ETH"
matcher.AddOrder(buyOrder, buySellSymbol)
matcher.RemoveOrder(buyOrder, buySellSymbol)
matcher.AddOrder(sellOrder, sellSellSymbol)
matcher.RemoveOrder(sellOrder, sellSellSymbol)
matcher.AddOrder(makeOrder(1, "ETH", 1), "USD")
matcher.AddOrder(makeOrder(1, "USD", 1), "ETH")
matcher.FindAllMatches()
match := <-matchCh
LogRed("Got match %v", match)
ob := matcher.orderbooks["USD"]["ETH"]
assertEqual(t, len(matchCh), 0)
assertEqual(t, ob.QuoteQueue.Len(), 0)
assertEqual(t, ob.BaseQueue.Len(), 0)
LogRed("Passed: cancellation")
fmt.Println()
}
func makeOrder(buyAmt uint64, buySymbol string, sellAmt uint64) Order {
orderId := rand.Uint64()
signature := []byte{}
var user [addressLength]byte
return Order{orderId, buySymbol, sellAmt, buyAmt, user, signature}
}
func assertEqual(t *testing.T, a interface{}, b interface{}) {
// Because "a != b" doesn't work
if fmt.Sprintf("%v", a) != fmt.Sprintf("%v", b) {
msg := fmt.Sprintf("Assertion failed: %v != %v", a, b)
t.Fatal(msg)
}
}