-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtests.js
163 lines (130 loc) · 6.22 KB
/
tests.js
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
/* eslint-disable max-len */
const { expect } = require("chai");
const { reconcileOrder } = require("./orderBook");
describe("Order Book", () => {
describe("reconcileOrder", () => {
it("adds an order to the book when the book is empty and thus cannot fulfill the order", () => {
const existingBook = [];
const incomingOrder = { type: "sell", quantity: 10, price: 6150 };
const updatedBook = reconcileOrder(existingBook, incomingOrder);
expect(updatedBook).to.deep.equal([
{ type: "sell", quantity: 10, price: 6150 },
]);
});
it("adds an order to the book when the book has orders of the corresponding type (i.e. a sell with no buys)", () => {
const existingBook = [{ type: "sell", quantity: 10, price: 6150 }];
const incomingOrder = { type: "sell", quantity: 12, price: 6000 };
const updatedBook = reconcileOrder(existingBook, incomingOrder);
expect(updatedBook).to.deep.equal([
{ type: "sell", quantity: 10, price: 6150 },
{ type: "sell", quantity: 12, price: 6000 },
]);
});
it("adds an order to the book when the book has a corresponding order type but it does not match", () => {
const existingBook = [{ type: "buy", quantity: 10, price: 6000 }];
const incomingOrder = { type: "sell", quantity: 12, price: 6150 };
const updatedBook = reconcileOrder(existingBook, incomingOrder);
expect(updatedBook).to.deep.equal([
{ type: "buy", quantity: 10, price: 6000 },
{ type: "sell", quantity: 12, price: 6150 },
]);
});
it("fulfills an order and removes the matching order when the book contains a matching order of the same quantity", () => {
const existingBook = [
{ type: "buy", quantity: 10, price: 6150 },
{ type: "sell", quantity: 12, price: 6250 },
];
const incomingOrder = { type: "sell", quantity: 10, price: 6150 };
const updatedBook = reconcileOrder(existingBook, incomingOrder);
expect(updatedBook).to.deep.equal([
{ type: "sell", quantity: 12, price: 6250 },
]);
});
it("fulfills an order and reduces the matching order when the book contains a matching order of a larger quantity", () => {
const existingBook = [
{ type: "buy", quantity: 15, price: 6150 },
{ type: "sell", quantity: 12, price: 6950 },
];
const incomingOrder = { type: "sell", quantity: 10, price: 6150 };
const updatedBook = reconcileOrder(existingBook, incomingOrder);
expect(updatedBook).to.deep.equal([
{ type: "sell", quantity: 12, price: 6950 },
{ type: "buy", quantity: 5, price: 6150 },
]);
});
it("partially fulfills an order, removes the matching order and adds the remainder of the order to the book when the book contains a matching order of a smaller quantity", () => {
const existingBook = [
{ type: "buy", quantity: 10, price: 6150 },
{ type: "sell", quantity: 12, price: 5950 },
];
const incomingOrder = { type: "sell", quantity: 15, price: 6150 };
const updatedBook = reconcileOrder(existingBook, incomingOrder);
expect(updatedBook).to.deep.equal([
{ type: "sell", quantity: 12, price: 5950 },
{ type: "sell", quantity: 5, price: 6150 },
]);
});
it("uses two existing orders to completely fulfill an order, removing the matching orders from the book", () => {
const existingBook = [
{ type: "buy", quantity: 10, price: 6150 },
{ type: "buy", quantity: 5, price: 6150 },
{ type: "sell", quantity: 12, price: 5950 },
];
const incomingOrder = { type: "sell", quantity: 15, price: 6150 };
const updatedBook = reconcileOrder(existingBook, incomingOrder);
expect(updatedBook).to.deep.equal([
{ type: "sell", quantity: 12, price: 5950 },
]);
});
it("uses two existing orders to completely fulfill an order, removing the first matching order from the book and reducing the second", () => {
const existingBook = [
{ type: "buy", quantity: 10, price: 6150 },
{ type: "buy", quantity: 10, price: 6150 },
{ type: "sell", quantity: 12, price: 6950 },
];
const incomingOrder = { type: "sell", quantity: 15, price: 6150 };
const updatedBook = reconcileOrder(existingBook, incomingOrder);
expect(updatedBook).to.deep.equal([
{ type: "sell", quantity: 12, price: 6950 },
{ type: "buy", quantity: 5, price: 6150 },
]);
});
it("uses two existing orders to partially fulfill an order, removing the matching orders from the book and reducing the incoming order before adding it to the book", () => {
const existingBook = [
{ type: "buy", quantity: 10, price: 6150 },
{ type: "buy", quantity: 10, price: 6150 },
{ type: "sell", quantity: 12, price: 6950 },
];
const incomingOrder = { type: "sell", quantity: 25, price: 6150 };
const updatedBook = reconcileOrder(existingBook, incomingOrder);
expect(updatedBook).to.deep.equal([
{ type: "sell", quantity: 12, price: 6950 },
{ type: "sell", quantity: 5, price: 6150 },
]);
});
it.skip("Extra Credit: it fulfills a mismatched order when both parties benefit", () => {
const existingBook = [
{ type: "buy", quantity: 15, price: 6000 },
{ type: "sell", quantity: 12, price: 6950 },
];
const incomingOrder = { type: "sell", quantity: 15, price: 5900 };
const updatedBook = reconcileOrder(existingBook, incomingOrder);
expect(updatedBook).to.deep.equal([
{ type: "sell", quantity: 12, price: 6950 },
]);
});
it.skip("Extra Credit: it does not fulfill a mismatched order when it does not benefit both parties", () => {
const existingBook = [
{ type: "buy", quantity: 15, price: 5900 },
{ type: "sell", quantity: 12, price: 6950 },
];
const incomingOrder = { type: "sell", quantity: 15, price: 6000 };
const updatedBook = reconcileOrder(existingBook, incomingOrder);
expect(updatedBook).to.deep.equal([
{ type: "buy", quantity: 15, price: 5900 },
{ type: "sell", quantity: 12, price: 6950 },
{ type: "sell", quantity: 15, price: 6000 },
]);
});
});
});