-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.cpp
1231 lines (1061 loc) · 51.7 KB
/
book.cpp
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2018 Bronislaw (Bronek) Kozicki
//
// Distributed under the MIT License. See accompanying file LICENSE
// or copy at https://opensource.org/licenses/MIT
struct assert_error {};
#define ASSERT(...) do { if((__VA_ARGS__) == 0) throw assert_error{}; } while(0)
#include "market/book.hpp"
#include <catch2/catch.hpp>
#include <set>
#include <vector>
namespace {
struct Level {
int ticks = -1; int size = -1;
template <market::side Side>
constexpr static bool compare(const Level& lh, const Level& rh) noexcept;
template <market::side Side>
constexpr static bool compare(int, const Level& rh) noexcept;
template <market::side Side>
constexpr static bool compare(const Level& lh, int) noexcept;
constexpr static int make(int i) {
return i;
}
};
template <>
constexpr bool Level::compare<market::side::bid>(const Level& lh, const Level& rh) noexcept {
return lh.ticks > rh.ticks;
}
template <>
constexpr bool Level::compare<market::side::bid>(int lh, const Level& rh) noexcept {
return lh > rh.ticks;
}
template <>
constexpr bool Level::compare<market::side::bid>(const Level& lh, int rh) noexcept {
return lh.ticks > rh;
}
template <>
constexpr bool Level::compare<market::side::ask>(const Level& lh, const Level& rh) noexcept {
return lh.ticks < rh.ticks;
}
template <>
constexpr bool Level::compare<market::side::ask>(int lh, const Level& rh) noexcept {
return lh < rh.ticks;
}
template <>
constexpr bool Level::compare<market::side::ask>(const Level& lh, int rh) noexcept {
return lh.ticks < rh;
}
bool operator==(const Level& lh, const Level& rh) {
return lh.ticks == rh.ticks && lh.size == rh.size;
}
std::ostream& operator<<(std::ostream& lh, const Level& rh) {
return (lh << '{' << rh.ticks << ',' << rh.size << '}');
}
struct SmallBook : market::book<Level> {
SmallBook() : book<Level>(data, 0, 0) {
reset();
}
book::data<3> data;
};
}
TEST_CASE("SmallBook_basics", "[book][data][capacity][size][empty][full][at][push_back][emplace_back][sort][remove]") {
using namespace market;
SmallBook book;
const int back = book.capacity * 2 - 1;
SECTION("elements for each of data.levels, data.sides and data.freel equal to twice capacity") {
REQUIRE(book.capacity == 3);
REQUIRE(back == 5);
REQUIRE(book.data.capacity == book.capacity);
REQUIRE(sizeof(book.data.levels) / sizeof(book.data.levels[0]) == back + 1);
REQUIRE(sizeof(book.data.sides) / sizeof(book.data.sides[0]) == back + 1);
REQUIRE(sizeof(book.data.freel) / sizeof(book.data.freel[0]) == back + 1);
}
SECTION("book initialised empty") {
CHECK(book.size<side::bid>() == 0);
CHECK(book.empty<side::bid>());
CHECK(not book.full<side::bid>());
CHECK(book.size<side::ask>() == 0);
CHECK(book.empty<side::ask>());
CHECK(not book.full<side::ask>());
CHECK(book.binary_search<side::bid>(130130) == SmallBook::npos);
CHECK(book.binary_search<side::ask>(130130) == SmallBook::npos);
}
SECTION("push_back() similar elements on each side to fill the capacity") {
REQUIRE(SmallBook::npos > 3);
CHECK(book.push_back<side::bid>(Level{130130, 100}) == 0);
CHECK(book.size<side::bid>() == 1);
CHECK(not book.empty<side::bid>());
CHECK(not book.full<side::bid>());
CHECK(book.push_back<side::bid>(Level{130130, 100}) == 1);
CHECK(book.size<side::bid>() == 2);
CHECK(not book.full<side::bid>());
CHECK(book.push_back<side::bid>(Level{130130, 100}) == 2);
CHECK(book.size<side::bid>() == 3);
CHECK(book.full<side::bid>());
CHECK(book.push_back<side::bid>(Level{130133, 400}) == SmallBook::npos);
CHECK(book.size<side::bid>() == 3);
CHECK(book.full<side::bid>());
CHECK(book.binary_search<side::bid>(130130) != SmallBook::npos);
CHECK(book.binary_search<side::bid>(130133) == SmallBook::npos);
CHECK(book.push_back<side::ask>(Level{130130, 100}) == 0);
CHECK(book.size<side::ask>() == 1);
CHECK(not book.empty<side::ask>());
CHECK(not book.full<side::ask>());
CHECK(book.push_back<side::ask>(Level{130130, 200}) == 1);
CHECK(book.size<side::ask>() == 2);
CHECK(not book.full<side::ask>());
CHECK(book.push_back<side::ask>(Level{130130, 300}) == 2);
CHECK(book.size<side::ask>() == 3);
CHECK(book.full<side::ask>());
CHECK(book.push_back<side::ask>(Level{130133, 800}) == SmallBook::npos);
CHECK(book.size<side::ask>() == 3);
CHECK(book.full<side::ask>());
CHECK(book.binary_search<side::ask>(130130) != SmallBook::npos);
CHECK(book.binary_search<side::ask>(130133) == SmallBook::npos);
SECTION("each element stored faithfully inside data.levels array") {
std::vector<const Level *> levels;
CHECK(book.at<side::bid>(0) == Level{130130, 100});
levels.emplace_back(&book.at<side::bid>(0));
CHECK(book.at<side::bid>(1) == Level{130130, 100});
levels.emplace_back(&book.at<side::bid>(1));
CHECK(book.at<side::bid>(2) == Level{130130, 100});
levels.emplace_back(&book.at<side::bid>(2));
CHECK(book.at<side::ask>(0) == Level{130130, 100});
levels.emplace_back(&book.at<side::ask>(0));
CHECK(book.at<side::ask>(1) == Level{130130, 200});
levels.emplace_back(&book.at<side::ask>(1));
CHECK(book.at<side::ask>(2) == Level{130130, 300});
levels.emplace_back(&book.at<side::ask>(2));
REQUIRE(levels.size() == 6);
for (const auto *l : levels) {
CHECK(l >= &book.data.levels[0]);
CHECK(l <= &book.data.levels[back]);
}
SECTION("each element in its own unique memory location") {
std::set<const Level *> copy {levels.begin(), levels.end()};
REQUIRE(copy.size() == 6);
}
}
}
SECTION("emplace_back() allows use of default ctor") {
CHECK(book.emplace_back<side::bid>() == 0);
CHECK(book.size<side::bid>() == 1);
CHECK(not book.empty<side::bid>());
CHECK(not book.full<side::bid>());
CHECK(book.at<side::bid>(0) == Level{});
CHECK(book.emplace_back<side::ask>() == 0);
CHECK(book.size<side::ask>() == 1);
CHECK(not book.empty<side::ask>());
CHECK(not book.full<side::ask>());
CHECK(book.at<side::ask>(0) == Level{});
}
SECTION("emplace_back() different elements on each side to fill the capacity") {
REQUIRE(SmallBook::npos > 3);
CHECK(book.emplace_back<side::bid>(130130, 100) == 0);
CHECK(book.size<side::bid>() == 1);
CHECK(not book.empty<side::bid>());
CHECK(not book.full<side::bid>());
CHECK(book.emplace_back<side::bid>(130131, 200) == 1);
CHECK(book.size<side::bid>() == 2);
CHECK(not book.full<side::bid>());
CHECK(book.emplace_back<side::bid>(130132, 300) == 2);
CHECK(book.size<side::bid>() == 3);
CHECK(book.full<side::bid>());
CHECK(book.emplace_back<side::bid>(130133, 400) == SmallBook::npos);
CHECK(book.size<side::bid>() == 3);
CHECK(book.full<side::bid>());
CHECK(book.emplace_back<side::ask>(130130, 500) == 0);
CHECK(book.size<side::ask>() == 1);
CHECK(not book.empty<side::ask>());
CHECK(not book.full<side::ask>());
CHECK(book.emplace_back<side::ask>(130131, 600) == 1);
CHECK(book.size<side::ask>() == 2);
CHECK(not book.full<side::ask>());
CHECK(book.emplace_back<side::ask>(130132, 700) == 2);
CHECK(book.size<side::ask>() == 3);
CHECK(book.full<side::ask>());
CHECK(book.binary_search<side::ask>(130130) == 0);
CHECK(book.binary_search<side::ask>(130131) == 1);
CHECK(book.binary_search<side::ask>(130132) == 2);
CHECK(book.binary_search<side::ask>(130133) == SmallBook::npos);
CHECK(book.emplace_back<side::ask>(130133, 800) == SmallBook::npos);
CHECK(book.size<side::ask>() == 3);
CHECK(book.full<side::ask>());
SECTION("each element stored faithfully inside data.levels array") {
// The important feature of market::book is that none of the operations (tested below)
// moves elements in memory. Hence, capture their addresses before we test these.
std::vector<const Level*> levels;
CHECK(book.at<side::bid>(0) == Level{130130, 100});
levels.emplace_back(&book.at<side::bid>(0));
CHECK(book.at<side::bid>(1) == Level{130131, 200});
levels.emplace_back(&book.at<side::bid>(1));
CHECK(book.at<side::bid>(2) == Level{130132, 300});
levels.emplace_back(&book.at<side::bid>(2));
CHECK(book.at<side::ask>(0) == Level{130130, 500});
levels.emplace_back(&book.at<side::ask>(0));
CHECK(book.at<side::ask>(1) == Level{130131, 600});
levels.emplace_back(&book.at<side::ask>(1));
CHECK(book.at<side::ask>(2) == Level{130132, 700});
levels.emplace_back(&book.at<side::ask>(2));
REQUIRE(levels.size() == 6);
for (const auto* l : levels) {
CHECK(l >= &book.data.levels[0]);
CHECK(l <= &book.data.levels[back]);
}
SECTION("each element in its own unique memory location") {
std::set<const Level *> copy {levels.begin(), levels.end()};
REQUIRE(copy.size() == 6);
}
SECTION("sort() does not move elements, only their indices") {
// Sorting of elements only changes their indices, but data does not move in memory.
std::vector<Level> copy;
for (const auto* l : levels) {
copy.emplace_back(*l);
}
REQUIRE(copy.size() == 6);
book.sort<side::bid>();
book.sort<side::ask>();
for (size_t i = 0; i < 6; ++i) {
const auto tmp1 = levels[i];
const auto& tmp2 = copy[i];
CHECK(*tmp1 == tmp2);
}
// Index of best bid moved to front, but actual data in original memory location
CHECK(book.at<side::bid>(0) == Level{130132, 300});
CHECK(levels[2] == &book.at<side::bid>(0));
CHECK(book.at<side::bid>(1) == Level{130131, 200});
CHECK(levels[1] == &book.at<side::bid>(1));
CHECK(book.at<side::bid>(2) == Level{130130, 100});
CHECK(levels[0] == &book.at<side::bid>(2));
CHECK(book.at<side::ask>(0) == Level{130130, 500});
CHECK(levels[3] == &book.at<side::ask>(0));
CHECK(book.at<side::ask>(1) == Level{130131, 600});
CHECK(levels[4] == &book.at<side::ask>(1));
CHECK(book.at<side::ask>(2) == Level{130132, 700});
CHECK(levels[5] == &book.at<side::ask>(2));
CHECK(book.binary_search<side::bid>(130132) == 0);
CHECK(book.binary_search<side::bid>(130131) == 1);
CHECK(book.binary_search<side::bid>(130130) == 2);
CHECK(book.binary_search<side::bid>(130133) == SmallBook::npos);
}
SECTION("const overload of at() returns the same data as non-const") {
const auto& cref = book;
CHECK(cref.at<side::bid>(0) == Level{130130, 100});
CHECK(levels[0] == &cref.at<side::bid>(0));
CHECK(cref.at<side::bid>(1) == Level{130131, 200});
CHECK(levels[1] == &cref.at<side::bid>(1));
CHECK(cref.at<side::bid>(2) == Level{130132, 300});
CHECK(levels[2] == &cref.at<side::bid>(2));
CHECK(cref.at<side::ask>(0) == Level{130130, 500});
CHECK(levels[3] == &cref.at<side::ask>(0));
CHECK(cref.at<side::ask>(1) == Level{130131, 600});
CHECK(levels[4] == &cref.at<side::ask>(1));
CHECK(cref.at<side::ask>(2) == Level{130132, 700});
CHECK(levels[5] == &cref.at<side::ask>(2));
}
SECTION("remove() does not move elements, only their indices") {
// Removal of an element only changes the indices of the following elements,
// but they do not move in memory.
// Remove top element on bid side
book.remove<side::bid>(0);
CHECK(book.size<side::bid>() == 2);
CHECK(not book.empty<side::bid>());
CHECK(not book.full<side::bid>());
CHECK(book.at<side::bid>(0) == Level{130131, 200});
CHECK(levels[1] == &book.at<side::bid>(0));
CHECK(book.at<side::bid>(1) == Level{130132, 300});
CHECK(levels[2] == &book.at<side::bid>(1));
// Other side is unaffected
CHECK(book.size<side::ask>() == 3);
CHECK(levels[3] == &book.at<side::ask>(0));
CHECK(levels[4] == &book.at<side::ask>(1));
CHECK(levels[5] == &book.at<side::ask>(2));
// Remove mid element on ask side
book.remove<side::ask>(1);
CHECK(book.size<side::ask>() == 2);
CHECK(not book.empty<side::ask>());
CHECK(not book.full<side::ask>());
CHECK(book.at<side::ask>(0) == Level{130130, 500});
CHECK(levels[3] == &book.at<side::ask>(0));
CHECK(book.at<side::ask>(1) == Level{130132, 700});
CHECK(levels[5] == &book.at<side::ask>(1));
CHECK(book.binary_search<side::ask>(130131) == SmallBook::npos);
// Other side is unaffected
CHECK(levels[1] == &book.at<side::bid>(0));
CHECK(levels[2] == &book.at<side::bid>(1));
}
SECTION("remove() all elements from top (front) works") {
book.remove<side::bid>(0);
book.remove<side::bid>(0);
CHECK(book.size<side::bid>() == 1);
CHECK(not book.empty<side::bid>());
CHECK(not book.full<side::bid>());
CHECK(book.at<side::bid>(0) == Level{130132, 300});
CHECK(levels[2] == &book.at<side::bid>(0));
// Remove last element on bid side
book.remove<side::bid>(0);
CHECK(book.size<side::bid>() == 0);
CHECK(book.empty<side::bid>());
// Other side is unaffected
CHECK(book.size<side::ask>() == 3);
CHECK(levels[3] == &book.at<side::ask>(0));
CHECK(levels[4] == &book.at<side::ask>(1));
CHECK(levels[5] == &book.at<side::ask>(2));
}
SECTION("remove() all elements from bottom (back) works") {
book.remove<side::ask>(2);
book.remove<side::ask>(1);
CHECK(book.size<side::ask>() == 1);
CHECK(not book.empty<side::ask>());
CHECK(not book.full<side::ask>());
CHECK(book.at<side::ask>(0) == Level{130130, 500});
CHECK(levels[3] == &book.at<side::ask>(0));
// Remove last element on ask side
book.remove<side::ask>(0);
CHECK(book.size<side::ask>() == 0);
CHECK(book.empty<side::ask>());
// Other side is unaffected
CHECK(book.size<side::bid>() == 3);
CHECK(levels[0] == &book.at<side::bid>(0));
CHECK(levels[1] == &book.at<side::bid>(1));
CHECK(levels[2] == &book.at<side::bid>(2));
}
SECTION("remove() actually frees space, which can be reused") {
// Remove some elements first
book.remove<side::bid>(0);
book.remove<side::bid>(0);
CHECK(book.size<side::bid>() == 1);
book.remove<side::ask>(2);
book.remove<side::ask>(0);
CHECK(book.size<side::ask>() == 1);
// Check remaining elements are unaffected
CHECK(book.at<side::bid>(0) == Level{130132, 300});
CHECK(levels[2] == &book.at<side::bid>(0));
CHECK(book.at<side::ask>(0) == Level{130131, 600});
CHECK(levels[4] == &book.at<side::ask>(0));
// emplace_back() more elements until full
CHECK(book.emplace_back<side::bid>(130140, 200) == 1);
CHECK(book.size<side::bid>() == 2);
CHECK(book.emplace_back<side::bid>(130141, 300) == 2);
CHECK(book.size<side::bid>() == 3);
CHECK(book.emplace_back<side::bid>(130142, 400) == SmallBook::npos);
CHECK(book.emplace_back<side::ask>(130120, 600) == 1);
CHECK(book.size<side::ask>() == 2);
CHECK(book.emplace_back<side::ask>(130129, 700) == 2);
CHECK(book.size<side::ask>() == 3);
CHECK(book.emplace_back<side::ask>(130128, 800) == SmallBook::npos);
// Check elements at the top unaffected
CHECK(book.at<side::bid>(0) == Level{130132, 300});
CHECK(levels[2] == &book.at<side::bid>(0));
CHECK(book.at<side::ask>(0) == Level{130131, 600});
CHECK(levels[4] == &book.at<side::ask>(0));
}
}
}
}
namespace {
struct AnySizeBook : market::book<Level> {
// Normally these two will not be exposed
using book::nothrow_t;
using book::nothrow;
// Maximum size allowed, required here because we are testing corner cases
Level levels[254] = {};
uint8_t sides[254] = {};
uint8_t freel[254] = {};
template <typename ... Args>
AnySizeBook(int i, Args&& ... t)
: book(levels, sides, freel, i, 0, 0, std::forward<Args>(t)...) {
this->accept();
}
template <market::side Side, size_t Size>
size_type populate(Level const (&input)[Size], size_type offset) {
size_type i = 0;
for (auto const &l : input) {
sides[(size_t)Side * capacity + i] = offset + i;
levels[offset + i++] = l; // Note: must post-increment
}
side_i[(size_t)Side] = i;
return i;
}
using market::book<Level>::reset;
using market::book<Level>::accept;
size_type *&base_freel() { return market::book<Level>::freel; }
size_type &base_tail() { return market::book<Level>::tail_i; }
size_type &base_size_bid() { return market::book<Level>::side_i[0]; }
size_type &base_size_ask() { return market::book<Level>::side_i[1]; }
};
}
TEST_CASE("AnySizeBook_construction", "[book][capacity][construction][bad_capacity][nothrow]") {
using namespace market;
constexpr auto npos = AnySizeBook::npos;
constexpr auto nothrow = AnySizeBook::nothrow;
std::unique_ptr<AnySizeBook> ptr = nullptr;
SECTION("can construct zero capacity book") {
CHECK_NOTHROW(ptr.reset(new AnySizeBook(0)));
// Curious little book - because it has 0 capacity, it is full and empty at the same time!
REQUIRE(ptr->capacity == 0);
REQUIRE(ptr->size<side::bid>() == 0);
REQUIRE(ptr->full<side::bid>());
REQUIRE(ptr->empty<side::bid>());
REQUIRE(ptr->size<side::ask>() == 0);
REQUIRE(ptr->full<side::ask>());
REQUIRE(ptr->empty<side::ask>());
REQUIRE(ptr->emplace_back<side::bid>() == npos);
REQUIRE(ptr->emplace_back<side::ask>() == npos);
// Using nothrow overload changes nothing, because the zero capacity is valid anyway
CHECK_NOTHROW(ptr.reset(new AnySizeBook(0, nothrow)));
REQUIRE(ptr->capacity == 0);
REQUIRE(ptr->emplace_back<side::bid>() == npos);
REQUIRE(ptr->emplace_back<side::ask>() == npos);
}
SECTION("can construct regular capacity book, no exceptions") {
CHECK_NOTHROW(ptr.reset(new AnySizeBook(40)));
REQUIRE(ptr->capacity == 40);
REQUIRE(ptr->size<side::bid>() == 0);
REQUIRE(ptr->size<side::ask>() == 0);
CHECK_NOTHROW(ptr.reset(new AnySizeBook(126)));
REQUIRE(ptr->capacity == 126);
REQUIRE(ptr->size<side::bid>() == 0);
REQUIRE(ptr->size<side::ask>() == 0);
CHECK_NOTHROW(ptr.reset(new AnySizeBook(127)));
REQUIRE(ptr->capacity == 127);
REQUIRE(ptr->size<side::bid>() == 0);
REQUIRE(ptr->size<side::ask>() == 0);
}
SECTION("invalid too-large capacity, exceptions thrown") {
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(128)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(129)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(130)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(253)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(254)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(255)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(256)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(1000)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook((int) 1e9)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(std::numeric_limits<int>::max())), AnySizeBook::bad_capacity);
}
SECTION("invalid negative capacity book, exceptions thrown") {
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(-1)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(-126)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(-127)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(-128)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(-129)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(-130)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(-253)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(-254)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(-255)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook((int) -1e9)), AnySizeBook::bad_capacity);
CHECK_THROWS_AS(ptr.reset(new AnySizeBook(std::numeric_limits<int>::min())), AnySizeBook::bad_capacity);
}
SECTION("can construct regular capacity book, no exceptions and none allowed") {
static_assert(std::is_same_v<AnySizeBook::nothrow_t, std::nothrow_t>);
CHECK_NOTHROW(ptr.reset(new AnySizeBook(10, std::nothrow)));
REQUIRE(ptr->capacity == 10);
CHECK_NOTHROW(ptr.reset(new AnySizeBook(126, AnySizeBook::nothrow_t{})));
REQUIRE(ptr->capacity == 126);
CHECK_NOTHROW(ptr.reset(new AnySizeBook(127, nothrow)));
REQUIRE(ptr->capacity == 127);
}
SECTION("invalid capacity book, reported zero capacity, no exceptions") {
CHECK_NOTHROW(ptr.reset(new AnySizeBook(-1, nothrow)));
// Same behaviour as 0 capacity book tested above
CHECK(ptr->capacity == 0);
REQUIRE(ptr->size<side::bid>() == 0);
REQUIRE(ptr->full<side::bid>());
REQUIRE(ptr->empty<side::bid>());
REQUIRE(ptr->size<side::ask>() == 0);
REQUIRE(ptr->full<side::ask>());
REQUIRE(ptr->empty<side::ask>());
REQUIRE(ptr->emplace_back<side::bid>() == npos);
REQUIRE(ptr->emplace_back<side::ask>() == npos);
CHECK_NOTHROW(ptr.reset(new AnySizeBook(128, nothrow)));
CHECK(ptr->capacity == 0);
REQUIRE(ptr->emplace_back<side::bid>() == npos);
REQUIRE(ptr->emplace_back<side::ask>() == npos);
CHECK_NOTHROW(ptr.reset(new AnySizeBook(-127, nothrow)));
CHECK(ptr->capacity == 0);
REQUIRE(ptr->emplace_back<side::bid>() == npos);
REQUIRE(ptr->emplace_back<side::ask>() == npos);
CHECK_NOTHROW(ptr.reset(new AnySizeBook((int) 1e9, nothrow)));
CHECK(ptr->capacity == 0);
REQUIRE(ptr->emplace_back<side::bid>() == npos);
REQUIRE(ptr->emplace_back<side::ask>() == npos);
CHECK_NOTHROW(ptr.reset(new AnySizeBook(std::numeric_limits<int>::min(), nothrow)));
REQUIRE(ptr->emplace_back<side::bid>() == npos);
REQUIRE(ptr->emplace_back<side::ask>() == npos);
CHECK_NOTHROW(ptr.reset(new AnySizeBook(std::numeric_limits<int>::max(), nothrow)));
CHECK(ptr->capacity == 0);
REQUIRE(ptr->emplace_back<side::bid>() == npos);
REQUIRE(ptr->emplace_back<side::ask>() == npos);
}
SECTION("call accept() on populated book") {
Level bids[2] = {Level{130130, 100}, Level{130140, 10}};
Level offers[3] = {Level{130190, 50}, Level{130180, 40}, Level{130170, 20}};
CHECK_NOTHROW(ptr.reset(new AnySizeBook(10, std::nothrow)));
REQUIRE(ptr->capacity == 10);
auto const offset = ptr->populate<side::bid>(bids, 0);
ptr->accept();
for (std::size_t i = 0; i < 20; ++i) {
CHECK((i >= 18 ? true : ptr->freel[i] == i + 2));
}
CHECK(ptr->size<side::bid>() == 2);
CHECK(ptr->size<side::ask>() == 0);
ptr->populate<side::ask>(offers, offset);
ptr->accept();
for (std::size_t i = 0; i < 20; ++i) {
CHECK((i >= 15 ? true : ptr->freel[i] == i + 5));
}
CHECK(ptr->size<side::bid>() == 2);
CHECK(ptr->size<side::ask>() == 3);
}
SECTION("ASSERT check") {
CHECK_NOTHROW(ptr.reset(new AnySizeBook(2, std::nothrow)));
SECTION("null freel") {
ptr->base_freel() = nullptr;
CHECK_THROWS_AS(ptr->reset(), assert_error);
CHECK_THROWS_AS(ptr->accept(), assert_error);
CHECK_THROWS_AS(ptr->push_back<side::bid>(Level{}), assert_error);
CHECK_THROWS_AS(ptr->push_back<side::ask>(Level{}), assert_error);
CHECK_THROWS_AS(ptr->emplace_back<side::bid>(1, 1), assert_error);
CHECK_THROWS_AS(ptr->emplace_back<side::ask>(1, 1), assert_error);
CHECK_THROWS_AS(ptr->remove<side::bid>(0), assert_error);
CHECK_THROWS_AS(ptr->remove<side::ask>(0), assert_error);
}
SECTION("bad bid size") {
ptr->base_size_bid() = 1;
CHECK_THROWS_AS(ptr->push_back<side::bid>(Level{}), assert_error);
CHECK_THROWS_AS(ptr->push_back<side::ask>(Level{}), assert_error);
CHECK_THROWS_AS(ptr->emplace_back<side::bid>(1, 1), assert_error);
CHECK_THROWS_AS(ptr->emplace_back<side::ask>(1, 1), assert_error);
CHECK_THROWS_AS(ptr->remove<side::bid>(0), assert_error);
CHECK_THROWS_AS(ptr->remove<side::ask>(0), assert_error);
}
SECTION("bad ask size") {
ptr->base_size_ask() = 1;
CHECK_THROWS_AS(ptr->push_back<side::bid>(Level{}), assert_error);
CHECK_THROWS_AS(ptr->push_back<side::ask>(Level{}), assert_error);
CHECK_THROWS_AS(ptr->emplace_back<side::bid>(1, 1), assert_error);
CHECK_THROWS_AS(ptr->emplace_back<side::ask>(1, 1), assert_error);
CHECK_THROWS_AS(ptr->remove<side::bid>(0), assert_error);
CHECK_THROWS_AS(ptr->remove<side::ask>(0), assert_error);
}
SECTION("bad tail") {
ptr->base_tail() = 1;
CHECK_THROWS_AS(ptr->push_back<side::bid>(Level{}), assert_error);
CHECK_THROWS_AS(ptr->push_back<side::ask>(Level{}), assert_error);
CHECK_THROWS_AS(ptr->emplace_back<side::bid>(1, 1), assert_error);
CHECK_THROWS_AS(ptr->emplace_back<side::ask>(1, 1), assert_error);
CHECK_THROWS_AS(ptr->remove<side::bid>(0), assert_error);
CHECK_THROWS_AS(ptr->remove<side::ask>(0), assert_error);
}
SECTION("bad index") {
CHECK_THROWS_AS(std::as_const(*ptr).at<side::bid>(0), assert_error);
CHECK_THROWS_AS(std::as_const(*ptr).at<side::ask>(0), assert_error);
CHECK_THROWS_AS(ptr->at<side::bid>(0), assert_error);
CHECK_THROWS_AS(ptr->at<side::ask>(0), assert_error);
CHECK_THROWS_AS(ptr->remove<side::bid>(0), assert_error);
CHECK_THROWS_AS(ptr->remove<side::ask>(0), assert_error);
}
SECTION("bad tail_i adding element") {
CHECK(ptr->push_back<side::bid>(Level{130140, 10}) == 0);
CHECK(ptr->push_back<side::bid>(Level{130130, 10}) == 1);
CHECK(ptr->push_back<side::ask>(Level{130140, 10}) == 0);
CHECK(ptr->push_back<side::ask>(Level{130150, 10}) == 1);
REQUIRE(ptr->size<side::bid>() == 2);
REQUIRE(ptr->size<side::ask>() == 2);
REQUIRE(ptr->full<side::bid>());
REQUIRE(ptr->full<side::ask>());
REQUIRE(ptr->base_tail() == npos);
// note, sections below intentionally corrupt internal state
// such that there is place to add new level without causing UB
SECTION("push_back() or emplace_back() on ask") {
ptr->base_size_bid() = 3;
ptr->base_size_ask() = 1;
CHECK_THROWS_AS(ptr->push_back<side::ask>(Level{}), assert_error);
CHECK_THROWS_AS(ptr->emplace_back<side::ask>(), assert_error);
REQUIRE(ptr->push_back<side::bid>(Level{}) == npos);
}
SECTION("push_back() or emplace_back() on bid") {
ptr->base_size_bid() = 1;
ptr->base_size_ask() = 3;
CHECK_THROWS_AS(ptr->push_back<side::bid>(Level{}), assert_error);
CHECK_THROWS_AS(ptr->emplace_back<side::bid>(), assert_error);
REQUIRE(ptr->push_back<side::ask>(Level{}) == npos);
}
}
}
}
TEST_CASE("AnySizeBook_binary_search", "[book][binary_search]") {
using namespace market;
constexpr auto npos = AnySizeBook::npos;
// Q: why so many seemingly identical tests? A: there is a bit of branching inside
// binary_search(), need to ensure we go through all possible execution paths.
SECTION("binary_search() in empty book") {
AnySizeBook book {0};
CHECK(book.binary_search<side::ask>(130130) == npos);
CHECK(book.binary_search<side::bid>(130130) == npos);
}
SECTION("binary_search() in book size=1") {
AnySizeBook book {1};
book.emplace_back<side::ask>(130130, 1);
CHECK(book.binary_search<side::ask>(130130) == 0);
CHECK(book.binary_search<side::ask>(130100) == npos);
CHECK(book.binary_search<side::ask>(130200) == npos);
}
SECTION("binary_search() in book size=2") {
AnySizeBook book {2};
book.emplace_back<side::ask>(130130, 1);
book.emplace_back<side::ask>(130132, 1);
CHECK(book.binary_search<side::ask>(130130) == 0);
CHECK(book.binary_search<side::ask>(130131) == npos);
CHECK(book.binary_search<side::ask>(130132) == 1);
CHECK(book.binary_search<side::ask>(130100) == npos);
CHECK(book.binary_search<side::ask>(130200) == npos);
}
SECTION("binary_search() in book size=2, bid side") {
AnySizeBook book {2};
book.emplace_back<side::bid>(130132, 1);
book.emplace_back<side::bid>(130130, 1);
CHECK(book.binary_search<side::bid>(130132) == 0);
CHECK(book.binary_search<side::bid>(130131) == npos);
CHECK(book.binary_search<side::bid>(130130) == 1);
CHECK(book.binary_search<side::bid>(130100) == npos);
CHECK(book.binary_search<side::bid>(130200) == npos);
}
SECTION("binary_search() in book size=3") {
AnySizeBook book {3};
book.emplace_back<side::ask>(130130, 1);
book.emplace_back<side::ask>(130132, 1);
book.emplace_back<side::ask>(130134, 1);
CHECK(book.binary_search<side::ask>(130130) == 0);
CHECK(book.binary_search<side::ask>(130131) == npos);
CHECK(book.binary_search<side::ask>(130132) == 1);
CHECK(book.binary_search<side::ask>(130133) == npos);
CHECK(book.binary_search<side::ask>(130134) == 2);
CHECK(book.binary_search<side::ask>(130100) == npos);
CHECK(book.binary_search<side::ask>(130200) == npos);
}
SECTION("binary_search() in book size=4") {
AnySizeBook book {4};
book.emplace_back<side::ask>(130130, 1);
book.emplace_back<side::ask>(130132, 1);
book.emplace_back<side::ask>(130134, 1);
book.emplace_back<side::ask>(130136, 1);
CHECK(book.binary_search<side::ask>(130130) == 0);
CHECK(book.binary_search<side::ask>(130131) == npos);
CHECK(book.binary_search<side::ask>(130132) == 1);
CHECK(book.binary_search<side::ask>(130133) == npos);
CHECK(book.binary_search<side::ask>(130134) == 2);
CHECK(book.binary_search<side::ask>(130135) == npos);
CHECK(book.binary_search<side::ask>(130136) == 3);
CHECK(book.binary_search<side::ask>(130100) == npos);
CHECK(book.binary_search<side::ask>(130200) == npos);
}
SECTION("binary_search() in book size=5") {
AnySizeBook book {5};
book.emplace_back<side::ask>(130130, 1);
book.emplace_back<side::ask>(130132, 1);
book.emplace_back<side::ask>(130134, 1);
book.emplace_back<side::ask>(130136, 1);
book.emplace_back<side::ask>(130138, 1);
CHECK(book.binary_search<side::ask>(130130) == 0);
CHECK(book.binary_search<side::ask>(130131) == npos);
CHECK(book.binary_search<side::ask>(130132) == 1);
CHECK(book.binary_search<side::ask>(130133) == npos);
CHECK(book.binary_search<side::ask>(130134) == 2);
CHECK(book.binary_search<side::ask>(130135) == npos);
CHECK(book.binary_search<side::ask>(130136) == 3);
CHECK(book.binary_search<side::ask>(130137) == npos);
CHECK(book.binary_search<side::ask>(130138) == 4);
CHECK(book.binary_search<side::ask>(130100) == npos);
CHECK(book.binary_search<side::ask>(130200) == npos);
}
SECTION("binary_search() in book size=5, bid side") {
AnySizeBook book {5};
book.emplace_back<side::bid>(130138, 1);
book.emplace_back<side::bid>(130136, 1);
book.emplace_back<side::bid>(130134, 1);
book.emplace_back<side::bid>(130132, 1);
book.emplace_back<side::bid>(130130, 1);
CHECK(book.binary_search<side::bid>(130130) == 4);
CHECK(book.binary_search<side::bid>(130131) == npos);
CHECK(book.binary_search<side::bid>(130132) == 3);
CHECK(book.binary_search<side::bid>(130133) == npos);
CHECK(book.binary_search<side::bid>(130134) == 2);
CHECK(book.binary_search<side::bid>(130135) == npos);
CHECK(book.binary_search<side::bid>(130136) == 1);
CHECK(book.binary_search<side::bid>(130137) == npos);
CHECK(book.binary_search<side::bid>(130138) == 0);
CHECK(book.binary_search<side::bid>(130100) == npos);
CHECK(book.binary_search<side::bid>(130200) == npos);
}
}
TEST_CASE("AnySizeBook_lower_bound", "[book][lower_bound]") {
using namespace market;
constexpr auto npos = AnySizeBook::npos;
SECTION("lower_bound() in empty book") {
AnySizeBook book {0};
CHECK(book.lower_bound<side::ask>(130130) == npos);
CHECK(book.lower_bound<side::bid>(130130) == npos);
}
SECTION("lower_bound() in book size=1") {
AnySizeBook book {1};
book.emplace_back<side::ask>(130130, 1);
CHECK(book.lower_bound<side::ask>(130100) == 0);
CHECK(book.lower_bound<side::ask>(130130) == 0);
CHECK(book.lower_bound<side::ask>(130200) == npos);
}
SECTION("lower_bound() in book size=4") {
AnySizeBook book {4};
book.emplace_back<side::ask>(130130, 1);
book.emplace_back<side::ask>(130132, 1);
book.emplace_back<side::ask>(130134, 1);
book.emplace_back<side::ask>(130136, 1);
CHECK(book.lower_bound<side::ask>(130100) == 0);
CHECK(book.lower_bound<side::ask>(130130) == 0);
CHECK(book.lower_bound<side::ask>(130131) == 1);
CHECK(book.lower_bound<side::ask>(130132) == 1);
CHECK(book.lower_bound<side::ask>(130133) == 2);
CHECK(book.lower_bound<side::ask>(130134) == 2);
CHECK(book.lower_bound<side::ask>(130135) == 3);
CHECK(book.lower_bound<side::ask>(130136) == 3);
CHECK(book.lower_bound<side::ask>(130200) == npos);
}
SECTION("lower_bound() in book size=5, bid side") {
AnySizeBook book {5};
book.emplace_back<side::bid>(130138, 1);
book.emplace_back<side::bid>(130136, 1);
book.emplace_back<side::bid>(130134, 1);
book.emplace_back<side::bid>(130132, 1);
book.emplace_back<side::bid>(130130, 1);
CHECK(book.lower_bound<side::bid>(130200) == 0);
CHECK(book.lower_bound<side::bid>(130138) == 0);
CHECK(book.lower_bound<side::bid>(130137) == 1);
CHECK(book.lower_bound<side::bid>(130136) == 1);
CHECK(book.lower_bound<side::bid>(130135) == 2);
CHECK(book.lower_bound<side::bid>(130134) == 2);
CHECK(book.lower_bound<side::bid>(130133) == 3);
CHECK(book.lower_bound<side::bid>(130132) == 3);
CHECK(book.lower_bound<side::bid>(130131) == 4);
CHECK(book.lower_bound<side::bid>(130130) == 4);
CHECK(book.lower_bound<side::bid>(130100) == npos);
}
}
TEST_CASE("AnySizeBook_upper_bound", "[book][upper_bound]") {
using namespace market;
constexpr auto npos = AnySizeBook::npos;
SECTION("upper_bound() in empty book") {
AnySizeBook book {0};
CHECK(book.upper_bound<side::ask>(130130) == npos);
CHECK(book.upper_bound<side::bid>(130130) == npos);
}
SECTION("upper_bound() in book size=1") {
AnySizeBook book {1};
book.emplace_back<side::ask>(130130, 1);
CHECK(book.upper_bound<side::ask>(130100) == 0);
CHECK(book.upper_bound<side::ask>(130130) == npos);
CHECK(book.upper_bound<side::ask>(130200) == npos);
}
SECTION("upper_bound() in book size=4") {
AnySizeBook book {4};
book.emplace_back<side::ask>(130130, 1);
book.emplace_back<side::ask>(130132, 1);
book.emplace_back<side::ask>(130134, 1);
book.emplace_back<side::ask>(130136, 1);
CHECK(book.upper_bound<side::ask>(130100) == 0);
CHECK(book.upper_bound<side::ask>(130130) == 1);
CHECK(book.upper_bound<side::ask>(130131) == 1);
CHECK(book.upper_bound<side::ask>(130132) == 2);
CHECK(book.upper_bound<side::ask>(130133) == 2);
CHECK(book.upper_bound<side::ask>(130134) == 3);
CHECK(book.upper_bound<side::ask>(130135) == 3);
CHECK(book.upper_bound<side::ask>(130136) == npos);
CHECK(book.upper_bound<side::ask>(130200) == npos);
}
SECTION("upper_bound() in book size=5, bid side") {
AnySizeBook book {5};
book.emplace_back<side::bid>(130138, 1);
book.emplace_back<side::bid>(130136, 1);
book.emplace_back<side::bid>(130134, 1);
book.emplace_back<side::bid>(130132, 1);
book.emplace_back<side::bid>(130130, 1);
CHECK(book.upper_bound<side::bid>(130200) == 0);
CHECK(book.upper_bound<side::bid>(130138) == 1);
CHECK(book.upper_bound<side::bid>(130137) == 1);
CHECK(book.upper_bound<side::bid>(130136) == 2);
CHECK(book.upper_bound<side::bid>(130135) == 2);
CHECK(book.upper_bound<side::bid>(130134) == 3);
CHECK(book.upper_bound<side::bid>(130133) == 3);
CHECK(book.upper_bound<side::bid>(130132) == 4);
CHECK(book.upper_bound<side::bid>(130131) == 4);
CHECK(book.upper_bound<side::bid>(130130) == npos);
CHECK(book.upper_bound<side::bid>(130100) == npos);
}
}
namespace {
std::pair<uint8_t, uint8_t> make(int lh, int rh) {
return std::make_pair((uint8_t) lh, (uint8_t)rh);
}
}
namespace Catch {
template<>
struct StringMaker<std::pair<uint8_t, uint8_t>> {
static std::string convert(std::pair<uint8_t, uint8_t> const& v) {
using namespace std;
return "{"s + to_string((unsigned int)v.first) + ","s + to_string((unsigned int)v.second) + "}"s;
}
};
}
TEST_CASE("AnySizeBook_equal_range", "[book][equal_range]") {
using namespace market;
constexpr auto npos = AnySizeBook::npos;
SECTION("equal_range() in empty book") {
AnySizeBook book {0};
CHECK(book.equal_range<side::ask>(130130) == make(npos, npos));
CHECK(book.equal_range<side::bid>(130130) == make(npos, npos));
}
SECTION("equal_range() in book size=1") {
AnySizeBook book {1};
book.emplace_back<side::ask>(130130, 1);
CHECK(book.equal_range<side::ask>(130100) == make(0, 0));
CHECK(book.equal_range<side::ask>(130130) == make(0, npos));
CHECK(book.equal_range<side::ask>(130200) == make(npos, npos));
}
SECTION("equal_range() in book size=4") {
AnySizeBook book {4};
book.emplace_back<side::ask>(130130, 1);
book.emplace_back<side::ask>(130132, 1);
book.emplace_back<side::ask>(130134, 1);
book.emplace_back<side::ask>(130136, 1);
CHECK(book.equal_range<side::ask>(130100) == make(0, 0));
CHECK(book.equal_range<side::ask>(130130) == make(0, 1));
CHECK(book.equal_range<side::ask>(130131) == make(1, 1));
CHECK(book.equal_range<side::ask>(130132) == make(1, 2));
CHECK(book.equal_range<side::ask>(130133) == make(2, 2));
CHECK(book.equal_range<side::ask>(130134) == make(2, 3));
CHECK(book.equal_range<side::ask>(130135) == make(3, 3));
CHECK(book.equal_range<side::ask>(130136) == make(3, npos));
CHECK(book.equal_range<side::ask>(130200) == make(npos, npos));
}
SECTION("equal_range() in book size=5, bid side") {
AnySizeBook book {5};
book.emplace_back<side::bid>(130138, 1);
book.emplace_back<side::bid>(130136, 1);
book.emplace_back<side::bid>(130134, 1);
book.emplace_back<side::bid>(130132, 1);
book.emplace_back<side::bid>(130130, 1);
CHECK(book.equal_range<side::bid>(130200) == make(0, 0));
CHECK(book.equal_range<side::bid>(130138) == make(0, 1));
CHECK(book.equal_range<side::bid>(130137) == make(1, 1));
CHECK(book.equal_range<side::bid>(130136) == make(1, 2));
CHECK(book.equal_range<side::bid>(130135) == make(2, 2));
CHECK(book.equal_range<side::bid>(130134) == make(2, 3));
CHECK(book.equal_range<side::bid>(130133) == make(3, 3));
CHECK(book.equal_range<side::bid>(130132) == make(3, 4));
CHECK(book.equal_range<side::bid>(130131) == make(4, 4));
CHECK(book.equal_range<side::bid>(130130) == make(4, npos));
CHECK(book.equal_range<side::bid>(130100) == make(npos, npos));
}
}
namespace {
struct ConstLevel {
const int ticks; // Regular assignment won't work here
template <market::side Side>
constexpr static bool compare(const ConstLevel& lh, const ConstLevel& rh) noexcept;
constexpr static ConstLevel make(int i) {