forked from alexey-ernest/go-hft-orderbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redblackbst_test.go
395 lines (330 loc) · 7.56 KB
/
redblackbst_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package hftorderbook
import (
"testing"
"math/rand"
//"fmt"
)
func TestRedBlackEmpty(t *testing.T) {
rb := NewRedBlackBST()
if rb.Size() != 0 || !rb.IsEmpty() {
t.Errorf("Red Black BST should be empty")
}
}
func TestRedBlackBasic(t *testing.T) {
st := NewRedBlackBST()
keys := make([]float64, 0)
for i := 0; i < 10; i+=1 {
k := rand.Float64()
keys = append(keys, k)
st.Put(k, nil)
}
if st.Size() != 10 {
t.Errorf("size should equals 10, got %d", st.Size())
}
if st.IsEmpty() {
t.Errorf("st should not be empty")
}
for _, k := range keys {
if !st.Contains(k) {
t.Errorf("st should contain the key %0.8f", k)
}
}
}
func TestRedBlackHeight(t *testing.T) {
st := NewRedBlackBST()
n := 100000
for i := 0; i < n; i+=1 {
k := rand.Float64()
st.Put(k, nil)
}
if st.Size() != n {
t.Errorf("size should equals %d, got %d", n, st.Size())
}
if st.IsEmpty() {
t.Errorf("st should not be empty")
}
height := st.Height()
if height < 17 || height > 34 {
t.Errorf("red black bst height should be in range lgN <= height <= 2*lgN, in our case from 17 to 34, but we got %d", height)
}
}
func TestRedBlackMinMax(t *testing.T) {
st := NewRedBlackBST()
for i := 0; i < 10; i+=1 {
st.Put(float64(10 - i), nil)
}
min := 1.0
if st.Min() != min {
t.Errorf("min %0.8f != %0.8f", st.Min(), min)
}
max := 10.0
if st.Max() != max {
t.Errorf("min %0.8f != %0.8f", st.Max(), max)
}
}
func TestRedBlackMinMaxCachedOnDelete(t *testing.T) {
st := NewRedBlackBST()
for i := 0; i < 100; i+=1 {
st.Put(float64(100 - i), nil)
}
min := 1.0
if st.Min() != min {
t.Errorf("min %0.8f != %0.8f", st.Min(), min)
}
max := 100.0
if st.Max() != max {
t.Errorf("min %0.8f != %0.8f", st.Max(), max)
}
st.DeleteMin()
st.DeleteMin()
for i := 3; i < 20; i += 1 {
st.Delete(float64(i))
}
st.DeleteMax()
st.DeleteMax()
for i := 98; i > 70; i -= 1 {
st.Delete(float64(i))
}
min = 20.0
if st.Min() != min {
t.Errorf("min %0.8f != %0.8f", st.Min(), min)
}
max = 70.0
if st.Max() != max {
t.Errorf("min %0.8f != %0.8f", st.Max(), max)
}
}
func TestRedBlackFloor(t *testing.T) {
st := NewRedBlackBST()
for i := 0; i < 10; i += 1 {
k := float64(20 - 2*i)
st.Put(k, nil)
}
keymiss := 3.0
flmiss := 2.0
if st.Floor(keymiss) != flmiss {
t.Errorf("floor != %0.8f", st.Floor(keymiss))
}
keyhit := 10.0
if st.Floor(keyhit) != keyhit {
t.Errorf("floor != %0.8f", st.Floor(keyhit))
}
}
func TestRedBlackCeiling(t *testing.T) {
st := NewRedBlackBST()
for i := 0; i < 10; i += 1 {
k := float64(20 - 2*i)
st.Put(k, nil)
}
keymiss := 3.0
clmiss := 4.0
if st.Ceiling(keymiss) != clmiss {
t.Errorf("ceiling != %0.8f", st.Ceiling(keymiss))
}
keyhit := 10.0
if st.Ceiling(keyhit) != keyhit {
t.Errorf("ceiling != %0.8f", st.Ceiling(keyhit))
}
}
func TestRedBlackSelect(t *testing.T) {
st := NewRedBlackBST()
for i := 0; i < 10; i+=1 {
k := float64(10 - i)
st.Put(k, nil)
}
key := 3.0
if st.Select(2.0) != key {
t.Errorf("element with rank=2 should be %0.8f", key)
}
key = 10.0
if st.Select(9.0) != key {
t.Errorf("element with rank=9 should be %0.8f", key)
}
}
func TestRedBlackRank(t *testing.T) {
st := NewRedBlackBST()
keys := make([]float64, 0)
for i := 0; i < 10; i+=1 {
k := float64(10 - i)
keys = append(keys, k)
st.Put(k, nil)
}
for i := range keys {
k := st.Select(i)
if st.Rank(k) != i {
t.Errorf("rank of %0.8f != %d", k, i)
}
}
if st.Rank(11.0) != len(keys) {
t.Errorf("rank of new maximum should equal to the number of nodes in the tree")
}
if st.Rank(11.0) != st.Rank(12.0) {
t.Errorf("rank of new maximum should not depend on the new maximum concrete value")
}
}
func TestRedBlackKeys(t *testing.T) {
st := NewRedBlackBST()
for i := 0; i < 10; i+=1 {
k := float64(10 - i)
st.Put(k, nil)
}
lo := 3.0
hi := 6.0
keys := st.Keys(lo, hi)
if len(keys) != 4 {
t.Errorf("keys len should equals 4, %+v", keys)
}
if keys[0] != lo {
t.Errorf("first key should be %0.8f", lo)
}
if keys[len(keys)-1] != hi {
t.Errorf("last key should be %0.8f", hi)
}
for i := 1; i < len(keys); i += 1 {
if keys[i] < keys[i-1] {
t.Errorf("non-decreasing keys order validation failed")
}
}
}
func TestRedBlackDeleteMin(t *testing.T) {
st := NewRedBlackBST()
for i := 0; i < 10; i+=1 {
k := float64(10 - i)
st.Put(k, nil)
}
st.DeleteMin()
if st.Size() != 9 {
t.Errorf("tree size should shrink")
}
if st.Contains(1.0) {
t.Errorf("minimum element should be removed from the tree")
}
if !st.IsRedBlack() {
t.Errorf("certification failed")
}
}
func TestRedBlackDeleteMax(t *testing.T) {
st := NewRedBlackBST()
for i := 0; i < 10; i+=1 {
k := float64(i)
st.Put(k, nil)
}
st.DeleteMax()
if st.Size() != 9 {
t.Errorf("tree size should shrink")
}
if st.Contains(9.0) {
t.Errorf("minimum element should be removed from the tree")
}
if !st.IsRedBlack() {
t.Errorf("certification failed")
}
}
func TestRedBlackDelete(t *testing.T) {
st := NewRedBlackBST()
for i := 0; i < 10; i+=1 {
k := float64(i)
st.Put(k, nil)
}
key := 5.0
st.Delete(key)
if st.Size() != 9 {
t.Errorf("tree size should shrink")
}
if st.Contains(key) {
t.Errorf("minimum element should be removed from the tree")
}
if !st.IsRedBlack() {
t.Errorf("certification failed")
}
}
func TestRedBlackPutLinkedListOrder(t *testing.T) {
st := NewRedBlackBST()
for i := 0; i < 100; i+=1 {
k := rand.Float64()
st.Put(k, nil)
}
min := st.MinPointer()
for p := min; p != nil && p.Next != nil; p = p.Next {
if p.Next.Key < p.Key {
t.Errorf("incorrect keys order")
break
}
}
}
func TestRedBlackPutDeleteLinkedListOrder(t *testing.T) {
st := NewRedBlackBST()
n := 1000
for i := 0; i < n; i += 1 {
k := rand.Float64()
st.Put(k, nil)
}
// deleting from both ends and in the middle 90% of the nodes
k := int(float64(n)*0.3)
for i := 0; i < k; i += 1 {
st.DeleteMin()
k := st.Select(rand.Intn(st.Size()))
st.Delete(k)
st.DeleteMax()
}
if st.Size() != n-3*k {
t.Errorf("incorrect tree size %d", st.Size())
}
min := st.MinPointer()
for p := min; p != nil && p.Next != nil; p = p.Next {
if p.Next.Key < p.Key {
t.Errorf("incorrect keys order")
break
}
}
}
func benchmarkRedBlackLimitedRandomInsertWithCaching(n int, b *testing.B) {
st := NewRedBlackBST()
// maximum number of levels in average is 10k
limitslist := make([]float64, n)
for i := range limitslist {
limitslist[i] = rand.Float64()
}
// preallocate empty orders
orders := make([]*Order, 0, b.N)
for i := 0; i < b.N; i += 1 {
orders = append(orders, &Order{})
}
// measure insertion time
b.ResetTimer()
limitscache := make(map[float64]*LimitOrder)
for i := 0; i < b.N; i += 1 {
// create a new order
o := orders[i]
o.Id = i
o.Volume = rand.Float64()
// o := &Order{
// Id: i,
// Volume: rand.Float64(),
// }
// set the price
price := limitslist[rand.Intn(len(limitslist))]
// append order to the limit price
if limitscache[price] != nil {
// append to the existing limit in cache
limitscache[price].Enqueue(o)
} else {
// new limit
l := NewLimitOrder(price)
l.Enqueue(o)
// caching limit
limitscache[price] = &l
// inserting into tree
st.Put(l.Price, &l)
}
}
}
func BenchmarkRedBlack5kLevelsRandomInsertWithCaching(b *testing.B) {
benchmarkRedBlackLimitedRandomInsertWithCaching(5000, b)
}
func BenchmarkRedBlack10kLevelsRandomInsertWithCaching(b *testing.B) {
benchmarkRedBlackLimitedRandomInsertWithCaching(10000, b)
}
func BenchmarkRedBlack20kLevelsRandomInsertWithCaching(b *testing.B) {
benchmarkRedBlackLimitedRandomInsertWithCaching(20000, b)
}